
// Define and register .delay() plugin with jQuery
jQuery.fn.delay = function(time, callback)
{
    jQuery.fx.step.delay = function() {};
    return this.animate({delay:1}, time, callback);
};


//-------------------------------------------------------------------------------------

// Define and register my own, awesome .vacate() plugin
(function($) {
	$.fn.vacate = function(callback)
	{
		var docBody = $('body');
		
		return this.each(function() {
			
			// Initialise common variables
			var $this = $(this);
			var increment, decrement, check;
			var hoverCount = 0;
			
			// Function to check current vacation counter
			// Will clear event handlers and execute callback
			check = function()
			{
				if (hoverCount < 1)
					return;
				$this.unbind('mouseenter', decrement);
				$this.unbind('mouseleave', increment);
				docBody.unbind('mouseenter', increment);
				callback();
			};
			
			// Increment vacation counter
			increment = function()
			{
				setTimeout(check, 1);
				++hoverCount;
			};
			
			// Decrement vacation counter
			decrement = function()
			{
				--hoverCount;
			};
			
			// Bind vacation events to body and chosen element
			$this.bind('mouseenter', decrement);
			$this.bind('mouseleave', increment);
			docBody.bind('mouseenter', increment);
			
		});
	};
})(jQuery);


//-------------------------------------------------------------------------------------

// When document is ready for processing...
$(document).ready(function() {
	
	// Alias main-menu list element
	var mainMenu = $('ul.mainMenu');
	
	
	//-------------------------------------------------------------------------------------
	
	// Apply event listeners to slide/unslide the mega-menu
	mainMenu.vacate(function() {
		
		mainMenu.parent().hoverIntent({
			
			over: function() {
				$('#layoutMegaMenu').slideDown(240);
			},
			
			out: function() {
				$('#layoutMegaMenu').slideUp(240);
			},
			
			interval: 100,
			timeout: 120
			
		});
		
	});
	
	
	//-------------------------------------------------------------------------------------
	
	// Apply event listeners to change mega-menu content
	mainMenu.children('li').each(
		function(index) {
			
			// Whenever the mouse hovers over an item on the main menu...
			$(this).hoverIntent({
				over: function() {
					var megaMenu = $('#layoutMegaMenu');
					megaMenu.empty();
					megaMenu.append(
						$(this).find('div.megaMenu').first().clone()
					);
				},
				out: jQuery.noop,
				interval: 20
			});
			
		}
	);
	
	
	//-------------------------------------------------------------------------------------
	
	// Alias news-ticker elements
	var ticker = $('#layoutNewsTicker');
	var marquee = ticker.find('p').first();
	
	// Pre-build ticker content switching and animation function
	var TickerLoop = function()
	{
		var ticker = $(this);
		
		var nextInd = ticker.data('nextInd');
		var headlines = ticker.data('headlines');
		if (nextInd >= headlines.length)
			nextInd = 0;
		ticker.find('a').first().replaceWith(
			headlines.eq(nextInd).clone()
		);
		ticker.data('nextInd', nextInd+1);
		
		ticker.
			animate({opacity:1.0}, 300, null).
			delay(4000).
			animate({opacity:0.0}, 300, null, ticker.data('loopFunc'));
	};
	
	// Configure and begin animating ticker marquee
	marquee.data('nextInd', 1);
	marquee.data('loopFunc', TickerLoop);
	marquee.data('headlines', ticker.find('ul.hide > li > a'));
	marquee.delay(4000).animate({opacity:0.0}, 300, null, TickerLoop);
	
	
	//-------------------------------------------------------------------------------------
	
	$('.js-Hide').css('display', 'none');
	$('.js-Show').removeClass('js-Show');
	
});

