var currentMenus = new Array();

function FloatingMenu(name, divName, buttonName, offsetX, offsetY)
{
	this.flag = false;
	
	// Name of the current object
	this.name = name;

	// Id for the popup menu div
	this.divName = divName;
	
	// Id for the open/close button
	this.buttonName = buttonName;
	
	// Offset/limit for the popup positioning
	this.offsetX = offsetX;
	this.offsetY = offsetY;
	
	// Size of the div
	this.sizeX = 0;
	this.sizeY = 0;
	
	// Position of the div
	this.divX = 0;
	this.divY = 0;

	// Timeout for menu exibition
	this.timeout = -1;
	
	// Methods
	this.Init = floatingMenu_Init;
	this.PositionMenu = floatingMenu_PositionMenu;
	this.ShowMenu = floatingMenu_ShowMenu;
	this.HideMenu = floatingMenu_HideMenu;

	function floatingMenu_Init()
	{
		var showscript = this.name + '.timeout = window.setTimeout(\'' + this.name + '.ShowMenu();\',500);';
		var showabortscript = 'if(' + this.name + '.timeout>=0){ window.clearTimeout(' + this.name + '.timeout); ' + this.name + '.timeout = -1;};';
		var hidescript = 'window.setTimeout(\'' + this.name + '.HideMenu();\',100);';
		
		// Attaches the mouseover/mouseout events to the div
		var objDiv = document.getElementById(this.divName);
		objDiv.onmouseout = new Function(hidescript);
		
		// Attaches the mouseover event to the button
		var objButton = document.getElementById(this.buttonName);
		objButton.onmouseover = new Function(showscript);
		objButton.onmouseout = new Function(showabortscript);
		
		// Registers in the current menu list
		currentMenus[currentMenus.length] = this;
	}

	function floatingMenu_PositionMenu()
	{
		var objImg = document.getElementById(this.buttonName);
		var objDiv = document.getElementById(this.divName);
		
		if(objImg!=null && objDiv!=null)
		{
			// Gets the position of the "parent" object
			var imgX = objImg.offsetLeft;
			var imgY = objImg.offsetTop;
								
			aux = objImg.offsetParent;
			while (aux)
			{
				imgX += aux.offsetLeft;		
				imgY += aux.offsetTop;		
				aux = aux.offsetParent;
			}
			
			
			// Positions the div using the relative offsets specified
			if(objDiv)
			{
				this.divX = imgX + this.offsetX;
				this.divY = imgY + this.offsetY;
			
				objDiv.style.left = imgX + this.offsetX + 'px';
				objDiv.style.top = imgY + this.offsetY + 'px';
				
			}
		}
	}
	
	function floatingMenu_ShowMenu()
	{
		// Hides all other menus
		for(var i=0;i<currentMenus.length;i++)
		{
			if(currentMenus[i]!=this) currentMenus[i].HideMenu();
		}
	
		// Sets the control flag
		this.visible = true;
	
		// Re-positions the popup
		this.PositionMenu();
		
		// Starts the monitoring thread
		document.onmousemove = GetMousePosition;
		
		// Makes the popup visible
		var objDiv = document.getElementById(this.divName);
		if(objDiv) objDiv.style.display = 'block';

		// Stores the div dimentions
		this.sizeX = objDiv.offsetWidth;
		this.sizeY = objDiv.offsetHeight;
	}
	
	function floatingMenu_HideMenu()
	{
		// Calculates the area of the popup
		var x1 = this.divX;
		var x2 = this.divX + this.sizeX;
		var y1 = this.divY;
		var y2 = this.divY + this.sizeY;

		// Aborts if we're still inside the 'area' of the popup
		if(this.flag)
			return;
		if(currentMouseX>x1 && currentMouseX<x2)
			if(currentMouseY>y1 && currentMouseY<y2) 
				return;

		// Sets the control flag
		this.visible = false;

		// Stops the monitoring thread
		document.onmousemove = null;

		// Makes the popup hidden
		var objDiv = document.getElementById(this.divName);
		if(objDiv) objDiv.style.display = 'none';
	}
}

function SetOff()
{
	libraryMenu.flag  = false;
}

function SetOn()
{
	libraryMenu.flag  = true;;
}


function SetOffRadios()
{
	RadiosMenu.flag  = false;
}

function SetOnRadios()
{
	RadiosMenu.flag  = true;;
}


function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function noLetter()
{
  var tecla = window.event.keyCode;
  tecla     = String.fromCharCode(tecla);
  if(!((tecla >= "0") && (tecla <= "9")))
  {
    window.event.keyCode = 0;
  }
}