//This is the menu script.
var intnumofmenuitems=8;
var stupidHackOffset = -8; //for aligning menu correctly when page loads
var NavBarWidth=738;
var activeMenuID = "";
var unlocked = true;

if (navigator.appName == "Netscape")
{
	var aPosition = new Array(55,64,170,211,315,403,537,615);
	layerRef="document.layers";
	styleSwitch="";
	ns = true;
	ie = false;
//    we have to wait a few seconds before setting onresize for
//    Nav. See comments in alignMenus() below.	
//    window.onresize  = NavReload;
    document.onload  = alignMenus;
}
else
{
	var aPosition = new Array(55,64,170,211,315,403,537,615);
	layerRef="document.all";
	styleSwitch=".style";
	ns = false;
	ie = true;
    window.onresize  = alignMenus;
    document.onload  = alignMenus;
}

//set function to capture mousemove events
(ns)? window.onmousemove = moveHandler : document.onmousemove = moveHandler;
if (ns) window.captureEvents(Event.MOUSEMOVE);

function moveHandler(e)
{
	if (activeMenuID != "")
	{
		var x = (ns)? e.pageX:event.clientX+document.body.scrollLeft;
		var y = (ns)? e.pageY:event.clientY+document.body.scrollTop;
		
		var menuTop = (ns)? document.layers[activeMenuID].top:document.all[activeMenuID].offsetTop;
		var menuLeft = (ns)? document.layers[activeMenuID].left:document.all[activeMenuID].offsetLeft;
		
		//the div's width/height in IE4 is much larger than the table it contians, so i have to use the table's width/height instead
		var menuHeight = (ns)? document.layers[activeMenuID].clip.height:document.all[activeMenuID + "Table"].clientHeight;
		var menuWidth = (ns)? document.layers[activeMenuID].clip.width:document.all[activeMenuID + "Table"].clientWidth;
		
		var menuBottom = menuTop + menuHeight;
		var menuRight = menuLeft + menuWidth;
		
		//extra space at top for the gap between the menu and the heading text
		menuTop -= 6; 
		
		if ((x < menuLeft || x > menuRight || y < menuTop || y > menuBottom) && unlocked) 
		{
			//alert("[" + menuTop + " " + y + " " + menuBottom + "]\n[" + menuLeft + " " + x + " " + menuRight + "]");
			hidemenu(activeMenuID);
			activeMenuID = "";
		}
	}
	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 alignMenus()
{
	if (ns) intNavOffset = Math.max(0, (window.innerWidth - NavBarWidth)/2) + stupidHackOffset;
	if (ie) intNavOffset = Math.max(0, (document.body.clientWidth - NavBarWidth)/2) + stupidHackOffset;
	intmenunumber=intnumofmenuitems;		
	while (intmenunumber>0)
	{
		intLeft = aPosition[intmenunumber-1] + intNavOffset;
		intcountdown="Submenu"+intmenunumber;
		
		eval(layerRef + '["' + intcountdown + '"]' + styleSwitch + '.left="' + intLeft + '"');
		
		intmenunumber--;
	}
	stupidHackOffset = 0; //only needed the offset value once
	
    // 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
	if (ns)
	{
	    window.setTimeout("NavSetReload()", 3000);
	}
}

function menuwrapper(layerNum)
{
	menu("Submenu" + layerNum);
}

function menu(layerName)
{
	if (activeMenuID != "") hidemenu(activeMenuID);
	activeMenuID = layerName;
	eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
	unlocked = false;
}

function unlockmenus()
{
	unlocked = true;
}

function hidemenu(id)
{
	if (unlocked) eval(layerRef + '["' + id + '"]' + styleSwitch + '.visibility="hidden"');
}

