/*
+-------------------------------------------------------------------------------------------+
|  Initialize global variables                                                              |
+-------------------------------------------------------------------------------------------+*/
//if this is a netscape <6.0 machine
if (document.layers ) {
	visible = 'show';
	hidden = 'hide';
//else if the browser does not support layers (IE), then define these commands in DIV syntax
}else{
	visible = 'visible';
	hidden = 'hidden';
}
//if this is IE, set a variable to true
var IE = document.all?true:false

var intMENU_ARRAY_SIZE = 4;
var intMENU_LINK_TEXT_INDEX = 0;
var intMENU_LINK_IMAGE_INDEX = 1;
var intMENU_LINK_URL_INDEX = 2;
var intMENU_PARENT_INDEX = 3;
var intMENU_ITEMS_ARRAY_INDEX = 4;


var intMENU_ITEM_ARRAY_SIZE = 2
var intMENU_ITEM_LINK_TEXT = 0;
var intMENU_ITEM_LINK_URL = 1;
var intMENU_ITEM_LINK_TARGET_MENU = 2;
var intMostRecentlyViewedMenu = -1;
var bolViewedMostRecentMenu = false;	
	
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Variables to hold mouse x-y pos.s
var mouseX = 0
var mouseY = 0

//create an array of menus
var arrMenuArray = new Array();
/*
+-------------------------------------------------------------------------------------------+*/

	
/*
+-------------------------------------------------------------------------------------------+
| Global functions for displaying menus                                                     |
+-------------------------------------------------------------------------------------------+*/
function addMenu(strMenuLinkText, strMenuLinkURL, strMenuLinkImage, intParentMenuIndex, intParentMenuLinkIndex){
	intNewMenuIndex = arrMenuArray.length;
	arrMenuArray[intNewMenuIndex] = new Array(intMENU_ARRAY_SIZE);	
	arrMenuArray[intNewMenuIndex][intMENU_LINK_TEXT_INDEX] = strMenuLinkText;
	arrMenuArray[intNewMenuIndex][intMENU_LINK_URL_INDEX] = strMenuLinkURL;
	arrMenuArray[intNewMenuIndex][intMENU_LINK_IMAGE_INDEX] = strMenuLinkImage;
	
	arrMenuArray[intNewMenuIndex][intMENU_PARENT_INDEX] = intParentMenuIndex;
	if (intParentMenuIndex != null){
		arrMenuArray[intParentMenuIndex][intMENU_ITEMS_ARRAY_INDEX][intParentMenuLinkIndex][intMENU_ITEM_LINK_TARGET_MENU] = intNewMenuIndex;
	}
	arrMenuArray[intNewMenuIndex][intMENU_ITEMS_ARRAY_INDEX] = new Array();
	return intNewMenuIndex;
}
	
function addMenuItem(intMenuIndex, strMenuItemText, strMenuItemURL){
	intNewMenuItemIndex = arrMenuArray[intMenuIndex][intMENU_ITEMS_ARRAY_INDEX].length;
	arrMenuArray[intMenuIndex][intMENU_ITEMS_ARRAY_INDEX][intNewMenuItemIndex] = new Array(intMENU_ITEM_ARRAY_SIZE);
	arrMenuArray[intMenuIndex][intMENU_ITEMS_ARRAY_INDEX][intNewMenuItemIndex][intMENU_ITEM_LINK_TEXT] = strMenuItemText;
	arrMenuArray[intMenuIndex][intMENU_ITEMS_ARRAY_INDEX][intNewMenuItemIndex][intMENU_ITEM_LINK_URL] = strMenuItemURL;
	return intNewMenuItemIndex;
}


