// eMenu, The BRS Elemental Menu Generator (4.4.11)
// Copyright Blue Ridge Solutions, Inc.  www.blueridges.com
//
// INSTRUCTIONS
// Select the menu items that trigger associated the menus and call eMenu() (ex. $('.menu_item').eMenu(); )
// All main menu items/links must be assigned an id of choice and a shared class. (ex. <a id="nav_main" class="menu_item" >)
// All drop menus must be assigned the same id as the main item id prepended with sub_.  (ex. <div id="sub_nav_main" class="drop_menu">)
// Not all main menu items require a drop menu. Set no-link triggers to '#'.
// Default setup: see 3 examples above 


(function($){ 
	$.fn.eMenu = function(options){ 
		var settings = {
			dropMenu:'[id^=sub_]',
			
			theSpeed : 200, 			// animation speed for showing menus [300]
			closeDropMenuDelay : 1500,	// delay to close the current open menu after the mouse leaves it [1000]
			openDropMenuDelay : 200,	// delay to open the drop menu after the mouse goes over the main menu item [200]
			
			matchWidth : 0, // [1] will set the drop menu width to match the main menu outer width (padding included)
			matchBottom : 1, // [1] will butt the drop menus up to the bottom of the main menu items
			matchLeft : 1 // [1] will align the drop menus to the left of the main menu items
		};
		
		return this.each(function(){  
			var $menu = $(this);
			
			// override settings with parameters
			if (options) $.extend(settings, options);

			// Hide
			$(settings.dropMenu).hide();
			
			$(window).load(function(){
				// Menu Variables
				var menuID = $menu.attr('id');
				var menuWidth = $menu.outerWidth();
				var menuPosition = $menu.position();
				var menuBottom = menuPosition.top + $menu.outerHeight();
				
				// Drop Menu Size and Position
				if(settings.matchWidth==1) $('#sub_'+menuID).css({width:menuWidth+'px'});
				if(settings.matchBottom==1) $('#sub_'+menuID).css({position:'absolute',top:menuBottom+'px'});
				if(settings.matchLeft==1) $('#sub_'+menuID).css({position:'absolute',left:menuPosition.left+'px'});
				
				// Activate Menus
				$menu.hover(function(){ onOverMenu($(this)); }, function(){ onOutMenu($(this)); }).click(function(e){ if($(e.target).attr('href') == '#') e.preventDefault(); });
				$(settings.dropMenu).hover(function(){ onOverMenu($(this)); }, function(){ onOutMenu($(this)); });
			});
			
		});
		
		var dropMenuActive = '';		// the currently active drop menu
		var dropMenuRequested = '';
		
		// FUNCTIONS
		function onOverMenu($menu) { 
			var itemId = $menu.attr('id');
			if(itemId.substring(0,4) == "sub_") { 
				// override the menu requested to the sub menu entered
				var targetId = itemId;
				dropMenuRequested = targetId;
			} else {
				var targetId = "sub_" + itemId;
				// stop any pending events for this menu item
				var theTimer = $menu.attr('timerid');
				if(theTimer) clearTimeout(theTimer);
				
				// request this submenu to open
				dropMenuRequested = targetId;
				$menu.attr('timerid', setTimeout(updateMenus, settings.openDropMenuDelay) );
			}
		}
		
		function onOutMenu($menu) {
			// stop any pending events for this menu item
			dropMenuRequested = '';
			var theTimer = $menu.attr('timerid');
			if(theTimer) clearTimeout(theTimer);
		
			// schedule to close the presently active sub menu
			$menu.attr('timerid', setTimeout(updateMenus, settings.closeDropMenuDelay) );
		}
		
		// called via timeout to control the sub menu display
		function updateMenus() {
			if(dropMenuActive == dropMenuRequested) return;  // don't do anything if the menu to display is current
			dropMenuActive = dropMenuRequested;
		
			// show the requested menu
			if(dropMenuActive != '') {
				if($('#' + dropMenuActive).length) $('#' + dropMenuActive + ', #' + dropMenuActive.replace('sub_','')).addClass('active').stop(true,true).slideDown(settings.theSpeed);
			}
				
			// hide all others
			$(settings.dropMenu).each(function(i) {
				if($(this).hasClass('active') && $(this).attr('id') != dropMenuActive) {
					var menuitem = $(this).attr('id');
					menuitem = menuitem.replace('sub_','#');
					$(this).removeClass('active').stop(true,true).slideUp(settings.theSpeed,function(){ $(menuitem).removeClass('active'); });
				}
			});
		}
	}; // eMenu Plugin
})(jQuery);

