/**<Globals>**/
var strCurrentID = '';
var strCurrentChild = '';
var arrOpen = new Array();
var objActiveMenu = new ActiveMenu("mnuMain",0);
var strOpen;
var loopCount = 0;	
var intHeight = new Number(0);
var intArrLen;
var intArrHeight;
var intBorder = 0;

/**</GlobalVariables>
<Functions>
<Function>
	<Name>ActiveMenu</Name>
	<Parameters>
		<Parameter name="intID" type="integer" description="ID given to the instance of the object"/>
	</Paramters>
	<Comment>
		Used to create a linked list of menus that are open
	</Comment>
**/
function ActiveMenu( intID, intIndex ) {
	this.id = intID;
	this.index = intIndex;
	this.next = null;
}	
/**
</Function>
<Function>
	<Name>GetMenu</Name>
	<Parameters>
		<Parameter name="strID" type="string" description="ID of the menu to search for"/>
	</Paramters>
	<Comment>	
		Searches through the ActiveMenu linked list for the specified menu ID 
		and returns the object associated with it.
	</Comment>
**/
function GetMenu( strID ) {
	var obj = objActiveMenu;
	var objTemp;
	var blnContinue = true;
	
	do {
		if (obj.id == strID) {
			blnContinue = false;
		} else {
			if (obj.next == null) {
				return false;
				blnContinue = false;													
			} else {
				obj = obj.next;
			}
		}					
	} while (blnContinue == true);
	return obj
}

/**
</Function>
<Function>
	<Name>RemoveMenu</Name>
	<Parameters>
		<Parameter name="strID" type="string" description="ID of the menu to search for"/>
	</Paramters>
	<Comment>	
		Searches through the ActiveMenu linked list for the specified menu ID 
		and sets it to null, thus removing it and it's children.
	</Comment>

**/
function RemoveMenu( strID ) {
	var obj = objActiveMenu;
	var objTemp;
	var blnContinue = true;
				
	do {
		if (obj.next == null) {
			blnContinue = false;						
		} else {
			objTemp = obj.next;						
			if (objTemp.id == strID) {												
				obj.next = null;							
				blnContinue = false;
			} else {
				obj = obj.next;
			}
		}
	} while (blnContinue == true);

}
/**
</Function>
<Function>
	<Name>GetIndex</Name>
	<Parameters>
		<Parameter name="objCollection" type="object" description="The menu object (DIV) parent to search"/>
		<Parameter name="strID" type="string" description="The ID to search for"/>		 
	</Paramters>
	<Comment>	
		Searches through the parent object provided to find the specified ID and return it's index
	</Comment>
**/
function GetIndex( objCollection, strID ) {
	var intIndex = 0;
	
	if (document.all.item(strID).className.search("Root") == -1) {
		for( var i=0; i < objCollection.children.length; i++) {
			if (objCollection.children(i).id == strID) {
				intIndex = i;
			break;
			}
		}
	}
	return intIndex;	
}

/**
</Function>
<Function>
	<Name>RootHover</Name>
	<Comment>	
		Display method that handles hovering over a root menu item.
	</Comment>
**/
function RootHover() {
	var objMenu;
	
	//Hide the previously hovered menu
	RootUnhover(true);				
	
	//Get the current menu column
	strCurrentID = window.event.srcElement.id;												
	objMenu = document.all.item(strCurrentID)				
	objMenu.className = "MenuItemRootHover";				
	//Check for children				
	if (objMenu.hasChildren == "1") {
		
		//There is a child menu, so show it
		document.all.item(objMenu.id + "Children").style.display = "block";
		var objRow = document.all.item("mnuMain").rows[0];
		var intColLength = objRow.cells.length;
		var intDistance = new Number(0);
		
		//Cycle through all the columns thus far, 
		//to get the distance to move the child menu
		for (var i=0; i <= intColLength; i++) {
			if (objRow.cells[i].id == strCurrentID) {
				//This is the current menu, so don't add, just quit.
				break;
			} else {							
				//intDistance += parseInt(objRow.cells[i].width);			

				//intDistance += parseInt(objRow.cells[i].clientWidth) + (intBorder * 2);
				intDistance += parseInt(objRow.cells[i].clientWidth) +1;
				//intDistance += (i); //Add border width
			}
		}
		intDistance = intDistance -1;
		//Position the child menu
		document.all.item(objMenu.id + "Children").style.display = "block";					
		document.all.item(objMenu.id + "Children").style.left = intDistance;					
		document.all.item(objMenu.id + "Children").style.top = document.all.item("mnuMain").top;		
		//Put in the array that something has been activated
		OpenMenu(objMenu.id);
	}				
	return true;
		
}

