var activeHeader = null;
var activeMenu = null;

var supported_browser = document.getElementById;

// body decrements this, menu sets it at 2. If it is ever below 1,
// mouse is no longer on a menu, so hide the current one.
var menumousetracker = 0;

// Sets the current header and menu
function setMenu(menuHeaderID, menuID)
{
	var top = 0;
	var left = 0;
	var currentEle;
	
	if (!supported_browser)
		return;

	if (activeHeader != null && activeMenu != null)
	{
		if (activeMenu.style.visibility != 'hidden')
		{
			menuHide();
			showSelect();
		}
	}

		activeHeader = document.getElementById(menuHeaderID);
		activeMenu = document.getElementById(menuID);

	menuChange(activeHeader);
	currentEle = activeHeader;
		
	//Find the top and left of header and its parent elements
	while (currentEle.tagName.toLowerCase() != 'body')
	{
		top += currentEle.offsetTop;
		left += currentEle.offsetLeft;
		currentEle = currentEle.offsetParent;
	}
	
	//Add the width of the header, and width of extra image.
	top += (activeHeader.offsetHeight);
				
	activeMenu.style.left = left;
	activeMenu.style.top = top;
	
	hideSelect();
	menuShow();
	
	if (document.all)
		event.cancelBubble = true;
}

// Show the current menu
function menuShow()
{
	if (!supported_browser)
		return;

	activeMenu.style.visibility = 'visible';
	menumousetracker = 2;
}

// Hide the current menu
function menuHide()
{
	menumousetracker = 0;

	if (supported_browser && activeMenu)
	{
		activeMenu.style.visibility = 'hidden';
		menuChange(activeHeader);
		activeHeader = null;
		activeMenu = null;
		showSelect();
	}
}

// Hide the current menu and reset vars
// if the moved to element is not contained
// within the menu.
function hideMenu()
{
	if (document.all
	  && (activeHeader != null)
	  && (activeMenu != null)
	  && (!activeMenu.contains(event.toElement)))
	{
		menuHide();
	}
}

// show dropdown when menu is hidden
function showSelect()
{
	if (!document.all)
		return;

	var obj;
	
	for (var i = 0; i < document.all.tags("select").length; i++)
	{
		obj = document.all.tags("select")[i];

		if (obj && obj.offsetParent)
			obj.style.visibility = 'visible';
	}
}

// hide dropdown so menu can cover it when menu is visible
function hideSelect()
{
	if (!document.all)
		return;

	var obj;
	var currentEle;
	var top = 0;
	var left = 0;
	var menuHeight;
	var timeout;
	
	for (var i = 0; i < document.all.tags("select").length; i++)
	{
		obj = document.all.tags("select")[i];
		currentEle = obj;
	
		while (currentEle.tagName.toLowerCase() != 'body')
		{
			top += currentEle.offsetTop;
			left += currentEle.offsetLeft;
			currentEle = currentEle.offsetParent;
		}

		if (activeMenu != null)
		{
			menuHeight = (activeMenu.offsetTop + activeMenu.offsetHeight);
			
			if (top < menuHeight)
			{
				if ((left < (activeMenu.offsetLeft + activeMenu.offsetWidth))
				  && (left + obj.offsetWidth > activeMenu.offsetLeft))
				{
					obj.style.visibility = 'hidden';
				}
			}
		}

		top = 0;
		left = 0;
	}
}

// Highlight the menu option
function menuChange(srcEle)
{				
	if (srcEle.className.toLowerCase() == 'menuregular')
		srcEle.className = 'menuHighlight';
	else
		srcEle.className = 'menuRegular';
}

