var sNavTimerID, tNavTimerID;
var elTop,leLeft = 0;
var tempEl;
var maxDivHeight = 78;
var el, elTop, elLeft;
var arrImgOn = new String("images/secondary_navs_arrow.gif");
var arrImgOff = new String("images/spacer.gif");
var pageIdx;
var homePage = 0;
var pageIdxAry = new Array();
var pID, sID, tID, pLength, sLength, tLengh;
var isLoaded = false;

//This function is called in the body
//tag of every page that has navigation
function initPage() {//982879288fffe62762631c7a059c9b04
			
	
	if(pageID == "982879288fffe62762631c7a059c9b04") {//home page ID
		//alert("inside home init")
		pId = 1;
		pLength = parseInt(pcnt);
		initSubNavs();
		//homePage = 1;
		eval("document.getElementById('pNavLinkHome').className = 'pNavLinkHighlight'");
	}
	else if(getNavIDX()) {
		//alert("inside all init")
		initSubNavs();
		initDisplayNav();
		initTertiaryNavs();
		setRowHeight()
		initPrimaryNav();
	}
	
	else {//used on sitemap, legal, etc.
		//alert("inside else init")
		pLength = parseInt(pcnt);
		initSubNavs();
		
	}
	
	isLoaded = true;
}

/************************************************************
* changeLanguageLink is used in the included header.asp file
*************************************************************/


//Utiliy function that gets the 
//top and left positions of named elements (elID)
function getElementPos(elID) {
	
	el = document.getElementById(elID);
	elTop = el.offsetTop;
	elLeft = el.offsetLeft;
	while(el.offsetParent != null) {
		tempEl = el.offsetParent;
		elTop += tempEl.offsetTop;
		elLeft += tempEl.offsetLeft;
		el = tempEl;
	}
	return elTop, elLeft;
}

//Matches pageID to element in URLAray (found in navdata.js)
//returns page's index position in the primaryNavs (pID),
//secondaryNavs (sID), and tertiaryNavs(tID)
//also sets values of pLength(number of primary nav elements)
//and sLength(number of secondary nav elements),
//and tLength(number of tertiary nav elements)
function getNavIDX() {
	for(i = 0; i<URLAry.length; i++) {
		if(pageID == URLAry[i]) {
			pageIdx = idxAry[i];
			pageIdxAry = pageIdx.match(/(\d\d)/g);
			//alert("pageIdxAry[0] = " + pageIdxAry[0] +"\npageIdxAry[1] = " + pageIdxAry[1] +"\npageIdxAry[2] = " + pageIdxAry[2])
		}
	}
	
	pID = pageIdxAry[0];
	sID = pageIdxAry[1];
	tID = pageIdxAry[2];
	
	if(pID)
	{
		pLength = parseInt(pcnt);
		sLength = eval("parseInt(scnt"+pID+")");
		tLengh 	= parseInt(tID);
		return true;
	}
	else
	{
		return false;
	}
	
	//alert("sLength = " + sLength);
	//alert(tID);
}
	
//Initiates and positions the secondary Navs
function initSubNavs() {
	for(var i = 1; i <= pLength; i++) {
		if(pLength < 9) {INCREMENT = "0" + i}
		eval("getElementPos('pNav" + INCREMENT + "')");
		elTop += 23;
		elLeft -= 1;
		eval("sNav" + INCREMENT + " = new Layer('subNav" + INCREMENT + "')" );
		eval("sNav" + INCREMENT + ".moveTo(elLeft,elTop)");
	
	}
}

//initiates and positions displayNavs
function initDisplayNav() {
	eval("getElementPos('displayNavTD')");
	eval("dNav = new Layer('displayNavDiv" + pID + "')");
	maxH = dNav.getHeight();
	maxDivHeight = Math.max(maxH, maxDivHeight);
	dNav.moveTo(elLeft, elTop);
	if(sID > 0) {
	 eval("document.getElementById('displayNavLink" + pID + sID + "').className = 'displayNavOn'");
	}
	dNav.show();

}

