var parentNavOffset = 178;
var numTopLevelMenuItems = 0;						// number of top level nav items in nav
var numSecondLevelMenuItems = 5;					// number of secondary nav items in nav
var numMenus = 3;									// number of secondary dhtml menus in play
var subMenus = 3;									// number of tertiary dhtml menus in play
var currDropDown = "";
var browWinHeight;
var browScrollDistance;
var arrayOfNavItems = new Array();


// --------------------------------------------------------------------------------------------
// clearAllMenus is called when hideCountdown runs or a new primary is moused-over (toggleMenu)
// --------------------------------------------------------------------------------------------
function clearAllMenus() {
	for (i = 1; i <= numMenus; i++) {
		foo = eval(docObj + "dropNav" + i + styleObj);
		foo.visibility = "hidden";
		currDropDown = "";
	}
	for (k = 1; k <= subMenus; k++) {
		soo = eval(docObj + "tertNav" + k + styleObj);
		soo.visibility = "hidden";
	}
	for(t=0; t < arrayOfNavItems.length; t++) {
		dimNav(arrayOfNavItems[t]);
	}
	
	/* 
	Track the navs display by reading their ids from the array 
	
	var aString = "String: \n";
	for(y=0; y<arrayOfNavItems.length; y++) {
		aString = aString + arrayOfNavItems[y] + "\n";
	}
	alert(aString);
	*/
	emptyArray();

}

function emptyArray() {
	// Need to set the length of the array before popping b/c length decreases as items pop.  
	// Note, 'pop' removes the LAST item in the array.
	var lengthBeforePopping = arrayOfNavItems.length;
	for(z=0; z <= lengthBeforePopping; z++) {
		arrayOfNavItems.pop();
	}
}
//---------------------------------------------------------------------------
// clearAMenu clears the specified menu based on passed value
//---------------------------------------------------------------------------
function clearAMenu(theMenuToClear) { 
//	var listOfNavItems = "";
	if(theMenuToClear != "") {
		zoo = eval(docObj + theMenuToClear + styleObj);
		zoo.visibility = "hidden";
		for(p=0; p < arrayOfNavItems.length; p++) {
			if (theMenuToClear == arrayOfNavItems[p]) arrayOfNavItems.pop();
		}
		//alert("the list: " + listOfNavItems);
//		dimNav(arrayOfNavItems[arrayOfNavItems.length-2]);
	}
}

//---------------------------------------------------------------------------
// delayToggleMenu is called on mouseover for any nav item within the nav
// It provides the delay for displaying any dhtml nav
//---------------------------------------------------------------------------
var timerID2 = 0;									// timerID2 is the timer for toggleMenu!
function delayToggleMenu(parNav,chiNav) {
	parentNav = parNav;
	childNav = chiNav;
	clearTimeout(reLightID);						// clear counter to re-highlight intial navs
	clearTimeout(timerID);							// clear counter for any previous clearAllMenus call
	clearTimeout(timerID2);							// clear counter for any previous toggleMenu call

	if((currDropDown != "")  && (currDropDown != childNav)) {
		timerID2 = setTimeout("toggleMenu(parentNav,childNav)",250);
	}
	else {
		toggleMenu(parentNav,childNav);
	}
}

function toggleMenu(parent,child) {
	theParent = parent;
	theChild = child;
	//alert(theChild);
	//if(theParent.indexOf('top') == 0) {					// if over a top level item
	if(!theChild) {
		clearAllMenus();								// turn off all highlights, menus and clear out array of items 
		arrayOfNavItems.push(theParent);				// just put the top itme in the array
		dimAll();										// dim all highlighted nav items, including initial highlights
		lightNav(theParent);							// light the top item
	}
	else {
		if(theParent.indexOf('primary') == 0) {			// mouseover on primary item so clear queue of items
			clearAllMenus();							// turn off all highlights and clear out array of items
			arrayOfNavItems.push(theParent, theChild);	// Note, 'push' puts the items at the end of the array
		}
		else if(theChild.indexOf('tert') == 0) {
			if(arrayOfNavItems[arrayOfNavItems.length-1].indexOf('tert') == 0) clearAMenu(arrayOfNavItems[arrayOfNavItems.length-1]);
			arrayOfNavItems.push(theChild);
		}
		else if(arrayOfNavItems.length < 4) {
			arrayOfNavItems.push(theParent, theChild);	// track which navs have been displayed by loading them in an array
		}
		dimAll();										// turn off all nav highlights
		
		for(z=0; z<arrayOfNavItems.length; z++) {		// light the items in the array; these will be the current series
			lightNav(arrayOfNavItems[z]);
		}
	
		currDropDown = theChild;						// Used by delayToggleMenu; update currDropDown with value of theChild; 1st time: currDropDown = "dropNav3", 2nd time: currDropDown = "tertNav1"
		showMenu(theChild);
	}
}