function RootInactiveHover() {
	var objMenu;
	
	//Hide the previously hovered menu
	RootUnhover(true);				
	
	//Get the current menu column
	strCurrentID = window.event.srcElement.id;												
	objMenu = document.all.item(strCurrentID)				
	objMenu.className = "MenuItemRootInactiveHover";				
	//Check for children				
	if (objMenu.hasChildren == "1") {
		
		//There is a child menu, so show it
		document.all.item(objMenu.id + "Children").style.display = "block";
		var objRow = document.all.item("mnuMain").rows[0];
		var intColLength = objRow.cells.length;
		var intDistance = new Number(0);
		
		//Cycle through all the columns thus far, 
		//to get the distance to move the child menu
		for (var i=0; i <= intColLength; i++) {
			if (objRow.cells[i].id == strCurrentID) {
				//This is the current menu, so don't add, just quit.
				break;
			} else {							
				//intDistance += parseInt(objRow.cells[i].width);			

				//intDistance += parseInt(objRow.cells[i].clientWidth) + (intBorder * 2);
				intDistance += parseInt(objRow.cells[i].clientWidth) +1;
				//intDistance += (i); //Add border width
			}
		}						
		//Position the child menu
		document.all.item(objMenu.id + "Children").style.display = "block";					
		document.all.item(objMenu.id + "Children").style.left = intDistance;					
		document.all.item(objMenu.id + "Children").style.top = document.all.item("mnuMain").top;		
		//Put in the array that something has been activated
		OpenMenu(objMenu.id);
	}				
	return true;
		
}


/**
</Function>
<Function>
	<Name>OpenMenu</Name>
	<Parameters>
		<Parameter name="strID" type="string" description="ID of the menu to add"/>
	</Paramters>
	<Comment>	
		Creates an ActiveMenu object at the end of the linked list
	</Comment>
**/
function OpenMenu( strID ) {				
	var obj = objActiveMenu;
	var blnContinue = true;
	
	do {
		if (obj.next == null) {
			blnContinue = false;
		} else {
			obj = obj.next;
		}
	} while (blnContinue == true);
	if (obj.id != strID) {
		obj.next = new ActiveMenu( strID, GetIndex(document.all.item(strID).parentElement, strID) );
	}
}
/**
</Function>
<Function>
	<Name>CloseMenu</Name>
	<Parameters>
		<Parameter name="strID" type="string" description="ID of the menu to search for"/>
	</Paramters>
	<Comment>	
		Closes the menu recursively and removes it from the chain
	</Comment>
**/
function CloseMenu( strID ) {			
	var obj = objActiveMenu;
	var objTemp;
	var blnContinue = true;
					
	do {
		if (obj.next == null) {
			blnContinue = false;
		} else {
			objTemp = obj.next;
			if (objTemp.id == strID) {												
				RecurseClose(obj);
				obj.next = null;							
				blnContinue = false;
			} else {
				obj = obj.next;
			}
		}					
	} while (blnContinue == true);
	
}
/**
</Function>
<Function>
	<Name>RecurseClose</Name>
	<Parameters>
		<Parameter name="objMenuClose " type="object" description="menu object to close"/>
	</Paramters>
	<Comment>	
		Closes all the object's children.
	</Comment>
**/
function RecurseClose( objMenuClose ) {
	var blnContinue = true;
	var obj = objMenuClose;
	var objTemp;
	
	do {
		if (obj.next == null) {						
			blnContinue = false;						
		} else {
			objTemp = obj.next;
			if (document.all.item(objTemp.id + 'Children')) {
				document.all.item(objTemp.id + 'Children').style.display = "none";
			}
			obj = obj.next
		}
	} while (blnContinue == true);
	return true;						
}
/**
</Function>
<Function>
	<Name>CloseMenuChildren</Name>
	<Parameters>
		<Parameter name="strChildID" type="string" description="ID of the child menu to search for"/>
	</Paramters>
	<Comment>	
		Closes the child menu specified.
	</Comment>
**/
function CloseMenuChildren( strChildID ) {			
	var obj = objActiveMenu;
	var objTemp;
	var blnContinue = true;
	var strID;
	
	strID = document.all.item(strChildID).parentElement.id;
	if (strID.search("Children") >0 ) {
		strID = strID.substring(0, strID.search("Children")) ;
	}

	do {
		if (obj.next == null) {
			blnContinue = false;
			
		} else {
			objTemp = obj.next;
			if (objTemp.id == strID) {						
				CloseMenu(strID);
				obj.next = null;
				blnContinue = false;
			} else {
				obj = obj.next;
			}
		}					
	} while (blnContinue == true);
	
}

