// Hide all sub-categories (before page is loaded, as fast as possible...)


$.noConflict();
jQuery(document).ready(function($) {
	
	/* PAGE LOAD -----> */
	
	if( $("ul#leftMenu > li > ul").length > 0 )
		$("ul#leftMenu > li > ul").hide();
	if( $("ul#leftMenu > li > ul > li > ul").length > 0 )
		$("ul#leftMenu > li > ul > li > ul").hide();
		
	// If some 1st level menu is selected, then slide down 2nd (if it exists)
	if( $("ul#leftMenu > li.current_page_item > ul").length > 0 ) {
		$("ul#leftMenu > li.current_page_item > ul").delay(800).slideToggle('slow');
	}
	
	// If 2nd level menu is selected
	if( $("ul#leftMenu > li > ul > li.current_page_item").length > 0 ) 
	{
		// 2st level should be displayed instantly, no sliding...
		$("ul#leftMenu > li.current_page_ancestor > ul").show();
		
		// Slide down 3rd level (if it exists)
		if( $("ul#leftMenu > li > ul > li.current_page_item > ul").length > 0 )
			$("ul#leftMenu > li > ul > li.current_page_item > ul").delay(800).slideToggle('slow');
	}
	
	// If 3rd level menu is selected
	if( $("ul#leftMenu > li > ul > li > ul > li.current_page_item").length > 0 ) 
	{
		// 2st and 3rd level should be displayed instantly, no sliding...
		$("ul#leftMenu > li.current_page_ancestor > ul").show();
		$("ul#leftMenu > li > ul > li.current_page_ancestor > ul").show();
	}

	/* -----> PAGE LOAD */




	/* MENU LINK CLICKED -----> */

	// Run this every time any 1st level link is clicked in the menu
	$("ul#leftMenu > li > a").click(function(ev){
		// Prevent the default action of the event --> stop the href in the 
		// anchor from being followed before the animation has started
		ev.preventDefault();
		
		// Store a reference to the anchor tag
		var $self = $(this);
		
		// If ANY 1st level menu currently is selected and that selected 
		// menu has children then always close entire menu
		if( ($("ul#leftMenu > li.current_page_item > ul").length > 0) ) {
				$("ul#leftMenu > li.current_page_item > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		else if( ($("ul#leftMenu > li.current_page_ancestor > ul").length > 0) ) {
				$("ul#leftMenu > li.current_page_ancestor > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		
		
		/*
		// If 2nd level menu currently is selected and has children
		else if( $("ul#leftMenu > li > ul > li.current_page_item > ul").length > 0 ) {
				$("ul#leftMenu > li > ul > li.current_page_item > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}*/
		else {
			// Just refresh... No slide-up is needed because no 2nd lv children exist
			document.location = $self.attr('href');
		}
	
	});
	
	// Run this every time any 2nd level link is clicked in the menu
	$("ul#leftMenu > li > ul > li > a").click(function(ev){
		// Prevent the default action of the event --> stop the href in the 
		// anchor from being followed before the animation has started
		ev.preventDefault();
		
		// Store a reference to the anchor tag
		var $self = $(this);
		
		// If there are any 3rd level children visible the slide them up
		if( ($("ul#leftMenu > li > ul > li.current_page_item > ul").length > 0) ) {
				$("ul#leftMenu > li > ul > li.current_page_item > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		else if( ($("ul#leftMenu > li > ul > li.current_page_ancestor > ul").length > 0) ) {
				$("ul#leftMenu > li > ul > li.current_page_ancestor > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		else {
			// Just refresh... No slide-up is needed because no 3rd lv children exist
			document.location = $self.attr('href');
		}
	
	});
	
	// Just refresh normally... No slide-up is needed because user has pressed a 3rd lv link
	
	/* -----> MENU LINK CLICKED */
	
	
	
});
