/* MENU.JS
   Menu display functions.

    Version Information:
     20020204a = Created.
	 20020516a = Modified for increased browser compatibility.
	 20030604a = Added rollMenu function.
	 20050420a = Added rollMenu2 and killMenu functions.
	 20050503a = Added menuTime, variable time to close for menu items.
	 20050523a = Added check of menu existence to rollMenu2.
	 20050617a = Added pw, parent width, option for rollMenu2 relPosition property.
	 20060216a = Added bc, bottom centered, option for rollMenu2.
	 20060301a = Modified killMenus to allow for unspecified menus in menuNames array.
	 20060327a = Modified rollMenu2 to keep the menu on the screen no matter what.
	 20080919a = Added upl and upc options for rollMenu2.
	 20081228a = Added support for default menus.

Copyright © 2006, Land Information Access Association
*/

/* Initialize Variables */
var menuNames = new Array();
var menuRel = new Array();
var timeID;
var menuTime = 5000;
var menuDefault, defaultID;

function showMenu(menuName) {
	toggleText(menuName, 1);
}

function rollMenu(menuName) {
	if (timeID) clearTimeout(timeID);
	killMenus();
	toggleText(menuName, 1);
	menuTime = parseInt(menuTime);
	if (menuTime > 0) timeID = setTimeout('killMenus()', menuTime);
}

function overMenu(menuName) {
	if (defaultID) clearTimeout(defaultID);
	toggleText(menuName, 1);
	if (menuRel[menuName]) overMenu(menuRel[menuName]);
}

function rollMenu2(objRef, menuName, relPosition, parentName, offsetX, offsetY) {
	var menuLeft, menuTop;
	if (timeID) clearTimeout(timeID);
	if (menuName && menuName != '') {
		if (parentName && parentName != '') {
			menuRel[menuName] = parentName;
		} else {
			menuRel = new Array();
			killMenus();
		}
		if (defaultID) clearTimeout(defaultID);
		if (!offsetX) offsetX = 0;
		if (!offsetY) offsetY = 0;
		getSize();
		getStyleObj();
		var theMenu = eval(styleObj.replace("~name~",menuName));
		if (theMenu) {
			var theMenuObj = getObjRef(menuName);
			var menuWidth = theMenuObj.offsetWidth;
			var menuHeight = theMenuObj.offsetHeight;
			var objCoords = getCoords(objRef);
			switch (relPosition) {
				case 'bl': /* bottom left of reference object */
					menuLeft = objCoords[3];
					menuTop = objCoords[2];
					break;
				case 'tr': /* top right of reference object */
					menuLeft = objCoords[1];
					menuTop = objCoords[0];
					break;
				case 'br': /* bottom right of reference object */
					menuLeft = objCoords[1];
					menuTop = objCoords[2];
					break;
				case 'pw': /* parent width: bottom left of reference object and forced to width of object if menu width is less */
					menuLeft = objCoords[3];
					menuTop = objCoords[2];
					if (menuWidth < objRef.offsetWidth) theMenuObj.width = objRef.offsetWidth;
					break;
				case 'bc': /* menu is centered on the bottom of the reference object */
					var widDif = objRef.offsetWidth - menuWidth;
					if (widDif == 0) {
						menuLeft = objCoords[3];
					} else {
						menuLeft = objCoords[3] + parseInt(widDif / 2);
					}
					menuTop = objCoords[2];
					break;
				case 'upl': /* menu appears above the reference object, aligned left */
					menuLeft = objCoords[3];
					menuTop = objCoords[0] - menuHeight;
					break;
				case 'upc': /* menu appears above reference object, centered on the object */
					var widDif = objRef.offsetWidth - menuWidth;
					if (widDif == 0) {
						menuLeft = objCoords[3];
					} else {
						menuLeft = objCoords[3] + parseInt(widDif / 2);
					}
					menuTop = objCoords[0] - menuHeight;
					break;
			}
			//Check if menu goes off the right side of the screen
			if (menuLeft + menuWidth + offsetX > winX) {
				//If this is a sub menu, move it to the left of the parent
				if (parentName) {
					menuLeft = menuLeft - menuWidth - offsetX - objRef.offsetWidth;
					if (menuLeft < 0) menuLeft = 0;
				} else {
					//Otherwise, set the right edge of the menu to the right side of the screen
					menuLeft = winX - menuWidth;
				}
				//menuLeft = menuLeft - menuWidth;
				//if (parentName) menuLeft = menuLeft - objRef.offsetWidth;
			} else {
				menuLeft = menuLeft + offsetX;
			}
			//Check if menu goes off the bottom of the screen
			if (menuTop + menuHeight + offsetY > winY) {
				//If so, align the bottom of the menu with the bottom of the screen
				menuTop = winY - menuHeight;
				//If it goes off the top of the screen, set the top to the top of the screen
				if (menuTop < 0) menuTop = 0;
				/*
				menuTop = menuTop - menuHeight;
				if (relPosition == 'tr') {
					menuTop = menuTop + objRef.offsetHeight;
				} else {
					menuTop = menuTop - objRef.offsetHeight;
				}
				*/
			} else {
				menuTop = menuTop + offsetY;
			}
			theMenu.position = 'absolute';
			theMenu.left = menuLeft + 'px';
			theMenu.top = menuTop + 'px';
			toggleText(menuName, 1);
		}
	}
	menuTime = parseInt(menuTime);
	if (menuTime > 0) timeID = setTimeout('killMenus()', menuTime);	
}

function killMenus() {
	if (defaultID) clearTimeout(defaultID);
	if (menuNames) {
		for (i = 1; i < menuNames.length; i++) {
			if (menuNames[i]) toggleText(menuNames[i], 0);
		}
	}
	if (menuDefault) defaultID = setTimeout('defaultMenu()', 10);
	return true;
}

function killMenu(menuName) {
	toggleText(menuName, 0);
}

function defaultMenu() {
	eval(menuDefault);
}

function initMenu() {
	if (document.layers) {
		document.captureEvents(Event.MOUSEDOWN);
		document.onMouseDown = killMenus;
	}
}
