////////////////////////////////////
///////  FLYOUT - MODE:WRAP  /////// 
////////////////////////////////////


$(document).ready(function() {
	
	// create variables
	var fTimer;
	var fOpen;
	
	var triggers = $(fly_location + ' .flyout_trigger');
			triggers.show();
	

	// Sidebar Menus Only
	if(fly_location == "#aside_primary") {
		
		if(fly_expand != '') {
			// Select menu to expand
			$(fly_location + " li." + fly_expand).addClass('selected');
			
			// Select link to highlight within that menu
			if(fly_expanded_link != '') {
				$(fly_location + " li." + fly_expand + " ul li." + fly_expanded_link + "> a").addClass('current');
			}	
		}
		
		// Expand menu chosen above
		var selected_sub_menu = $(fly_location + " li.selected .flyout_trigger").next('ul');
		selected_sub_menu.show();
		
		// Click handling for expand/contract
		$(fly_location + " li.hassub .flyout_trigger a").click(function() {
			var fl = $(this).parents(".flyout_trigger");
					fl.next('ul').toggle();
			
			// For +/- styling
			if(fl.is('.expanded')) {
				fl.removeClass('expanded');
			} else {
				fl.addClass('expanded');
			}
			
			if($(this).is('.expanded')) {
				$(this).removeClass('expanded');
			} else {
				$(this).addClass('expanded');
			}
			
			return false; 
		});
		
		// Click handling for expand/contract
		var third_level = $(fly_location + " li.hassub ul.level3");
		if(third_level) {
			
			var third_trigger = third_level.prev("a");
			third_trigger.addClass('arrowclosed');
			
			third_trigger.click(function() {
				
				// For +/- styling
				if($(this).is('.arrowclosed')) {
					$(this).addClass('arrowopen');
					$(this).removeClass('arrowclosed');	
				} else {
					$(this).addClass('arrowclosed');
					$(this).removeClass('arrowopen');
				}
				
			    return false; 
            });
		} // end third level
				
		// if link is selected on load, make sure to show it's lil kids
		var contains_third_level = $(fly_location + " li.hassub ul li.selected a").next("ul.level3");
		if(contains_third_level !== '') {
			contains_third_level.prev('a').removeClass('arrowclosed').addClass('arrowopen');
		}
		
	} // end aside-primary
	
								
	// Loop through each trigger
	triggers.each(function(index) {
		var tr = $(this);
		
		var tf = tr.next('.flyout');
				tf.clone().appendTo(tr);
				tf.empty();	
		
		var tn = tr.children('.flyout');		
				tn.css("position","absolute");
				tn.css("left","5px");
				tn.css("top","25px");
	});
		
	// to make sure that when user mouses over sub menu ul it stays open
	$('.flyout').mouseover(function() {
	 $(this).show();
	 // lets remember what's open
	 fOpen = $(this).parent('.flyout_trigger');
	});

	// when user mouses over main item in navbar
	$(".flyout_trigger").mouseover(function() {
	 
	 // close other nav item submenus
	 if (fOpen != null) {
	 	fOpen.children(".flyout").hide();
	 }
	 
	 // stop the timer
	 clearTimeout(fTimer);
	 
	 // show this nav item's sub menu
	 if ($(this) != fOpen) {
	 	$(this).children(".flyout:hidden").show();
	 }
	 
	});

	// when user's mouse leaves the navbar item
	$(".flyout_trigger").mouseout(function() {
	 
	 	// lets keep tabs of what is open
	  fOpen = $(this);
	 	
	 	// start the timer for 2 seconds until it closes.
	  fTimer = setTimeout(fCloser, 1000);
	});	

	function fCloser() {
		fOpen.children(".flyout:visible").hide();
	}
});