function showMenus(strContainerForPopus, strMenuToDisplay, strLayout){
	var objMenuDivs = getObject(strContainerForPopus);
	var objMenuLinks = getObject(strMenuToDisplay);

	var strMenuText = "";
	var strMenuDivs = "";
	var intMaxLinkLength = 0;
	var rExp = /MAXLEN/g;
	//strLayout = strLayout.toUpperCase();
	
	//iterate through the visible menu links
	for (var intCounter = 0; intCounter < arrMenuArray.length; intCounter++){
		var intMaxItemLinkLength = 0;
		//write out a DIV for each menu 
		strMenuDivs += "<div class=\"top\" id=\"MenuDiv" + intCounter + "\">";
		//iterate through the links for menu items 	
		for (var intItemCounter = 0; intItemCounter < arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX].length; intItemCounter++){
			strMenuDivs += "<a class=\"popUpLink\" style=\"width:MAXLEN;\" href=\""
			strMenuDivs += arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX][intItemCounter][intMENU_ITEM_LINK_URL]
			strMenuDivs += "\" "
			if (arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX][intItemCounter][intMENU_ITEM_LINK_TARGET_MENU] != null){
				strMenuDivs += " onMouseover=\"showMenuByIndex(" + arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX][intItemCounter][intMENU_ITEM_LINK_TARGET_MENU] + ",'SubMenuLink" + intCounter + "_" +intItemCounter + "', 'VERTICAL' )\" "
			}else{
				strMenuDivs += " onMouseover=\"if(intMostRecentlyViewedMenu != " + intCounter + ") {hideMenuByIndex(intMostRecentlyViewedMenu);intMostRecentlyViewedMenu="+intCounter+";bolViewedMostRecentMenu=true }\" "
			}
			strMenuDivs += " id=\"SubMenuLink" + intCounter + "_" +intItemCounter + "\">"
			strMenuDivs += 	arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX][intItemCounter][intMENU_ITEM_LINK_TEXT] + "</A>"
			//set a variable for the longest link text in the popup.  use to set witdth
			if (arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX][intItemCounter][intMENU_ITEM_LINK_TEXT].length > intMaxItemLinkLength)
				intMaxItemLinkLength = arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX][intItemCounter][intMENU_ITEM_LINK_TEXT].length;
		}
		
		
		strMenuDivs = strMenuDivs.replace(rExp, ((intMaxItemLinkLength*7)+37).toString() )
		//end menu div
		strMenuDivs += "</div>";

		//write out a menu link for each item
		if((arrMenuArray[intCounter][intMENU_LINK_URL_INDEX] !="")&&(arrMenuArray[intCounter][intMENU_LINK_URL_INDEX] !=null)){
			if (strLayout=="VERTICAL"){
				strMenuText += "<a style=\"width:MAXLEN;\" "
			}else{
				strMenuText += "<a "
			}
			strMenuText += " class=\"visibleLink\" href=\"" + arrMenuArray[intCounter][intMENU_LINK_URL_INDEX] + "\""
			
			strMenuText += " onMouseover=\"showMenuByIndex(" + intCounter + ",'MenuLink" + intCounter + "', '" + strLayout + "'  )\"" 

			if(arrMenuArray[intCounter][intMENU_ITEMS_ARRAY_INDEX].length ==0){
				strMenuText += " onMouseout=\"hideMenuByIndex("+intCounter+")\""; 
			}

			strMenuText += " id=\"MenuLink" + intCounter + "\">"
			
			if(arrMenuArray[intCounter][intMENU_LINK_IMAGE_INDEX] != null) {
				strMenuText += 	"<img src=\" " + arrMenuArray[intCounter][intMENU_LINK_IMAGE_INDEX] 
				strMenuText += 	" \" border=\"0\" > "
			}
			strMenuText += arrMenuArray[intCounter][intMENU_LINK_TEXT_INDEX] + "</a><span class=\"TopMenu\">&nbsp;|&nbsp;</span>"
		}
		
		//set a variable for the longest link text in the visible menu.  use to set witdth
		if (arrMenuArray[intCounter][intMENU_LINK_TEXT_INDEX].length > intMaxLinkLength)
			intMaxLinkLength = arrMenuArray[intCounter][intMENU_LINK_TEXT_INDEX].length;
	}
	strMenuText = strMenuText.replace(rExp, (intMaxLinkLength*9+5).toString() )
	objMenuLinks.innerHTML = strMenuText;
	objMenuDivs.innerHTML = strMenuDivs;

}
	


	
//get the layer corresponding to the passed name
function getDivObject(strDivName){
	//if the browser supports layers (netscape)
	if (document.layers) {
		objDiv = document.layers[strDivName];
	}
	//else if the browser does not support layers (IE)
	else if (document.all) {
		objDiv = document.all(strDivName).style;
	}
	//else if this is a netscape 6 machine
	else if(document.getElementById(strDivName)) {
		objDiv = document.getElementById(strDivName);
	}
	return 	objDiv;
}

//get the object corresponding to the passed name
function getObject(strName){
	var objObject;
	//if IE
	if (document.all) {
		objObject = document.all(strName);
	}
	//else if this is a netscape 6 machine
	else if(document.getElementById(strName)) {
		objObject = document.getElementById(strName);
	}
	return 	objObject;
}


//set the layer corresponding to the passed name visible
function setVisible(name){
	objMyDiv = getDivObject(name);
	if (navigator.appName + parseInt(navigator.appVersion) == "Netscape5")
		objMyDiv.style.visibility = visible;
	else
		objMyDiv.visibility = visible;
}
	
//set the DIV XY coordinates
function setLocation(name, intXCoord, intYCoord){
	objMyDiv = getDivObject(name);
	//objMyDiv = getObject(name);
		
	if  (navigator.appName + parseInt(navigator.appVersion) == "Netscape5"){
		objMyDiv.style.left = intXCoord;
		objMyDiv.style.top = intYCoord;
	}else{
		objMyDiv.pixelLeft = intXCoord;
		objMyDiv.pixelTop = intYCoord;
	}	
}
	
//set the layer corresponding to the passed name hidden
function setHidden(name){
	objMydiv = getDivObject(name);
	if (navigator.appName + parseInt(navigator.appVersion) == "Netscape5")
		objMydiv.style.visibility = hidden;
	else
		objMydiv.visibility = hidden;
}

function getPageCoords (elementId) {
	var coords = {x: 0, y: 0, height:0, width:0}
	var element;
	if (document.all)
		element = document.all[elementId];
	else if (document.getElementById)
		element = document.getElementById(elementId);

	coords.height = element.offsetHeight;
	coords.width = element.offsetWidth;
		
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}

