// creates an onload type effect

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// Finds the first List-item in the drop-down menu and class it

function findFirst() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("navigation")) return false;
	
	var nav1 = document.getElementById("program");
	var nav2 = document.getElementById("venue");
	var nav3 = document.getElementById("visitor");
	var nav4 = document.getElementById("register");
	
	var navList1 = nav1.getElementsByTagName("li");
	var navList2 = nav2.getElementsByTagName("li");
	var navList3 = nav3.getElementsByTagName("li");
	var navList4 = nav4.getElementsByTagName("li");
		
	if (navList1.length != 0) { navList1[0].className = "first"; }
	if (navList2.length != 0) { navList2[0].className = "first"; }
	if (navList3.length != 0) { navList3[0].className = "first"; }
	if (navList4.length != 0) { navList4[0].className = "first"; }
	
}

addLoadEvent(findFirst);

// Finds the current link

function rightTrim(sString) {
	while(sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
	
function highlightNav() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("navigation")) return false;
	
	var pattern = /^(.*)(\/)/;

	//run the window location URL against our regex pattern
	
	var currentUrl = window.location.href;
	var currentPath = currentUrl.replace(pattern, "");
	
	var rightNav = document.getElementById("navigation");
	var navLinks = rightNav.getElementsByTagName("a");
	
	for (var i=0; i<navLinks.length; i++) {
		var linkUrl = navLinks[i].getAttribute("href");
		
		var linkPath = linkUrl.replace(pattern, "");
		var trimPath = rightTrim(linkPath);

		if (trimPath == currentPath) {
				
				navLinks[i].className = "current";
		}
	}
}

addLoadEvent(highlightNav);

// IE Flyout Fix

sfHover = function() {
	var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);
