/*
jQuery ActiveMenu v1.0
Author: Miguel Sanchez
12/2009
*/


var timeouts = [];  //for hiding the menu purposes


function initMenu(){
	var offclass;	
	var menuAnchor;
	jQuery("#activeMenu").find("li").each(function() {
	
		//get a reference to the anchor inside the li
		menuAnchor = jQuery(this).children(":first");
		
		//get the name of the offclass
		offclass = jQuery(menuAnchor).attr("id")+"_off";
		
		//assign off class to item
		jQuery(menuAnchor).addClass(offclass);
		
		//assign hover event handler to main menu
		jQuery(menuAnchor).hover(
		  function (event) {
		    submenu_show(event.target);
		  },
		  function (event) {
		   	submenu_hide(event.target);
		  }
		);		
		
	});
	
	jQuery("#activeMenu2").find("li").each(function() {
	
		//get a reference to the anchor inside the li
		menuAnchor = jQuery(this).children(":first");
		
		//get the name of the offclass
		offclass = jQuery(menuAnchor).attr("id")+"_off";
		
		//assign off class to item
		jQuery(menuAnchor).addClass(offclass);
		
		//assign hover event handler to main menu
		jQuery(menuAnchor).hover(
		  function (event) {
		    submenu_show(event.target);
		  },
		  function (event) {
		   	submenu_hide(event.target);
		  }
		);		
		
	});
	
	jQuery("#activeMenu3").find("li").each(function() {
	
		//get a reference to the anchor inside the li
		menuAnchor = jQuery(this).children(":first");
		
		//get the name of the offclass
		offclass = jQuery(menuAnchor).attr("id")+"_off";
		
		//assign off class to item
		jQuery(menuAnchor).addClass(offclass);
		
		//assign hover event handler to main menu
		jQuery(menuAnchor).hover(
		  function (event) {
		    submenu_show(event.target);
		  },
		  function (event) {
		   	submenu_hide(event.target);
		  }
		);		
		
	});
	
	//assign hover events to submenu
	jQuery("div [id$=_submenu]").hover(
	  function (event) {		  	
	    submenu_over(event.target);
	  },
	  function (event) {
	   submenu_out(event.target);
	  }
	);
	
	
}


function submenu_show(caller){
	//hide all the submenus
	jQuery("div [id$=_submenu]").hide();
	
	//get the id of the main menu item
	var mainMenuItemId = jQuery(caller).attr("id")
	
	//get the "on" class name	
	var onclass = mainMenuItemId+"_on";
	//get the "off" class name	
	var offclass = mainMenuItemId+"_off";
	
	//remove off class to item
	jQuery(caller).removeClass(offclass);
	
	//assign on class to item
	jQuery(caller).addClass(onclass);
	
	// get the id of the submenu
	var targetSubMenuId = mainMenuItemId+"_submenu";
	//show the submenu
	jQuery("#"+targetSubMenuId).slideDown("fast");
	


}

function submenu_hide(caller){
	
	//get the id of the main menu item
	var mainMenuItemId = jQuery(caller).attr("id");
	
	//get the "on" class name	
	var onclass = mainMenuItemId+"_on";
	//get the "off" class name	
	var offclass = mainMenuItemId+"_off";
	
	//remove on class to item
	jQuery(caller).removeClass(onclass);
	
	//assign off class to item
	jQuery(caller).addClass(offclass);
	
	timeouts[mainMenuItemId] = setTimeout(function() {
        jQuery("#"+mainMenuItemId+"_submenu").slideUp("fast");
    }, 200);

}

function submenu_over(caller){

	//get a reference to the containing div
	var subMenuDiv = jQuery(caller).closest("div");
	//show the div
	jQuery(subMenuDiv).show();
	
	//get the id of the main menu
	var mainMenuItemId = jQuery(subMenuDiv).attr("id");
	mainMenuItemId = mainMenuItemId.replace("_submenu","");

	clearTimeout(timeouts[mainMenuItemId]);

	
	//remove the "off" class
	jQuery("#"+mainMenuItemId).removeClass(mainMenuItemId+"_off");
	//add the "on" class
	jQuery("#"+mainMenuItemId).addClass(mainMenuItemId+"_on");	
}


function submenu_out(caller){
	//get a reference to the containing div
	var subMenuDiv = jQuery(caller).closest("div");
	jQuery(subMenuDiv).hide();
	//get the id of the main menu
	var menuAnchorId = jQuery(subMenuDiv).attr("id");
	menuAnchorId = menuAnchorId.replace("_submenu","");
	
	//remove the "on" class
	jQuery("#"+menuAnchorId).removeClass(menuAnchorId+"_on");
	
	//add the "off class
	jQuery("#"+menuAnchorId).addClass(menuAnchorId+"_off");	
}

