//This is the menu script.
var actvMnuID = "";
var curvMnuID = "";
var unlocked = true;
var mnuChldLft;
var mnuParntLeft;
var NetPageY;
var cstHyphn = "-";


function mnuInit()
{
	if (navigator.appName == "Netscape")
	{
		// This array needs a new element for each menu section that is added.
		layerRef = "document.layers";
		styleSwitch = "";
		ns = true;
		ie = false;
		/* we have to wait a few seconds before setting onresize for Nav. 
		See comments in body_onload() below. */
	//  window.onresize = NavReload;
	}
	else
	{
		// This array needs a new element for each menu section that is added.
		layerRef = "document.all";
		styleSwitch = ".style";
		ns = false;
		ie = true;
	}

	//set function to capture mousemove events
	(ns)? window.onmousemove = moveHandler : document.onmousemove = moveHandler;
	if (ns) window.captureEvents(Event.MOUSEMOVE);
}

function moveHandler(e)
{
var x;
var y;
var mnuTop;
var mnuLft;
var mnuBotm;
var mnuRght;
var mnuHght;
var mnuWdth;
var strHyphn;

	if(ns) NetPageY=e.pageY;  // if netscape, capture the pageY value for use in the mnu funtion
	if (actvMnuID != "")
	{
		x = (ns)? e.pageX:event.clientX+document.body.scrollLeft;
		y = (ns)? e.pageY:event.clientY+document.body.scrollTop;

		mnuTop = (ns)? document.layers[actvMnuID].top:document.all[actvMnuID].offsetTop;
		mnuLft = (ns)? document.layers[actvMnuID].left:document.all[actvMnuID].offsetLeft;
		
		//the div's width/height in IE4 is much larger than the table it contains, so i have to use the table's width/height instead
		mnuHght = (ns)? document.layers[actvMnuID].clip.height:document.all[actvMnuID + "Tbl"].clientHeight;
		mnuWdth = (ns)? document.layers[actvMnuID].clip.width:document.all[actvMnuID + "Tbl"].clientWidth;
		
		mnuBotm = mnuTop + mnuHght;
		mnuRght = mnuLft + mnuWdth;
		//determines gaps for horizontal menus
		if(((actvMnuID.indexOf("C"))==-1)&&((actvMnuID.indexOf("P"))==-1))		
			//extra space at top for the gap between the mnu and the heading text on horizontal menu
			mnuTop -= 6;
		if ((x < mnuLft || x > mnuRght || y < mnuTop || y > mnuBotm) && unlocked) 
		{
			hideMnu(actvMnuID);
			//if the active menu is a child menu item, which we just hide
			if((actvMnuID.indexOf("C"))>-1)
			{
				//make the active menu the parent menu and determine if we need to hide it as well
				//by checking the mouse location in relation to the parent menu area, if the mouse
				// is over the parent area, do not hide it, otherwise, hide it.
				actvMnuID =actvMnuID.replace("C","P");
				strHyphn = actvMnuID.search(cstHyphn);
				actvMnuID  = actvMnuID.substr(0,strHyphn);
			}
		}
	}
	return true;
}



// Nav 4.07 has a bug where resize is fired once onload is done.
// to get around this we wait a few seconds before setting onresize
function NavSetReload()
{
    window.onresize = NavReload;
}

// Nav has a bug where if anything on the page has position:absolute, 
// visibility:hidden, or z-index: anything, when you resize the page,
// the content gets screwed up.
// As a work-around, we just reload the page whenever a resize event occurs. 
function NavReload()
{
    window.location.reload();
}

function mnuOpen(layerNum)
{
	mnu("subMnu" + layerNum);
}


function mnu(layerName)
{

	var mnuVTop;
	var tmpvMnuID;
	var strHyphn;
	var strIndex;
	var mnuParntWdth;
	var mnuParntRght;


	//if we aren't trying to open a 'child' menu item, hide the active menu 
	if((layerName.indexOf("C"))==-1)
	{
		if (actvMnuID != "")
			hideMnu(actvMnuID);
	}
	actvMnuID = layerName;
	//if the menu element we are trying to open is a child, position it relative to the parent and make visible
	//-1 means the substring was NOT found
	if((layerName.indexOf("C"))>-1)
	{
		tmpvMnuID =actvMnuID.replace("C","P");  //we need a temp var to hold the parent menu of the current child
		strHyphn = tmpvMnuID.search(cstHyphn);    // find the hyphen 
		tmpvMnuID  = tmpvMnuID.substr(0,strHyphn);   //and strip everything to the left leaving the parent menu name
		//determine the top of the object from which the event was fired
		mnuVTop =(ns)? (NetPageY-(NetPageY-document.layers[tmpvMnuID].top)):(event.clientY - window.event.offsetY);		
		strHyphn = layerName.search(cstHyphn)+ 1;    //find the hyphen in the menu name
		strIndex = layerName.substr(strHyphn,1);  //get the first value after the hyphen
			//determine if the child menu is zero level, ie. 2-0, then set a spacer to compensate for menu borders
		if(strIndex =="0")
			mnuVTop -= 2;
		else
			mnuVTop -= 4;		
		//find the width of the parent menu
		mnuParntWdth = (ns)? document.layers[tmpvMnuID].width:document.all[tmpvMnuID].offsetWidth;
		mnuParntRght =(ns)? document.layers[actvMnuID].left:document.all[tmpvMnuID].offsetLeft;   //get the right edge of the parent menu	
		mnuChldLft = mnuParntRght + mnuParntWdth;
		mnuVTop += document.body.scrollTop;
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.position="absolute"');
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.top=' + (mnuVTop));
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.left=' + (mnuChldLft));
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
	}
	else if((layerName.indexOf("P"))>-1)
	{

		//determine the top of the object from which the event was fired
		mnuVTop =(ns)? (NetPageY-(NetPageY-document.layers[actvMnuID].top)):(event.clientY - window.event.offsetY);
		//dymanically set the Parent menu location
		mnuVTop -= 1;
		if(mnuParntLeft == undefined)   //the left edge of the grandparent menu item only has to be set once
		{
			//declare the parent id of the side navigation menu(I don't like to hardcode but could find no other way, given the current restrictions)
			var actvSidMnuID= 'sidenav';
			mnuParntLeft = (ns)? document.layers[actvSidMnuID].left:document.all[actvSidMnuID].offsetLeft;
			var mnuParntWdth = (ns)? document.layers[actvSidMnuID].width:document.all[actvSidMnuID].offsetWidth;
			mnuParntLeft = mnuParntLeft + mnuParntWdth;
		}
		mnuVTop += document.body.scrollTop;
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.position="absolute"');
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.top=' + (mnuVTop));
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.left=' + (mnuParntLeft));
		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
	}
	else
	{
		//if the user trys to access this menu before it finishes loading, the layerName will be undefined
		if (layerName!= undefined)
		{
			eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
		}
	}
	unlocked = false;
}


function mnuClose()
{
	unlocked = true;
}


function hideMnu(id)
{
	if (unlocked) eval(layerRef + '["' + id + '"]' + styleSwitch + '.visibility="hidden"');
}



