var tmr;

window.onload = function() {
	if (document.getElementById) {
		var nav = document.getElementById('nav');
		
		var aTags = nav.getElementsByTagName('a');
		var liTags = nav.getElementsByTagName('li');
		
		for (var x = 0; x < aTags.length; x++) {
			aTags[x].onmouseover = showSubNav;
			aTags[x].onmouseout = hideSubNav;
			aTags[x].onfocus = showSubNav;
			aTags[x].onblur = hideSubNav;
		}
	}
}

function showSubNav() {
	//get the parent element of the anchor with focus (or hover)	
	var pNode = this.parentNode;
	
	//Variable flag to determine whether or not the li element is in the top level of navigation.	
	var isTopLevel = true;
	
	//Check the parent element of the pNode. If the parent element's ID is 'nav' then the LI is in the top level of the navigation.
	if (pNode.parentNode.id != 'nav') {
		isTopLevel = false;
	}
	else {
		isTopLevel = true;
	}
	
	if (isTopLevel) {
		hideMenus(); //Hide other menus that might be visible
		clearTimeout(tmr); //Clear any timers that might be active
		if (String(pNode.className).indexOf('selected') > -1) {
			pNode.className = 'over selected';
		}
		else {
			pNode.className = 'over';
		}
	}
	else {
		clearTimeout(tmr); //Clear any timers that might be active
		pNode.className = 'over';
	}
}

function hideSubNav() {
	tmr = setTimeout("hideMenus()", 100);
}

function hideMenus() {
	//This function will hide any submenus that are currently visible.
	var nav = document.getElementById('nav');
	
	var liTags = nav.getElementsByTagName('li');
	
	for (var x = 0; x < liTags.length; x++) {
		if (String(liTags[x].className).indexOf('over') > -1) {
			if (String(liTags[x].className).indexOf('selected') > -1) {
				liTags[x].className = 'selected';
			}
			else {
				liTags[x].className = '';
			}
		}
	}
}
