// Determine browser type IE 5 or greater.
var isIE5 = (navigator.userAgent.indexOf("MSIE 5") > 0) ? 1 : 0;

// Global variable for tracking the currently active button.
var activeButton = null;

// Capture mouse clicks on the page so any active button can be deactivated.
document.onmousedown = pageMousedown;

function pageMousedown(event) 
{
	var className;
	// If the object clicked on was not a menu button or item, close any active
	// menu.
	
	className = window.event.srcElement.className;
	if (className != "menuButton" && className != "menuItem" && activeButton)
	{
		resetButton(activeButton);

		// If the page has a variable defining combo boxes to hide
		if (typeof(sDivsToHide) != "undefined")
		{
			// split combo box into an array and loop through.
			aryDivsToHide = sDivsToHide.split("|");
			for(var i=0; i < aryDivsToHide.length; i++)
			{
				// make sure the control exists
				if ( document.all[aryDivsToHide[i]] )
				{
					document.all[aryDivsToHide[i]].style.visibility="visible";
				}
			}
		}
	}
}

function buttonClick(button, menuName) 
{
	// Blur focus from the link to remove that annoying outline.
	button.blur();

	// Associate the named menu to this button if not already done.
	if (!button.menu)
		button.menu = document.getElementById(menuName);

	// Reset the currently active button, if any.
	if (activeButton&& activeButton != button)
		resetButton(activeButton);

	// Toggle the button's state.
	if (button.isDepressed)
		resetButton(button);
	else
		depressButton(button);
	
	return (false);
}

function buttonMouseover(button, menuName) 
{
	// If any other button menu is active, deactivate it and activate this one.
	if (activeButton && activeButton != button) 
	{
		resetButton(activeButton);
		if (menuName)
	      		buttonClick(button, menuName);
	}
}

function depressButton(button) 
{
	var menuHeight;
	var menuWidth;
	var menuTop;
	var menuLeft;
	var cboHeight;
	var cboWidth;
	var cboTop;
	var cboLeft;
	var aryDivsToHide;
	
	// Save current style values so they can be restored later. Only needs to be
	// done once.
	if (!button.oldBackgroundColor) 
	{
		button.oldBackgroundColor = button.style.backgroundColor;
		button.oldBorderBottomColor = button.style.borderBottomColor;
		button.oldBorderRightColor = button.style.borderRightColor;
		button.oldBorderTopColor = button.style.borderTopColor;
		button.oldBorderLeftColor = button.style.borderLeftColor;
		button.oldColor = button.style.color;
	    button.oldLeft = button.style.left;
	    button.oldPosition = button.style.position;
	    button.oldTop = button.style.top;
	}

	// Change style value to make the button looks like it's
	// depressed.
	button.style.backgroundColor = "#FFFFFF";
	button.style.borderBottomColor = "#1E3DB0";
	button.style.borderRightColor = "#1E3DB0";
	button.style.borderTopColor = "#0F1F58";
	button.style.borderLeftColor = "#0F1F58";
	button.style.color = "#000000";
	button.style.left = "1px";
	button.style.position = "relative";
	button.style.top = "0px";

	// For IE, force first menu item to the width of the parent menu, this
	// causes mouseovers work for all items even when cursor is not over the
	// link text.

	if (isIE5 && !button.menu.firstChild.style.width)
		button.menu.firstChild.style.width = button.menu.offsetWidth + "px";

	// Position the associated drop down menu under the button and show it. Note
	// that the position must be adjusted according to the brower type.

	x = getPageOffsetLeft(button);
	y = getPageOffsetTop(button) + button.offsetHeight;
	y += 4;
	button.menu.style.left = x + "px";
	button.menu.style.top  = y + "px";
	button.menu.style.visibility = "visible";

	// If the page has a variable defining combo boxes to hide
	if (typeof(sDivsToHide) != "undefined")
	{
		// split combo box into an array and loop through.
		aryDivsToHide = sDivsToHide.split("|");
		for(var i=0; i < aryDivsToHide.length; i++)
		{
			// make sure the control exists
			if ( document.all[aryDivsToHide[i]] )
			{
				//get the position and dimensions of the menu we are displaying
				menuHeight = document.all[button.menu.name].offsetHeight;
				menuWidth = document.all[button.menu.name].offsetWidth;
				menuTop = getAbsoluteTop(button.menu.name);
				menuLeft = getAbsoluteLeft(button.menu.name);
				//get the position and dimensions of the current combo box
				cboHeight = document.all[aryDivsToHide[i]].offsetHeight;
				cboWidth = document.all[aryDivsToHide[i]].offsetWidth;
				cboTop = getAbsoluteTop(aryDivsToHide[i]);
				cboLeft = getAbsoluteLeft(aryDivsToHide[i]);
				//if the menu we are displaying would cover the combo box (IE bug leaves combo in front of menu),
				// then hide the combo box
				if ((menuTop + menuHeight > cboTop) && (menuLeft + menuWidth > cboLeft) && (menuLeft < cboLeft + cboWidth))
					document.all[aryDivsToHide[i]].style.visibility="hidden";
				else
					document.all[aryDivsToHide[i]].style.visibility="visible";
			}
		}
	}

	// Set button state and let the world know which button is active.
	button.isDepressed = true;
	activeButton = button;
}

