<!-- Script to show the drop down menus in the header -->
<!-- number in l2Hover(#) is how many to show in the list -->
/* SE1.0.3.1: Drop down menu functions : start. */
centerl2(); // centers l2 menu
window.numScroll = null;
window.active=false;
window.browserName=navigator.appName; 
window.browserVer=parseInt(navigator.appVersion);	 
l2Hover = function(maxNumToShow) {
	var l2s = getElementsByClassName(document, "l2");
	var l2Subs = getElementsByClassName(document, "l2_sub");
	
	centerl2(); // centers l2 menu
	
	window.maxNumToShow = maxNumToShow;
		
	window.active=false;
	for (var i=0; i<l2s.length; i++) {
		l2s[i].pos = i;
		l2s[i].origClassName = l2s[i].className;
				
		var link = l2s[i].getElementsByTagName("a")[0];
		l2s[i].linkName = link.innerHTML;
		
		if (!link.onmouseover) {
			// allCategories calls this manually
			link.onmouseover=function() {
				showL3Menu(this.parentNode);
			}
		}
		
		if (!link.onmouseout) {
			link.onmouseout=function() {
				registerHide(this.parentNode);
			}
		}
		
		l2s[i].onmouseout=function(event) {
			var listItem = this;
			if (window.pos != listItem.pos) return;
			
			if (!event) { var event = window.event; }
			if (event && event.clientX) {
				
				// the actual mouse position is a combination of screen position and scrolling
				var mouse = new Object();
				mouse.x = event.clientX + window.scrollX;
				mouse.y = event.clientY + window.scrollY;
				
				var x1, x2, y1, y2;
				x1 = y1 = 0;
				
				// find the client position of the <a> tag
				var link = this.getElementsByTagName("a")[0];
				var list = this.getElementsByTagName("ul")[0];
				var element = link;
				
				var linkCoord = getElementCoordinates(link);
				var listCoord = getElementCoordinates(list);
				
				// shrink to avoid early mouseouts
				linkCoord = linkCoord.shrink(1);
				listCoord = listCoord.shrink(1);
								
				// if the mouse pointer is over the union of the <a> or <ul>,
				//	keep menu alive
				if (mouse.x > linkCoord.x1 && mouse.x < linkCoord.x2 && 
						mouse.y > linkCoord.y1 && mouse.y < listCoord.y2 ||
						mouse.x > listCoord.x1 && mouse.x < listCoord.x2 && 
						mouse.y > listCoord.y1 && mouse.y < listCoord.y2) {
							
					unregisterHide(listItem);
					return;
				}
				
			}
			
			registerHide(listItem);
						
		}
		
	}
	for (var i=0; i<l2Subs.length; i++) {
		l2Subs[i].onmouseover=function() {
			unregisterHide(this.parentNode);
		}
		l2Subs[i].onmouseout=function() {
			registerHide(this.parentNode);
		}
	}
}
scrollDown = function(b) {
	if (b) {
		d=setTimeout("scrollDownOne()",50);
	} else {
		clearTimeout(d);
	}
}
scrollUp = function(b) {
	if (b) {
		u=setTimeout("scrollUpOne()",50);
	} else {
		clearTimeout(u);
	}
}
scrollUpOne = function() {
	if ((numScroll) > 0) {
		numScroll--;
		updateScroll();
		u=setTimeout("scrollUpOne()",50);
	}

}
scrollDownOne = function() {
	if ((numScroll) < (subs.length - window.maxNumToShow)) {
		numScroll++;
		updateScroll();
		d=setTimeout("scrollDownOne()",50);
	}
}
resetInnerHTML = function() {
	if (numScroll > 0) {
		numScroll = 0;
		updateScroll();
	}
}

function updateScroll() {
	for (var i=0; i<window.maxNumToShow; i++) {
		subs[i].innerHTML = subsInnerHTML[i+numScroll];
	}
}


function centerl2() {
	var navmenu = document.getElementById("nav");
	
	if (!navmenu || navmenu.centered) return; // stop if not loaded or already centered
	
	navmenu.style.visibility = "hidden";
		
	// find the selected l1 menu item using the img src
	var l1 = getElementsByClassName(document, "nav_l1")[0];
	var l1s = l1.getElementsByTagName("img");
	for (var i = 0; i < l1s.length; i++) {
		var l1img = l1s[i];
		if (l1img.src && l1img.src.indexOf("_sel.gif") >= 0) {
			break;
		}
	}
	
	if (i < l1s.length) {
		// coordinates
		var	imgCoords = getElementCoordinates(l1s[i]);
		var navCoords = null; // tallies the child nodes, and not the absolute bounds
		
		// get the number of L2 children
		var children = navmenu.childNodes;
		var numLi = 0;
		for (var i = 0; i < children.length; i++) {
			if (children[i].tagName == "LI") {
				if (navCoords != null) {
					var liCoords = getElementCoordinates(children[i]);
					navCoords.x1 = Math.min(navCoords.x1, liCoords.x1);
					navCoords.y1 = Math.min(navCoords.y1, liCoords.y1);
					navCoords.x2 = Math.max(navCoords.x2, liCoords.x2);
					navCoords.y2 = Math.max(navCoords.y2, liCoords.y2);
				} else {
					navCoords = getElementCoordinates(children[i]);
				}
				numLi++;
			}
		}
		navCoords = computeWidthAndCenters(navCoords);
		
			
		// starting center
		var xC = imgCoords.xc - navCoords.x1;
		
		// if numLi is odd, center around the middle Li, not menu center
		if (numLi % 2 == 1) {
			var LI = 0;
			for (var i = 0; i < children.length; i++) {
				if (children[i].tagName == "LI") {
					LI++;
					if (LI == Math.ceil(numLi / 2)) {
						break;
					}
				}
			}
			
			var midLi = children[i];
			var midA = midLi.getElementsByTagName("a")[0];
			var aCoords = getElementCoordinates(midA);
			aCoords.x1 += 10; // account for stylesheet padding
			aCoords = computeWidthAndCenters(aCoords);
			
			xC -= aCoords.xc - navCoords.xc;
		} else {
			xC += 5; // account for stylesheet padding
		}
		
		// left position is the center - width/2
		var marginLeft = xC - navCoords.w / 2;
		if (marginLeft >= 0) {
			navmenu.style.marginLeft = marginLeft.toPixels();
		}
	}
	
	navmenu.centered = true;
	navmenu.style.visibility = "visible";
}

function showL3Menu (listItem) {
	if (!window.active || (window.pos != listItem.pos)) {
		// hide the all category menu if open
		if (document.getElementById("allCategories") && window.pos == document.getElementById("allCategories").pos) {
			hideMenu();
		}
		
		closeL3Menu(); // close menu if open
				
		window.pos = listItem.pos;
				
		listItem.className+=" sfhover"; // show menu

		// move position slightly for IE
		var ulList = listItem.getElementsByTagName("ul")[0];/* SE1.0.3.1: Updated retrieval method. */
		window.objLeft = ulList.style.left;
		if (browserName=="Microsoft Internet Explorer")
		{		
			if (typeof document.body.style.maxHeight != "undefined") {
  			// IE 7
				ulList.style.top = '17px';/* SE1.0.3.1: Updated retrieval method. */
				ulList.style.left = (listItem.offsetLeft+50);/* SE1.0.3.1: Updated retrieval method. */
			} else {
  				// IE6, older browsers
				ulList.style.left = (listItem.offsetLeft+50);/* SE1.0.3.1: Updated retrieval method. */
			}	
		}
		
		//show only 10 if longer showDefault();
		subs = getElementsByClassName(listItem, "menuItem");
		subsInnerHTML = new Array();
		
		for (var i=0; i<subs.length; i++) {
			if (i >= window.maxNumToShow) {
				subs[i].style.visibility='hidden';
				subs[i].style.display='none';
			}
			var subLink = subs[i].getElementsByTagName("a")[0];
			subsInnerHTML.push(subs[i].innerHTML);
			numScroll = 0;
		}
		//
		
		window.active = true;
		
	} else {
		// unregister timeout if exists
		unregisterHide(listItem);
	}
		
}

// closes the open menu, if it exists (used by timeout and called directly)
function closeL3Menu() {
	if (window.obj) {
		window.obj.className = window.obj.origClassName;
		if (browserName=="Microsoft Internet Explorer")
		{
			window.obj.getElementsByTagName("ul")[0].style.left = '-999px';/* SE1.0.3.1: Updated retrieval method. */
		}
		resetInnerHTML();
		window.obj = null;
		window.active = false;
		
	}
	
}

function registerHide(listItem) {
	unregisterHide(listItem); // unregister any existing timeouts
	
	// close any opening menus immediately
	if (window.obj && window.obj != listItem) {
		closeL3Menu();
	}
	
	// set up menu for closure on timeout
	window.obj = listItem;
	listItem.t=setTimeout("if (window.obj && window.obj.t != null) { closeL3Menu(); }",500);
}

function unregisterHide(listItem) {
	// unregister menu timeout
	if (listItem.t != null) {
		clearTimeout(listItem.t);
		listItem.t = null;
	}
}
/* SE1.0.3.1: Drop down menu functions : end. */