var Menu_Supported = false;

if (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.appVersion.substring(0,1) > 3)
{
	Menu_Supported = true;
}

if (Menu_Supported)
{
	var newLineChar = String.fromCharCode(10);
	var char34 = String.fromCharCode(34);
	var CurMenu = "";
	var IsMenuDropDown = true;
	var HTMLStr;
	var x = 0;
	var y = 0;
	var x2 = 0;
	var y2 = 0;
	var MenuMinWidth = 600;
	var ToolbarMenu;
	var aDefColor = new Array(3);
	var aCurColor = new Array(3);
	var Font;
	var MaxMenu = 30;
	var TotalMenu = 0;
	var arrMenuInfo = new Array(30);
        var topmargin = 80;
       
	document.write("<SPAN ID='StartMenu' STYLE='display:none;'></SPAN>");

	HTMLStr =
		"<DIV ID='Menu' STYLE='position:absolute;top:"+topmargin+";left:0;height:13;'><!--MENU_TITLES-->" +
		"<SCRIPT TYPE='text/javascript'>" + 
		"   var ToolbarMenu = StartMenu;" + 
		"</SCRIPT>";

	// Define event handlers
	window.onresize  = resizeMenu;

	// Intialize global variables
	Font = "bold 8pt sans-serif"; //Font
	
	aDefColor[0] = aCurColor[0] = "#6F7BB5";	// bgcolor;
	aDefColor[1] = aCurColor[1] = "#FFFFFF";	// text font color
	aDefColor[2] = aCurColor[2] = "#80C523";	// mouseover font color
}

function drawMenu()
{
	HTMLStr += "</DIV>";
	document.write(HTMLStr);

	Menu.style.backgroundColor = aDefColor[0];
	resizeMenu();

	for (i = 0; i < TotalMenu; i++) 
	{
		thisMenu = document.all(arrMenuInfo[i].IDStr);
		if (thisMenu != null)
			thisMenu.style.width = arrMenuInfo[i].width + 'ex';
	}
}


function resizeMenu()
{
	w = Math.max(MenuMinWidth, document.body.clientWidth) - MenuMinWidth;
	Menu.style.width = MenuMinWidth + w;
}

function setDefaultMenuColor(bgColor, fontColor, mouseoverColor)
{	
	if (bgColor != "") aDefColor[0] = bgColor;
	if (fontColor != "") aDefColor[1] = fontColor;
	if (mouseoverColor != "") aDefColor[2] = mouseoverColor;
}

function setSubMenuWidth(MenuIDStr)
{
	var fFound = false;
	if (TotalMenu == MaxMenu)
	{
		alert("Unable to process menu. Maximum of " + MaxMenu + " reached.");
		return;
	}
	
	for (i = 0; i < TotalMenu; i++)
		if (arrMenuInfo[i].IDStr == MenuIDStr)
		{
			fFound = true;
			break;
		}

	if (!fFound)
	{
		arrMenuInfo[i] = new menuInfo(MenuIDStr);
		TotalMenu += 1;
	}
}

// This function creates a menuInfo object instance.
function menuInfo(MenuIDStr)
{
	this.IDStr = MenuIDStr;
	this.width = 0;
	this.count = 0;
}

function updateSubMenuWidth(MenuIDStr, SubMenuStr)
{
	for (i = 0; i < TotalMenu; i++)
		if (arrMenuInfo[i].IDStr == MenuIDStr)
		{
			if (arrMenuInfo[i].width < SubMenuStr.length) 
				arrMenuInfo[i].width = SubMenuStr.length;
			arrMenuInfo[i].count = arrMenuInfo[i].count + 1;
			break;
		}
}

function addMenu(MenuIDStr, MenuDisplayStr, MenuURLStr)
{
/*
	if (addMenu.arguments.length > 3)
		TargetStr = addMenu.arguments[3];
	else
		TargetStr = "_top";
*/

	tagStr = "<!--MENU_TITLES-->";

	MenuStr = newLineChar;
	MenuStr += "<A ID='AM_" + MenuIDStr + "'" + " STYLE='text-decoration:none;cursor:hand;font:" + Font + ";background-color:" + aDefColor[0] + ";color:" + aDefColor[1] + ";'";

	if (MenuURLStr != "")
		MenuStr += " HREF='" + MenuURLStr + "'";
	else
		MenuStr += " HREF='' onclick='window.event.returnValue=false;'";

	MenuStr +=
		" onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "'); hideMenu();" + char34 + 
		" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "'); doMenu('"+ MenuIDStr + "');" + char34 + ">" +
		"&nbsp;" + MenuDisplayStr + "&nbsp;</a>";

	MenuStr += tagStr;
	
	HTMLStr = HTMLStr.replace(tagStr, MenuStr);	
	setSubMenuWidth(MenuIDStr);
}

