/*
Developed By: James Thomson
Company: Red Label Communications Ltd.
Date: 04/2010
Version: 1.0
*/

(function($){
	$.fn.dropmenu = function(options) {
		//Declare default settings
		var defaults = {
			animate: true, // Sets the speed of each transition
			animSpeed: 200, // Sets the drop animation speed
			delay: 300 // Sets the delay speed after hovering off the menu
		},
		settings = $.extend({}, defaults, options); //Take options, compare and replace over matched defaults, merge into new array to create settings.
		
		
		this.each(function(){
			var $this = $(this),	
				$x = $this.find('div'), //Find ul within THIS
				$h = $x.outerHeight(), // Get height (incl. padding, margin, and border) of THIS ul
				$w = ($x.outerWidth()+20 > 360 ? 360 : $x.outerWidth()+20), // Get width (incl. padding, margin, and border) of THIS ul
				to  = null; 			
			$x.css({'display': 'none', 'position': 'absolute', 'z-index': '1000', 'height': '0px'}); //Hide all drop menu's onload
			
			function _mouseOver(){
				if(settings.animate){
					$x.css({'display': 'block'}).stop().animate({height: $h, width: $w}, settings.animSpeed);
				}
				else{
					$x.css({'display': 'block', 'height': $h});
				}
			}
			
			function _mouseOut(){
				if(settings.animate){
					$x.stop().animate({height: 0}, 200, function(){
						$x.css({'display': 'none'});
					});
				}
				else {
					$x.css('display','none');
				}	
			}
			
			$this.hover(function(){
				clearTimeout(to);
				_mouseOver();
			}, function(){
				to = setTimeout(_mouseOut, settings.delay);		
			});
		});
		return this;
	}	
})(jQuery);
