// Menu Animation & State Scrpts for YWCA
$(document).ready(function(){	
						   
	function setSubmen(){ // This function opens menu to show initial values
		if(mainSect){ // If the main Section has been passed show its submen
			$('#menu a:contains('+ mainSect +') + .submen').slideDown(); // Show the submenu for the section we are in
			$('#menu a:contains('+ mainSect +')').addClass('active1'); // add the class so we know that it is open
		};			
	};
						   
	// ============== Initialize =======================
	$('#menu ul').addClass('submen');	// Make sure even generated submenus have the right class.
	$('#menu ul ul').addClass('sub-submen'); // Make sure even generated subsubmenus have the right class.
	$('#menu .submen').hide(); // hide all submenus first off because we might not need them yet
	// Check the the title of the current page (passed in through wordpress php) and add a class to it.
	$(".sub-submen a:contains('" + thistitle + "')").addClass("currentmenitem");
	setSubmen();
	
	
	// ============== Level 1 ============================

	var isactive1 = $('#menu a').hasClass('active1');
	// console.log('is there an active1 :' + isactive1 );
	function level1Hover(){
		var selfClick = $(this).next().is(':visible'); // checks if the submenu is visible to see if we clicked on the item that is alrady open.
		var isactive1 = $('#menu a').hasClass('active1'); // Checks to find any other open menus
		var hasSubmen = $(this).next().is('ul'); // Check to see if the current item even has a subemenu to bother fireing.
		// console.log('The value of selfClick is : ' + selfClick );
		// console.log('is there an active1 :' + isactive1 );

		if(hasSubmen){ // if the item clieck on has a Submen then go ahead
			 if(isactive1){ // If there are any open menus anywhere else close them and remove the class
				$('.active1').removeClass('active1').next('.submen').animate({ "height": "toggle", "opacity": "toggle"},{queue: false});
				
				if(!selfClick){ // Well I clicked on an already open menu so I better fix it
					$(this).addClass('active1').next('.submen').animate({ "height": "toggle", "opacity": "toggle"},{queue: false}); 
				};
			}else{ // There are no other Acive menus open so go ahead and open away
				$(this).addClass('active1').next('.submen').animate({ "height": "toggle", "opacity": "toggle"},{queue: false}); 
			}; 
		};		
	};
	function level1Out(){};
	var config = {
		sensitivity:2,
		interval:100,
		over: level1Hover,
		timeout: 200,
		out: level1Out
	};
	$('#menu > li > a').hoverIntent( config );		


		// ============== Reset on Mouse Out ============================	
		$('#menu').mouseleave(function(){ // When we stop hovering on the main menu reset the menu to where we are.
			var mainSectOpen = $('#menu a:contains('+ mainSect +')').next('ul').is(':visible'); // return is the section we want open is open.
			var mainHasSubmen = $('#menu a:contains('+ mainSect +')').next().is('ul');
			if(!mainSectOpen) { // if the section is not visible then double check then open it and the submenu.
				if(mainSect){ // If the main section was passed by Wordpress/PHP
					setTimeout("", 100);				
					$('#menu .active1').removeClass('active1').next().animate({ "height": "toggle", "opacity": "toggle"},{queue: false} ); // hide all submenus first off because we might not need them yet
					if(mainHasSubmen ){ // need to check if it has a submen before trying to set it.
						setTimeout(setSubmen, 500);
					}else{// it doesn't have a sub menu but it still needs to set the class
						$('#menu a:contains('+ mainSect +')').addClass('active1'); // add the class so we know that it is open						
					};
				};
			} else {// the main section is already open so...
				var thirdLevelOpen = $(".submen > a:contains('" + catTitle + "')").next('ul').is(':visible'); // ... return if the sub-submenu is visible
				if(!thirdLevelOpen){ // if it's not open then... 
					setSubSubmen(); // reset the submenu
				};
			};
		});

});

