addEvent(window, 'load', function(){
setCurrent();
});
var totalululs = 0; var nowopen = ""; var wantopen = ""; working="false"; setId = 0; var speed = 50; var targH = 0; var ccounter = 0;

function swapout(what){
	working = "true"; // stops people clicking during transitions
	temp = what.split("sublevelnk"); wantopen = ("sublevel" + temp[1]);
	if(nowopen == ""){ // nothing open so open the new link
		setId = setInterval("nOpen(wantopen);", speed);
	}
	else { // else close the open one and then open the new one
		setId = setInterval("nClose(nowopen);", speed);
	}
}

function nOpen(what){
	ccounter++;
	var opening = document.getElementById(what); 
	if(ccounter == 1){ // setup initial ul for resizing
		opening.style.display = "block"; targH = parseInt(opening.scrollHeight, 10); opening.style.display = "none"; // figure out how tall it is
		for(var f=0; f< opening.childNodes.length;f++){ // hide the li's for now
			if(opening.childNodes[f].nodeName == "LI"){ opening.childNodes[f].style.display= "none"; }
		}
		opening.style.height = "0px"; opening.style.display= "block"; addClass(opening,"open_ul"); addClass(opening.parentNode.firstChild,"open_parent");

	}
	var nowOpenHeight = parseInt(opening.scrollHeight, 10);
	if(nowOpenHeight < targH){
		opening.style.height = ((nowOpenHeight + 10) + "px");
	}
	else {
		opening.style.height = targH + "px";
		for(var g=0; g< opening.childNodes.length;g++){ // hide the li's for now
			if(opening.childNodes[g].nodeName == "LI"){ opening.childNodes[g].style.display= "inline"; }
		}
		clearInterval(setId); nowopen = wantopen; wantopen= ""; ccounter = 0; working= "false";
	}
}

function nClose(what){
	var closing = document.getElementById(what);  
	for(var f=0; f< closing.childNodes.length;f++){
		if(closing.childNodes[f].nodeName == "LI"){ closing.childNodes[f].style.display= "none"; }
	}
	var nowHeight = parseInt(closing.scrollHeight, 10);
	if(nowHeight >= 10){
		closing.style.height = ((nowHeight - 10) + "px");
	}
	else {
		closing.style.display = "none"; closing.style.height = "auto";
		for(var g=0; g< closing.childNodes.length;g++){
			if(closing.childNodes[g].nodeName == "LI"){ closing.childNodes[g].style.display="block"; }
		}
		clearInterval(setId); nowopen = ""; setId = setInterval("nOpen(wantopen);", speed);
	}
   removeClass(closing,"open_ul");
removeClass(closing.parentNode.firstChild,"open_parent");
}

function setCurrent(){
	temp = document.location.href.split("/"); 
if(temp[3]=="sites"){ i = 5; } else { i = 3; }
        var pagename = temp[i]; temp = pagename.indexOf(".");

	if(temp != -1){ temp = pagename.split("."); pagename = temp[0]; }

	var container = document.getElementById('nav');
	for(var i=0; i < container.childNodes.length; i++){ // loop through all 1st nodes
		if(container.childNodes[i].nodeName == "LI"){
			for(var b=0;b< container.childNodes[i].childNodes.length;b++){ // loop through a 1st levels li child nodes
			// do a test for a level 1 a here
				etemp = container.childNodes[i].firstChild.href;
				if(etemp.charAt(etemp.length - 1) == "/"){ etemp = etemp.substr(0, etemp.length - 1); }
				etemp2 = etemp.split("/"); etemp3 = etemp2[etemp2.length -1];
				if(etemp3.indexOf(".") != -1){ etemp4 = etemp3.split("."); etemp3 = etemp4[0]; }
				
if(etemp3 == pagename){ // if this page is in the primary-level navigation menu
					econtaininglnk = container.childNodes[i].firstChild; 
					econtaininglnk.setAttribute('class', 'current'); econtaininglnk.setAttribute('className', 'current');
				}
			// end test
			// do a secondary level test
				if(container.childNodes[i].childNodes[b].nodeName == "UL"){ //loopthrough 2nd level nodes
					container.childNodes[i].firstChild.setAttribute('id', 'sublevelnk' + totalululs);
					container.childNodes[i].firstChild.setAttribute('href','javascript:void(0)');
					container.childNodes[i].childNodes[b].setAttribute('id', 'sublevel' + totalululs);
					addEvent(container.childNodes[i].firstChild, 'click', function(){ swapout(this.id) });
					totalululs++; // assign ids to uls
					for(var c=0; c < container.childNodes[i].childNodes[b].childNodes.length; c++){
						if(container.childNodes[i].childNodes[b].childNodes[c].nodeName == "LI"){ // loop through 2nd levels li child nodes
							dtemp = container.childNodes[i].childNodes[b].childNodes[c].firstChild.href;
							if(dtemp.charAt(dtemp.length - 1) == "/"){ dtemp = dtemp.substr(0, dtemp.length - 1); }
							dtemp2 = dtemp.split("/"); dtemp3 = dtemp2[dtemp2.length -1]; 
							if(dtemp3 == pagename){ // if this page is in the secondary-level navigation menu
								containingul = container.childNodes[i].childNodes[b]; containingul.style.display = "block"; containingul.parentNode.firstChild.className = "open_parent";
								containinglnk = container.childNodes[i].childNodes[b].childNodes[c].firstChild; 
								containinglnk.setAttribute('class', 'current'); containinglnk.setAttribute('className', 'current');
								nowopen = ("sublevel" + (totalululs - 1)); document.getElementById('sublevel' + (totalululs -1)).style.height = document.getElementById('sublevel' + (totalululs -1)).scrollHeight + "px";
							}
						}
					}
				} 
				// end test
			}
		}
	} // end for-loop
} // end function

function hasClass(ele,cls) {
   return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
 

function addClass(ele,cls) {
   if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
  if (hasClass(ele,cls)) {
    var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
    ele.className=ele.className.replace(reg,' ');
  }
}


