/* Created 2005.2.13 by Karina Rodriguez for Lebanese American University */
/* Changes by KR 2007.5.25 */

var navid = "navlist"; // <-- enter the id of the UL that has the nav links
var levelsdeep = 2; // <-- enter number of levels (the root excluded)
var activeimg = 'arrow-hassubs.gif'; // image to use to indicate presence of subs
var openimg = 'arrow-unfolded.gif'; // image to use to indicate sub is visible
var regularimg = 'arrow-nosubs.gif'; // obvious

/* end of user-configurable options */

var levelup = 'parentNode.parentNode';
var flag;

var navroot;
var lis;

var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
var isBadIE = ((navigator.appName == "Microsoft Internet Explorer") && (version < 7)) ? true : false;

// filter out old browsers (they will still see all navigation links, but without the dynamics)
if (document.getElementById) { window.onload = startNav; }

function startNav() {
	navroot = document.getElementById(navid); // UL that has nav links
	lis = navroot.getElementsByTagName('li'); // each and every list item under it
	for (var i=0; i<lis.length; i++) {
		// register toggle event for those that have ULs;
		// register "do nothing" event for ULs, to avoid conflicts;
		// only LIs will trigger events
		if (lis[i].getElementsByTagName('ul').length != 0) {
			lis[i].onclick = toggle;
			//lis[i].style.backgroundImage = 'url(' + activeimg + ')'; // change img to indicate presence of submenu
			if (lis[i].className.indexOf('here') != -1) { lis[i].className = 'active here' }
			else { lis[i].className = 'active'; }
			if (!isBadIE) { lis[i].style.cursor = 'pointer'; } else { lis[i].style.cursor = "hand"; }
			lis[i].getElementsByTagName('ul')[0].onclick = doNothing;
			lis[i].getElementsByTagName('ul')[0].style.cursor = "auto";
			lis[i].getElementsByTagName('a')[0].onclick = doNothing;
		}
	}
	// hide all subs; no argument means *each and every* UL.
	hideAll();
	if (thisPageId.length > 0) {
		for (var i=0; i<thisPageId.length; i++) { show(thisPageId[i]); }
	}
}

function toggle(e) {
	var thesub; // will hold UL in question

	// make sure the event isn't propagating, 
	// to avoid triggering this many times with one click due to nested items.
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

	// assign thesub the UL in question
	thesub = this.getElementsByTagName('ul')[0];

	// if it's hidden, show it (but hide all the others first)
	if (thesub.style.display == 'none') {
//		hideAll(this); // argument means: keep this one on.
//		thesub.style.display = 'block';
//		this.style.backgroundImage = 'url('+ openimg +')'; // change img to indicate change of state
		show(this.id);
	}
	
	// if not, show it
	else {
		// the following six lines would be necessary 
		// if we were using the visibility property instead of display:
		//var subsubs = this.getElementsByTagName('ul');
		//for (var i=0; i<subsubs.length; i++) {
		//	if (subsubs[i].style.display == 'block') {
		//		subsubs[i].style.display = 'none';
		//	}
		//}
//		thesub.style.display = 'none';
//		this.style.backgroundImage = 'url('+ activeimg +')'; // restore img to indicate change of state
		hide(this.id);
	}
}

function show(x) {
	//document.getElementById(x).style.backgroundImage = 'url('+ openimg +')';
	if (document.getElementById(x).className.indexOf('here') != -1) { document.getElementById(x).className = "open here"; }
	else { document.getElementById(x).className = "open"; }
	document.getElementById(x).getElementsByTagName('ul')[0].style.display = 'block';
}
function hide(x) {
	document.getElementById(x).getElementsByTagName('ul')[0].style.display = 'none';
	//document.getElementById(x).style.backgroundImage = 'url('+ activeimg +')';
	if (document.getElementById(x).className.indexOf('here') == -1) { document.getElementById(x).className = "active"; }
	else { document.getElementById(x).className = 'here'; }
}

function doNothing(e) {
	// make sure the event isn't propagating, 
	// to avoid triggering this many times with one click due to nested items.
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	// and do nothing:
	return;
}

function hideAll(obj) {
// if there is an argument, this function will hide only the ULs under LIs that are:
// - top-level;
// - not the clicked LI;
// - parents of submenus [otherwise we get an error].
if (obj) {
	for (var i=0; i<lis.length; i++) {
		if (lis[i].parentNode.id == navid && lis[i].getElementsByTagName('ul').length != 0) {
			if (!isCurrent(lis[i],obj)) {
				lis[i].getElementsByTagName('ul')[0].style.display = 'none';
				//lis[i].style.backgroundImage = 'url('+ activeimg +')'; // restore img to indicate change of state
				if (lis[i].className.indexOf('here') != -1) {
					lis[i].className = "active here"; // restore img to indicate change of state
				}
				else { lis[i].className = "active"; }
			}
		}
	}
}
// if there's no argument this function will hide absolutely all ULs
else {
	for (var i=0; i<lis.length; i++) {
		if (lis[i].getElementsByTagName('ul').length != 0) {
			lis[i].getElementsByTagName('ul')[0].style.display = 'none';
		}
	}
}
}

function isCurrent(x,y) {
	levelup = 'parentNode.parentNode';
	for (var i=0; i<levelsdeep; i++) {
		if (x == y.eval(levelup)) { flag = true; i=levelsdeep;  }
		else { flag = false; }
		levelup += '.parentNode.parentNode';
	}
	return flag;
}
