var debug = true;

//function at_mouseup() {
	//var p = document.getElementById(this["at_parent"]);
	//p.className=p["class_out"];
//	
	//if (p["location"] != '') document.location=p["location"];
//}

//#############################################################################################################################################
//#############################################################################################################################################
//#############################################################################################################################################

Array.prototype.has = function(value) {
	var i;
	for (var i = 0, loopCnt = this.length; i < loopCnt; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function mr_mouseover(e) {
	var menuVal;
	var thisMenuVal;
	
	if (!e) var e = window.event;
	var relTarg = e.relatedTarget || e.fromElement;	
	
	if (debug) {
		var debug = document.getElementById('debug');
		debug.innerHTML = debug.innerHTML + '[over: ' + relTarg.id + ' -> ' + this.id + '] ';
	}

	//clear any timeout for this item to prevent it from hiding
	//clearTimeout(this["timeout"]);

	//set class for this item
	this.className = this["class_over"];

	//set class name for parent
	//if (this["parentObj"] != null) this["parentObj"].className = this["parentObj"]["class_over"];

	//hide all visible menus that are not ancestors of this menu item's container menu
	if ((this["parentObj"] == null) || ((this["parentObj"] != null) && (this["parentObj"].submenu != undefined))) {
		if (this["parentObj"] == null) {
			thisMenuVal = "";
		}
		else {
			thisMenuVal = Right(this["parentObj"].submenu, this["parentObj"].submenu.length - 5);
		}
		
		i = 0;
		while (i < visibleSubmenus.length) {
			menuVal = Right(visibleSubmenus[i].id, visibleSubmenus[i].id.length - 5);
			if ((Left(thisMenuVal, menuVal.length) != menuVal) && (visibleSubmenus[i].id != this["submenu"])) { //neither my container menu nor my container menu's ancestor nor my own submenu
				if (debug) debug.innerHTML = debug.innerHTML + ' #HIDE MENU ' + menuVal + ' (' + thisMenuVal + ', ' + menuVal.length + ', ' + Left(thisMenuVal, menuVal.length) + ')#';
				visibleSubmenus[i].style.visibility = 'hidden';
				visibleSubmenus.splice(i, 1);
				numSubmenusVisible--;
			}
			else {
				i++;
			}
		}
	}

	//if submenu exists, ensure it is visible
	if (this["submenu"] != undefined) {
		var submenu = document.getElementById(this["submenu"]);
		//show it if not already visible
		if (!visibleSubmenus.has(submenu)) {
		
			//alert(this.offsetHeight);
			//alert(this.style.lineHeight);
		
			//var top = (this["position"] == "y") ? this.offsetHeight: 0; //MR not sure why we have to subtract 3 here...
			var top = (this["position"] == "y") ? 19: 0;
			var left = (this["position"] == "x") ? this.offsetWidth + 2 : 0;

			var o = this;
			for (; o; o = o.offsetParent)
			{
				top  += o.offsetTop;
				left += o.offsetLeft;
			}

			submenu.style.position = "absolute";
			submenu.style.top = top +'px';
			submenu.style.left = left +'px';
			submenu.style.visibility = "visible";
			
			//add to list of visible submenus
			visibleSubmenus[numSubmenusVisible] = submenu;
			numSubmenusVisible++;
			
			if (debug) debug.innerHTML = debug.innerHTML + ' (added submenu ' + submenu.id + ' - ' + numSubmenusVisible + ' total) ';
		}
	}
	
	//cancelBubbling();
}

function hideAllVisibleSubmenus() {
	var i;
	
	if (debug) {
		var debug = document.getElementById('debug');
		debug.innerHTML = debug.innerHTML + '#HIDING_ALL_VISIBLE_SUBMENUS# ';
	}
	
	for (i = 0; i < numSubmenusVisible; i++) {
		visibleSubmenus[i].style.visibility = 'hidden';
	}
	visibleSubmenus.length = 0;
	numSubmenusVisible = 0;
}

function mr_mouseout(e) {
	if (!e) var e = window.event;
	var relTarg = e.relatedTarget || e.toElement;

	if (debug) {
		var debug = document.getElementById('debug');
		debug.innerHTML = debug.innerHTML + '[out: ' + this.id + ' -> ' + relTarg.id + '] ';
	}

	//set class for this item
	this.className = this["class_out"];

	if ((relTarg == undefined) || (Left(relTarg.id, 5) != 'menu_')) {
		//hide all visible submenus
		hideAllVisibleSubmenus();
	}

	//cancelBubbling();
}

function menu_mouseover(e) {
	
	//if (!e) var e = window.event;
	//var relTarg = e.relatedTarget || e.fromElement;	
	
	if (debug) {
		var debug = document.getElementById('debug');
		debug.innerHTML = debug.innerHTML + '[m over: ' + relTarg.id + ' -> ' + this.id + '] ';
	}
	
	//cancelBubbling();
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function menu_mouseout(e) {
	if (!e) var e = window.event;
	var relTarg = e.relatedTarget || e.toElement;

	if (debug) {
		var debug = document.getElementById('debug');
		debug.innerHTML = debug.innerHTML + '[m out: ' + this.id + ' -> ' + relTarg.id + '] ';
	}
	
	if ((relTarg == undefined) || (Left(relTarg.id, 5) != 'menu_')) {
		//hide all visible submenus
		hideAllVisibleSubmenus();
	}
	
	cancelBubbling();
}

function cancelBubbling() {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function mr_mouseup(e) {
	document.location = this["location"];
}

//var menuArray = new Array();
var visibleSubmenus = new Array();
var numSubmenusVisible = 0;

function ProcessMenuItem(parentObj, m) {
	var id = m[0];
	var location = m[1];
	var j;
	
	var thisObj = document.getElementById(id);
	if (thisObj == undefined) alert(id + ' missing');
	
	thisObj["parentObj"] = parentObj;
	thisObj["id"] = id;
	thisObj["location"] = location;
	thisObj["class_over"] = "menu_button_over";
	thisObj["class_out"] = "menu_button";
	
	thisObj.onmouseover = mr_mouseover;
	thisObj.onmouseout = mr_mouseout;
	thisObj.onmouseup = mr_mouseup;
	
	if (m.length > 2) {
		var position = m[2];
		var cursor = m[3];
		var submenu = m[4];
		thisObj["position"] = position;
		thisObj["cursor"] = cursor;
		thisObj["submenu"] = submenu;
		
		var o = document.getElementById(submenu);
		o.onmouseover = menu_mouseover;
		o.onmouseout = menu_mouseout;
		
		//recursively process the submenu
		for (j = 5; j < m.length; j++) {
			ProcessMenuItem(thisObj, m[j]);
		}
	}
}

function ProcessMenu(m) {
	var i;
	for (i = 0; i < m.length; i++) {
		ProcessMenuItem(null, m[i]);
	}
}

