function getXY(obj, firstLevel){
	var left = 0;
	var top  = 0;
	
	left += obj.offsetWidth;
	
	version = navigator.appVersion;
		
	if (version.search(/safari/i) != -1 && firstLevel == true)
	{
		left = 0;
		//alert(navigator.appVersion);
		top = -16;
		obj = obj.offsetParent;
	}

	while (obj.offsetParent){
		
		//alert(obj);
		//alert(left);
		
		left  += obj.offsetLeft;
		top  += obj.offsetTop;
		obj  = obj.offsetParent;
		
	}

	
	left += obj.offsetLeft;
	top  += obj.offsetTop;


	return [left, top];
}


ParentDIV = "";
flyoutArr = new Array();
fi = 0;
openLink = false;
currLevel = "";
prevLevel = "";
theLink = "";
rollover = false;
currDiv = "";
prevDiv = "";
t = null;
firstCheck = false;


// This function performs the menu operations.
// firstLevel should be set to tru by the calling link if it is the top level object and if a special style is required for the first dropdown object
function doMenu(td, type, level, theParent, firstLevel){
	
	// If the item is a first level notional page (specified in the template), run hideAllMenus()
	
	if (theParent) {
		ParentDIV = theParent;
	}
	

//	document.getElementById('debug').innerHTML += "Type: "+type+" DIVTOCLOSE: "+divToClose+" CURR: "+td+" <strong>sbo:</strong> "+destination[fi]+" => "+printR(destination)+" ipos: " +fi+" <strong>openby:</strong> "+origin[fi]+" => "+printR(origin)+" <BR><BR>";
	divPos = 0;
	prevLevel = currLevel;
	currLevel = level;
	prevDiv = currDiv;
	currDiv = td;
	rollover = true;

	if (type == "Link"){
		if (td) openLink = true;
		else openLink = false;
	}


	if (td){
		if (fi == 0){
			flyoutArr[fi] = td;
			fi++;
		} else {
			//now check if this array exists
			found = false;
			for (i = 0; i < fi; i++){
				if ((flyoutArr[i] == td)){
					found = true;
					
					// get the position of the current div we're on
					if (type != "Link") divPos = i;
					i = fi;
				}
			}
			
			if (!found){
				flyoutArr[fi] = td;
				fi++;
			}
		}

		for (i = 0; i < fi; i++){
			doCleanUp = false;
			// only if we're opening a link; not if we're rolling back onto a div
			if (type == "Link"){
				// Find the positioner spacer
				if (document.getElementById(flyoutArr[i])!=null) {
					if (i == 0 || firstLevel == true) 
					{
						pos = getXY(document.getElementById(flyoutArr[i]+"Img"), true);
						document.getElementById(flyoutArr[i]+"Div").style.top = pos[1] + 27 + "px";
						document.getElementById(flyoutArr[i]+"Div").style.left =  pos[0] + "px";
					}
						else 
					{
						pos = getXY(document.getElementById(flyoutArr[i]+"Img"), false);
						document.getElementById(flyoutArr[i]+"Div").style.top = pos[1] + "px";
						document.getElementById(flyoutArr[i]+"Div").style.left =  pos[0] + 6 + "px";
					}
					
					document.getElementById(flyoutArr[i]+"Div").style.display="";
				}

				theLink = td;
			} else {// it's a div type
				// if we're opening a flyout menu, don't do this bit; this bit to execute only if we roll back onto a parent div
				if (!openLink){ 
					// type check
					action = "none";
					if (i <= divPos) action = "";
					if (document.getElementById(flyoutArr[i]+"Div")!=null) document.getElementById(flyoutArr[i]+"Div").style.display=action;
					if (action == "none") {
						flyoutArr[i] = "";
//						HideDiv();
					}
				} else {
					doCleanUp = true;
					cleanUpLinks(td, theLink);
					theLink = "";
				}
			}
		}
	}
}

