$(document).ready(function() {
 
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				var num_col=0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); //function (jquery api) returns element width minus padding and margin
					num_col++;
				});
				rowWidth += 10*num_col;	//add column padding
			};
			jQuery.fn.calcLeft = function() {
				offset = $(this).offset().left - 35;
				headerOffset = $("#header").offset();
				headerRightSide = headerOffset.left + $("#header").width()+12;
				if((offset+rowWidth)>headerRightSide){
					offset -= (offset+rowWidth) - headerRightSide;
				}
				
				
			};
		})(jQuery); 
		
		$(this).calcSubWidth();
		$(this).calcLeft();
		//Set Width
		
		$(this).find(".sub").css({'width' : rowWidth, 'left':offset});
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
 
 
	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 50, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 300, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul#nav_dropdownUL li .sub").css({'opacity':'0'});
	$("ul#nav_dropdownUL li").hoverIntent(config);
	
	//Change span color
	$(".nav_item_ministry").hover(
  			function () {
   		 		$(this).find('.green_medium').css({'color':'#fff'});
  			}, 
  			function () {
   				$(this).find('.green_medium').css({'color':'#6ba227'});
  			}
	);
			 
 
});
