/********************************************************************
/	File: 		menu.js
/	Author:		Jason Brubaker
/	Purpose:	(1) provide hover-over sub-menu effect
/
/********************************************************************/

//some global vars
var mn_current = null;
var mn_arrSubs = new Array();

//crawls the main menu, attaching handlers & remembering which main menu items have sub-menus
function mn_crawlMenu(){
	var objMain = document.getElementById("MenuMain");
	var objItem = objMain.firstChild;
	var iFirstLeft;
	
	while(objItem != null){
		if(objItem.nodeType == 1){
			//get the position of the first main menu item
			if(iFirstLeft == null){
				iFirstLeft = hp_getPos(objItem)[0];
			}
			
			//attach handler
			objItem.onmouseover = mn_itemOver;
			
			//does the item have a sub-menu?
			if(objItem.id){
				var sName = objItem.id.substr(objItem.id.indexOf("_") + 1);
				var objSub = document.getElementById("MenuSub_" + sName);
				if(objSub != null){
					mn_arrSubs[objItem.id] = objSub;
					
				}
			}
		}
		
		//move to the next sibling
		objItem = objItem.nextSibling;
	}
	
	//set width of sub-menu container
	var iMainLeft = hp_getPos(objMain)[0];
	var iWidthSub = objMain.offsetWidth - (iFirstLeft - iMainLeft);
	var objSubContainer = document.getElementById("MenuSub");
	objSubContainer.style.width = iWidthSub + "px";
	objSubContainer.style.marginLeft = (objMain.offsetWidth - iWidthSub) + "px";
	
	//preset the current main id (if applicable)
	if(document.getElementById("menu_currentId") != null){
		var s_currentId = document.getElementById("menu_currentId").value;
		mn_doItemOver(s_currentId);
	}
}

//handles mouseover on a main menu item
function mn_itemOver(e){
	//get the target of the event & pass use its id to show the sub menu
	if (!e) var e = window.event;
	var sId = hp_getTarget(e).id;
	mn_doItemOver(sId);
}

//called when mousing over a main menu item
function mn_doItemOver(sId){
	//hide current sub menu
	if (mn_current != null){
		mn_current.style.display = "none";
		document.getElementById("MenuSub").style.display = "none";
	}
	
	//show new sub menu (if any)
	var objSub = mn_arrSubs[sId];
	if(objSub != null){
		objSub.style.display = "block";
		mn_current = objSub;
		document.getElementById("MenuSub").style.display = "block";
	}
}

//do the initialization here so we don't have to call it from the page
mn_crawlMenu();