
(function($) {

	$.fn.navDrop = function() {

		return this.each(function() {

			$(this).children('li').children('ul').addClass('opened');

			var to;
			var $shutDown = null;
			var $duration = null;
			$(this).children('li').children('a').mouseenter(function() {
				
				//alert("Entered <a>");
				
				clearTimeout(to);
				$('ul.opened').hide().siblings('a').removeClass('over');

				$(this).addClass('over');
				$shutDown = $(this).siblings('ul').show();
			}).mouseleave(function() {
				$shutDown = $(this).siblings('ul.opened');
				
				if ( $(this).siblings('ul').hasClass("dropdown") ) {
					$duration = 500;
				} else {
					$duration = 0;	
				}
				
				to = setTimeout(function() {
					closeme($shutDown);
				}, $duration);
			});

			
			$('ul.opened').mouseenter(function() {
				//alert('Entered the UL');
				clearTimeout(to);
			}).mouseleave(function() {
				$shutDown = $(this);
				
				//alert("Left the UL")
				
				if ( $(this).siblings('a').hasClass('over') ) {
					//alert('oh boy');
				} else {
					//alert('no boy');
				}
				
				to = setTimeout(function() {
					closeme($shutDown);
				}, 500);
				
				
			});
			

		});

	};
	
	function closeme(o) {
		o.hide();
		//o.removeClass('over');
		o.siblings('a').removeClass('over');
	}

})(jQuery);


$(document).ready( function() {

	$('ul#mainNav_ul').navDrop();

} );