<!-- ----------------------------------------------------------------------------------
//
//  Author:	Dan Carlson.
//
//  Description:	Controls the display of popup menus.
//
//--------------------------------------------------------------------------------- -->

//-------------------------------------------------------------------------------------
// Attach events.
//-------------------------------------------------------------------------------------
window.onload = InitPage;

//-------------------------------------------------------------------------------------
// Module level variables.
//-------------------------------------------------------------------------------------
var moParentWindow;
var msMenuName = "";
var mbIsSubMenu = false;

var miHideDelay = 0;
var miHideTimerID = 0;

//-------------------------------------------------------------------------------------
// Loads table with array of menu items.
//-------------------------------------------------------------------------------------
function CreateMenuItems(aItems) {

	var td = tblMenuItems;
	var oRow;
	var oCell;
	var sItemText;
	var bDisabled;
	var bSubmenu;
	var oCellSubMenuIndicator;
	
	if (aItems == null) return;
	
	//Clear existing.
	td.innerText = "";

	//Load table with menu items.
	for(var i=0;i<aItems.length;i++) {
		if(aItems[i] == "-") {
			//Use bottom border as a menu separator.
			td.rows(td.rows.length-1).cells(0).className = "Separator";
            td.rows(td.rows.length-1).cells(1).className = "Separator";
    	}
		else {
			sItemText = aItems[i];
			bDisabled = false;
			bSubmenu  = false;
			
			//Is menu item disabled?
			if (sItemText.substr(0, 10) == "[disabled]") {
				bDisabled = true;
				sItemText = aItems[i].substr(10);
			}
			
			//Does menu item have a sub-menu associated with it?
			if (sItemText.substr(0, 9) == "[submenu]") {
				bSubmenu = true;
				sItemText = aItems[i].substr(9);
			}
					
			oRow = td.insertRow();
			oRow.style.height = 18;

			oRow.attachEvent('onmouseover', SetCellHilite);
			oRow.attachEvent('onmouseout',  SetCellHilite);
				 		
			oCell = oRow.insertCell();
			oCell.setAttribute("SubMenu", "");
			
			oCellSubMenuIndicator = oRow.insertCell();
			oCellSubMenuIndicator.innerText = " ";
			oCellSubMenuIndicator.setAttribute("SubMenu", "");
			
			with (oCell) {
			    if ((i > 0) && (td.rows(td.rows.length-2).cells(0).className == "Separator")) {
			        style.paddingTop = 6;
                }			        
			
			    setAttribute("immobilize", false);
			
				if (bDisabled) {
					setAttribute("immobilize", true);
					style.color = "gray";
				}
				else if (bSubmenu) {
					setAttribute("SubMenu", sItemText);
					oCellSubMenuIndicator.innerHTML = "&raquo";
    				oCellSubMenuIndicator.setAttribute("SubMenu", sItemText);
				}
				else {
					attachEvent('onclick', DoMenuItem);
					
					//Allow submenu indicator (space) to respond.
    				oCellSubMenuIndicator.id = aItems[i];
					oCellSubMenuIndicator.attachEvent('onclick', DoMenuItem);
				}

     			//innerText = " " + sItemText + "   ";
     			innerText = sItemText;
				id = aItems[i];
				noWrap = true;


			}	
		}
	}	
	
	td.refresh();		
} 

//-------------------------------------------------------------------------------------
//Calls function in content frame in response to user clicking on an menu item.
//-------------------------------------------------------------------------------------
function DoMenuItem() {
	
	var oPopupMenu = window.frameElement;

	if (event.srcElement.immobilize) return;

	//Save menu name before Hide() function clears it.
	var sMenuName = msMenuName;
	
	//If a delayed hide action is in effect, cancel it.
	if (miHideTimerID > 0) {
		clearTimeout(miHideTimerID);
		miHideTimerID = 0;
		miHideDelay = 0;
	}

	//Hide popup menu if not sticky.
	if (!oPopupMenu.getAttribute("Sticky")) Hide();

    //Hide parent of submenu.
	if (mbIsSubMenu) {
	    try {
	        top.document.all.PopupMenu.style.visibility = "hidden";
	    }
	    catch (e) {
	    }    
    }			    
	
	//Let the parent window respond to menu item.
	moParentWindow.DoMenuItem(sMenuName, event.srcElement.id);
}

//-------------------------------------------------------------------------------------
// Determines the menu item that is currently selected. Returns	index of item that is
// hilited, or -1 if nothing is hilited.
//-------------------------------------------------------------------------------------
function GetCurrentSelection() {
 
	var iCurrentItem = -1;
	
	for (i=0; i<tblMenuItems.rows.length; i++) {
		if(tblMenuItems.rows(i).cells(0).style.backgroundColor == "steelblue") {
			iCurrentItem = i;
			break;
		}	
	}
	
	return iCurrentItem;
}

