// ============================================================================== //
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html         //
// cross-browser event handling for IE5+,  NS6 and Mozilla                        //
// ============================================================================== //

function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

//

function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

//

function NA_ieChildren(id,tag) {
	if(document.getElementById(id))	{
		var els = document.getElementById(id).getElementsByTagName(tag);
		for (var i=els.length-1; i>=0; i--) {
			els[i].firstChild.className += " first-child";
			els[i].lastChild.className  += " last-child";
		}
	}
}

function NA_ieChildren_init() {
	NA_ieChildren("nav-main","UL");
	NA_ieChildren("content-subsidiaries","UL");
	NA_ieChildren("siteinfo-legal","UL");
	NA_ieChildren("content","TR");
}

addEvent(window, "load", NA_ieChildren_init);

	// ============================================================================== //
	// NA_navMain() loops through #nav-main and sets all the ids and events needed    //
	// for this navigation scheme to work properly. After setting all the proper ids  //
	// and events, it calls NA_navMain()                                              //
	// ============================================================================== //

	var nav_on  = "";
	var timiton = "";
	var timitof = "";
	
	function ohh(id) {
		try{
			clearTimeout(timitof);

			if(document.getElementById("nav-main-li-" + nav_on)) {
				document.getElementById("nav-main-li-" + nav_on).className = "";
				document.getElementById("nav-main-img-" + nav_on).src = document.getElementById("nav-main-img-" + nav_on).src.replace("-on", "-off").replace(".gif", ".jpg");
			}
			if(document.getElementById("nav-main-li-" + id)) {
				document.getElementById("nav-main-li-" + id).className = "hover";
				document.getElementById("nav-main-img-" + id).src = document.getElementById("nav-main-img-" + id).src.replace("-off", "-on").replace(".jpg", ".gif");
			}
			nav_on = id;
		}catch(ex){
		 // alert(ex.description)
		}
	}
	function ahh(id) {
		try{
			nav_on = nav_onOriginal;
			for (i=0; i<4; i++) {
			    document.getElementById("nav-main-li-" + i).className = "";
				document.getElementById("nav-main-img-" + i).src = document.getElementById("nav-main-img-" + i).src.replace("-on", "-off").replace(".gif", ".jpg");
			}
			if(document.getElementById("nav-main-li-" + nav_on)) {
				document.getElementById("nav-main-li-" + nav_on).className = "hover";
				document.getElementById("nav-main-img-" + nav_on).src = document.getElementById("nav-main-img-" + nav_on).src.replace("-off", "-on").replace(".jpg", ".gif");
			}
		}catch(ex){
		 // alert(ex.description)
		}
	}

	// handles main pages
	function NA_navMain() {
		// check to make sure this isn't opera 7
		if((window.opera && Math.round(navigator.appVersion.substring(0, 1)) > 7) || !window.opera) {
			if(document.getElementById("nav-main")) {
				// loop through all IMGs
				var els = document.getElementById("nav-main").getElementsByTagName("IMG");
				var counter = 0;
				for (var i=0; i<els.length; i++) {
					// top level IMGs only
					if (els[i].parentNode.parentNode.parentNode.parentNode.id == "nav-main") {
						// sets id of IMG
						els[i].id = "nav-main-img-" + counter;
						// sets id of parent LI - this can't be set via JS, if not present, nav won't show
						//els[i].parentNode.parentNode.id = "nav-main-li-" + counter;
						// sets event of parent LI
						els[i].parentNode.parentNode.onmouseover = function() {
							clearTimeout(timitof);
							j = right(this.id, 1);
							timiton = setTimeout(function() {ohh(j);}, 250);
						}
						els[i].parentNode.parentNode.onmouseout = function() {
							clearTimeout(timiton);
							j = right(this.id, 1);
							timitof = setTimeout(function() {ahh(j);}, 300);
						}
						counter = counter+1;
					}
				}
				// loop through all ULs
				var els = document.getElementById("nav-main").getElementsByTagName("UL");
				for (var i=1; i<els.length; i++) {
					// add events so that the nav resets when you mouseout of #nav-main
					els[i].onmouseover = function() {
						clearTimeout(timiton);
						clearTimeout(timitof);
					}
				}ahh()
			}
		}
	}
	
	

	addEvent(window, "load", NA_navMain);


	// ============================================================================== //
	// end navigation JS                                                              //
	// ============================================================================== //

