
// MyMenu is an array with a reference to all Menus created
var MyMenus = new Array;

function Menu(sName)
{
	this.Name = sName;
	this.IsIE = (!!document.all);
	this.Visible = false;
	this.LayerNumber = 0;
	
	//Position of the menu
	this.top = 0;
	this.left = 0;
	this.width = 0;
	this.height = 0;
	
	this.MoveX = 0;
	this.MoveY = 0;
	
	//Style properties;
	this.MenuStyle = "";
	this.MenuItemStyleOn = "";
	this.MenuItemStyleOff = "";

	this.AddItem = AddItem;
	this.Hide = Hide;
	this.Show = ShowMenu;
	this.WriteMenu = WriteMenu;
	this.OnMouseOut = OnMouseOut;
		
	this.Items = new Array;
	this.ItemOnClick = new Array;
	
	// Add a reference to the current menu (this) in MyMenus Array
	MyMenus[MyMenus.length] = this;

}

function AddItem(sName, sOnClick)
{
	this.Items[this.Items.length] = sName;
	this.ItemOnClick[this.ItemOnClick.length] = sOnClick;
}

function OnMouseOut(evt)
{
	var iMouseX = 0;
	var iMouseY = 0;

	var sAlert = "";

	if (this.IsIE)
	{
		iMouseX = window.event.clientX;
		iMouseY = window.event.clientY;
	}
	else
	{
		iMouseX = evt.pageX;
		iMouseY = evt.pageY;
	}
	
	sAlert = sAlert + "MouseX: " + iMouseX + "\n";
	sAlert = sAlert + "MouseY: " + iMouseY + "\n";
	sAlert = sAlert + "top: " + this.top + "\n";
	sAlert = sAlert + "left: " + this.left + "\n";
	sAlert = sAlert + "width: " + this.width + "\n";
	sAlert = sAlert + "heigh: " + this.height + "\n";
	
	//alert(sAlert);

	if (!((iMouseX > this.left) && (iMouseX < (this.left + this.width))) || !((iMouseY > this.top - 5) && (iMouseY < (this.top + this.height))))
	{
		this.Hide();			
	}
}

function WriteMenu()
{
	var sReturn = "";
	var sInsert = "";
	

	sReturn += "<table class=\"" + this.MenuStyle + "\" border=0 cellspacing=0 cellpadding=0 width=\"10%\">";

	for (var x = 0; x < this.Items.length; x++)
	{
		sReturn += "<tr><td nowrap>";

		sReturn += "<div class=\"" + this.MenuItemStyleOff + "\" onmouseout=\"this.className = '" + this.MenuItemStyleOff + "'\" onmouseover=\"this.className = '" + this.MenuItemStyleOn + "'\">"
		sReturn += "<a href=\"#\" onclick=\"" + this.ItemOnClick[x] + "; return false;\" class=\"" + this.MenuItemStyleOff + "\" onmouseout=\"this.className = '" + this.MenuItemStyleOff + "';" + this.Name + ".OnMouseOut(event)\" onmouseover=\"this.className = '" + this.MenuItemStyleOn + "'\">"
		sReturn += "&nbsp;&nbsp;" + this.Items[x] + "&nbsp;&nbsp;";
		sReturn += "</a>";
		sReturn += "</div>";

		sReturn += "</td></tr>";
	}

	sReturn += "</table>";
	
	if (this.IsIE)
	{
		sInsert = "<div id=\"" + this.Name + "\" name=\"" + this.Name + "\" style=\"display: none;position: absolute\" onmouseout=\"" + this.Name + ".OnMouseOut(event)\" class=\"" + this.MenuStyle + "\">" + sReturn + "</div>";
	
		document.body.insertAdjacentHTML("beforeEnd", sInsert);
	}
	else
	{

		sInsert = "<layer style=\"position: static\" onmouseout=\"" + this.Name + ".Hide()\">" + sReturn + "</layer>";
sInsert = sReturn;
		var NewLayer = new Layer(1000);
		
      NewLayer.document.open("text/html");
      NewLayer.document.writeln(sInsert);
      NewLayer.document.close();

		//NewLayer.captureEvents(Event.MOUSEOUT);
      //NewLayer.onmouseout = this.Hide();

		this.LayerNumber = (document.layers.length - 1);
	}

}
function ShowMenu(evt)
{
	var iFor = 0;
	
	// Hide all menus
	for (iFor = 0; iFor < MyMenus.length; iFor++)
	{
		MyMenus[iFor].Hide();
	}

	var iTop = GetAbsoluteTop(evt, this) + this.MoveY;
	var iLeft = GetAbsoluteLeft(evt, this) + this.MoveX;

	if (!this.Visible)
	{
		if (this.IsIE)
		{
			document.all[this.Name].style.top = iTop;
			document.all[this.Name].style.left = iLeft;
			document.all[this.Name].style.display = "block";
			document.all[this.Name].style.visibility = "visible";

			this.top = parseInt(iTop);
			this.left = parseInt(iLeft);
			this.width = parseInt(document.all[this.Name].clientWidth);
			this.height = parseInt(document.all[this.Name].clientHeight);
		}
		else
		{
			document.layers[this.LayerNumber].top = iTop;
			document.layers[this.LayerNumber].left = iLeft;
			document.layers[this.LayerNumber].display = "block";
			document.layers[this.LayerNumber].visibility = "visible";

			this.top = parseInt(iTop);
			this.left = parseInt(iLeft);
			this.width = parseInt(document.layers[this.LayerNumber].clip.width);
			this.height = parseInt(document.layers[this.LayerNumber].clip.height);
		}
	}	

	
	this.Visible = true;
		
	return false;
}

function Hide()
{
	if (this.IsIE)
	{
		document.all[this.Name].style.display = "none";
	}
	else
	{
		document.layers[this.LayerNumber].visibility = "hidden";
		document.layers[this.LayerNumber].display = "none";
	}
	this.Visible = false;
}

	function GetAbsoluteTop(evt, ref)
	{
		var topPosition = 0;

		if (!!document.all)
		{
			var elem = window.event.srcElement;
			topPosition = elem.offsetHeight;
			while (elem)
			{
				if (elem.tagName == 'BODY')
				{
					break;
				}
				topPosition += elem.offsetTop;
				elem = elem.offsetParent;
			}
			topPosition += elem.style.height;
		}
		else
		{
			topPosition = document.layers["NS_" + ref.Name].pageY;
			topPosition += document.layers["NS_" + ref.Name].clip.height;
		}
		return parseInt(topPosition);
	}

	// Get real Left value with respect to client area
	function GetAbsoluteLeft(evt, ref)
	{
		var leftPosition = 0;
			
		if (!!document.all)
		{
			var elem = window.event.srcElement;

			while (elem)
			{
				if (elem.tagName == 'BODY')
				{
					break;
				}
				leftPosition += elem.offsetLeft;
				elem = elem.offsetParent;
			}
		}
		else
		{
			leftPosition = document.layers["NS_" + ref.Name].pageX;
		}
		return parseInt(leftPosition);
	}