//-------------------------------------------------------------------------------------
// Hide the popup menu (inline frame). 
//-------------------------------------------------------------------------------------
function Hide() {
	
	//If this is not a submenu, and a submenu is currently displayed, don't hide 
	//this menu.
	if (!mbIsSubMenu) {
	    try {
		    if (top.document.all.PopupSubMenu.style.visibility == "visible") return;
        }
        catch (e) {
        }
	}

	//If a delayed close has been requested, set a timer and get outta here.
	if (miHideDelay > 0 && miHideTimerID == 0) {
		miHideTimerID = setTimeout(Hide, miHideDelay);
		return;
	}

    //Clear.
  	msMenuName = "";

	//Detach event.
	try {
	    moParentWindow.document.detachEvent("onmousemove", Hide);
	}
	catch (e) {
	}    

    //Hide the inline frame.
	var oPopupMenu = window.frameElement;
	oPopupMenu.style.visibility = "hidden";
}


//-------------------------------------------------------------------------------------
// Initializes the page, fires immediately after the browser loads page. 
//-------------------------------------------------------------------------------------
function InitPage() {

	var oPopupMenu = window.frameElement;
	
	//Set flag to indicate if this is a Sub-Menu.
	mbIsSubMenu = (window.frameElement.id == "PopupSubMenu") ? true : false;

	//Set menu color.
    tblMenuItems.style.background = "whitesmoke";

	//This dosen't work if user moves the mouse to fast.
	oPopupMenu.attachEvent("onmouseout", Hide);
	
	//Allow keyboarding.
	tblMenuItems.attachEvent("onkeydown", OnKeyDown);
}

//-------------------------------------------------------------------------------------
// Initialize the display of a submenu. 
//-------------------------------------------------------------------------------------
function InitSubMenu(oMenuItem) {
    
	var sSubMenuName = oMenuItem.getAttribute("SubMenu");
	
	var oPopupMenu = top.document.all.PopupMenu; 
	var x = oPopupMenu.offsetLeft + oPopupMenu.offsetWidth - 3;
	var y = oPopupMenu.offsetTop + oMenuItem.offsetTop;

	moParentWindow.ShowSubMenu(sSubMenuName, x, y);
}

//-------------------------------------------------------------------------------------
// Called when down arrow key is pressed, hilites the next menu item.
//-------------------------------------------------------------------------------------
function KeyDown() {

	if (tblMenuItems.rows.length == 0) return;
	
	var iCurrentItem = GetCurrentSelection();

	if (iCurrentItem == -1) {
		iCurrentItem = 0;
	}
	else {
		tblMenuItems.rows(iCurrentItem).cells(0).fireEvent("onmouseout");	
		iCurrentItem = (iCurrentItem == tblMenuItems.rows.length-1) ? 0 : iCurrentItem+1;
	}
	
	tblMenuItems.rows(iCurrentItem).cells(0).fireEvent("onmouseover");	
}

//-------------------------------------------------------------------------------------
// Called when up enter key is pressed, fires the onclick event.
//-------------------------------------------------------------------------------------
function KeyEnter() {

	if (tblMenuItems.rows.length == 0) return;
	
	var iCurrentItem = GetCurrentSelection();

	if (iCurrentItem == -1) return;
	
	tblMenuItems.rows(iCurrentItem).cells(0).fireEvent("onclick");	
}

//-------------------------------------------------------------------------------------
// Called when up arrow key is pressed, hilites the previous menu item.
//-------------------------------------------------------------------------------------
function KeyUp() {

	if (tblMenuItems.rows.length == 0) return;
	
	var iCurrentItem = GetCurrentSelection();

	if (iCurrentItem == -1) {
		iCurrentItem = tblMenuItems.rows.length-1;
	}
	else {
		tblMenuItems.rows(iCurrentItem).cells(0).fireEvent("onmouseout");	
		iCurrentItem = (iCurrentItem == 0) ? tblMenuItems.rows.length-1 : iCurrentItem-1;
	}
	
	tblMenuItems.rows(iCurrentItem).cells(0).fireEvent("onmouseover");	
}

//-------------------------------------------------------------------------------------
// Called when key is pressed, handles all relevant keystrokes.
//-------------------------------------------------------------------------------------
function OnKeyDown() {
    
    switch(event.keyCode) {
        case moMain.giKeyDown:
            KeyDown();
            break;            
        case moMain.giKeyUp:
            KeyUp();
            break;            
        case moMain.giKeyReturn:
            KeyEnter();
            break;            
        case moMain.giKeyEscape:
			miHideDelay = 0;
            Hide();
            break;      
     }
}

