 /*

Filename:	menu.js

Author:	Alex Jonas

Project:	ICONS Project Administrative Database System

Notes:		This external JavaScript file supports menu operations for the ICONS Administrative Database Interface.

Last Updated: 	12/15/2009

*/

	var inMenu = false;
	var currentMenuItem = null;
	var oldMenuItem = null;
	var timeoutId = null;


    	function countSubMenus() {
	
		//Puts all divs into an array;
		//Assumes that all those starting with the string "sub_"
		//are sub menus; increments and finally returns counter.

		var numSubMenus = 0;

		var myDivs = document.getElementsByTagName('div');
                
		for (var i = 0; i < myDivs.length; i++) {
                        
			if ( myDivs[i].getAttribute('id') && myDivs[i].getAttribute('id').substring(0, 4).toLowerCase() == 'sub_' ) { 
                        
				numSubMenus++;
                        
               }

		}
		
		return numSubMenus;

	}

	
	function menuOn(objMenu) {

		//Hide any and all open submenus;
		//Highlight menu item;
		//Check for a corresponding submenu;
		//If a submenu is found, call the showMenu() function.

		if (!document.getElementById) return;

		var id = objMenu.getAttribute('id');
		var subMenuId = 'sub' + id;

		hideAllSubMenus();
		
		oldMenuItem = currentMenuItem;
		currentMenuItem = objMenu;

		if (oldMenuItem) unHighlightMenuItem(oldMenuItem);
		if (currentMenuItem) highlightMenuItem(currentMenuItem);
		
		(document.getElementById(subMenuId)) ? showMenu(objMenu): alert('No submenu to display.');

	}

	
	function menuOff(objMenu) {

		//For now, simply un-highlight the item via the appropriate function.

		if (!objMenu) return;

		unHighlightMenuItem(objMenu);

	}

	
	function highlightMenuItem(objMenu) {

		//Highlight the submitted item;

		if (!objMenu) return;
		
		objMenu.style.color = '#000066';
		objMenu.style.backgroundColor = '#E7E6E0';
		inMenu = true;

	}

	
	function unHighlightMenuItem(objMenu) {

		//Un-highlight the submitted item;
		
		if (!objMenu) return;

		objMenu.style.color = '#000000';
		objMenu.style.backgroundColor = '#C0C0C0';
		inMenu = false;

	}


	function showMenu(objMenu) {

		//Show the selected menu.

		if (!document.getElementById) return;

		var id = objMenu.getAttribute('id');
		var subId = 'sub' + id;
		var objSubMenu = document.getElementById(subId);
		
		if (document.addEventListener) {
		
			objSubMenu.addEventListener('mouseout', function() { timeoutId = setTimeout('hideAllSubMenus()', 3000); }, false);
			
		} else {
		
			objSubMenu.onmouseout = function() { timeoutId = setTimeout('hideAllSubMenus()', 3000); };
			
		}
		
		objSubMenu.style.left = parseInt(objMenu.offsetLeft, 10) + 'px';  //All browsers
		
		if (objMenu.offsetTop && objMenu.offsetLeft && objMenu.offsetHeight) {	//Firefox, Internet Explorer, Opera
		
			objSubMenu.style.top = ( parseInt(objMenu.offsetTop, 10) + parseInt(objMenu.offsetHeight, 10) + 1 ) + 'px';
			
		} else if (objMenu.offsetTop == 0 && objMenu.offsetHeight) {	//Google Chrome, Safari
		
			objSubMenu.style.top = ( parseInt(objMenu.offsetTop, 10) + parseInt(objMenu.offsetHeight, 10) + 2 ) + 'px';
			
		} else {
		
			alert('Could not determine position of submenu.');
			return;
			
		}

		objSubMenu.style.visibility = 'visible';

	}


	function hideAllSubMenus() {

		//Unhighlight any and all menu items.
		//Hide any and all submenus.
		
		if (!document.getElementById) return;

		if (!inMenu) {
		
			var objMenu;
			var objSubMenu;
		
			var numSubMenus = countSubMenus();
		
			for (var i = 1; i <= numSubMenus; i++) {

				objMenu = document.getElementById('_' + i.toString());
				unHighlightMenuItem(objMenu);

				objSubMenu = document.getElementById('sub_' + i.toString());

				if (document.removeEventListener) {
		
					objSubMenu.removeEventListener('mouseout', function() { timeoutId = setTimeout('hideAllSubMenus()', 3000); }, false);
			
				} else {
		
					objSubMenu.onmouseout = null;
			
				}
		
				objSubMenu.style.visibility = 'hidden';
				
				clearTimeout(timeoutId);

			}

		} else {
		
			return;
			
		}
		
	}


	function toggleSubMenu(objMenu) {

		//Toggles visibility of submenu object when menu item is clicked.

		if (!objMenu) return;
		
		if (!inMenu) return;

		var objSubMenu = document.getElementById('sub' + objMenu.getAttribute('id'));
		
		objSubMenu.style.visibility = (objSubMenu.style.visibility == 'visible') ? 'hidden' : 'visible';

	}


	function response(objMenu) {

		//Tell 'em what they've chosen, Johnny!
		//Unhighlight the current menu item.

		if (!document.getElementById) return;

		alert('You chose: ' + objMenu.firstChild.nodeValue);
		unHighlightMenuItem(document.getElementById(currentMenuItem));

	}




