//////////////////////////////////////////////////////////////////
//
// Navigation
//
///////////////////////////////////////////////////////////////////

/*	Declare global variables	*/

// the timer object to reset everything to default position
var NavTimer = null;

// NavTimerDelay: the countdown in milliseconds
var NavTimerDelay = 750;

// array to hold all navbtn objects
var navbtns_array = new Array();

// array to hold all pulldown objects
var pulldowns_array = new Array();

// if no nav btn is selected by default for this page, set its value to an empty string
if (!current_navbtn_id)
	var current_navbtn_id = "";

// the button that is currently being moused over.
// default value set to current section button.
var selected_navbtn_id = current_navbtn_id;


/*	timer functions	*/
function startNavTimer() {
	NavTimer = setTimeout('restoreDefaults();', NavTimerDelay);
}

// stops the timer
function stopNavTimer() {
	NavTimer = clearTimeout(NavTimer);
}



/*	main navigation functions	*/

// hides all pulldowns
function hideAllPulldowns() {
	for (i=0; i<pulldowns_array.length; i++) {
		pulldowns_array[i].style.display = 'none';
	}
}

// shows pulldown for selected nav button
function showPulldown() {
	var btn_id = selected_navbtn_id;
	if (btn_id != "") {
		pulldown_id = btn_id.replace("navbtn","pulldown");
		var pulldown_obj = document.getElementById(pulldown_id);
		// if a pulldown exists for this button, show it
		if (pulldown_obj) {
			pulldown_obj.style.display = '';
		}
	}
}

function selectNavBtn(id) {
	// turn off all images except the one passed as the argument
	for (i=0; i<navbtns_array.length ;i++) {
		var obj = navbtns_array[i].firstChild;
		// make sure node is an object element
		if (obj.nodeType == 1) {
			//alert("it works");
		} else {
			//alert("Error: there is space between the anchor tags and the image tag for this nav button");
			obj = obj.nextSibling;
		}
		// turn selected btn on
		if (navbtns_array[i].id == id) {
			obj.src = obj.src.replace("_off", "_on");
			//alert(obj.src);
		// turn all others off
		} else {
			obj.src = obj.src.replace("_on", "_off");
		}
	}
}

// shows the selected button/pulldown onmouseover
function selectNav() {
	// stop timer
	stopNavTimer();
	// image swap
	selectNavBtn(selected_navbtn_id);
	// hide all pulldowns
	hideAllPulldowns();
	// show selected pulldown
	showPulldown();
}

// called by the timer
function restoreDefaults() {
	// hide the pulldowns
	hideAllPulldowns();
	// select the original navbutton
	selectNavBtn(current_navbtn_id);
}

/*	create event handlers	*/

// attach behaviors - called on page load
function setupNavigation() {
	// define globals
	// set values for navbtns array
	var anchors_array = document.getElementsByTagName("a");
	var j=0;
	for (i=0; i<anchors_array.length; i++) {
		if (anchors_array[i].className == 'navbtn') {
			navbtns_array[j] = anchors_array[i];
			j++;
		}
	}
	// set values for pulldowns array
	var divs_array = document.getElementsByTagName("div");
	var k=0;
	for (i=0; i<divs_array.length; i++) {
		if (divs_array[i].className == 'pulldown') {
			pulldowns_array[k] = divs_array[i];
			k++;
		}
	}
	
	
	// attach events
	// set up events for nav buttons
	for (i=0; i<navbtns_array.length; i++) {
		navbtns_array[i].onmouseover = function() {
			// set selected navbutton id value
			selected_navbtn_id = this.id;
			// selects btn and pulldown and closes the others
			selectNav();
		}
		navbtns_array[i].onmouseout = function() {
			// timed hide
			startNavTimer();
		}	
	}
	// set up events for pulldowns
	for (i=0; i<pulldowns_array.length; i++) {
		pulldowns_array[i].onmouseover = function() {
			// stop timer
			stopNavTimer();
		}
		pulldowns_array[i].onmouseout = function() {
			// start timed hide
			startNavTimer();
		}
	}
}