//-------------------------------------------------------------------------------------
// Shows/removes the hilite when user is moving the mouse over menu items. 
//-------------------------------------------------------------------------------------
function SetCellHilite() {

    if (msMenuName.length == 0){
        event.srcElement.style.cursor = "default";
        return;
    }    

	var oSrc = event.srcElement;
    var sSubMenu = oSrc.getAttribute("SubMenu");

	if (oSrc.immobilize) return;
			
	if (event.type == "mouseover") {
	    oSrc.parentElement.cells(0).style.color = "yellow";
	
		if (sSubMenu.length > 0) {
			InitSubMenu(oSrc);
		}
		else {
			if (!mbIsSubMenu) {
			    try {
			        top.document.all.PopupSubMenu.style.visibility = "hidden";
			    }
			    catch (e) {
			    }    
            }			    
		}
	}
	else {
	    oSrc.parentElement.cells(0).style.color = "white";
	}
}

//-------------------------------------------------------------------------------------
// Displays a popup menu. 
//-------------------------------------------------------------------------------------
function Show(oParent, sMenuName, aItems, x, y, sPosition, bIsSubMenu, iHideDelay, oWithin, bSticky) {
    
	var oPopupMenu = window.frameElement;
	
	//Save stickiness settting as atttribute of menu object. This determines if the menu closes 
	//immediately after the user clicks a menu item, or if it closes after the mouse moves off the 
	//menu, thus allowing the user to click multiple menu items without needing to reopen the menu.
	oPopupMenu.setAttribute("Sticky", bSticky);
	
	//Save delay period (seconds), if specified.
	miHideDelay = iHideDelay;
	
	//If a delayed hide action is in effect, cancel it.
	if (miHideTimerID > 0) {
		clearTimeout(miHideTimerID);
		miHideTimerID = 0;
	}
	
	//Save for callback.
    moParentWindow = oParent;
	msMenuName = sMenuName;

    if (sMenuName.length == 0) {
        //Popup is displaying help information.
     	tblMenuItems.style.color = "blue";
    	tblMenuItems.style.backgroundColor = "yellow";
    }
    else {
     	tblMenuItems.style.color = "";
    	tblMenuItems.style.backgroundColor = "";
    }

	//Display large menu, initially, so menu auto-sizing will work correctly.
	oPopupMenu.style.display = "inline";
	oPopupMenu.style.visibility = "hidden";
	oPopupMenu.style.width  = 400;
	oPopupMenu.style.height = 300;
	oPopupMenu.style.zIndex = 10000;
	
	//Load menu items into auto-sized table.
	CreateMenuItems(aItems);
	
	//Adjust the menu size.
	with (oPopupMenu) {
		style.width  = tblMenuItems.clientWidth  + 3;
		style.height = tblMenuItems.clientHeight + 3;
		style.left = x;
		style.top  = y;
	
		if (sPosition == "below") {
			//Place menu above if it getting close to the bottom of the screen.
			if ((parseInt(style.height) + y) > (screen.height - 250)) {
				style.top  = y - (parseInt(style.height)/2);
			}
			else {
				style.top = y;
			}
		}
		else if (sPosition == "within") {
			//Make sure the menu fits within the parent vertically.
			var sParentBottomPos = oWithin.offsetTop + oWithin.offsetHeight;
			var sParentTopPos = oWithin.offsetTop;
			
			
			var sMsg  = "X=" + x + ", Y=" + y + "\n";
			sMsg += "ParentTop=" + sParentTopPos + "\n";
			sMsg += "ParentBot=" + sParentBottomPos + "\n";
			sMsg += "Menu Height=" + offsetHeight + "\n";
			sMsg += "Menu Top=" + offsetTop + "\n";
			//alert(sMsg); 
			
			
			if (offsetHeight >= oWithin.offsetHeight) {
				style.height = oWithin.offsetHeight - 5;
				style.top = oWithin.offsetTop + 2;
				top.document.body.scrolling = "yes"			}
			else {
				style.visibility = "visible";
				var iTop = y - (parseInt(style.height)/2);
				
				if (iTop < parseInt(sParentTopPos)) {
					for (i=0; i<100; i++) {
						if (iTop > parseInt(sParentTopPos)) break;
						iTop = iTop + 10;
					}
				}
				else if (parseInt(iTop + offsetHeight) > parseInt(sParentBottomPos)) {
					for (i=0; i<100; i++) {
						if (parseInt(iTop + offsetHeight) < parseInt(sParentBottomPos)) break;
						iTop = iTop - 10;
					}
				}
				style.top = iTop;
			}
			
			
			var sMsg  = "X=" + x + ", Y=" + y + "\n";
			sMsg += "ParentTop=" + sParentTopPos + "\n";
			sMsg += "ParentBot=" + sParentBottomPos + "\n";
			sMsg += "Menu Height=" + offsetHeight + "\n";
			sMsg += "Menu Top=" + offsetTop + "\n";
			//alert(sMsg); 
			
		}
		else {
			//style.top  = y - parseInt(style.height) + 14;
		}

		style.visibility = "visible";
	}	
	
	//Make sure menu is hidden when the mouse moves outside the menu's borders.
	oParent.document.attachEvent("onmousemove", Hide);
	
	//Place focus on the menu.
	tblMenuItems.focus();
}

-->