/**
</Function>
<Function>
	<Name>RootUnhover</Name>
	<Parameters>
		<Parameter name="blnActive" type="boolean" description="override that decides if the method is to timeout or not."/>
	</Paramters>
	<Comment>	
		Display method that handles hovering away from a root menu item.
	</Comment>
**/
function RootUnhover( blnActive ) {				
	
	if (blnActive == true) {					
		
		//If there is a hovered item
		if (strCurrentID != '')  {
			//...grab it and unhover it
			var objMenu = document.all.item(strCurrentID);
			if (!objMenu)
				return true;

			objMenu.className = "MenuItemRoot";
			
			//If the next element is a submenu then don't hide the menu, else hide it
			if (!window.event)
				return true;

			if (window.event.toElement != null) {
				if (window.event.toElement.className != "MenuItem") {								
//					setTimeout("CloseMenu(objMenu.id);",250,"Javascript");
					CloseMenu(objMenu.id);
				}
			} else {							
//				setTimeout("CloseMenu(objMenu.id);",250,"Javascript");
				CloseMenu(objMenu.id);
			}
		} 					
		return true;
	} else {
		//This is a delayed unhover
		setTimeout("RootUnhover(true);",50,"Javascript");
		return true;
	}
}
/**
</Function>
<Function>
	<Name>GetLastMenu</Name>
	<Comment>	
		Cycle through the objActiveMenu array object and return the id of the last menu in the list
	</Comment>
**/
function GetLastMenu() {
	var obj = objActiveMenu;
	var blnContinue = true;
	var strID;
	
	do {
		if (obj.next == null) {
			blnContinue = false;						
			break;
		} 
		obj = obj.next;	
	} while (blnContinue == true);
	return obj.id;
}
/**
</Function>
<Function>
	<Name>ChildHover</Name>
	<Comment>	
		Display method that handles hovering over a child menu item
	</Comment>
**/
function ChildHover() {
	ChildUnhover(true);
	
	//Get the current menu column
	strCurrentChild  = window.event.srcElement.id;												
	objMenu = document.all.item(strCurrentChild )				
	objMenu.className = "MenuItemHover";	
	
	//window.status = GetIndex(objMenu.parentElement, objMenu.id);
	//window.status = 'Children: ' + objMenu.parentElement.children.item(objMenu.id).index;	
	//window.status = 'Children: ' + objMenu.parentElement.children.length;	
	
	//Check for children				
	if (objMenu.hasChildren == "1") {
		//There is a child menu, so show it
		var objChildren = document.all.item(objMenu.id + "Children");
		if (objChildren)
		{
			document.all.item(objMenu.id + "Children").style.display = "block";
			var objRow = document.all.item("mnuMain").rows[0];
			var intColLength = objRow.cells.length;
			var intDistance = new Number(0);
			
			//Cycle through all the columns thus far, 
			//to get the distance to move the child menu
			for (var i=0; i <= intColLength; i++) {
				if (objRow.cells[i].id == strCurrentID) {
					//This is the current menu, so don't add, just quit.
					break;
				} else {							
					intDistance += parseInt(objRow.cells[i].clientWidth);
					//intDistance += (i); //Add border width
				}
			}
			//Position the child menu
			document.all.item(objMenu.id + "Children").style.display = "block";		
			//DisplayArray();
			
			OpenMenu(objMenu.id);
			DisplayArray();		
			
			document.all.item(objMenu.id + "Children").style.pixelLeft = intDistance + ((intArrLen -2) * parseInt(objMenu.parentElement.clientWidth)) + (intBorder *2);									
			document.all.item(objMenu.id + "Children").style.pixelTop = parseInt(document.all.item("mnuMain").top) + parseInt(intArrHeight * parseInt(objMenu.clientHeight));
		}
	}				
	return true;
}

