
var menuArray;
var closeTimer;
var MenuList = new Array();
var firstTime = false;
var doesExist;
var cached_menu_contents = [];
var obj_offsets = [];

var lookupObjects = true;

//var shouldReposPopup = true;

function MenuItemClick(nthis) {
    if (nthis.neverTest != null) {
        return;
    }

    if (nthis.menuObj == null) {
        var found = false;
        for (i = 0; i<nthis.childNodes.length; i++) {
		    if (nthis.childNodes[i].tagName == "DIV") {
		        if (nthis.childNodes[i].islink == "0") {
		            nthis.neverTest = true;
		            nthis.style.cursor = "";
		            return;
		        }
		    
			    //nthis.childNodes[i].click();
			    var srcElement = nthis.childNodes[i];
                var menuInt = nthis.childNodes[i].menuid * 1;
                found = true;
                nthis.menuObj = srcElement;
			    break;
		    }
	    }
        
        if (found == false) {
            //alert("matching menu not found");
            return;
        }
    } else {
        var srcElement = nthis.menuObj;
        var menuInt = srcElement.menuid * 1;
    }

    nthis = srcElement;

	for (i = 0; i<nthis.childNodes.length; i++) {
		if (nthis.childNodes[i].tagName == "A") {
			nthis.childNodes[i].click();
			break;
		}
	}
}

function LoadArrows() {
	if (firstTime) {
		return;
	}
	var curMenu;
	
	for (var i = 0; i<MenuList.length; i++) {
		mname = MenuList[i];
		eval("curMenu = " + mname + ";");
		if (curMenu.length >= 2) {
			//eval(mname + "_div.style.background = '#F1F1F1 url(/apldn-online/tri.gif) no-repeat middle right'");
			eval(mname + "_div.style.background = 'url(/data/i/tri.gif) no-repeat middle right'");
		}
	}
	firstTime = true;
}

function ShowMenu(nthis) {
    if (nthis.neverTest != null) {
        return;
    }

	if (lookupObjects) {
		if (menuPopup == null) {
			alert("menuPopup must be present in template.");
			return;
		}
		lookupObjects = false;
	}

    if (nthis.menuObj == null) {
        var found = false;
        for (i = 0; i<nthis.childNodes.length; i++) {
		    if (nthis.childNodes[i].tagName == "DIV") {
		        if (nthis.childNodes[i].islink == "0") {
		            nthis.neverTest = true;
		            nthis.style.cursor = "";
		            return;
		        }
		    
			    //nthis.childNodes[i].click();
			    var srcElement = nthis.childNodes[i];
                var menuInt = nthis.childNodes[i].menuid * 1;
                found = true;
                nthis.menuObj = srcElement;
			    break;
		    }
	    }
        
        if (found == false) {
            //alert("matching menu not found");
            return;
        }
    } else {
        var srcElement = nthis.menuObj;
        var menuInt = srcElement.menuid * 1;
    }

	LoadArrows();
	clearTimeout(closeTimer);
	menuPopup.style.visibility = "Hidden";
	
	srcElement.style.borderColor = "#999999";
	//srcElement.style.borderColor = "#FF0000";
	srcElement.style.backgroundColor = "#CCCCCC";
	//srcElement.style.backgroundColor = "#FF0000";

	var objtop;
//	status = srcElement.uniqueID;
	if (obj_offsets[srcElement.uniqueID]) {
		objtop = obj_offsets[srcElement];
		//status = "top from cache: " + objtop;
	} else {
		//var objtop = srcElement.offsetTop;
		objtop = 0;
		var curelement = srcElement;
		while (curelement != null) {
			objtop += curelement.offsetTop;
			curelement = curelement.offsetParent
		}
		//off set of 5 pixels to nudge the popup above the menu item.
		objtop -= 5;

		obj_offsets[srcElement] = objtop;
	}

	var objleft = srcElement.offsetLeft;
	var objwidth = srcElement.offsetWidth;

	menuPopup.style.posTop = objtop;
	menuPopup.style.posLeft = (objleft + objwidth);

	menuPopup.innerHTML = "";

	
	eval("doesExist = typeof(Menu" + menuInt + ");");
	if ("undefined" != doesExist) {
		eval("menuArray = Menu" + menuInt + ";");
	} else {
		return;
	}

	if (menuArray.length < 2) {
		return;
	}

	if (cached_menu_contents["Menu" + menuInt]) {
		menuPopup.innerHTML = cached_menu_contents["Menu" + menuInt];
	} else {
		var i = 0;

		var allhtml = "";
		var nhtml; // = "<table cellpadding='0' cellspacing='0' border='0'>";
		//menuPopup.innerHTML += nhtml;
		while (i < menuArray.length) {
			menuTitle = menuArray[i];
			menuLink = menuArray[i+1];
			menuEscape = "SubMenu_" + menuInt + "_" + i;
	
			nhtml = "";
			nhtml += "<div onmouseenter='TDMouseOver(this)' onmouseleave='TDMouseOut(this)' onclick='" + menuEscape + ".click();' style='CURSOR: hand; border:1px solid #F1F1F1;' class='NavRow'>"; //class='SectionNavigation'
			nhtml += "<a id='" + menuEscape + "' href='" + menuLink + "'>" + menuTitle + "</a>";
			nhtml += "</div>";
			allhtml += nhtml;
			i += 2;
		}
		menuPopup.innerHTML = allhtml;

		cached_menu_contents["Menu" + menuInt] = allhtml;
	}

	
	menuPopup.style.visibility = "Visible";
	//menuPopup.style.display = "";
	//menuPopup.style.zindex = 1000;
	//menuPopup.style.border = "2px solid red"

}

