/*
jQuery ActiveMenu v1.0
Author: Miguel Sanchez
12/2009
*/


var timeouts = []; //for hiding the menu purposes


function initMenu(){
var offclass;
var menuAnchor;
$("#activeMenu").find("li").each(function() {

//get a reference to the anchor inside the li
menuAnchor = $(this).children(":first");

//get the name of the offclass
offclass = $(menuAnchor).attr("id")+"_off";

//assign off class to item
$(menuAnchor).addClass(offclass);

//assign hover event handler to main menu
$(menuAnchor).hover(
function (event) {
submenu_show(event.target);
},
function (event) {
submenu_hide(event.target);
}
);		

});

//assign hover events to submenu
$("div [id$=_submenu]").hover(
function (event) {
submenu_over(event.target);
},
function (event) {
submenu_out(event.target);
}
);


}


function submenu_show(caller){
//hide all the submenus
$("div [id$=_submenu]").hide();

//get the id of the main menu item
var mainMenuItemId = $(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
$(caller).removeClass(offclass);

//assign on class to item
$(caller).addClass(onclass);

// get the id of the submenu
var targetSubMenuId = mainMenuItemId+"_submenu";
//show the submenu
$("#"+targetSubMenuId).slideDown("fast");



}

function submenu_hide(caller){

//get the id of the main menu item
var mainMenuItemId = $(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
$(caller).removeClass(onclass);

//assign off class to item
$(caller).addClass(offclass);

timeouts[mainMenuItemId] = setTimeout(function() {
$("#"+mainMenuItemId+"_submenu").slideUp("fast");
}, 200);

}

function submenu_over(caller){

//get a reference to the containing div
var subMenuDiv = $(caller).closest("div");
//show the div
$(subMenuDiv).show();

//get the id of the main menu
var mainMenuItemId = $(subMenuDiv).attr("id");
mainMenuItemId = mainMenuItemId.replace("_submenu","");

clearTimeout(timeouts[mainMenuItemId]);


//remove the "off" class
$("#"+mainMenuItemId).removeClass(mainMenuItemId+"_off");
//add the "on" class
$("#"+mainMenuItemId).addClass(mainMenuItemId+"_on");
}


function submenu_out(caller){
//get a reference to the containing div
var subMenuDiv = $(caller).closest("div");
$(subMenuDiv).hide();
//get the id of the main menu
var menuAnchorId = $(subMenuDiv).attr("id");
menuAnchorId = menuAnchorId.replace("_submenu","");

//remove the "on" class
$("#"+menuAnchorId).removeClass(menuAnchorId+"_on");

//add the "off class
$("#"+menuAnchorId).addClass(menuAnchorId+"_off");
}
