function addEvent(oTargetElem, sOnWhat, oFunction) {
	if (oTargetElem.attachEvent) {
		oTargetElem.attachEvent('on'+sOnWhat, oFunction);
	}
	else if (oTargetElem.addEventListener) {
		oTargetElem.addEventListener(sOnWhat, oFunction, false);
	}
	else {
		// Cannot attach event
	} // end-if
}

addEvent(window, 'load', initTutorAlivemenu);

// Initialise menu - only called when menu first loaded
function initTutorAlivemenu() {
	 
	var x = document.getElementsByTagName('div');
	for (var i = 0; i < x.length; i++) {
		if (x[i].className == 'navheader') {
			x[i].onmousedown = clickNav;
			x[i].onmouseover = function () {this.className="navheaderselected";};
			x[i].onmouseout  = function () {this.className="navheader";};
		}
	}

	closeNav();

	setNav(getPageId(), 'currentPage');
}

function closeNav() {
	//Return list of elements with the div tag name.
	var x = document.getElementsByTagName('div');
	
	for(var i = 0; i < x.length; i++)	{
		if (x[i].className == 'submenu') {
			if (x[i].id != 'AcademicSubMenu')
				x[i].style.display = 'none';
		}
	}
}

function clickNav(e) {
	closeNav();
	if (!e) var e = window.event;

	if (e.target)
		var tg = e.target;
	else if(e.srcElement)
		var tg = e.srcElement;
			
	var nextSib = tg.nextSibling;

	while (nextSib.nodeType != 1) nextSib = nextSib.nextSibling;
	
	var nextSibStatus = (nextSib.style.display == 'none') ? 'block' : 'none';
	
	nextSib.style.display = nextSibStatus;
}

function setNav(page, newID) {

	if (page != null && page != "")
	{
		var x = document.getElementsByTagName('a');
		var i;
		for (i = 0; i < x.length; i++) {
			if (x[i].id == getPageId())	{
				x[i].id = newID;
				x[i].className = 'navitemselected';
				break;
			}
		}

		if(i < x.length && newID == 'currentPage')
		{
			var parDiv = x[i];
			while (parDiv.parentNode.tagName == 'DIV') {
				parDiv = parDiv.parentNode;
				parDiv.style.display = 'block';
			}
		}    
	}
}

var pageId = "";
function getPageId() {
   return pageId;
}

function setPageId(page) {
   pageId = page;
}