function MenuMouseOver(nthis) {

    if (nthis.neverTest != null) {
        return;
    }
    
    if (nthis.menuObj == null) {
        var found = false;
        for (i = 0; i<nthis.childNodes.length; i++) {
		    if (nthis.childNodes[i].tagName == "DIV") {
		        if (nthis.childNodes[i].islink == "0") {
		            nthis.neverTest = true;
		            nthis.style.cursor = "";
		            return;
		        }
		    
			    //nthis.childNodes[i].click();
			    var srcElement = nthis.childNodes[i];
                var menuInt = nthis.childNodes[i].menuid * 1;
                found = true;
                nthis.menuObj = srcElement;
			    break;
		    }
	    }
        
        if (found == false) {
            //alert("matching menu not found");
            return;
        }
    } else {
        var srcElement = nthis.menuObj;
        var menuInt = srcElement.menuid * 1;
    }

	//event.srcElement.style.border = "thin outset";
	//event.srcElement.style.background = "#CFC8CF";

	//srcElement.style.borderColor = "#999999";
	//srcElement.style.backgroundColor = "#CCCCCC";
	
	//var objtop = srcElement.offsetTop + 90;

	var objtop;
	if (obj_offsets[srcElement.id]) {
		objtop = obj_offsets[srcElement.id];
	} else {
		//var objtop = srcElement.offsetTop;
		objtop = 0;
		var curelement = srcElement;
		while (curelement != null) {
			objtop += curelement.offsetTop;
			curelement = curelement.offsetParent
		}
		//off set of 5 pixels to nudge the popup a little above the menu item.
		objtop -= 5;

		obj_offsets[srcElement.id] = objtop;
	}

	var objleft = srcElement.offsetLeft;
	var objwidth = srcElement.offsetWidth;
	
	menuPopup.style.posTop = objtop;

	//minus 5 pixels to nudge the popup to just over the right edge of the menu.
	menuPopup.style.posLeft = (objleft + objwidth - 5);

	menuPopup.style.visibility = "Visible";
}

function MenuMouseOut(nthis) {
	//event.srcElement.style.border = "";
	//event.srcElement.style.background = "#EFEFEF";
	
	//srcElement.style.borderColor = "#F1F1F1";
	//srcElement.style.backgroundColor = "#F1F1F1";


    if (nthis.menuObj == null) {
        var found = false;
        for (i = 0; i<nthis.childNodes.length; i++) {
		    if (nthis.childNodes[i].tagName == "DIV") {
			    //nthis.childNodes[i].click();
			    var srcElement = nthis.childNodes[i];
                var menuInt = nthis.childNodes[i].menuid * 1;
                found = true;
                nthis.menuObj = srcElement;
			    break;
		    }
	    }
        
        if (found == false) {
            //alert("matching menu not found");
            return;
        }
    } else {
        var srcElement = nthis.menuObj;
        var menuInt = srcElement.menuid * 1;
    }
    

	srcElement.style.borderColor = "#F1F1F1";
	srcElement.style.backgroundColor = "";
	
	closeTimer = setTimeout("MenuClose()", 1000);
}

function MenuPopupOver() {
	clearTimeout(closeTimer);
}

function MenuPopupOut() {
	closeTimer = setTimeout("MenuClose()", 1000);
}

function MenuClose() {
	menuPopup.style.posTop = -100;
	menuPopup.style.posLeft = -100;
	menuPopup.style.visibility = "Hidden";
}

function TDMouseOver(srcElement) {
	srcElement.style.borderColor = "#999999";
	srcElement.style.backgroundColor = "#CCCCCC";

	//event.srcElement.style.border = "thin outset";
	//event.srcElement.style.background = "#CFC8CF";

	//srcElement.style.borderColor = "#999999";
	//srcElement.style.backgroundColor = "#CCCCCC";
}

function TDMouseOut(srcElement) {
	srcElement.style.borderColor = "#F1F1F1";
	srcElement.style.backgroundColor = "";


	//event.srcElement.style.border = "";
	//event.srcElement.style.background = "#EFEFEF";

	//srcElement.style.borderColor = "#F1F1F1";
	//srcElement.style.backgroundColor = "#F1F1F1";
}
