/*
 * jQuery Drop Down panel menu v.1
 *
 * Copyright (c) 2011 Pieter Pareit
 *
 * http://www.scriptbreaker.com
 *
 */

//plugin definition
(function($){
	$.fn.extend({

		//pass the options variable to the function
		dropDownPanels: function(options) {

			var defaults = {
				speed: 250,
				resetTimer: 1000
			};

			var hovering;

			// Extend our default options with those provided.
			var opts = $.extend(defaults, options);
			//Assign current element to variable, in this case is UL element
			var $this = $(this);

			var closetimer;

			function resetMenu(){
				$this.find(".hover").removeClass("hover");
				$this.find(".submenu:visible").slideUp(opts.speed);
			}

			function activateTimer(){
				if(!hovering) {
					closetimer = window.setTimeout(resetMenu, opts.resetTimer);
				}
			}

			function cancelTimer(){
				if(closetimer){
					window.clearTimeout(closetimer);
					closetimer = null;
				}
			}

			$this.find(">li").mouseenter(function(){
				hovering = true;
				cancelTimer();
			}).mouseleave(function(){
				hovering = false;
				activateTimer();
			});

			$this.find(">li a").hover(function() {
				parentLi = $(this).parent();
				cancelTimer();
				if(!parentLi.find(".submenu").is(":visible")){
					parentLi.parent().find(".hover").removeClass("hover");
					parentLi.parent().find(".submenu:visible").hide();
					if(parentLi.has(".submenu")){
						parentLi.find(".submenu").slideDown(opts.speed);
						parentLi.find("a:first").addClass("hover");
					}
				}
			}, function(){
				activateTimer();
			});
		}
	});
})(jQuery);
