// initialize/globalize variables
var primaryMaxNumItems;
var secondaryMaxNumItems;
var secondaryTier1;
var secondaryTier2;
var primaryNavSetTimeout;
var secondaryNavSetTimeout = 0;
var navObjReference;
var selectArray;

function dropDownVisibility(dropVisibile)
{ // resolves issue with navigation appearing behind select fields
	// obtain all select elements - assign to an array
	var selectArray = document.getElementsByTagName("select");

	if (vamIniState != 1) {
		if (document.all)
		{ // if IE
			for (var i = 0; i < selectArray.length; i++)
			{ // hide all drop down elements
				selectArray[i].style.visibility = dropVisibile;
			} // end for
		} // end if
	} // end if
} // end funciton

function findPosX(obj)
{ // find the horizontal position [http://www.quirksmode.org/js/findpos.html]

	var curleft = 0;
	if (obj.offsetParent)
	{ // 
		while (obj.offsetParent)
		{ //
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		} // end while
	} // end if
	else if (obj.x)
		curleft += obj.x;
	return curleft;
} // end function

function findPosY(obj)
{ // find the vertical position [http://www.quirksmode.org/js/findpos.html]
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		} // end while
	} // end if
	else if (obj.y)
		curtop += obj.y;
	return curtop;
} // end function

function setLyr(obj,lyr, tier2)
{ // set the position [http://www.quirksmode.org/js/findpos.html]
	for (var i = 0; i < stationList.length; i++)
	{ // loop through station section (i.e. right section)
		if (stationList[i] == tier2)
		{ // station section - obtain/apply right hand justification
			var newX = findPosX(obj) - secondaryNavWidth + obj.offsetWidth + 10;
			break;
		} else
		{ // news section - obtain/apply left hand justification
			var newX = findPosX(obj);
		} // end if
	} // end for
	var newY = findPosY(obj);
	var x = new getObj(lyr);
	if (document.all)
	{ // ie browsers
		x.style.left = newX - 5 + 'px';
	} else
	{ // gecko browsers
		x.style.left = newX - 5 + 'px';
	} // end if
} // end function

function getObj(name)
{ // get object by name [http://www.quirksmode.org/js/findpos.html]
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers)
	{
		if (document.layers[name])
		{
			this.obj = document.layers[name];
			this.style = document.layers[name];
		} else
		{
			this.obj = document.layers.testP.layers[name];
			this.style = document.layers.testP.layers[name];
		} // end if
	} // end if
} // end function

function navDisplay(navLevel, event, maxNumItems, tier1, tier2, tier3, obj)
{ // handle actions for all nav events

	navObjReference = obj;
	if ( navLevel == "primary" )
	{ // primary nav selected
		if ( event == "on" )
		{ // event on
			// cancel primary on and off timeout
			clearTimeout(primaryNavSetTimeout);

			// cancel secondary on and off timeout
			clearTimeout(secondaryNavSetTimeout);

			primaryNavSetTimeout = setTimeout( function() { onPrimaryNav( maxNumItems, tier1, tier2, tier3); }, navDelayOn );
		} else if ( event == "off" )
		{ // event off
			// cancel primary on and off timeout
			clearTimeout(primaryNavSetTimeout);

			// cancel secondary on and off timeout
			clearTimeout(secondaryNavSetTimeout);

			primaryNavSetTimeout = setTimeout( function() { offPrimaryNav(maxNumItems); }, navDelayOff );
		} // end if
	} else if ( navLevel == "secondary" )
	{ // secondary nav selected
		if ( event == "on" )
		{ // event on
			// cancel primary on and off timeout
			clearTimeout(primaryNavSetTimeout);

			// cancel secondary on and off timeout
			clearTimeout(secondaryNavSetTimeout);
		} else if ( event == "off" )
		{ // event off
			// cancel primary on and off timeout
			clearTimeout(primaryNavSetTimeout);

			// cancel secondary on and off timeout
			clearTimeout(secondaryNavSetTimeout);

			secondaryNavSetTimeout = setTimeout( function() { offSecondaryNav(maxNumItems, tier1, tier2, tier3); }, navDelayOff );
		} // end if
	} // end if
} // end function

function onPrimaryNav(maxNumItems, tier1, tier2, tier3)
{ //primary nav selected
	primaryMaxNumItems = maxNumItems;

	dropDownVisibility('hidden');

	for (i = 1; i <= maxNumItems; i++ )
	{ // apply passive style to all primary items
		if (document.getElementById("primaryNav" + i) != null)
		{ // detect if element exists
			document.getElementById("primaryNav" + i).className = 'navPassive';
		} // end if
	} // end for

	for (i = 1; i <= primaryMaxNumItems; i++ )
	{ // hide all secondary items
		if (document.getElementById("secondaryNav" + i) != null)
		{ // detect if element exists
			document.getElementById("secondaryNav" + i).className = 'navPassive';
		} // end if
	} // end for

	// apply active style to selected primary item
	if (document.getElementById("primaryNav" + tier2) != null)
	{ // detect if element exists
		document.getElementById("primaryNav" + tier2).className = 'navActive';
	} // end if

	// set secondary position
	setLyr(navObjReference, "secondaryNav" + tier2, tier2);

	// display linked secondary item
	if (document.getElementById("secondaryNav" + tier2) != null)
	{ // detect if element exists
		document.getElementById("secondaryNav" + tier2).className = 'navActive';
	} // end if

	// update secondaryNavLevel
	secondaryNavLevel = tier2;
} // end function

function offPrimaryNav(maxNumItems)
{ // primary nav deselected

	dropDownVisibility('visible');

	for (i = 1; i <= primaryMaxNumItems; i++ )
	{ // apply passive style to all primary items
		if (document.getElementById("primaryNav" + i) != null)
		{ // detect if element exists
			document.getElementById("primaryNav" + i).className = 'navPassive';
		} // end if
	} // end for

	for (i = 1; i <= primaryMaxNumItems; i++ )
	{ // hide all secondary items
		if (document.getElementById("secondaryNav" + i) != null)
		{ // detect if element exists
			document.getElementById("secondaryNav" + i).className = 'navPassive';
		} // end if
	} // end for

	if ( defaultPrimaryNav != "" )
	{ // apply active style to default primary item
		if (document.getElementById(defaultPrimaryNav) != null)
		{ // detect if element exists
			document.getElementById(defaultPrimaryNav).className = 'navActive';
		} // end if
	} // end if
} // end function

function offSecondaryNav(maxNumItems, tier1, tier2, tier3)
{ // secondary nav deselected

	dropDownVisibility('visible');

	for (i = 1; i <= primaryMaxNumItems; i++ )
	{ // apply passive style to all primary items
		if (document.getElementById("primaryNav" + i) != null)
		{ // detect if element exists
			document.getElementById("primaryNav" + i).className = 'navPassive';
		} // end if
	} // end for

	for (i = 1; i <= primaryMaxNumItems; i++ )
	{ // apply passive style to all secondary items
		if (document.getElementById("secondaryNav" + i) != null)
		{ // detect if element exists
			document.getElementById("secondaryNav" + i).className = 'navPassive';
		} // end if
	} // end for

	if ( defaultPrimaryNav != "" )
	{ // apply active style to default primary item
		if (document.getElementById(defaultPrimaryNav) != null)
		{ // detect if element exists
			document.getElementById(defaultPrimaryNav).className = 'navActive';
		} // end if
	} // end if
} // end function