/******************************************
* AWD JS Library
* Set: Horizontal Menu
* Author: Matthew Camp
* Created: 1st March 2009
* Last Updated: 1st March 2009
* ________________________________________________________________________________
* AUTOMATIC ASSIGNED FUNCTION LIST within the addEventListener, none of these functions needs to called in the HTML
* ________________________________________________________________________________
* HorizontalMenu - assigns li tags with a rollover menu
* ________________________________________________________________________________
* MAIN FUNCTION LIST, call each function within the HTML using the class attribute of the element
* ________________________________________________________________________________
* JavaScript:HorizontalMenu - [ul only] - must be placed on the parent ul tag that holds the whole navigation
* JavaScript:RollOverMenu - [li only] - must be place on the li item that includes a child ul that will act as the rollover menu
*
/******************************************
* INITIATE GLOBAL VARIABLES AND ADD EVENT LISTENERS
*******************************************/
var timeOut	= 500;
var closeTimer	= 0;
AddOnloadEvent(HorizontalMenu);

function HorizontalMenu(){
	var linkLists = document.getElementsByTagName("ul");
	for(var i = 0; i < linkLists.length; i ++){
		if(linkLists[i].className.match("JavaScript:HorizontalMenu")){
			var listItems = linkLists[i].getElementsByTagName("li");
			for(var j = 0; j < listItems.length; j ++){
				if(listItems[j].className.match("JavaScript:RollOverMenu")){
					listItems[j].onmouseover = function(){
						hideCurrentRollovers();
						ShowHorizontalMenu(this);
					}
				}
				else{
					listItems[j].onmouseover = function(){
						hideCurrentRollovers();
					}	
				}
			}
		}
	}
}
// displays a horizontal menu
function ShowHorizontalMenu(element){
	//get element properties
	var anchorLink = element.getElementsByTagName("a")[0];
	var xPos = leftPosition(anchorLink);
	var yPos = topPosition(anchorLink) + anchorLink.offsetHeight - 1;
	//show menu
	var menu = element.getElementsByTagName("ul")[0];
	menu.style.display = "block";
	menu.style.left = xPos + "px";
	menu.style.top = yPos + "px";
	//attach events
	menu.onmouseout = function(){
		closeTimer = window.setTimeout(function(){
			menu.style.display = "none";						   
		},timeOut);
	}
	menu.onmouseover = function(){
		cancelRollOut();
	}
	anchorLink.onmouseout = function(){
		closeTimer = window.setTimeout(function(){
			menu.style.display = "none";						   
		},timeOut);
	}
	anchorLink.onmouseover = function(){
		cancelRollOut();
	}
	var listItems = menu.getElementsByTagName("li");
	for(var i =0; i < listItems.length; i ++){
		listItems[i].onmouseover = function(){
			cancelRollOut();
		}
	}
	
}
// cancels mouseout commands if the element is within the menu
function cancelRollOut(){
	if(closeTimer){
		window.clearTimeout(closeTimer);
		closeTimer = null;
	}
}
// removes any current mouse overs
function hideCurrentRollovers(){
	var linkLists = document.getElementsByTagName("li");
	for(var i = 0; i < linkLists.length; i ++){
		if(linkLists[i].className.match("JavaScript:RollOverMenu")){
			var menu = linkLists[i].getElementsByTagName("ul")[0];
			menu.style.display = "none";
		}
	}
}

/******************************************
* CALLED FUNCTIONS
*******************************************/
 // Returns the left position of an element in relation to the document
 function leftPosition(element){
	var xPos = element.offsetLeft;
	var tempElement = element.offsetParent;
	while (tempElement != null) {
		xPos += tempElement.offsetLeft;
		tempElement = tempElement.offsetParent;
	}
	return xPos;	
}
// Returns the top position of an element in relation to the document
function topPosition(element){
	var yPos = element.offsetTop;
	var tempElement = element.offsetParent;
	while (tempElement != null) {
		yPos += tempElement.offsetTop;
		tempElement = tempElement.offsetParent;
	}
	return yPos;
}
// allows to attach multiple onload event with multiple js scripts
function AddOnloadEvent(fnc){
	if(typeof window.addEventListener != "undefined"){
		window.addEventListener( "load", fnc, false );
	}
	else if(typeof window.attachEvent != "undefined"){
		window.attachEvent("onload", fnc);
	}
	else{
		if(window.onload != null){
			var oldOnload = window.onload;
			window.onload = function(e){
				oldOnload(e);
				window[fnc]();
			}
		}
		else{
			window.onload = fnc;
		}
	}
}