/**
</Function>
<Function>
	<Name>ChildUnhover</Name>
	<Parameters>
		<Parameter name="blnActive" type="boolean" description="override that decides if the method is to timeout or not."/>
	</Paramters>
	<Comment>	
		Display method that handles hovering away from a child menu item.			
	</Comment>
**/
function ChildUnhover( blnActive ) {				
	if (blnActive == true) {					
		
		//If there is a hovered item
		if (strCurrentChild != '')  {
			
			//...grab it and unhover it
			var objMenu = document.all.item(strCurrentChild);
			if (!objMenu)
				return true;

			objMenu.className = "MenuItem";
			
			//If the next element is a submenu then don't hide the menu
			if (!window.event)
				return true;

			if (window.event.toElement != null) {
				var objTo = window.event.toElement; 
				/**
				MULTIPLE SUBMENU ALGORITHM
				
				1	Check to see if the element you are moving to is a menu item itself.  If
					it isn't, then unhover the root.
				**/
				if (objTo.className != "MenuItem") {																						
					RootUnhover(true);
				} else {			
				/**							
				2	If it is, then collect the parent ID (without the 'Children' suffix) of the 
					element you are moving to.  
				**/
					var strParentID;
					strParentID = objTo.parentElement.id;								
					strParentID = strParentID.substring(0, strParentID.search("Children")) ;
				/**
				3	Check to see if the parent ID you just received is the same as the current
					menu's id.  If it is, then don't add the menu to the array, as it is already there.
				**/	
					if (strParentID != objMenu.id) {
				/**
				4	Get the value of the last menu in the array.  Then, using it as a base, apply 
					the 'Children' suffix and see if that is equal to the element you are moving to's 
					parentElement ID.  If it is, then don't close the menu as you are simply moving
					down the table and have not moved back from a submenu.
				**/
						var intLastID = GetLastMenu();									
						if ( (intLastID + 'Children') != objTo.parentElement.id) {
				/**
				5	If it isn't, then you have come back from a submenu to somewhere on the parent
					menu.  Check to see if the parent of the element you've come from (closeID)
					and the element you're on are the same. 							
				**/
							var objParentElement = null;
							if (window.event.fromElement)
							{
								if (window.event.fromElement.parentElement)
									objParentElement = window.event.fromElement.parentElement;
							}
							var strCloseID = "";
							if (objParentElement)
								strCloseID = objParentElement.id;
								
							var strSourceParentID = "";
							if (window.event.srcElement)
								if (window.event.srcElement.parentElement)
									strSourceParentID = window.event.srcElement.parentElement.id;

							if (strSourceParentID == strCloseID && (strSourceParentID != "") ) { 
				/**
				6	If they are, then close the 'Children' menu of the last item in the array.
				**/
								document.all.item(intLastID + 'Children').style.display = "none";
								RecurseClose(GetMenu(intLastID));
								RemoveMenu(intLastID);
							} else {
				/**
				7	If not, then remove the 'Children' suffix from the closeID and close that menu,
					manually removing it from the array also.
				**/
								strCloseID = strCloseID.substring(0, strCloseID.search("Children")) ;
								var objChildren = document.all.item(strCloseID + 'Children');
								if (objChildren)
									document.all.item(strCloseID + 'Children').style.display = "none";
								RecurseClose(GetMenu(strCloseID));
								RemoveMenu(strCloseID);
							}
						}									
					}
				}
			} else {						
				//This fires if the user ALT-TABs away from the screen as there will be no toElement														
				CloseMenu(strCurrentChild);				
			}
		} 					
		return true;
	} else {
		//This is a delayed unhover
		setTimeout("ChildUnhover(true);",2000,"Javascript");
		return true;
	}
}
/**
</Function>
<Function>
	<Name>RecurseUnhover</Name>
	<Parameters>
		<Parameter name="strID" type="string" description="ID of the menu to unhover"/>
	</Paramters>
	<Comment>	
		Display method that handles hovering away from a menu item.
	</Comment>
**/
function RecurseUnhover( strID ) {
	var objParent;
	
	objParent = document.all.item(strID);
	objParent.style.display = "none";
	
	if (objParent.parentElement.className == "MenuItem") {
		RecurseUnhover(objParent.parentElement.id);
	} else {					
		RootUnhover(true);
	}

}

