function initNav(divs){
	var effects_a = new Array();
	var lis = divs.getElementsByTagName('li');
	for(var i =0;i<lis.length;i++){
		lis[i].onmouseover = mlover;
		lis[i].onmouseout = mloutSetTimeout;
		var uls = lis[i].getElementsByTagName('ul');
		for(var k=0;k<uls.length;k++){
			var found = 'no';
			for(var z=0;z<effects_a.length;z++){
				if(effects_a[z] == uls[k]){
					found = 'yes';
				}
			}
			if(found == 'no'){
				effects_a[effects_a.length] = uls[k];
				uls[k].style.zIndex = '1000';
				mlEffectLoad(uls[k]);
			}
		}
	}
}
function mloutSetTimeout(e){
	if(!e){
		var the_e = window.event;
	}
	else{
		var the_e = e;
	}
	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.toElement;
	if(reltg){
		var under = ancestor(reltg,this);
		if(under === false && reltg != this){
			window.mlLast = this;
			var parent = this.parentNode;
			while(parent.parentNode && parent.className.indexOf('mlmenu') == -1){
				parent = parent.parentNode;
			}
			window.mlTimeout = setTimeout(function(){mlout()},1000);
		}
	}
}
function mlout(){
if(window.mlLast==null)return false;
	var uls = window.mlLast.getElementsByTagName('ul');
	for(var i=0;i<uls.length;i++){
		mlEffectOut(uls[i]);
		window.mlLast.className = 'haschild hide';
	}
	window.lastover = null;
}
function mlover(e){
	if(!e){
		var the_e = window.event;
	}
	else{
		var the_e = e;
	}
	the_e.cancelBubble = true;
	if(the_e.stopPropagation){
		the_e.stopPropagation();
	}
	clearTimeout(window.mlTimeout);
	if(window.mlLast && window.mlLast != this && ancestor(this,window.mlLast) == false){
		mlout();
	}
	else{
		window.mlLast = null;
	}
	var reltg = (the_e.relatedTarget) ? the_e.relatedTarget : the_e.fromElement;
	var ob = this.getElementsByTagName('ul');
		var under = ancestor(reltg,this);
		if(ob[0] && under == false){
			if(window.lastover != ob[0]){
				this.className = 'haschild';
				mlEffectOver(ob[0],this);
				window.lastover = ob[0];
			}
		}
}
function mlEffectOver(ob,parent){
	parent.className="hover";
	ob.className = 'show';
}
function mlEffectOut(ob){
	var parent = ob.parentNode;
	parent.className="";
	ob.className = '';
}
function mlEffectLoad(ob){
	var parent = ob.parentNode;
	while(parent.parentNode && parent.className.indexOf('mlmenu') == -1){
		parent = parent.parentNode;
	}
}
function ancestor(child, parent){
	if(child==null)return false;//Saves checking elsewhere
	//This is a fix for a Firefox bug *gasp*
	//Aparantly causes a bug in Opera!
	//I see no choice but a browser detect. *sigh* I didn't want to have to do this.
	if(navigator.userAgent.indexOf('Gecko') != -1 && navigator.userAgent.indexOf('Opera') == -1){
		//This should only be run by Gecko based browsers. this code should be fine in everything but Opera so forge away browsers.
		var allc = parent.getElementsByTagName('*');
		for(var i= 0;i<allc.length;i++){
			if(allc[i] == child){
				return true;
			}
		}
	}
	else{
		//http://www.dynamicdrive.com/forums/showthread.php?t=12341 Thanks Twey!
		for(; child.parentNode; child = child.parentNode){
			if(child.parentNode === parent) return true;
		}
	}
	return false;
}

function initSubnav(){
	var subnav = document.getElementById('subnav');
	var subnavas = subnav.getElementsByTagName('a');
	for (var a=0; a<subnavas.length; a++){
		var currenthref=String(subnavas[a].href);
		var currentloc=String(document.location);
		currentloc = currentloc.split("#");
		currentloc = currentloc[0];
		if (unescape(currenthref)==unescape(currentloc)){
			subnavas[a].className="current"
			break;
		}
	}
}

function initButtons(){
	var inputs = document.getElementsByTagName("input");
	for (var i = 0; i<inputs.length; i++){
		if(inputs[i].type=="image"){
			inputs.item(i).style.borderWidth = "0px"
		}
	}
}

function initLinks(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");	
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className += " external";
		obj.title += "This link will open in a new window.";
	};
	this.checkpdf = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("pdf")!=-1) ? true : false;				
	};
	this.setpdf = function(obj){
		obj.target = "_blank";
		if (obj.className!="large" && obj.className!="thumb") {
			obj.innerHTML += " <img src=\"/phs/img/ic-pdf.gif\" />";
			obj.className += " pdf";
		}
		obj.title += "This PDF will open in a new window.";
	};
	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
		if(checkpdf(a[i])) setpdf(a[i]);
	};
};

function sfTarget () {
	var sfEls=document.getElementsByTagName("div");
	var aEls = document.getElementsByTagName("a");
	document.lastTarget = null;
	for (var i=0; i<sfEls.length; i++) {
		if (sfEls[i].id) {
			if (location.hash==("#" + sfEls[i].id)) {
				sfEls[i].className+=" sftarget";
				document.lastTarget=sfEls[i];
			}
			for (var j=0; j<aEls.length; j++) {
				if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i]; aEls[j].onclick = function() {
					if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sftarget\\b"), "");
					if (this.targetEl) this.targetEl.className+=" sftarget"; document.lastTarget=this.targetEl;
					return true;
				}
			}
		}
	}
}

function initTabs(){
	tog = document.getElementById("tabs").getElementsByTagName('a');
	for (var i = 0; i < tog.length; i++){
		if (tog[i].href.indexOf("#")!=-1) addAnEvent(tog.item(i),'click',toggleTabs);
		tog[i].innerHTML += "<img src=\"../img/tabcorner-r.gif\" width=\"6\" height=\"6\" alt=\"\" />";
	}
}

function toggleTabs(){
	for (var i = 0; i < tog.length; i++){
		tog[i].className = "";
	}
	document.getElementById("faculty").style.display = document.getElementById("facultydept").style.display = document.getElementById("staff").style.display = document.getElementById("sports").style.display = "none";
		
	this.className = "current";
	id = this.href.split("#");	
	document.getElementById(id[1]).style.display = "block";
	return false;
}

function addAnEvent(target, eventName, functionName){
	eval('target.on'+eventName+'=functionName');
}

function initPage(){
	if (document.getElementById){
		if (document.all) sfTarget();
		if (document.getElementById("shortcuts")) initNav(document.getElementById("shortcuts"));
		initNav(document.getElementById("nav"));
		if (document.getElementById("subnav")) initSubnav();
		initButtons();
		initLinks();
		if (document.getElementById("tabs")) initTabs();
	}
}

window.onload=initPage;