$(document).ready(function() {
 	
	function megaHoverOver(){
		var $subDiv = $(this).find("#submenu");
		var $anchor = $(this).find('a').first();
		$subDiv.stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				var menuWidth = $anchor.width();					
				rowWidth = $subDiv.width();
				if(rowWidth > 0){
					$subDiv.css({'border-top-left-radius':'0'});
					$anchor.css({'border-bottom-left-radius':'0','border-bottom-right-radius':'0','-moz-box-shadow':'0px 2px 3px 0px #6d6d6d', '-webkit-box-shadow':'0px 2px 3px 0px #6d6d6d', 'box-shadow':'0px 2px 3px 0px #6d6d6d'});
				}
				if(rowWidth < menuWidth){
					rowWidth = menuWidth + 24; //compensate for padding
					$subDiv.css({'border-top-right-radius':'0'});
				}				
			};
			jQuery.fn.calcLeft = function() {
				offset = $(this).offset().left + 5;
				header_offset = $('#header').offset().left;
				offset = offset - header_offset;				
			};
		})(jQuery); 
		
		$(this).calcSubWidth();
		$(this).calcLeft();
		//Set Width
		$anchor.first().animate({backgroundColor: "#72bd36", color: "#FFF"}, 300);
		$subDiv.css({'width':rowWidth,'left':offset});
	}
	
	function megaHoverOut(){ 
		var $subDiv = $(this).find("#submenu");
		var $anchor = $(this).find('a').first();
	  	$subDiv.stop().fadeTo('fast', 0, function() {
		 	$(this).hide(); 
	  	});
	  	$anchor.animate({backgroundColor: "#fefffb", color: "#5a5a59"}, 300)
	  	.css({'-moz-box-shadow':'0px 0px 0px 0px #fefffb', '-webkit-box-shadow':'0px 0px 0px 0px #fefffb', 'box-shadow':'0px 0px 0px 0px #fefffb'});
	}
 
 
	var config = {    
		 sensitivity: 50, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 20, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 150, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul#nav_dropdownUL li #submenu").css({'opacity':'0'});
	$("ul#nav_dropdownUL li").hoverIntent(config);
	 
});
