(function($){
	$.fn.hovermenu = function(options) {
		var defaults = {
			timeout: 0750
		};
		
		var settings = $.extend({}, defaults, options),
			isActive = false,
			submenus = $(this),
			ourTimer;
			
		return this.each(function(){
								  
			var $this = $(this);
								  
			var anchor = $this.parent().find('a');
			anchor.hover(
				function(){
					showhide(true);
				},
				function(){
					showhide(false);
				}
			);
			
			$this.hide().hover(
				function(){
					layerCheck(true);
				},
				function(){
					layerCheck(false);
				}
			);
			
			function showhide(onlink)
			{			
				
				if(onlink)
				{
					clearTimeout(ourTimer);
					
					submenus.hide();
					
					$this.show();
					isActive = true;
				}
				else
				{
					isActive = false;
					layerTimer();
				}
			}
			
			function layerTimer()
			{
				ourTimer = setTimeout(function(){
											   
					if( !isActive )
						$this.hide();
						
				},settings.timeout);
			}
			
			function layerCheck(flag)
			{
				if(flag)
				{
					isActive = true;
					clearTimeout(ourTimer);
				}
				else
				{
					isActive = false;
					layerTimer();
				}
			}
			
		});
		
	};
})(jQuery);
