// +---------------------------------------------------------------------+
// | MEDIAVISIE.NL - MENU                                                |
// +---------------------------------------------------------------------+
// | Copyright © 2010, Mediavisie B.V., Enschede                         |
// |                                                                     |
// | Alle rechten voorbehouden.                                          |
// | Niets uit deze uitgave mag worden gekopieerd, gepubliceerd en/of    |
// | verspreid in welke vorm dan ook, zonder voorafgaande toestemming    |
// | van de uitgever.                                                    |
// +---------------------------------------------------------------------+
(function($){

// mvMenu
$.fn.mvMenu = function(options) {
	var defaults = {
		speed: 500,
		hideDelay: 500,
		method: 2 // 1 = Show/Hide, 2 = FadeIn/FadeOut
	};
	var options = $.extend(defaults, options);
	
	return this.each(function() {
		obj = $(this); // Reference
		var ul = $('ul:first', obj); // Reference to Ul.				  
		var hideTimer; // HideTimeout.
		var visible; // State to remember if it's visible or not.
		
		// TImer functions.
		var clearTimer = function() {
			if (hideTimer != null)
				clearTimeout(hideTimer);
		}
		var startTimer = function() {
			hideTimer = setTimeout(hideUl, options.hideDelay);
		}
		
		// Ul functions.
		var hideUl = function() {
			if (defaults.method == 1) {
				// Hide the ul and remove hover state from a.
				ul.css({'display' : 'none'});

				// Remove hover state from the a.
				$('a.main', ul.parent()).removeClass('over');
			} else if (defaults.method == 2) {
				// Hide the ul and remove hover state from a.
				ul.fadeOut(options.speed, function() { $('a.main', ul.parent()).removeClass('over'); });	
			}
		}
		var showUl = function() {
			if (defaults.method == 1) {
				// Show the ul.
				ul.css({'display' : 'block'});

				// Set hover state on the a.
				$('a.main', ul.parent()).addClass('over');
			} else if (defaults.method == 2) {
				$('a.main', ul.parent()).addClass('over');
				// Show the ul and set hover state on the a.
				ul.fadeIn(options.speed);
			}
		}
		
		// Mouse over for the link.
		obj.hover(
			function() {
				// Show the ul.
				showUl();
				
				// Clear the timer.
				clearTimer();
			},
			function() {
				// Clear the timer.
				clearTimer();
				// Set a new timeout.
				startTimer();
			}
		);
		
		// Mouse over for the UL.
		ul.hover(
			function() {
				// Dispaly the block.
				showUl();
				// Clear the timer.
				clearTimer();
			}, 
			function() {
				// Clear the timer.
				clearTimer();
				// Set a new timeout.		
				startTimer();
			}
		);
	});
};

})(jQuery);