//initiates and positions the tertiary navs
//shows the default tertiary nav
function initTertiaryNavs() {
	//alert("start tNav Init")
	eval("getElementPos('tertiaryTD')");
	for(i = 1; i <= sLength; i++) {
		var INCREMENT = sLength;
		if(sLength < 9) {INCREMENT = "0" + i}
		//alert("before init tertNavDiv")
		//tertNavDiv0101
		eval("tNav" + pID + INCREMENT + " = new Layer('tertNavDiv" + pID + INCREMENT + "')");
		eval("tNav" + pID + INCREMENT + ".moveTo(elLeft, elTop)");
		maxH = eval("tNav" + pID + INCREMENT + ".getHeight()");
		maxDivHeight = Math.max(maxH, maxDivHeight);
	}
	if(sLength > 0) {
		//alert("before showDefaultTNav");
		showDefaultTNav();
		//alert("after showDefaultTNav");
	}
	if(tID > 0) {
		 eval("document.getElementById('tertNavLink" + pID + sID + tID + "').className = 'tertNavOn'");
	}
	//alert("end tNav Init")
}

//adjusts the height of the row that contains the displayNavs and tertNavs
//setting the height to the largest value
function setRowHeight() {
	eval("document.images['rowHeight'].height = " + maxDivHeight);
}

//initiates and highlights the primaryNavs
function initPrimaryNav() {
	var pOn = parseInt(pID);
	if(pOn < 9) {pOn = "0" + pOn; }
	eval("document.getElementById('pNavLink" + pOn + "').className = 'pNavLinkHighlight'");
}

//Makes subnavs visible
//idx is corresponding subnav
function showSubNav(idx) {
	if(isLoaded){
		hideSubNavs();
		eval("sNav"+idx+".show()");
		if(!homePage) {	
			showDefaultTNav();
		}
	}
}

//hides subnav
//idx i corresponding subnav
function hideSubNav(idx) {
	if(isLoaded){
		sNavTimerID = setTimeout("hideSubNavs()", 1000);
	}
}

//hides all of the subNavs
function hideSubNavs() {
	if(sNavTimerID) { clearTimeout(sNavTimerID); }
	for(var i = 1; i <= pcnt; i++) {
		var INCREMENT = i;
		if(pcnt < 9) {INCREMENT = "0" + i}
		eval("sNav" + INCREMENT + ".hide()")
	}	
}

//cancels the call to hideSubNavs()
function clearHideSubNavs() {
	if(sNavTimerID) { clearTimeout(sNavTimerID); }
}

//highlights the areas of the subNavs
//when user mouses over this element(el)
function setSubNavRowOn(el) {
	document.getElementById(el).className = "subNavCellOn";
}

//un-highlights the areas of the subNavs
//when user mouses out this element(el)
function setSubNavRowOff(el) {
	document.getElementById(el).className = "subNavCell";
}

//hides all tertiary navs
function hideTNavs() {
	
	for(i=1; i<=sLength; i++) {
		var INCREMENT = i;
		if(sLength < 9) {INCREMENT = "0" + i}
		eval("tNav" + pID + INCREMENT + ".hide()");
	}
}

//shows corresponding tertiary Nav(idx)
function showTertNav(idx) {
	if(isLoaded){
		hideSubNavs();
		hideTNavs();
		arrowsOff();
		arrowOn(idx);
		if(tNavTimerID) clearTimeout(tNavTimerID)
		eval("tNav" + idx + ".show()");
	}
}

//shows deault tertiary nav
function showDefaultTNav() {
	hideTNavs();
	arrowsOff();
	if(sID > 0) {
		arrowOn(pID+sID);
	}
	if(sID > 0) {
		eval("tNav" + pID + sID +".show()");
	}
	
}

//restores deault tertiary nav
function restoreDefaultTNav() {
	tNavTimerID = setTimeout("showDefaultTNav()", 1000);
}

//cancels restoring of default tertiary nav
function clearRestoreDefaultTNav() {
	clearTimeout(tNavTimerID);
}

//turns on arrow in displayNav
function arrowOn(arrowID) {
	if(isLoaded){
		if(arrowID > 0) {
			eval("document.images['arrow" + arrowID + "'].src = arrImgOn");
		}
	}
}

//turns off arrows in displayNav
function arrowsOff() {
	if(isLoaded){
		for(i=1; i<=sLength; i++) {
			var INCREMENT = i;
			if(i < 10) {INCREMENT = "0" + i}
			eval("document.images['arrow" + pID + INCREMENT + "'].src = arrImgOff")
		}
	}
	
}

//clears cookie and forwards user to splash page
function clearCookie(){
	document.cookie = "language=;expires=Fri, 02-Jan-1970 00:00:00 GMT";//expire the cookie
	window.location = "/"; // URL of splash page, set per site (coded)
}

function goSplash() {
	window.location = splashPageURL;
}
/*
if(!homePage) {
	var changeLanguageLink = "";
	changeLanguageLink += "<a href=\""+splashPageURL+"\" class=\"changeLanguageLink\">" + changeLanguageLabel + "</a>";
}
*/
