jQuery(function() {

	$('#nav li:has(ul)').each(function() {
		
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 500;
		
		var hideDelayTimer = null;
		
		// tracker
		var beingShown = false;
		var shown = false;
		
		var trigger = $(this);
		var subLeft = parseInt((trigger.outerWidth() / 2) - 75);
		var subnav = $(this).children('ul').css('opacity', 0);
		
		// set the mouseover and mouseout on both element
		$(trigger.get(0)).mouseover(function () {		  	
		  	trigger.addClass('lihover');						// Add hover class		  	
		  	if (hideDelayTimer) clearTimeout(hideDelayTimer);	// stops the hide event if we move from the trigger to the bubble element
		  	if (subnav.is(':animated,:visible')) {		// don't trigger the animation again if we're being shown, or already visible
		    	return;
		  	} else {		    	
			    subnav.css({			// reset position of subnav
			      	top: 24,
			      	left: subLeft,
			      	display: 'block' 	// brings the subnav in to view
			    }) 
		    	.animate({				// now animate it's opacity and position
		      		top: '-=' + distance + 'px',
		      		opacity: 1
		    	}, time, 'easeInQuad');
		  	}
		}).mouseout(function () {
		  	// reset the timer if we get fired again - avoids double animations
		  	if (hideDelayTimer) clearTimeout(hideDelayTimer);
		  
		  	// store the timer so that it can be cleared in the mouseover if required
		  	hideDelayTimer = setTimeout(function () {
		    	hideDelayTimer = null;
		    	subnav.animate({
		      		top: '-=' + distance + 'px',
		      		opacity: 0
		    	}, time, 'easeOutQuad', function () {
		      		// once the animate is complete, set the tracker variables
		      		shown = false;
		      		// hide the subnav entirely after the effect (opacity alone doesn't do the job)
		      		subnav.css('display', 'none');
					trigger.removeClass('lihover');
		    	});
		  	}, hideDelay);
		});
	});
	
	$('.menu-nav a').live('click', function() {

		$.blockUI({ message: null });
		$('.menu-nav a').removeClass('menu-nav-selected');
		$(this).addClass('menu-nav-selected');
		
		var item = $(this).attr('href').split('#');
		var section = item[1];
		var page = $(this).parents('.menu-wrapper').attr('id');
		$("div.menuarea").load("/menu/"+page+".php #"+section, { a: 1 }, function() {
			$('#'+section).show();
			$.unblockUI();
		}).show();
		return false;
	});		
	
});