/*-----------------------------------------------------------------------------\

	Author:		Chad Harrington
	Date:		October 28, 2010
	Filename: 	menu.js
	Supports:	left_nav.html, menu.css
	
	Documentation:
	
	1) jQuery's ".ready()" event - script can be run as soon as DOM
	hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code; specify a function to execute when the DOM is fully loaded.
	
	2) Selects all navigation headers ('#menu ul > li') and 
	subheader ('#menu > li > ul > li') and binds a click function.
	
	3) Create variables to hold data (boolean) about which link was clicked in the menu: (a) TRUE if a menuheader is clicked. (b) TRUE if a menuheader is clicked that does not contain a submenu. (c) TRUE if a submenuheader is clicked (d) TRUE if a subheader is clicked and is not a submenuheader (it does not contain a subcategory). (e) TRUE if a subcategory item is clicked. (f) TRUE if menuheader or submenuheader is clicked, and that item is already "active" or "open". (g) Stores the time (in milliseconds) for the animation to complete, change the value for a longer/shorter animation period.
	


\-----------------------------------------------------------------------------*/


$(function() { //(1)
	
	$('#menu ul > li, #menu > li > ul > li').click(function(event) { //(2)
		
		var menuHeaderClick = $(this).is('li.header'); //(3a)
		var menuHeaderClickNoExp = $(this).is('li[class=header no-expand]'); //(3b)
		var subHeaderClick = $(this).is('li[class$=submenuheader]'); //(3c)
		var subHeaderClickNoExp = $(this).is('li.header ul.categoryitems li.no-expand'); //(3d)
		var subItemClick = $(this).is('ul.subcategoryitems li'); //(3e)
		var selfClick = $(this).find('ul:first').is(':visible'); //(3f)
		var time = 800; //(3g)
		
		
		if(menuHeaderClick && selfClick) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: menuHeaderClick and selfClick');
			
			$(this).find('ul.subcategoryitems:visible').slideUp(time);
				
			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
			
		}
		
		else if(subHeaderClick) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: subHeaderClick and selfClick');
			
			$(this).find('ul.subcategoryitems:hidden').slideDown(time);
				
			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
			
		}
		
		else if(subHeaderClick && selfClick) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: subHeaderClick and selfClick');
				
			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
			
		}
		else if(menuHeaderClick && !selfClick && !menuHeaderClickNoExp ) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: menuHeaderClick NOTselfClick NOTmenuHeaderClickNoExp');
			
			$(this).parent().find('ul.categoryitems:visible').slideUp(time);
			$(this).find('a:first').addClass('active-category');
			$(this).find('ul.categoryitems:hidden').slideDown(time);

			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
		}
		else if(menuHeaderClickNoExp && !selfClick) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: menuHeaderClickNoExp and !selfClick');
			
			$(this).parent().find('ul.categoryitems:visible').slideUp(time);
			$(this).find('a:first').addClass('active-category-noexp');
				
			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
			
		}
		else if(subHeaderClick && !selfClick  && !subItemClick ) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: subHeaderClick NOTselfClick NOTsubItemClick ');
			
			$(this).parent().find('ul.subcategoryitems:hidden').slideDown(time);
			

			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
		}
		else if(subHeaderClickNoExp && !selfClick  && !subItemClick ) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: subHeaderClickNoExp NOTselfClick NOTsubItemClick ');
			
			$(this).parent().find('ul.subcategoryitems:visible').slideUp(time);
			

			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
		}
		else if(!selfClick) {
			event.preventDefault();
			event.stopPropagation();
			
			var url = $(this).find('a').attr('href');
			
			//alert('I am currently testing the MENU. Disregard this alert. Click OK to go to the directed page. INFO: not self clicked');
			
			$(this)
				.parent()
				.find('>li ul:first:visible')
				.slideToggle(time);

			window.setTimeout(function(){document.location.href=url;}, time); //timeout and waiting until effect is complete
		}


// LEAVE-IN AS COMMENT - FOR TESTING PURPOSES ONLY
// 		$(this)
// 			.parent()
// 			.find('>li ul:visible')
// 			.slideDown(time);
// 			
// 		$(this)
// 			.find('ul:first')
// 			.stop(true, true)
// 			.slideToggle(time);
			
	});	

});
