// Variables defined for below functions 

// turntosingle must be set to 0 to make sure the correct sub menu is displayed when entering a new page.
var turntosingle=0 
// divBlank variable is used to se the homepage correctly. It ensures that the submenu doesn't collapse on the homepage, when no submenu is displayed.
var divBlank = document.getElementById('div0');
// myurl reads the url of the page
var myUrl = document.location.href;

// the variables below are searching for specific charactiers within the url.
var myAbout = myUrl.search("/aboutTheCollege");
var myAcad = myUrl.search("/academicPrograms");
var myTools = myUrl.search("/cfrTools");
var myCourses = myUrl.search("/courses");
var myCenters = myUrl.search("/centersPrograms");
var myOutreach = myUrl.search("/outreach");
var myPeople = myUrl.search("/people");
var myResearch = myUrl.search("/research");
var mySupport = myUrl.search("/support");

//When one of the above character sets is found in the URL, the following code sets the correct initial tab to be displayed. 
//If none are found (this only occurs on the homepage or on the spotlight page) then the 'divBlank' variable is displayed, so that the submenu space does not collapse.
if (myAbout > -1) {
	var initialtab=[1, "sub1"] 
}
if (myAcad > -1) {
	var initialtab=[3, "sub3"] 
}
if (myTools > -1) {
	var initialtab=[9, "sub9"] 
}
if (myCourses > -1) {
	var initialtab=[4, "sub4"] 
}
if (myOutreach > -1) {
	var initialtab=[6, "sub6"] 
}
if (myPeople > -1) {
	var initialtab=[2, "sub2"] 
}
if (myResearch > -1) {
	var initialtab=[5, "sub5"] 
}
if (mySupport > -1) {
	var initialtab=[8, "sub8"] 
}
if (myCenters > -1) {
	var initialtab=[7, "sub7"] 
}
else {
	divBlank.style.display = 'block';
}

var previoustab=""

// When a main nav link is hovered upon, it calls the expandcontent function below. (check the mainNavigation.html file within the includes folder to see how it is called.
function expandcontent(cid, aobject){
	//divBlank must be hidden before displaying other submenus
	divBlank.style.display = 'none';
	// if there is an object stored (which there is based on what mainNavigation.html says: "expandcontent('sub4', this)" where 'this' = the object (in this case, an <a> tag).
	if (document.getElementById){
		//calls the highlighttab function (the following function)
		highlighttab(aobject)
		if (turntosingle==0){
			if (previoustab!="")
				document.getElementById(previoustab).style.display="none"
				document.getElementById(cid).style.display="block"
				previoustab=cid
		}
	}
}
// this function sets the class of the correct main nav link to "current" so that it changes to show it is selected.
function highlighttab(aobject){
	if (typeof tabobjlinks=="undefined")
	collecttablinks()
	for (i=0; i<tabobjlinks.length; i++)
	tabobjlinks[i].className=""
	aobject.className="current"
}

// the following 2 functions and if statement load the tablist links (<a> tags) into an array so that they are numbered and can be referred to in their position within the arrray.
function collecttablinks(){
var tabobj=document.getElementById("tablist")
tabobjlinks=tabobj.getElementsByTagName("A")
}

function do_onload(){
collecttablinks()
expandcontent(initialtab[1], tabobjlinks[initialtab[0]-1])
highlightCurrentMenuItem()
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload


// the following 2 functions are supposed to highlight the correct page withing a submenu, but doesn't work properly. Probably only needs a little tweaking
function getLeaf(url) { 
	return url.substring(32); 
} 

function highlightCurrentMenuItem() { 
	var currentLocation = getLeaf(document.location.href);
	var menu = document.getElementById(initialtab[1]); 
	links = menu.getElementsByTagName("a"); 
	for (i=0; i<links.length; i++) { 
		var currentHref = links[i].getAttribute("href"); 
		var currentLeafName = currentHref; 
		if (currentLeafName==currentLocation) { 
			// Setting class is needed for Mozilla compatibility - className appears to be correct 
			// according to the DOM spec 
			links[i].setAttribute("class", "current"); 
			links[i].setAttribute("className", "current"); 
		} 
	} 
	var menuSide = document.getElementById("navigation_side"); 
	linksSide= menuSide.getElementsByTagName("a"); 
	for (i=0; i<linksSide.length; i++) { 
		var currentHrefSide = linksSide[i].getAttribute("href"); 
		var currentLeafNameSide = currentHrefSide; 
		if (currentLeafNameSide==currentLocation) { 
			// Setting class is needed for Mozilla compatibility - className appears to be correct 
			// according to the DOM spec 
			linksSide[i].setAttribute("class", "current"); 
			linksSide[i].setAttribute("className", "current"); 
		} 
	} 
}