function resetButton(button) 
{
	// Restore the button's style settings.
	button.style.backgroundColor = button.oldBackgroundColor;
	button.style.borderBottomColor = button.oldBorderBottomColor;
	button.style.borderRightColor = button.oldBorderRightColor;
	button.style.borderTopColor = button.oldBorderTopColor;
	button.style.borderLeftColor = button.oldBorderLeftColor;
	button.style.color = button.oldColor;
	button.style.left = button.oldLeft
	button.style.position = button.oldPosition;
	button.style.top = button.oldTop;

	// Hide the button's menu.
	if (button.menu)
	{
		button.menu.style.visibility = "hidden";
	
		// If the page has a variable defining combo boxes to hide
		if (typeof(sDivsToHide) != "undefined")
		{
			// split combo box into an array and loop through.
			aryDivsToHide = sDivsToHide.split("|");
			for(var i=0; i < aryDivsToHide.length; i++)
			{
				// make sure the control exists
				if ( document.all[aryDivsToHide[i]] )
				{
					document.all[aryDivsToHide[i]].style.visibility="visible";
				}
			}
		}
	}

	// Set button state and clear active menu global.
	button.isDepressed = false;
	activeButton = null;
}

function getPageOffsetLeft(el) 
{
	// Return the true x coordinate of an element relative to the page.
	return el.offsetLeft + (el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
}

function getPageOffsetTop(el)
{
	return el.offsetTop + (el.offsetParent ? getPageOffsetTop(el.offsetParent) : 0);
}

//function gets the absolute top position of an element regradless of the nesting level
function getAbsoluteTop(objName){
	var nTop = 0;
	var i = 0;
	var obj;

	obj = document.all[objName];
	var bBodyReached = false;
	//loop upwards until we reach the body tag, adding up the offsetTops
	while(!bBodyReached){
		i++;
		nTop += obj.offsetTop;
		if (obj.offsetParent.tagName.toUpperCase() == "BODY" || i > 30){
			bBodyReached = true;
			return nTop;
			}
		else
			obj = obj.offsetParent;
	}
}

//function gets the absolute left position of an element regradless of the nesting level
function getAbsoluteLeft(objName){
	var nLeft = 0;
	var i = 0;
	var obj;
	obj = document.all[objName];
	var bBodyReached = false;
	//loop upwards until we reach the body tag, adding up the offsetLefts
	while(!bBodyReached){
		i++;
		nLeft += obj.offsetLeft;
		if (obj.offsetParent.tagName.toUpperCase() == "BODY" || i > 30){
			bBodyReached = true;
			return nLeft;
			}
		else
			obj = obj.offsetParent;
	}
}

