var oldMenuOnload = window.onload;
var forceHovers = (document.all && document.getElementById);
var selects;
var tOut;
var lastMenu = "";
var lastFunction = "";

if(!document.getElementById && document.all) {
	try {
		document.prototype.getElementById = document.all;
	} catch(e) {
		document.getElementById = document.all;
	}
}


__startMenus = function () {
    for (j = 0; j < __InigoMenus.length; j++) {
        // Only setup the events for menus that are not second level menus
        //if(__InigoMenus[j].indexOf("inigosecondlevelmenu") == -1) {
        try {
            AddEvents($get(__InigoMenus[j]).getElementsByTagName("LI"), (__InigoMenus[j].indexOf("inigosecondlevelmenu") != 0), __InigoMenus[j]);
        } catch (err) {
        }
        var edlink = $get("menuEdLink" + __InigoMenus[j].split("inigomenu").join(""));
        if (edlink != null) {
            var firstLI = $get(__InigoMenus[j]).getElementsByTagName("LI")[0];
            firstLI.style.position = "relative";
            firstLI.appendChild(edlink);
            edlink.style.top = "0px";
            edlink.style.left = "0px";
            edlink.style.padding = "0px 3px";
            edlink.style.fontSize = "7pt";
            edlink.style.color = "#006";
            edlink.style.textDecoration = "none";
            edlink.style.lineHeight = "15px";
        }
        //}
    }
    selects = document.getElementsByTagName("select");
    if (typeof (oldMenuOnload) == "function")
        oldMenuOnload();
}

function AddEvents(lis, addDynamicEffects, menuname) {
	for (var i=0; i<lis.length; i++) {
		if(addDynamicEffects) {
			lis[i].onmouseover=function() { mouseOver(this, menuname); } 
			lis[i].onmouseout=function() { if(document.all) { window.clearTimeout(tOut); lastMenu = menuname; lastFunction="mouseOut('" + this.id + "')"; tOut = window.setTimeout("mouseOut('" + this.id + "')",1000); } else { mouseOut(this); } }
			//lis[i].setAttribute("menuname", menuname);
		}
		var a = lis[i].getElementsByTagName("a")[0];	
		a.onclick = function() { setMenuCookie(this); }			
	}
}

// Only load the startMenus function if a getElementById function is now defined
if(document.getElementById)
	window.onload = __startMenus;

function mouseOver(element, menuname) {
	if(forceHovers) element.className = " menuhover";
	
	// <-- Ensure only this menus items are displayed so rolloff all others....
	mOutEl = ""; 
	// Cancel timeout if it was from the same menu
	if(lastMenu == "" || lastMenu == menuname) { 
		window.clearTimeout(tOut); 
	} else {
		// run function and then cancel the timeout
		if(lastFunction != "") eval(lastFunction);
		window.clearTimeout(tOut); 
	}
	
	rollOffAll(element.parentNode.childNodes, element);
	
	// Hide any flash or select boxes which might display through the menu
	hideElements(element);
		
	// Ensure parent items are highlighted
	var par = element.parentNode.parentNode;
	if(par.tagName == "LI") {
		var a = par.getElementsByTagName("a")[0];
		a.className = " menuhover";
		
		// if mouseover function defined then call it - allows for image rollovers
		if(typeof(a.onmouseover) == "function")	a.onmouseover();
	}
}



function rollOffAll(els, element) {
	if(els != null) {
		for (var i=0; i<els.length; i++) {
			var el = els[i];
			if(el.nodeName.toUpperCase() == "LI" && el.className != "" && el.id != element.id)
				mouseOut(el);
			var lis = els.childNodes;
			rollOffAll(lis, element);
		}
	}
}


function mouseOut(element) {
	//return;
	if(typeof(element) != "object") {		
		element = document.getElementById(element);
	}
	
	if(element != "" && element != null) {
		if(forceHovers) element.className = "";
		
		showElements(element);
		
		// Remove all highlighted rollovers
		var a = element.getElementsByTagName("a")[0];			
		if(a.getAttribute("selected") == null || a.getAttribute("selected") != "true") 	a.className = "";
			
		// if mouseout function defined then call it - allows for image rollovers
		if(typeof(a.onmouseout) == "function") a.onmouseout();
		
		var uls = element.getElementsByTagName("UL");
		var nodes = (uls != null && uls.length > 0) ? uls[0].childNodes : new Array();
			
		for(var i=0;i<nodes.length; i++) {
			if(nodes[i].nodeName.toUpperCase() == "LI") {
				mouseOut(nodes[i]);
			}
		}
	}
}

