/*
	get the browser type
*/
ns4 = (document.layers)? true : false;
ie4 = (document.all)? true : false;
dom = (document.getElementById)? true : false;

/*
	initilization variables
*/
var parentDivId = "navItems";
var subNavExt = "sub_";
var menuOverOnExt = "Over";
var hideDelay = 2000;
var menuTimer = 0;
var trackCurrentMenu = null;
var trackOnMenu = null;

function menuInit() {
	/*
		initilization function for the menu - called onload
	*/
	if (dom || ie4) {
		menu = new addMenuItems(parentDivId);
		trackOnMenu = getOnMenu();
		startMenuOn();
//		displayNone();
	}
}

function changeStyleOff() {
	/*
		turns the style of the main nav back to its original class 
		by removing the 
	*/
	if (trackCurrentMenu != null) {
		obj = getElement(trackCurrentMenu);
		if (obj.attributes.id.value != trackOnMenu)
			obj.className=obj.className.replace(menuOverOnExt, "")
	}
}

function addMenuItems(divId) {
	/*
		adds the functionality to the menu items that have 
		secondary items utilitizing the ids on each of the 
		menu links and prepending the subNavExt to them
	*/
	obj = getElement(divId);
		
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
						
			node.onmouseout= function() {
				/*
					add the functionality for the mouse out and 
					hiding the appropriate menu
				*/
				subNode = getSubNode(this);
							
				if (subNode) {
					
					subNode.onmouseout = function() {
						/*
							add the functionality for the sub menu 
							items for the mouse out
						*/
						startTimer(this)
					}
					
					subNode.onmouseover = function() {
						/*
							add the functionality for the sub menu 
							items for the mouse over
						*/
						clearTimeout(menuTimer);
					}
					startTimer(subNode)
					
				} else {
					/*
						for menus that do not have any sublevels 
						we must return to the On Menu
					*/
					clearTimeout(menuTimer);
					showCurrentMenu();
					changeStyleOff();
				}
			}
			
			node.onmouseover= function() {
				/*
					add the functionality for the mouse over and 
					showing the appropriate menu
				*/
				changeStyleOff();
				if (this.attributes.id.value != trackOnMenu) {
					if (this.className.indexOf(menuOverOnExt) == -1)
						this.className+=menuOverOnExt;
				}
				
				clearMenus();
				trackCurrentMenu = this.attributes.id.value;
				
				subNode = getSubNode(this);
				clearTimeout(menuTimer);
				
				if (subNode) {
					displayBlock(subNode)
				}
			}
		}
	}	
}

function startTimer(node) {
	/*
		timeout for the menu system - controlled in millseconds 
		using hideDelay
	*/
	ID = node.attributes.id.value;
	if (trackCurrentMenu != trackOnMenu)
		menuTimer = setTimeout("displayNoneById('" + ID + "')", hideDelay);
}

function showCurrentMenu() {
	/*
		shows the current menu after a user has rolled off others 
		if there is a OnMenu
	*/
	//alert(trackOnMenu)
	if (trackOnMenu != null) {
		obj = getElement(trackOnMenu);
		subNode = getSubNode(obj);
		if (subNode != null) {
			displayBlock(subNode);
		}
	}
}

function displayBlock(node) {
	/*
		changes display to block depending on the node - which 
		is typically a sub nav item
	*/
	node.style.display = "block";
}
function displayNone(node) {
	/*
		changes display to none depending on the node - which 
		is typically a sub nav item
	*/
	node.style.display = "none";
}
function displayNoneById(ID) {
	/*
		changes display to none based on the ID of an element 
		that you pass it
	*/
	obj = getElement(ID);
	obj.style.display = "none";
	showCurrentMenu();
	changeStyleOff();
}

function clearMenus() {
	/*
		turn off all sub menus if there are sub menus to be 
		turned off
	*/
	obj = getElement(parentDivId);
	
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
			subNode = getSubNode(node);
			
			if (subNode != null)
				displayNone(subNode)
		}
	}
}

function getSubNode(node) {
	/*
		find the sub nodes based on the extension and the 
		node that it is passed
	*/
	nodeIdName = node.attributes.id.value
	nodeIdName = subNavExt + nodeIdName;
	subNode = getElement(nodeIdName);
	
	return subNode;
}

function getOnMenu() {
	/*
		find the current on utilizing the On within the class of 
		the node and then use this for tracking purposes
	*/
	obj = getElement(parentDivId);
	
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
			if (node.className.indexOf("On") > 0)
				return node.attributes.id.value;
		}
	}
}

function startMenuOn() {
	/*
		turn the subnav on if there is a sub node for it on the 
		load of the page
	*/	
	obj = getElement(parentDivId);
	
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
		
			if (node.attributes.id.value == trackOnMenu) {
				subNode = getSubNode(node);
				if (subNode != null)
					displayBlock(subNode)
			}
		}
	}
}

function getElement(ID) {
	if (ns4) {
		return null;
	} else if (dom) {
		return (document.getElementById(ID))? document.getElementById(ID) : null;
	} else if (ie4) {
		return (document.all[ID])? document.all[ID] : null;
	}
}



