/*
* this object implements a button
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("menu.js requires the NC JavaScript framework");
 }

 
 
 /*
  * Manages all menus for show/hide purposes
  * 
  */	
(function () {

 NC.widget.MenuManager = function() {
 	// private variables
 	
 	var visibleMenus = {};
 	var showTimerId = null;
 	
 	// private methods
 	function _hideVisibleMenus()
 	{
    	for (var i in visibleMenus) {
    		_hideMenu(i);
    	}
 	}
 	
 	function _hideMenu( menuId) {
			var oMenu = new NC.widget.Menu( menuId);
			oMenu.init();
 			oMenu.removeMenuEvents(menuId);
 			oMenu.removeMenuHighLight(menuId);
			var menuElement = document.getElementById( menuId);     // get the element
			var shimId = makeShimId( menuId);
			var shimElement = document.getElementById( shimId);
			var oShim = new NC.widget.IFrameShim( shimId, menuElement);
			oShim.setShimIframe( shimElement);
			oShim.hideIframe();
			NC.util.Dom.setStyle( menuElement, 'visibility', 'hidden');
			delete visibleMenus[menuId]; // remove from list of visible menus
 	}
 	
 	function makeShimId( menuId) {
 		return menuId + "-iShim";
 	}
 	
	
 	// public methods
 	return {
 		/*
 		 * menuId is an elementid
 		 * menupos is an array x = menupos[0], y = menupos[1]
 		 */
 		SHOW_TIMER_DELAY_MILLISECONDS: 100,
 		
 		clearShowMenuTimer: function() {
 			if (showTimerId) { // clear the timer
 				window.clearTimeout( showTimerId);
// 				NC.util.Debug.writeToConsole( "clear timer " + showTimerId);
 				showTimerId = null;
 			}
 		},
		 
 		_showMenu: function( menuId, menupos, ownerId) { // divid that contains the menu
 			this.clearShowMenuTimer();
 			_hideVisibleMenus();
			var menuElement = document.getElementById( menuId);     // get the element
			var oMenu = new NC.widget.Menu( menuId);
			oMenu.init();
 			oMenu.addMenuEvents(menuId);
			oMenu.setOwner( ownerId);
			var shimId = makeShimId(menuId);
			var shim = document.getElementById( shimId);
			var oShim = new NC.widget.IFrameShim( shimId, menuElement); // create the shim object
			if (shim == null) { // no iframe yet
				oShim.createIframe();
//				NC.util.Debug.writeToConsole("create iframe shim");
			}
			else {
//				NC.util.Debug.writeToConsole("use existing iframe shim");
				oShim.setShimIframe( shim);
			}
			// position it
  			NC.util.Dom.setAbsoluteXY( menuElement, menupos, true); // set its position
  			oShim.config();
			NC.util.Dom.setStyle( menuElement, 'visibility', 'visible');
			visibleMenus[menuId] = oMenu; // add to list of visible menus
//			NC.util.Debug.writeToConsole("show menu " + menuId);
 		},

 		showMenu: function( menuId, menupos, ownerId) { // divid that contains the menu
 			
 			function doShow() {
 				NC.widget.MenuManager._showMenu( menuId, menupos, ownerId);
 			}
 			if (!showTimerId) { // if the timer is not already set
	 			showTimerId = window.setTimeout( doShow, this.SHOW_TIMER_DELAY_MILLISECONDS);
//	 			NC.util.Debug.writeToConsole( "showmenu:timer id is " + showTimerId);
 			}
 		},

 		hideMenu: function( menuId) { // divid that contains the menu
 			_hideMenu( menuId);
 		},
 		
 		hideAllMenus: function( ) { // divid that contains the menu
 			_hideVisibleMenus( );
 		},
 		
 		isMenuVisible: function( menuId) {
 			if (typeof visibleMenus[menuId] == 'undefined') {
 				return false;
 			}
 			else {
 				return true;
 			}
 		},
 		
 		getMenu: function( menuId) {
 			return visibleMenus[menuId];
 		}
 		
 		
 	};
 	
 }	
();

})(); 	
