var parentNavOffset = 178;
//var numTopLevelMenuItems = 7;						// number of top level nav items in nav
//var numSecondLevelMenuItems = 6;					// number of secondary nav items in nav
//var numMenus = 5;									// number of secondary dhtml menus in play
//var subMenus = 1;									// 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++) {
		//alert("about to 'dimNav' " + arrayOfNavItems[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++) {
		//	listOfNavItems = listOfNavItems + arrayOfNavItems[p] + "\n";
		//}
		//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)) {
		//alert(parentNav + " and " + childNav);
		timerID2 = setTimeout("toggleMenu(parentNav,childNav)",500);
	}
	else {
		toggleMenu(parentNav,childNav);
	}
}

function toggleMenu(parent,child) {
	theParent = parent;
	theChild = child;
	
	// if there is no fly out to display...
	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
	}
	// if there is a fly out to display
	else {
		if((theParent.indexOf('primary') == 0) || (theParent.indexOf('top') == 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(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"
		positionNav(theParent,theChild); 				// Position the nav about to be displayed
	}
}

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){
	//alert("running hideSubCountdown");
	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]);
	}
}

//-------------------------------------------------------------------------------
// 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";
	}
}	

//----------------------------------------------------------------------------
// findPosX and findPosY find the top and left coordinate of any object passed
//----------------------------------------------------------------------------
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curTop += obj.y;
	return curtop;
}

//-----------------------------------------------------------------------------
// Called from positionNav, this function assigns the height of the viewable 
// area of the browser window to a global variable, browWinHeight.  
//-----------------------------------------------------------------------------

function getBrowserWinHeight() {
	if((document.all && document.getElementById) && (document.documentElement.clientHeight == 0)) {
		// < IE 6
		browWinHeight = document.body.clientHeight;
	}
	else {
		// IE 6+
		browWinHeight = document.documentElement.clientHeight;
	}
}

//-----------------------------------------------------------------------------
// Called from positionNav, this function checks to see how much the user has 
// scrolled since loading or reloading the page.  Assigns the value to a global
// variable, browScrollDistance.  The if/else logic is to cover
// IE 5.5 or less, IE 6, or DOM browsers.
//-----------------------------------------------------------------------------

function getScrollDistance() {
	// Get the inner browser window height; for < IE 6
	if((document.all && document.getElementById) && (document.documentElement.clientHeight == 0)) {
		// < IE 6
		browScrollDistance = document.body.scrollTop;
	}
	else if((document.all && document.getElementById) && (document.documentElement.clientHeight != 0)) {
		// IE 6+
		browScrollDistance = document.documentElement.scrollTop;
	}
	else {
		//dom
		browScrollDistance = window.pageYOffset;
	}
}


function positionNav(theParentNav,theChildNav) {
	getBrowserWinHeight();												// get the size of the inner browser window
	getScrollDistance(); 												// get the distance user has scrolled
	
	thisParent = theParentNav;
	thisChild = theChildNav;
	
	parentNavObj = eval(docObj + thisParent + "')");
	childNavObj = eval(docObj + thisChild + "')");
														
	var xPosForChildNav = (findPosX(parentNavObj) + parentNavOffset);	// is x pos of parent plus width of parent
	var yPosForChildNav = findPosY(parentNavObj);						// is y pos of parent initially
								
	childNavObj.style.left = xPosForChildNav + "px";
	
	if((yPosForChildNav + childNavObj.clientHeight) > (browWinHeight + browScrollDistance)){
		childNavObj.style.top = (yPosForChildNav - ((yPosForChildNav + childNavObj.clientHeight) - (browWinHeight + browScrollDistance))) + "px";
	}
	else if((yPosForChildNav + childNavObj.clientHeight) <= (browWinHeight + browScrollDistance)){
		childNavObj.style.top = yPosForChildNav + "px";
	}
	showMenu(thisChild);
}

//------------------------------------------------------------------------
// lightNav and dimNav hightlight 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) {
			if(itemObj.className == "subNavNoArrow") {
				itemObj.className="subNavNoArrowActive"; 		//handle the one secondary nav that doesn't have a sub menu
			}
			if(itemObj.className == "subNav") {
				itemObj.className = "subNavActive";
			}
		}
		if(theItem.indexOf("top") == 0) {
			if(itemObj.className == "leftGlobal") {
				itemObj.className = "leftGlobalOn";
			}
			else if(itemObj.className == "leftGlobalArrow") {
				itemObj.className = "leftGlobalArrowOn";
			}
			else {
				itemObj.className = "mainNavActive";
			}
		}
		if(theItem.indexOf("link") == 0) {
			itemObj.className = "secondaryOn";
		}
	}
}

function dimNav(dimItem) {
	var theItem = dimItem; 
	var itemObj = eval(docObj + theItem + "')");
	if(theItem.indexOf("primary") == 0) {
		if(itemObj.className == "subNavNoArrowActive") {
			itemObj.className="subNavNoArrow";
		}
		if(itemObj.className == "subNavActive") {
			itemObj.className="subNav";
		}
	}
	if(theItem.indexOf("link") == 0) {
		itemObj.className = "secondaryWithSub";
	}
	if((theItem.indexOf("top")) == 0) {
		if(itemObj.className == "leftGlobalOn" || itemObj.className == "leftGlobal") {
			itemObj.className = "leftGlobal";
		}
		else if(itemObj.className == "leftGlobalArrowOn" || itemObj.className == "leftGlobalArrow") {
			itemObj.className = "leftGlobalArrow"
		}
		else {
			itemObj.className = "mainNavOff";
		}
	}
}

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);
}