function NA_search() {
	if(document.getElementById("nav-search"))	{
		var els = document.getElementById("nav-search").getElementsByTagName("INPUT");
		els[0].onfocus = function() {
			if(this.value == "Search") { this.value = ""; }
		}
		els[0].onblur = function() {
			if(this.value == "") { this.value = "Search"; }
		}
		if (els[0].value == "") {
			els[0].value = "Search";
		}
	}
}

addEvent(window, "load", NA_search);

//

function NA_parent() {
	if(document.getElementById("branding-parent")) {
		var els = document.getElementById("branding-parent").getElementsByTagName("IMG");
		for (var i=0; i<els.length; i++) {
			els[i].onmouseover = function() {
				this.src = this.src.replace("-off", "-on");
			}
			els[i].onmouseout = function() {
				this.src = this.src.replace("-on", "-off");
			}
		}
	}
}

addEvent(window, "load", NA_parent);

//

function NA_cols() {
	for (var i=0; i<4; i++) {
		if(document.getElementById("col" + i)) {
			document.getElementById("col" + i).onclick = function() {
				expand(right(this.id, 1));
			}
		}
	}
}

addEvent(window, "load", NA_cols);

// ============================================================================== //
// na_height.js 1.5                                                               //
// by NavigationArts: navarts.com                                                 //
// ============================================================================== //
// This function is built to take X elements, compare the actual browser          //
// rendered heigths, and then makes the shorter ones taller. Useful when trying   //
// to emulate a table based design grid.                                          //
// ============================================================================== //

//alert(window.getComputedStyle != undefined);                      // IE false, FF true,  Opera true,  Safari false
//alert(document.defaultView != undefined);                         // IE false, FF true,  Opera true,  Safari true
//alert(document.getElementById(els[0]).currentStyle != undefined); // IE true,  FF false, Opera false, Safari false
	
// px could be % or em

function NA_height(elements) {
	els = NA_height.arguments;
	css = document.defaultView != undefined ? "padding-top,padding-bottom,border-top-width,border-bottom-width" :
	                                          "paddingTop,paddingBottom,borderTopWidth,borderBottomWidth";
	css = css.split(",");
	for(i=0; i<els.length; i++) {
		if(document.getElementById(els[i])) {
			obj = document.getElementById(els[i]); // object we are manipulating
			eval("pHeight" + i + "=" + 0); // creating the var that stores the height of padding/borders
			if (navigator.userAgent.toLowerCase().indexOf("msie 5") == -1) { // MSIE5 Check
				for(j=0; j < css.length; j++) {
					val = document.defaultView != undefined ? (parseInt(document.defaultView.getComputedStyle(obj, "").getPropertyValue(css[j]))) : // W3C model
					                                          (parseInt(obj.currentStyle[css[j]])); // IE Model
					if (! isNaN(val)) {
						eval("pHeight" + i + "=" + eval(eval("pHeight" + i) + val)); // build padding/border var for this object
					}
				}
			}
			obj.style.height = obj.offsetHeight - eval("pHeight" + i) + "px"; // so we no longer have to deal with offsetHeight (it won't be right the 2nd loop through)
		}
	}
	for(x=0; x<2; x++) { // loop throught twice because it's incremental
		for(i=0; i<els.length; i++) {
			j = i == els.length-1 ? 0 : i+1; // if last compare with first
			iHeight = eval(document.getElementById(els[i]).style.height.replace("px", "")) + eval("pHeight" + i);
			jHeight = eval(document.getElementById(els[j]).style.height.replace("px", "")) + eval("pHeight" + j);
			dHeight = iHeight - jHeight;
			if (dHeight < 0) {
				dHeight = dHeight * -1; // make the difference addable to either obj
			}
			if (dHeight > 0) {
				k = iHeight < jHeight ? "i" : "j"; // determine with obj to add to
				dHeight = eval(k + "Height") - eval("pHeight" + eval(k)) + dHeight;
				document.getElementById(els[eval(k)]).style.height = dHeight + "px";
			}
		}
	}
}

