// Megamenu

$(document).ready(function() {

	function megaHoverOver() {
	
		// Find sub and show it
    	$(this).find(".sub").show();

		// Function to calculate total width of all ul's
    	(function($) {
        	jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
            	//Calculate row
            	$(this).find("ul").each(function() { //for each ul...
        			rowWidth += $(this).width(); //Add each ul's width together
            	});
        	};
    	
		})(jQuery); 

		if ($(this).find(".row").length > 0 ) { // If row exists...

			var biggestRow = 0;

	        $(this).find(".row").each(function() {	// for each row...
	            $(this).calcSubWidth(); // Call function to calculate width of all ul's
	            if (rowWidth > biggestRow) { biggestRow = rowWidth; }
	        });
			
	        $(this).find(".sub").css({'width' :biggestRow}); // Set width
	        $(this).find(".row:last").css({'margin':'0'});  // Kill last row's margin

    	} else { // If row does not exist...
	
			$(this).calcSubWidth();  // Call function to calculate width of all ul's
			$(this).find(".sub").css({'width' : rowWidth}); // Set Width
		}
    
		var mypos = $(this).offset();
		
		// 2012-01-04: MES Added to keep careers from spilling right on wide monitors.
		var myboundary = mypos.left + $(this).find('.sub').width();
		var pageboundary = $('#globalHdr').width() + $('#globalHdr').offset().left; // Compares with page right instead of window right.
 
		if( myboundary > $(window).width() || myboundary > pageboundary ) { // If the nav dropdown exceeds the right boundary of the screen,
			var newleft = -1 * $(this).find(".sub").width() + $(this).find('a').first().width() - 34; // change this constant to move the mega-dropdown left or right
			$(this).find(".sub").css({'left' :newleft}); // have the right of the dropdown match the right of the navigation item
		}

	}

	// On Hover Out
	function megaHoverOut(){
		//$(this).find(".sub").stop().hide();
		$(this).find(".sub").hide();
		$(this).find(".sub").stop();
	}

	// Set custom configurations
	var config = {
		sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
		interval: 5, // number = milliseconds for onMouseOver polling interval
		timeout: 0, // number = milliseconds delay before onMouseOut
		over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	// Hover "buffer" to delay rollover menu show/hide
	$("ul#topnav li").hoverIntent(config); // Trigger Hover intent with custom configurations

});

/* END NAVIGATION CODE */