function toggleChildren(a) {
	a.blur();
	var li = a.parentNode;
	var uls = li.getElementsByTagName("UL");
	
	var img = a.childNodes[0];
	
	if(uls.length > 0) {
		var ul = uls[0];
		if(ul.style.display == "none") {
			ul.style.display = "block";
			img.src = img.src.split("plusik").join("minus");	
			//ul.style.marginTop = ul.style.marginBottom = "0px";		
			//ul.style.backgroundImage = "url(" + siteRoot + "/images/treeicons/icons/vertical.gif)";
			//ul.style.backgroundRepeat = "repeat-y";
			//ul.style.backgroundPositionX = "1px";
		} else {
			ul.style.display = "none";
			img.src = img.src.split("minus").join("plusik");	
		}
		return false;
	}
	return true;
}

function hideFlash(el) {
	var params = el.getElementsByTagName("param");
	for(var i=0; i<params.length; i++) {
	    if (params[i].getAttribute("name") != null &&
	        params[i].getAttribute("name").toLowerCase() == "wmode" &&
	        (params[i].getAttribute("value").toLowerCase() == "opaque" || params[i].getAttribute("value").toLowerCase() == "transparent"))
	        return false;
	}
	return true;
}

/* Positioning scripts to allow for flash, and selects to be hidden which may display in front of the menus */
function hideElements(obj) {
	try {
		var uls = obj.getElementsByTagName("UL");
		obj.hiddenFields = new Array();
		
		// Only attempt to hide if a submenu ul list exists
		if(uls.length > 0) {
			var ul = uls[0];
			var flash = document.getElementsByTagName("object");

			for(var i=0;i<flash.length; i++) {
				if(__menuIsInside(ul, flash[i]) && hideFlash(flash[i])) {
					obj.hiddenFields[obj.hiddenFields.length] = flash[i];
					flash[i].style.visibility = "hidden";
				}
			}
			
			var selects = document.getElementsByTagName("select");
			for(var i=0;i<selects.length; i++) {
				if(__menuIsInside(ul, selects[i])) {
					obj.hiddenFields[obj.hiddenFields.length] = selects[i];
					selects[i].style.visibility = "hidden";
				}
			}
		}
	} catch(e) {
		alert(e.description);
	}
}

function showElements(obj) {
	if(typeof(obj.hiddenFields) != "undefined") {
		for(var i=0; i<obj.hiddenFields.length; i++) {
			obj.hiddenFields[i].style.visibility = "visible";
		}
	}
}

function __menuIsInside(menuObj, el) {
	var l = __menuGetAbsX(el);
	var r = l + el.offsetWidth;
	var t = __menuGetAbsY(el);
	var b = t + el.offsetHeight;
	
	var elLeft = __menuGetAbsX(menuObj);
	var elRight = elLeft + menuObj.offsetWidth;
	var elTop = __menuGetAbsY(menuObj);
	var elBottom = elTop + menuObj.offsetHeight;
	//alert("MENU ITEM " + elLeft + "," + elRight + "|" + elTop + "," + elBottom + "\nFLASH IS AT " +
	//		l + "," + r + "|" + t + "," + b);
	if(r < elLeft) return false;
	if(l > elRight) return false;
	if(b < elTop) return false;
	if(t > elBottom) return false;
	return true;
}


function __menuGetAbsX(obj) {
	return (obj.x) ? obj.x : __menuGetAbsPos(obj,"Left");
}

function __menuGetAbsY(obj) {
	return (obj.y) ? obj.y : __menuGetAbsPos(obj,"Top");
}

function __menuGetAbsPos(obj,which) {
	iPos=0;
	while (obj!=null && obj.tagName != "BODY") { //"BODY"
		iPos+=obj["offset"+which];
		obj=obj.offsetParent;
	}
	return iPos;//iPos;
}

// ===============================================================
// Sets a cookie indicating which was the last menu item clicked
// ===============================================================
function setMenuCookie (a) {

  var value = a.parentNode.id;
  var argv = setMenuCookie.arguments;
  var argc = setMenuCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = "/";
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  
  document.cookie = "menuid=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
    
  // Check if cookies disabled, if so set the querystring on the link to parse in the menuid
  if(document.cookie == "") {
	a.href = a.href + "?menuid=" + value;
  } 
}