// Get the mouse position
function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		mouseX = event.clientX + document.body.scrollLeft
		mouseY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		mouseX = e.pageX
		mouseY = e.pageY
	}  
	// catch possible negative values in NS4
	if (mouseX < 0){mouseX = 0}
	if (mouseY < 0){mouseY = 0}  
		
	//if a menu is open
	if (intMostRecentlyViewedMenu != -1){
		//get the coordinates of the top, bottom, left and right of the most recently opened DIV		
		var intDivTop, intDivBottom, intDivLeft, intDivRight;
		var objObject = getPageCoords('MenuDiv'+intMostRecentlyViewedMenu);
		intDivTop = objObject.y;
		intDivBottom = intDivTop + objObject.height;
		intDivLeft = objObject.x;
		intDivRight = intDivLeft + objObject.width;
		
		//if the mouse is inside the most recently opened menu, then set a flag
		if ((intDivTop < mouseY ) && (intDivBottom > mouseY ) && (intDivLeft < mouseX )  && (intDivRight > mouseX )){
			bolViewedMostRecentMenu = true;
		}
			
		//if the mouse has been over the most recently viewed menu, check to see whether it still is
		if (bolViewedMostRecentMenu == true){
			while(intMostRecentlyViewedMenu != -1){
				//if the mouse is currently over the most recently opened, then do nothing
				objObject = getPageCoords('MenuDiv'+intMostRecentlyViewedMenu);
				intDivTop = objObject.y;
				intDivBottom = intDivTop + objObject.height;
				intDivLeft = objObject.x;
				intDivRight = intDivLeft + objObject.width;
		
				//if the mouse is still inside the most recently opened menu, do nothing
				if ((intDivTop < mouseY ) && (intDivBottom > mouseY ) && (intDivLeft < mouseX )  && (intDivRight > mouseX )){
					bolViewedMostRecentMenu = true;
					return;
				}else{
					//close most recently opened
					hideMenuByIndex(intMostRecentlyViewedMenu);

					//if this menu has a parent, set most recently viewed to that
					if(arrMenuArray[intMostRecentlyViewedMenu][intMENU_PARENT_INDEX] != null){
						intMostRecentlyViewedMenu = arrMenuArray[intMostRecentlyViewedMenu][intMENU_PARENT_INDEX];
					//else set most recently opened to negative one and set viewed flag to false
					}else{
						intMostRecentlyViewedMenu = -1;
						bolViewedMostRecentMenu = false;
						return;
					}
				}					
			}
		}
	}			
	
	return true;
}


function hideMenuByIndex(intMenuIndex){
	try{

		setHidden('MenuDiv'+intMenuIndex);
		
		//set the link background color if there is a link
		if (arrMenuArray[intMenuIndex][intMENU_LINK_TEXT_INDEX]!= ""){
			var objLinkObject = getObject('MenuLink' + intMenuIndex);
			objLinkObject.className  = "visibleLink";
		
		}
	}catch(e){}	
}

//show the menu corresponding to the passed number
function showMenuByIndex(intMenuIndex, strParentName, strLayout){
	try{
		var intOpenY = 0;
		//if there is an open menu and its parent it is not the parent of '
		//window we are opening, close it
		while((intMostRecentlyViewedMenu != -1)&& (intMostRecentlyViewedMenu != arrMenuArray[intMenuIndex][intMENU_PARENT_INDEX])){
			//close most recently opened
			hideMenuByIndex(intMostRecentlyViewedMenu);

			//if this menu has a parent, set most recently viewed to that
			if(arrMenuArray[intMostRecentlyViewedMenu][intMENU_PARENT_INDEX] != null){
				intMostRecentlyViewedMenu = arrMenuArray[intMostRecentlyViewedMenu][intMENU_PARENT_INDEX];
			//else set most recently opened to negative one and set viewed flag to false
			}else{
				intMostRecentlyViewedMenu = -1;
				bolViewedMostRecentMenu = false;
			}
		}

		//set the link background color if there is a link
		if (arrMenuArray[intMenuIndex][intMENU_LINK_TEXT_INDEX]!= ""){
			var objLinkObject = getObject('MenuLink' + intMenuIndex);
			objLinkObject.className  = "visibleLinkHover";
		
		}
		
		//set the Y coordinate to open the window at. Get the coordinates of the passed object
		var ParentCoordinates = getPageCoords(strParentName);
		if (strLayout=="VERTICAL"){
			menuX = ParentCoordinates.x + ParentCoordinates.width ;
			menuY = ParentCoordinates.y;
		}else{
			menuX = ParentCoordinates.x;
			menuY = ParentCoordinates.y + ParentCoordinates.height ;
		}
		setLocation("MenuDiv"+intMenuIndex,menuX,menuY);
		setVisible("MenuDiv"+intMenuIndex);
		intMostRecentlyViewedMenu = intMenuIndex;
		bolViewedMostRecentMenu = false;
	}catch(e){}	
}
/*
+-------------------------------------------------------------------------------------------+*/
