/*
Template: commonjs.js
Purpose:  Provides JavaScript required by the css fly-out menu.
Author:   ? (Copied from another site)
Date Copied: November of 2007
*/

/* Loops over LI's in the nav bar, setting the "urhere" class on whichever LI's id
matches the className in ul_mainMenu (populated dynamically via CF) */
function initURHere() {
	if (!document.getElementById) return;
	urhereID = document.getElementById("nav");
	if (urhereID && urhereID.className.length) {
		var mnItms = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<mnItms.length; i++) {
			// First level navigation
			if (mnItms[i].id.length && mnItms[i].id == urhereID.className) {
				mnItms[i].className+=" urhere";
				/* for ie6 - change of class on anchor tag's parent doesn't affect, so
				we need to drill down to the anchor tag explicitly. */
				x = mnItms[i].getElementsByTagName("a");
				if (x.length > 0) x[0].style.backgroundPosition = 'top left';
				//
				break;
			}
			// Fly-outs - force the parents to be highlighted
			else if (mnItms[i].parentNode.parentNode.id == urhereID.className) {
				mnItms[i].parentNode.parentNode.className+=" urhere";
				/* for ie6 - change of class on anchor tag's parent doesn't affect, so
				we need to drill down to the anchor tag explicitly. */
				x = mnItms[i].parentNode.parentNode.getElementsByTagName("a");
				if (x.length > 0) x[0].style.backgroundPosition = 'top left';
				//
				break;
			}
		}
	}
	// Following is for Site Map and Privacy Policy
	urhereID = document.getElementById("nav_footer");
	if (urhereID && urhereID.className.length) {
		var mnItms = document.getElementById("nav_footer").getElementsByTagName("LI");
		for (var i=0; i<mnItms.length; i++) {
			if (mnItms[i].id.length && mnItms[i].id == urhereID.className) {
				mnItms[i].className+=" urhere";
				/* for ie6 - change of class on anchor tag's parent doesn't affect, so
				we need to drill down to the anchor tag explicitly. */
				x = mnItms[i].getElementsByTagName("a");
				if (x.length > 0) x[0].style.backgroundPosition = 'bottom left';
				//
				break;
			}
		}
	}
}

/* Basically, this applies the 'sfhover' class to li elements in the 'nav' id'd 
ul element when they are 'moused over' and removes it, using a regular expression, when 'moused out'. */
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
			/* Above doesn't work for IE 6. The following does. */
			x = this.getElementsByTagName("UL");
			if (x.length > 0) x[0].style['display'] = 'block';
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			/* Above doesn't work for IE 6. The following does. */
			x = this.getElementsByTagName("UL");
			if (x.length > 0) x[0].style['display'] = 'none';
		}
	}
}

/* Function to allow an event to be added to window.onload, as some individual
pages need to do so and the main window.onload is located in a seoarate script. */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
        	oldonload();
			func();
		}
	}
}

/* The followuing works for IE AND Firefox */
window.onload = function() { 
	initURHere();
}