//------------------- NAVIGATION -------------------//

// Declarations
var imgBase = '/newSite/images/navMain/';
var mouseOutSuffix = '.png';
var mouseOverSuffix = '_on.png';
var pathArray = new Array();
var mainSubNavs = new Array();
var navSectSubs = new Array();
var winTimer;

var navUls;
var sectAHref;
var accordionContent = [];

if (document.getElementById('accordionHolder')) {
	accordionContent = document.getElementById('accordionHolder').getElementsByTagName('div');
	// close all but one
	for(var i=0;i<accordionContent.length;i++) {
		if (accordionContent[i].id != keepOpen && accordionContent[i].className == 'wrapper') {
			accordionContent[i].className = 'hideItem';
			first_child(node_before(accordionContent[i])).className = 'headerClose';
		}
	}
}

if (document.getElementById('main')) {
	navUls = document.getElementById('main').getElementsByTagName('ul');
}
if (document.getElementById('section')) {
	sectAHref = document.getElementById('section').getElementsByTagName('a');
}
if (document.getElementById('subNavStates')) {
	document.getElementById('subNavStates').style.display='none';
}
//var allARef = document.getElementsByTagName('a');
//var thisPage;

//loop through the a href array, find any that match the current page (document.location.href) and set state(via class name) to hover
//for (var i = 0;i < allARef.length;i++) {
//	if (allARef[i].href==document.location.href) {
		//allARef[i].className=allARef[i].className+'Hover';
		//thisPage=allARef[i].id;	
		//}
//}

//loop through the section nav a href array, find any that match the current page (document.location.href) and set state(via class name) to hover
if (sectAHref) {
for (var i = 0;i < sectAHref.length;i++) {
	if (sectAHref[i].href==document.location.href) {
		sectAHref[i].className=sectAHref[i].className+'Hover';
		}
}
}

//cycle through all divs and store nav sub divs in arrays
for (var i=0; i< navUls.length; i++) {
	if (navUls[i].id.match(/^mainSubNav/)){
		mainSubNavs.push(navUls[i]);
	}
}

//Set nav state
//get the folder of the current page (which, in this case, is the 3rd element in pathArray)
//the folder name and the id of the image are set up to use the same name
//we can thus use the folder name to set the img source to hover
//also designate that this is current page by affixing 'Selected' to its id
//this is used as a flag so we don't change its state in mouseover and out events

pathArray = document.location.href.split("/");

//for (var i=0;i < pathArray.length;i++) {
				//alert("Path Array: " + pathArray[i]);
	//}

	
if (document.getElementById(pathArray[4])) {
	// use folder name to set img source to hover
	document.getElementById(pathArray[4]).src=imgBase+pathArray[4]+mouseOverSuffix;
	// flag img so we don't change its state in mouseover and out events
	document.getElementById(pathArray[4]).id=document.getElementById(pathArray[4]).id+'Selected';
	}
	
hideMainSubNavs();

function hideMainSubNavs() {
	for (var i=0;i < mainSubNavs.length;i++) {
				mainSubNavs[i].className = mainSubNavs[i].className.replace('showNav', 'hideNav');	
	}
}

function showMainSubNav(thisNav) {
if (thisNav.match(/Selected$/))
{
	thisNav = thisNav.replace(/Selected$/,'');
}
	window.clearTimeout(winTimer);
	hideMainSubNavs();
  if (document.getElementById('mainSubNav_'+thisNav)) {
		document.getElementById("mainSubNav_" + thisNav).className = document.getElementById("mainSubNav_" + thisNav).className.replace('hideNav', 'showNav');
	}
}

function waitASec() {
	window.clearTimeout(winTimer);
	winTimer = window.setTimeout('hideMainSubNavs()', 300);
}

//image swap
//need to pass it the object when called i.e. onmouseover = imgOver(this)
function navImgOver() {
	var img = arguments[0];
	if (!(img.id.match(/Selected$/))){
		img.src = ((img.origSrc)?img.origSrc:img.src).replace(/(.*)(\..*)$/, '$1_on$2');
	}
}

//need to pass it the object when called i.e. onmouseout = imgOut(this)
function navImgOut() {
	var img = arguments[0];
	if (!(img.id.match(/Selected$/))){
		img.src = ((img.origSrc)?img.origSrc:img.src).replace('_on', ''); 
	}
}

function closeAccordion(notThisOne) {
	// close all but one
	for(var i=0;i<accordionContent.length;i++) {
		if (accordionContent[i].id != notThisOne && accordionContent[i].className == 'wrapper') {
			accordionContent[i].className = 'hideItem';
			first_child(node_before(accordionContent[i])).className = 'headerClose';
		}
		if (accordionContent[i].id == notThisOne && accordionContent[i].className == 'hideItem') {
			accordionContent[i].className = 'wrapper';
			first_child(node_before(accordionContent[i])).className = 'headerOpen';
		}
	}
}