var currentSection, kay,turnOff;

function shutOff(){
	
	if(turnOff != currentOn ){
	//	alert("turnOff="+turnOff.nodeName+"."+turnOff.id+"|"+currentOn.nodeName+"="+currentOn.id);
		while(turnOff.className.indexOf(" over") != -1)
			turnOff.className=turnOff.className.replace(" over","");
	
		if(currentSection !=null){
			var nav = document.getElementById("nav");
		
			var over = false;
			
			for (var i=0; i<nav.childNodes.length; i++) {
				if(nav.childNodes[i].nodeName.toLowerCase() == "li" ){
		
						if(nav.childNodes[i] != currentSection && nav.childNodes[i].className.indexOf(" over")!=-1){
							if(nav.childNodes[i]!=currentOn){
								while(nav.childNodes[i].className.indexOf(" over") != -1)
									nav.childNodes[i].className=turnOff.className.replace(" over","");
				
							}
							over = true;
							
							}
				}
				
			}
		
	//	window.status ="turnOff="+turnOff.nodeName+"."+turnOff.id+"|"+currentOn.nodeName+"="+currentOn.id+" over="+over;
			if(!over){
				currentSection.id = "currentSection";
				currentSection.getElementsByTagName("ul")[0].style.display = "block";
			}
		}
	}
}


startList = function() {

if (document.getElementById) {
	currentSection =  document.getElementById("currentSection");
	
	addEventListenert(document,'mouseover',follow);
	
	var location = window.location.toString();
	var locKay = location.split("#")[0];
	if(location.indexOf("page=") != -1){
		kay = location.split("=").pop();
		locKay = kay.substring(0,5);
	}
	var subnavSet = false;
	var navLI = document.getElementById("nav").getElementsByTagName("li");

	for (var i=navLI.length-1; i>=0; i--) {
		var node = navLI[i];
		if(node.className.indexOf("parentItem") != -1){
			if (node != currentSection){
				node.onmouseover=function() {
					if(currentSection != null){
						currentSection.id = "off";
						currentSection.getElementsByTagName("ul")[0].style.display = "none";
					}
				 	this.className += " over";
				 }
				  node.onmouseout=function() {
				  // shutOff();
				  
				  	turnOff = this;
				 	setTimeout('shutOff()',50);
				
				  }
			}
		}else if( !subnavSet){
			var link = node.getElementsByTagName("a")[0];
			
		//	if (link.href.endsWith(locKay) ){
			if (locKay.indexOf(link.href) != -1){
				link.className += " current";
				subnavSet = true;
				
			}else if(locKay.indexOf("http://") == -1 &&link.href.indexOf(locKay) !=-1){
				
					link.className += " current";
					subnavSet = true;
			
			}
			node.onmouseover=function() {
				
				this.className+=" over";
				
			  }
			  node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
					
			  }
  		}
 	 }
 	 var navUL = document.getElementById("nav").getElementsByTagName("ul");
 	 for (var i=0; i<navUL.length; i++) {
		var node = navUL[i];
		if(node.parentNode != currentSection)
		  node.onmouseout=function() {
					turnOff = this.parentNode;
				 	setTimeout('shutOff()',50);
			  }
	}
 	setTertiarynav(locKay);
 }

 
}


function setTertiarynav(locKay){
	if(document.getElementById("tertiarynav")){
		var links = document.getElementById("tertiarynav").getElementsByTagName("a");
		if(locKay.indexOf("http://")==-1)
			locKay= window.location.toString();
		for(var i=0; i<links.length; i++){
			
			if(links[i].href.split("#")[0].endsWith(locKay) ){
				links[i].className += " current";
			//	break;
			}
		
		}
	}
}

String.prototype.endsWith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
  }
  


function addEventListenert(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
//	elm=getRawObject(elm);
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    //alert("Handler could not be added");
  }
} 


follow = function(evt){
	var targetEl = evt.target ? evt.target : evt.srcElement;
	
	while(targetEl.parentNode != "undefined" && targetEl.parentNode != null){
		targetEl = targetEl.parentNode;
		if(targetEl.className && targetEl.className.indexOf("parentItem") != -1 ){
			break;
			
			}
	}
	currentOn = targetEl;

}

window.onload=startList;