/**
</Function>
<Function>
	<Name>DEBUG: TrackMouse</Name>
	<Comment>	
		Display the x and y co-ods of the mouse in real time
	</Comment>
**/
function TrackMouse() {
	//window.status = window.event.clientX + ', ' + window.event.clientY;
	return true;
}
/**
</Function>
<Function>
	<Name>DisplayArray</Name>
	<Comment>	
		Calculates the various variables for displaying the objects 
		in the array
	</Comment>
**/
function DisplayArray() {

	var objMenus;
	loopCount = 0;
	strOpen = objActiveMenu.id + ' [' + objActiveMenu.index + ']:: ';
	intArrLen = 1;
	intArrHeight = objActiveMenu.index;
	
	objMenus = objActiveMenu.next
	DisplayArrayRecurse(objMenus);	
	//document.all.item("divDisplay").innerHTML = strOpen + ' <br/><br/>&raquo;::: LEN ' + intArrLen + '::: HEIGHT ' + intArrHeight + '&laquo;' ;
	//setTimeout("DisplayArray();", 100);				
	return true;
	
}

/**
</Function>
<Function>
	<Name>DisplayArrayRecurse</Name>
	<Parameters>
		<Parameter name="objMenus" type="object" description="active menu array object"/>
	</Paramters>
	<Comment>	
		Recurses through the array to generate the numbers for the DisplayArray() function
	</Comment>
**/
function DisplayArrayRecurse( objMenus ) {
	if (objMenus != null) {
		strOpen += objMenus.id + ' [' + objMenus.index + ']:: ';
		intArrLen++;
		intArrHeight += objMenus.index;		
		DisplayArrayRecurse( objMenus.next );
	}								
	return objMenus;
}
/**
</Function>
<Function>
	<Name>MonitorMenu</Name>	
	<Comment>	
		Will execute any attached links or onclicks
	</Comment>
**/
function MonitorMenu() 
{
	var objMonitor = window.event.srcElement;
	var x = window.event.offsetX;
	var y = window.event.offsetY;
	
	if ( (objMonitor.tagName == "DIV") ||
	     (objMonitor.tagName == "TD") )
	{
		if (objMonitor.className.search("Menu") > -1)
		{
			if (typeof(objMonitor.action) != 'undefined')
			{
				//Onclick action
				eval(objMonitor.action);
			}

			if (typeof(objMonitor.link) != 'undefined')
			{
				//Link HREF
				window.location.href = objMonitor.link;
			}
		}
		else
		{
			// Check if clicked on the Warehouse Logo
			if ( ( x >= 12 && x <= 137 ) &&
			     ( y >= 18 && y <=  68 ) )
			{
				// Go home
				document.location.href = "content.cfm";
			}
		}
		RootUnhover(true);
	}
}
/**</Function>
</Functions>
**/
document.onclick= MonitorMenu;