function cleanUpLinks(td, theLink){
	// function to clean up the array if there are any currently opened divs but didn't close due to another div being opened
	// immediately after the current duv menu opened
	var l = flyoutArr.length;
	// get the position of the td value
	var tdP = 0;
	var tlP = 0;
	if (td && theLink){
		for (i = 0; i < l; i++){
			if (td.toString() == flyoutArr[i]){
				tdP = i;
				i = l;
			}
		}
		
		// get the position of the theLink value
		for (i = 0; i < l; i++){
			if (theLink.toString() == flyoutArr[i]){
				tlP = i;
				i = l;
			}
		}
		
		// now remove any links in between the 2 starting points
		if (tdP < tlP){

			tdP++; // we want a non-inclusive select
			for (i = tdP; i < tlP; i++){
				if (document.getElementById(flyoutArr[i]+"Div")!=null) document.getElementById(flyoutArr[i]+"Div").style.display="none";
				flyoutArr[i] = "";
			}
			// 19/06/08 added because the parent links aren't notional pages; they're hardcoded pages eg About Us link, Courses etc...
			//HideDiv();
			openLink = false;
		}
	}
}

function printR(arr){
	str = "{";
	for (j = 0; j < arr.length; j++) str += arr[j] + ",";
	str += "}";
	return str;
}

function clearMenu(td, type, level){
	rollover = false;
	t = setTimeout('checkrollover()', 10);
}

function checkrollover(){
	if ((rollover == false) && (!firstCheck)) {
		// check again to make sure we really have rolled off all the submenus/divs
		firstCheck = true;
		if (t) clearTimeout(t);
		t = setTimeout('checkrollover()', 10);
		
	} else if ((rollover == false) && (firstCheck)) {
		// if we really have rolled off all the submenus/divs, close everything in the array and reset it
		for (i = 0; i < fi; i++){
			if (document.getElementById(flyoutArr[i]+"Div")!=null) document.getElementById(flyoutArr[i]+"Div").style.display = "none";
		}
		//document.getElementById(theParent).visibility = "hidden";
		flyoutArr = new Array();
		fi = 0;
		firstCheck = false;
	//	HideDiv();

	} else if (rollover == true){
		// check if we're rolling from a parent div to another parent div
		// or if we're rolling from a child window to a parent div

		if ((currLevel == 0 && prevLevel == 0) || (currLevel == 0 &&  typeof(prevLevel)=="undefined") ||  (typeof(prevLevel)=="undefined" &&  typeof(currLevel)=="undefined")){
			for (i = 0; i < fi; i++){
				if (document.getElementById(flyoutArr[i]+"Div")!=null) document.getElementById(flyoutArr[i]+"Div").style.display = "none"
			}
			//HideDiv();

				//	document.getElementById(theParent).visibility = "hidden";
			flyoutArr = new Array();
			if (currDiv) {
				flyoutArr[0] = currDiv;
				fi = 1;
				document.getElementById(currDiv+"Div").style.display = "";
			}
			
		} 
		if (t) clearTimeout(t);
		firstCheck = false;
	}
}
childArr = new Array();
parentArr = new Array();
nameArr = new Array();
i=0;

function revealSubmenu(menuID){
/*	// check it's in the array
	var found = false;
	for (l = 0; l < i; l++){
		// hide the current menus
		if (document.getElementById(childArr[l])) document.getElementById(childArr[l]).style.display = "none";		
		// color the main menus differently
		if (document.getElementById(parentArr[l])) document.getElementById(parentArr[l]).style.backgroundColor  = "#860020";
	}

	
	// display current menu
	document.getElementById(parentID).style.backgroundColor  = "#a44058";
	document.getElementById(menuID).style.display = "block";*/
	if (document.getElementById(menuID).style.display == "none"){
		document.getElementById(menuID).style.display = "";
	} else {
		document.getElementById(menuID).style.display = "none";
	}
	
}


function hideAllMenus()
{
	alert("Hiding All");
	// Hides all menus currently registered in the flyout menu array
	var l = flyoutArr.length;

	for (i = 0; i < l; i++)
	{
		alert("Hiding: " + flyoutArr[i]);
		document.getElementById(flyoutArr[i]+"Div").style.display = "none";
	}
	flyoutArr = new Array();
}