function showMenu(theMenu) {
	dom = eval(docObj + theMenu + styleObj);
	dom.visibility = "visible";
}

// ----------------------------------------------------------------------
// Set up timer for menu hide onmouseout
// ----------------------------------------------------------------------
var timerID = 0;													// timerID is the timer for clearAllMenus
var reLightID = 0;													// reLightID is the timer for relighting initial(current nav) item
function hideCountdown(){
	clearTimeout(timerID2); 										//clear the delay timer to show another drop nav
	timerID = setTimeout("clearAllMenus()",500);
	reLightID = setTimeout("relightInitialNavs()",500);	//start timer to re-highlight the initial navs
}

var timerSubID = 0;
function hideSubCountdown(navName){
	someVar = navName;
	clearTimeout(timerID2); 										//clear the delay timer to show another drop nav
	timerSubID = setTimeout("clearAMenu(someVar)",500);
}

//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------

function clearTimeoutsListed() {
	for(q = 0; q < clearTimeoutsListed.arguments.length; q++) {
		clearTimeout(clearTimeoutsListed.arguments[q]);
	}
}

function clearTimeThree()  {
	clearTimeout(timerID);
	clearTimeout(timerID2);
	clearTimeout(reLightID);
}

//-------------------------------------------------------------------------------
// The following 3 functions serve to hide select menus underneath a dhtml menu.
// Currently NOT used within CPCCS.
//-------------------------------------------------------------------------------
function getSelectObj(selectStr) {
	if(document.getElementById(selectStr) != null) {
		domObj = eval(docObj + selectStr + styleObj);
		return domObj;
	}
	else {
		return false;
	}
}

function hideSelect(selectId) {
	dom = getSelectObj(selectId);
	if(dom != false) {
		dom.visibility = "hidden";
	}
}	

function showSelect(selectId) {
	dom = getSelectObj(selectId);
	if(dom != false) {
		dom.visibility= "visible";
	}
}	


//------------------------------------------------------------------------
// lightNav and dimNav hightlight or turn off the nav elements as triggered
// by updating the class name applied to a given nav element
//------------------------------------------------------------------------

function lightNav(lightItem) {
	if(lightItem != "") {
		var theItem = lightItem; 
		var itemObj = eval(docObj + theItem + "')");
		if(theItem.indexOf("primary") == 0 || theItem.indexOf("top") == 0) {
			if(theItem != topNavItem) {
				itemObj.getElementsByTagName('img')[0].src = itemObj.getElementsByTagName('img')[0].src.replace('_off.gif','_on.gif');
			}
		}
		if(theItem.indexOf("link") == 0) {			
			itemObj.className = (itemObj.className == "") ? "selected" : itemObj.className+" selected";
		}
	}
}

function dimNav(dimItem) {
	var theItem = dimItem; 
	var itemObj = eval(docObj + theItem + "')");
	if(theItem.indexOf("primary") == 0 || theItem.indexOf("top") == 0) {
		if(theItem != topNavItem) {
			itemObj.getElementsByTagName('img')[0].src = itemObj.getElementsByTagName('img')[0].src.replace('_on.gif','_off.gif');
		}
	}
	if(theItem.indexOf("link") == 0) {
		if (itemObj.className.indexOf("selected") == 0) itemObj.className = "";
		else itemObj.className = itemObj.className.substring(0,itemObj.className.indexOf("selected")-1);
	}
}

function dimAll() {
	for(q=1; q <= numTopLevelMenuItems; q++) {
		var topItemToDim = "top" + q;
		dimNav(topItemToDim);
	}	
	for(r=1; r <= numSecondLevelMenuItems; r++) {
		var primItemToDim = "primary" + r;
		dimNav(primItemToDim);
	}	
}


function relightInitialNavs() {
	lightNav(topNavItem);
	lightNav(activeNavItem);
}