function addSubMenu(MenuIDStr, SubMenuStr, SubMenuURLStr)
{
/*
	if (addSubMenu.arguments.length > 3)
		TargetStr = addSubMenu.arguments[3];
	else
		TargetStr = "_top";
*/

	var MenuPos = MenuIDStr.toUpperCase().indexOf("MENU");
	if (MenuPos == -1)
		MenuPos = MenuIDStr.length;

	var LookUpTag  = "<!--" + MenuIDStr + "-->";
	var sPos = HTMLStr.indexOf(LookUpTag);

	if (sPos <= 0)
		HTMLStr +=
			newLineChar + newLineChar + "<SPAN ID='" + MenuIDStr + "'" +
			" STYLE='display:none;position:absolute;background-color:" + aDefColor[0] + ";padding-top:0;padding-left:0;padding-bottom:10;z-index:9;'" +
			" onmouseout='hideMenu();'>" + "<DIV STYLE='position:relative;left:0;top:5;'>";

	if (SubMenuURLStr != "")
		URLStr = " HREF='" + SubMenuURLStr + "'";
	else
		URLStr = " HREF='' onclick='window.event.returnValue=false;'";

	TempStr =
		newLineChar + "<A ID='AS_" + MenuIDStr + "'" +
		" STYLE='text-decoration:none;cursor:hand;font:" + Font + ";color:" + aDefColor[1] + "'" + URLStr +
		" onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "');" + char34 + 
		" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "');" + char34 + ">" +
		"&nbsp;" + SubMenuStr + "</A><BR>" + LookUpTag;

	if (sPos <= 0)
		HTMLStr += TempStr + "</DIV></SPAN>";
	else
		HTMLStr = HTMLStr.replace(LookUpTag, TempStr);	

	updateSubMenuWidth(MenuIDStr, SubMenuStr);	
}

function mouseMenu(id, MenuIDStr) 
{
	IsMouseout = (id.toUpperCase().indexOf("OUT") != -1);

	if (IsMouseout)
	{
		color = aDefColor[1];
		if (MenuIDStr == CurMenu && aCurColor[1] != "") 
			color = aCurColor[1];
	}
	else
	{
		color = aDefColor[2];
		if (MenuIDStr == CurMenu && aCurColor[2] != "") 
			color = aCurColor[2];
	}
	window.event.srcElement.style.color = color;
}

function doMenu(MenuIDStr) 
{
	var thisMenu = document.all(MenuIDStr);
	if (ToolbarMenu == null || thisMenu == null || thisMenu == ToolbarMenu) 
	{
		window.event.cancelBubble = true;
		return false;
	}

	// Reset dropdown menu
	window.event.cancelBubble = true;
	ToolbarMenu.style.display = "none";
	showElement("SELECT");
	showElement("OBJECT");
	ToolbarMenu = thisMenu;

	// Set dropdown menu display position
	x  = window.event.srcElement.offsetLeft + window.event.srcElement.offsetParent.offsetLeft;
	x2 = x + window.event.srcElement.offsetWidth;
	y  = Menu.offsetHeight;
	thisMenu.style.top  = y+1;
	thisMenu.style.left = x;
	thisMenu.style.clip = "rect(0 0 0 0)";
	thisMenu.style.display = "block";

	// delay 2 millsecond to allow the value of ToolbarMenu.offsetHeight be set
	window.setTimeout("showMenu()", 2);
	return true;
}

function showMenu() 
{
	if (ToolbarMenu != null) 
	{ 
		y2 = y + ToolbarMenu.offsetHeight;
		ToolbarMenu.style.clip = "rect(auto auto auto auto)";
		hideElement("SELECT");
		hideElement("OBJECT");
	}
}

function hideMenu()
{
	if (ToolbarMenu != null && ToolbarMenu != StartMenu) 
	{
		// Don't hide the menu if the mouse move between the menu and submenus
		cY = event.clientY + document.body.scrollTop;
		if ( (event.clientX >= (x+5) && event.clientX <= x2) &&
			 ((IsMenuDropDown == true  && cY > (y-10) && cY <= y2+topmargin)      ||
			  (IsMenuDropDown == false && cY >= y     && cY <= (y2+10)) ))
		{
			window.event.cancelBubble = true;
			return; 
		}

		ToolbarMenu.style.display = "none";
		ToolbarMenu = StartMenu;
		window.event.cancelBubble = true;

		showElement("SELECT");
		showElement("OBJECT");
	}
}

function hideElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;

		// Find the element's offsetTop and offsetLeft relative to the BODY tag.
		objLeft   = obj.offsetLeft;
		objTop    = obj.offsetTop;
		objParent = obj.offsetParent;
		while (objParent.tagName.toUpperCase() != "BODY")
		{
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		}
		// Adjust the element's offsetTop relative to the dropdown menu
		objTop = objTop - y;

		if (x > (objLeft + obj.offsetWidth) || objLeft > (x + ToolbarMenu.offsetWidth))
			;
		else if (objTop > ToolbarMenu.offsetHeight)
			;
		else if (IsMSMenu && (y + ToolbarMenu.offsetHeight) <= 80)
			;
		else
			obj.style.visibility = "hidden";
	}
}

function showElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
	}
}
