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

	/* -----> INIT */
	
	

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

	// Run this every time any 1st level link is clicked in the menu
	$("ul#sideMenu > 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#sideMenu > li.current_page_item > ul").length > 0) ) {
				$("ul#sideMenu > li.current_page_item > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		else if( ($("ul#sideMenu > li.current_page_ancestor > ul").length > 0) ) {
				$("ul#sideMenu > 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#sideMenu > li > ul > li.current_page_item > ul").length > 0 ) {
				$("ul#sideMenu > 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#sideMenu > 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#sideMenu > li > ul > li.current_page_item > ul").length > 0) ) {
				$("ul#sideMenu > li > ul > li.current_page_item > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		else if( ($("ul#sideMenu > li > ul > li.current_page_ancestor > ul").length > 0) ) {
				$("ul#sideMenu > 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 */
	
	
	
	
	
	
	
	
	
	// POSTS ---->>
	
	
	
	
	
	
	/* INIT -----> */
	
	// If some 1st level menu is selected, then slide down 2nd (if it exists)
	if( $("ul#sideMenuPosts > li.current-cat > ul").length > 0 ) {
		$("ul#sideMenuPosts > li.current-cat > ul").delay(800).slideToggle('slow');
	}
	
	// If 2nd level menu is selected
	if( $("ul#sideMenuPosts > li > ul > li.current-cat").length > 0 ) 
	{
		// 2st level should be displayed instantly, no sliding...
		$("ul#sideMenuPosts > li.current-cat-parent > ul").show();
		
		// Slide down 3rd level (if it exists)
		if( $("ul#sideMenuPosts > li > ul > li.current-cat > ul").length > 0 )
			$("ul#sideMenuPosts > li > ul > li.current-cat > ul").delay(800).slideToggle('slow');
	}
	
	// If 3rd level menu is selected
	if( $("ul#sideMenuPosts > li > ul > li > ul > li.current-cat").length > 0 ) 
	{
		// 2st and 3rd level should be displayed instantly, no sliding...
		$("ul#sideMenuPosts > li > ul").show();
		$("ul#sideMenuPosts > li > ul > li.current-cat-parent > ul").show();
	}

	/* -----> INIT */
	
	

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

	// Run this every time any 1st level link is clicked in the menu
	$("ul#sideMenuPosts > 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#sideMenuPosts > li.current-cat > ul").length > 0) ) {
				$("ul#sideMenuPosts > li.current-cat > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		else if( ($("ul#sideMenuPosts > li.current-cat-parent > ul").length > 0) ) {
				$("ul#sideMenuPosts > li.current-cat-parent > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		
		// This one is extra for posts menus since top level doesn't show 'parent'...
		// I.e. a 3rd level menu is selected, but user clicks on a 1st level topic
		else if( ($("ul#sideMenuPosts > li > ul > li.current-cat-parent > ul").length > 0) ) {
				$("ul#sideMenuPosts > li > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		
		
		/*
		// If 2nd level menu currently is selected and has children
		else if( $("ul#sideMenu > li > ul > li.current_page_item > ul").length > 0 ) {
				$("ul#sideMenu > 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#sideMenuPosts > 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#sideMenuPosts > li > ul > li.current-cat > ul").length > 0) ) {
				$("ul#sideMenuPosts > li > ul > li.current-cat > ul").slideToggle('slow', function() {
					document.location = $self.attr('href');
				});
		}
		else if( ($("ul#sideMenuPosts > li > ul > li.current-cat-parent > ul").length > 0) ) {
				$("ul#sideMenuPosts > li > ul > li.current-cat-parent > 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 */
	
});
