/*This script creates the dynamic menus used in the MBC website by showing/hiding different elements of the menus based on what page is being used and where the mouse is.  It is meant to drive the menu.php file.*/

$(document).ready(function() { //when the document is loaded
	/*Initialization of the menus (run when first loaded):*/
	var url, active; //create variables for the url of the page and for the type of page that we're on (active page)
	url=window.location.pathname; //get the path part of the url (after the main .org)
	$('li.active').hide(); //hide all of the "active" main menu items
	if(url.length == 1) { //if the url path is of length 1 (i.e. just a "/", indicating home)...
		$('li.home_active_li').show(); //show the home "active" image
		$('li.home_li').hide(); //hide the home "nonactive" image
		$('div.header_sub_menu').hide(); //hide the submenu
		active = "home"; //set the active page as home
	}
	else if(url.substr(0,7) == "/about/") { //if the path is about...repeat as for home
		$('div.about_submenu').show();
		$('li.about_active_li').show();
		$('li.about_li').hide();
		active = "about";
	}
	else if(url.substr(0,12) == "/ministries/") { //if it's ministries...
		$('div.ministries_submenu').show();
		$('li.ministries_active_li').show();
		$('li.ministries_li').hide();
		active = "ministries";
	}
	else if((url.substr(0,8) == "/events/") || (url.substr(0,6) == "/blog/")) { //if it's events...
		$('div.events_submenu').show();
		$('li.events_active_li').show();
		$('li.events_li').hide();
		active = "events";
	}
	else if(url.substr(0,11) == "/bookstore/") { //if it's the bookstore...
		$('div.bookstore_submenu').show();
		$('li.bookstore_active_li').show();
		$('li.bookstore_li').hide();
		active = "bookstore";
	}
	else { //otherwise, assume it's a media page (this accounts for categories, archives, single pages, etc)
		$('div.media_submenu').show();
		$('li.media_active_li').show();
		$('li.media_li').hide();
		active = "media";
	}

/*Code to detect hovering over a link:*/
     $('a.home').hover(function() { //if we hover on the home link
	$('div.sub_navlinks').hide(); //hide all the submenus (to get a "clean slate")
	$('li.active').hide(); //hide the nonactive home image
	$('li.nonactive').show(); //show the nonactive images for all (again, for a clean slate)
        $('li.home_active_li').show(); //show the active image for the home link
     });
     $('a.about').hover(function() { //repeat as for home but for about link
	$('div.sub_navlinks').hide();
	if(active != "home") {
        $('div.about_submenu').show();
	}
	$('li.active').hide();
	$('li.nonactive').show();
        $('li.about_active_li').show();
     });
     $('a.ministries').hover(function() { //ministries link
	$('div.sub_navlinks').hide();
	if(active != "home") {
        $('div.ministries_submenu').show();
	}
	$('li.active').hide();
	$('li.nonactive').show();
        $('li.ministries_active_li').show();
     });
     $('a.events').hover(function() { //events link
	$('div.sub_navlinks').hide();
        $('div.events_submenu').show();
	$('li.active').hide();
	$('li.nonactive').show();
        $('li.events_active_li').show();
     });
     $('a.media').hover(function() { //media link
	$('div.sub_navlinks').hide();
	if(active != "home") {
        $('div.media_submenu').show();
	}
	$('li.active').hide();
	$('li.nonactive').show();
        $('li.media_active_li').show();
     });
     $('a.bookstore').hover(function() { //bookstore link
	$('div.sub_navlinks').hide();
	if(active != "home") {
        $('div.bookstore_submenu').show();
	}
	$('li.active').hide();
	$('li.nonactive').show();
        $('li.bookstore_active_li').show();
     });

/*Code to detect when the mouse enters/exits the menu area (using the menu_container div, which surrounds both the main and submenu items):*/
     $('div.menu_container').hover(function() { //when we hover over the div, do nothing (this allows us to use the hover functions for links defined above)
     },
     function() { //when we move off the div, reset the active menu and submenu to the original for the page:
	$('li.active').hide(); //hide all active images
	$('div.sub_navlinks').hide(); //hide all active submenus
	if(active == "home") { //repeat as for the initialization, except this time just check what the active page type is with the "active" variable
		$('li.home_active_li').show();
		$('li.home_li').hide();
	}
	if(active == "about") {
		$('div.about_submenu').show();
		$('li.about_active_li').show();
		$('li.about_li').hide();
	}
	if(active == "ministries") {
		$('div.ministries_submenu').show();
		$('li.ministries_active_li').show();
		$('li.ministries_li').hide();
	}
	if(active == "events") {
		$('div.events_submenu').show();
		$('li.events_active_li').show();
		$('li.events_li').hide();
	}
	if(active == "media") {
		$('div.media_submenu').show();
		$('li.media_active_li').show();
		$('li.media_li').hide();
	}
	if(active == "bookstore") {
		$('div.bookstore_submenu').show();
		$('li.bookstore_active_li').show();
		$('li.bookstore_li').hide();
	}
     });
 });

