var oMainMenu;              // The td object that houses the top-level menu links.
var aMainMenus = new Array; // An array of main menu objects to accommodate multiple instances of the control.
var oMainMenuLink;          // The anchor tag object of the first-level menu.
var oFlyout;                // First-level flyout menu <div> object.
var oChildMenu;             // Child menu <div> object.
var aChildMenuDefinition;   // Child Menu Definition array used for tracking active child menus.
var aChildMenusCollection;  // Collection of aChildMenuDefinition arrays for all currently active child menus.
var oChildMenuLink;         // The anchor tag object of each subsequent child menu.
var sOriginalLinkText;      // Original display text for the first-level link.
var sFontSize = "12px";     // Current font size for menu link text.
var sBaseFontSize = "12px"; // Base font size.
var iMenuPos;               // Starting position for the flyout menu.
var arMenuPos;              // Array of starting positions for flyout menus.
var sTempID;                // Temporary placeholder for IDs.
var iTimer;                 // ID variable for timer function.
// Array management functions...
function AddItemToArray(aArray, oItem){
	if(aArray == null || aArray.length == 0) aArray = new Array(oItem);
	else aArray.push(oItem);
	return aArray;
}
function PullItemFromArray(aArray){
	if(aArray == null || aArray.length == 0) return null;
	else return aArray.pop();
}
// Get the top-level menu object.
for(i=0; i<document.getElementsByTagName("td").length; i++){
	sTempID = document.getElementsByTagName("td").item(i).id;
	if(sTempID > "" && sTempID.substring(sTempID.indexOf("_") + 1, sTempID.length) == "tdMenuContainer"){
		oMainMenu = document.getElementById(sTempID);
		aMainMenus = AddItemToArray(aMainMenus, oMainMenu);
	}
}
// Get a reference to the attached style sheet.
var oStyleSheet, sHref;
for(i=0; i<document.styleSheets.length; i++){
  oStyleSheet = document.styleSheets[i];
  sHref = oStyleSheet.href.toLowerCase();
  // If the currently selected style sheet is anything other than the
  // print style sheet, we have the right one. Break out of the loop.
  if(sHref.indexOf("print") == -1) break;  
}
if(oStyleSheet.cssRules){
	var objStyles = oStyleSheet.cssRules;		// Netscape/Firefox-type reference to attached style sheet.
	var bIsMozilla = true;
} else {
	var objStyles = oStyleSheet.rules;			// IE-type reference to attached style sheet.
	var bIsMozilla = false;
}
// Get the font sizes from the .aBrowser class in the attached style sheet.
for(i=0; i<objStyles.length; i++){
  if(objStyles[i].selectorText == ".aBrowser")
		sBaseFontSize = objStyles[i].style.fontSize;			
	if(objStyles[i].selectorText == ".aBrowser:hover")
		sFontSize = objStyles[i].style.fontSize;
	if(sFontSize == undefined || sFontSize == "")
	  sFontSize = sBaseFontSize;
}
// Show the selected first-level submenu.
function ShowSubMenu(sLinkID){
	var oAnchor;             // Current anchor tag.
	var sCatName;            // Catalog/Category name.
	var iLength = 0;         // Length of the longest anchor label in the current menu.
	var oHidden;             // HTML input hidden tag that holds the current catalog or category name.
	var oCurrNode;           // Current node object, used for walking the object hierarchy.
	var iTopPixels = 0;      // Number of pixels from the top of the screen to the top of the sub menu.
  var iCurrLink = -1;      // Ordinal position of the link being hovered over, used for positioning its child menu.
	// If there is a currently displayed flyout menu, get rid of it.
	if(oFlyout!=null || aChildMenusCollection!=null) HideSubMenu();
	// Use the following information to pick which flyout to display,
	// based on the catalog or category selected...
	sTempID = sLinkID.substring(0, sLinkID.lastIndexOf("_") + 1);
	if(document.getElementById(sTempID + "txtHdnCatalog") != null)
		oHidden = document.getElementById(sTempID + "txtHdnCatalog");
	else
		oHidden = document.getElementById(sTempID + "hdntxtCategoryName");
	// Set the catalog/category name.
	sCatName = oHidden.value;
	// Get the flyout menu object.
	oFlyout = document.getElementById(sLinkID.substring(0, sLinkID.indexOf("_") + 1) + "|" + sCatName + "_browserFlyout");
	// Show the indicator arrow and continue, if there is a flyout menu to show.	
	if(oFlyout != null){
		oMainMenuLink = document.getElementById(sLinkID);
		sOriginalLinkText = oMainMenuLink.innerHTML;
		if(oFlyout.getElementsByTagName("a").length == 0)
		  return;
		if(oMainMenuLink.innerHTML.length < 18)
		  oMainMenuLink.innerHTML += "&nbsp;&gt;";
	}else	return;	
	// Find the appropriate width for the flyout menu.
	for(x=0; x<oFlyout.getElementsByTagName("a").length; x++){
		var regSpace = /&nbsp;|&emsp;/g;
    var regAmp = /&amp;/g;
    var regPoint = /&nbsp;&gt;/g;
    var tmpString = "";
    // Set up a temporary version of the innerHTML string and clean it up.
  	oAnchor = oFlyout.getElementsByTagName("a")[x];
  	tmpString = oAnchor.innerHTML;
  	tmpString = tmpString.replace(regPoint, "");
  	tmpString = tmpString.replace(regSpace, " ");			
  	tmpString = tmpString.replace(regAmp, "&");						
  	if(iLength < tmpString.length) iLength = tmpString.length;
  	// Replace any regular spaces in the anchor tag with non-breaking spaces.
  	regSpace = /\s/g;
  	oAnchor.innerHTML = oAnchor.innerHTML.replace(regSpace, "&nbsp;");
	}
	// Walk up the tree from the oMainMenu to find its true position on the page.
	for(i=0; i<aMainMenus.length; i++){
	  sTempID = aMainMenus[i].id;
	  if(sTempID.substring(0, sTempID.indexOf("_")) == oFlyout.id.substring(0, oFlyout.id.indexOf("_"))){
	    oMainMenu = aMainMenus[i];
	    break;
	  }
	}
	iTopPixels = oMainMenuLink.offsetTop;
	oCurrNode = oMainMenuLink.offsetParent;
	while(oCurrNode.tagName != "BODY"){
		if(oCurrNode.tagName != null){
			iTopPixels += oCurrNode.offsetTop;
			oCurrNode = oCurrNode.offsetParent;
		}
	}
	// Find which link in the main menu is being hovered over.
	for(n=0; n<oMainMenu.getElementsByTagName("a").length; n++){
		iCurrLink += 1;
		if(oMainMenu.getElementsByTagName("a")[n].id == oMainMenuLink.id) break;
	}	
	// Add the appropriate number of pixels to iMenuPos.
	iMenuPos = iTopPixels - (parseInt(sBaseFontSize) * .5);
	// Set the top position and width of the flyout and show it.
	oFlyout.style.top = parseInt(iMenuPos);
	oFlyout.style.width = (iLength + 3) * (parseInt(sFontSize) * .7) + "px";
	oFlyout.style.display = "block";	
}
function ShowSubMenuChild(sLinkID){
	var sChildItemID;                           // The ID for the child menu div.
	var sLinkElements = sLinkID.split("_");     // Split the incoming link ID into an array of strings.
	var sCategoryName = sLinkElements[2];       // The current category name, extracted from the array.
	var sDispName = document.getElementById(sLinkID).innerHTML;	// The display text of the current link.
	var oAnchor;                                // The current anchor tag under the mouse cursor.
	var iLength = 0;                            // Length of the longest link text in the current menu.
	var iLinkOrder = 0;                         // Ordinal position of the current link.
	var oParentMenu;                            // Parent menu object.
	// ID of the div that contains the current link.
	var sParentMenuID =
			document.getElementById(sLinkID).parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	// Determine whether the currently displayed child menu(s) (if any)
	// should go away or stick around for a while...
	if(sDispName.indexOf("&nbsp;&gt;") == -1){	
		// The link the cursor is hovering over is NOT the one displaying its
		// child menu, so hide all down-level child menus before continuing.
		HideSubMenuChildren(sParentMenuID, sLinkID);
	}else{
		// The cursor is hovering over a link that is displaying its child menu.
		// Strip the carets from the display name variable before proceeding.
		sDispName = sDispName.substring(0, sDispName.indexOf("&nbsp;&gt;"));
	}
	// Loop through the divs on the page and find the correct child menu to display.
	for(i=0; i<document.getElementsByTagName("div").length; i++){
		sChildItemID = document.getElementsByTagName("div").item(i).id;
		if(sChildItemID.substring(sChildItemID.indexOf("|") + 1, sChildItemID.length) == sCategoryName + "_browserFlyout"){
			sParentMenuID =
				document.getElementById(sLinkID).parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id;
			// Store the IDs of the child menu and its parent in an array object.
			aChildMenuDefinition = new Array(sChildItemID, sParentMenuID);
			// Now add that array to an array of aChildMenuDefinition objects.
			aChildMenusCollection = AddItemToArray(aChildMenusCollection, aChildMenuDefinition);
			// Show the child menu link indicator arrow.
			oChildMenuLink = document.getElementById(sLinkID);
			if(oChildMenuLink.innerHTML.indexOf("&nbsp;&gt;") == -1)
				oChildMenuLink.innerHTML += "&nbsp;&gt;";
			// Set the left edge of the child menu based on the position and width of its parent.
			oChildMenu = document.getElementById(sChildItemID);
			if(bIsMozilla){
			  oChildMenu.style.left = 3 + parseInt(document.getElementById(sParentMenuID).style.left)
									                + parseInt(document.getElementById(sParentMenuID).style.width);
			} else {
			  oChildMenu.style.left = parseInt(document.getElementById(sParentMenuID).style.left)
									            + parseInt(document.getElementById(sParentMenuID).style.width);
			}
			// Find the length of the longest string in the child menu and use it to set the menu's width.
			iLength = 0;				
			for(p=0; p<oChildMenu.getElementsByTagName("a").length; p++){
			  var regSpace = /&nbsp;/g;
			  var regAmp = /&amp;/g;
			  var regPoint = /&nbsp;&gt;/g;
			  var tmpString = "";
        // Set up a temporary version of the innerHTML string and clean it up.
				oAnchor = oChildMenu.getElementsByTagName("a")[p];
				tmpString = oAnchor.innerHTML;
				tmpString = tmpString.replace(regPoint, "");
				tmpString = tmpString.replace(regSpace, " ");			
				tmpString = tmpString.replace(regAmp, "&");						
				if(iLength < tmpString.length) iLength = tmpString.length;
				// Replace any regular spaces in the anchor tag with non-breaking spaces.
				regSpace = /\s/g;
				oAnchor.innerHTML = oAnchor.innerHTML.replace(regSpace, "&nbsp;");			
			}
			// Set the width of the child menu.						
			if(parseInt(oChildMenu.style.width) != (iLength + 3) * (parseInt(sFontSize) * .7))
			  oChildMenu.style.width = (iLength + 3) * (parseInt(sFontSize) * .7) + "px";
			// Get the ordinal position of the current link in the parent menu.
			oParentMenu = document.getElementById(sParentMenuID);
			for(n=0; n<oParentMenu.getElementsByTagName("a").length; n++){
				oAnchor = oParentMenu.getElementsByTagName("a")[n];
				if(oAnchor.innerHTML.indexOf("&nbsp;&gt;") != -1)	iLinkOrder = n;
			}
			// Set the vertical position of the child menu and show it.
			// NOTE: The line directly below sets the child menus so that they will be displayed
			//		   next to their parent links. The following line sets the top of each child menu
			//		   even with the top of its parent menu, so that all submenus are at the same height.
			//		   Use one or the other, but not both.
			oChildMenu.style.top = parseInt(oParentMenu.style.top) + ((parseInt(sBaseFontSize) + 5) * iLinkOrder);
			//oChildMenu.style.top = parseInt(oParentMenu.style.top);
			oChildMenu.style.display = "block";
			break;
		}
	}
}
function BeginHideMenus(){
  iTimer = setTimeout("HideSubMenu()", 800); // Change this number to increase or decrease the delay.
}
function DontHideMenus(){
  if(iTimer != null && iTimer != undefined)
    clearTimeout(iTimer);
}  
function HideSubMenu(){
	// Hide all child submenus.
	if(aChildMenusCollection!=null){
		for(i=0; i<aChildMenusCollection.length; i++){
			aChildMenuDefinition = aChildMenusCollection[i];
			oChildMenu = document.getElementById(aChildMenuDefinition[0]);
			oChildMenu.style.display = "none";
		}
		aChildMenusCollection = null;
	}
	// Hide the first-level submenu.
	if(oFlyout!=null){
		oFlyout.style.display = "none";
		oMainMenuLink.innerHTML = sOriginalLinkText;
	}
	// Reset any link labels that might have an indicator arrow.
	var oAnchor;
	for(i=0; i<document.getElementsByTagName("a").length; i++){
		oAnchor = document.getElementsByTagName("a")[i];
		if(oAnchor.innerHTML.indexOf("&nbsp;&gt;") != -1)
			oAnchor.innerHTML = oAnchor.innerHTML.substring(0, oAnchor.innerHTML.indexOf("&nbsp;&gt;"));
	}
}
function HideSubMenuChildren(sParentID, sLinkID){
  // Hide all submenus that are children of the given menu.
	if(aChildMenusCollection!=null){
	  var oAnchor;
	  var oParentDiv = document.getElementById(sParentID);
	  var sChildID = "";
	  // Find the submenu.
		for(i=0; i<aChildMenusCollection.length; i++){
			aChildMenuDefinition = aChildMenusCollection[i];
			if(aChildMenuDefinition[1] == sParentID){
			  sChildID = aChildMenuDefinition[0];
			  oChildMenu = document.getElementById(aChildMenuDefinition[0]);
			  // Reset any indicator arrows on the submenu.
			  for(n=0; n<oChildMenu.getElementsByTagName("a").length; n++){
			    oAnchor = oChildMenu.getElementsByTagName("a")[n];
			    if(oAnchor.innerHTML.indexOf("&nbsp;&gt;") != -1)
			      oAnchor.innerHTML = oAnchor.innerHTML.substring(0, oAnchor.innerHTML.indexOf("&nbsp;&gt;"));
			  }
			  // Hide the submenu.
			  oChildMenu.style.display = "none";
			}			
		}
		for(i=0; i<aChildMenusCollection.length; i++){
		  aChildMenuDefinition = aChildMenusCollection[i];
			if(sChildID != "" && aChildMenuDefinition[1] == sChildID){
			  oChildMenu = document.getElementById(aChildMenuDefinition[0]);
			  // Reset any indicator arrows on the submenu.
			  for(n=0; n<oChildMenu.getElementsByTagName("a").length; n++){
			    oAnchor = oChildMenu.getElementsByTagName("a")[n];
			    if(oAnchor.innerHTML.indexOf("&nbsp;&gt;") != -1)
			      oAnchor.innerHTML = oAnchor.innerHTML.substring(0, oAnchor.innerHTML.indexOf("&nbsp;&gt;"));
			  }
			  // Hide the submenu.
			  oChildMenu.style.display = "none";			  
			}
		}		
		// Reset indicator arrows on all links except the current link in the given parent menu.	 
	  for(i=0; i<oParentDiv.getElementsByTagName("a").length; i++){
		  oAnchor = oParentDiv.getElementsByTagName("a").item(i);
		  if(oAnchor.id != sLinkID && oAnchor.innerHTML.indexOf("&nbsp;&gt;") != -1)
			  oAnchor.innerHTML = oAnchor.innerHTML.substring(0, oAnchor.innerHTML.indexOf("&nbsp;&gt;"));
	  }	
	}
}
