$(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;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
					//alert(rowWidth);
				});	
			};
		})(jQuery); 
		
		if (0) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			$(this).calcSubWidth();
			//Set Width
var o = $(this).find('.sub').offset();
//alert(o);
var p = $('#container').offset();
//alert(p);
var pw = document.getElementById('container').clientWidth;
var tw = rowWidth;
//o.left = o.left - p.left;
//var tml = (tw + o.left) - pw;
var ml = 0;

var cs = $(this).find('.sub').attr('id');
//alert(cs);

if (rowWidth == '352') {
	ml = '289';
}
//$(this).find(".sub").css({'width' : rowWidth}).css({'margin-left':'-' + ml + 'px'});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  //$(this).css({'margin-left':'0'});
		  $(this).parent().find('a').removeClass('nav-on');
		  $(this).hide(); 
	  });
	  //document.getElementById('newsletter_nav').style.backgroundPosition='top center';
	}

	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 20, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 50, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'1'});
	$("ul#topnav li").hoverIntent(config);
	$("ul#topnav li").hover(function(){
		var test = $(this).find('.sub').attr('class');
		if(test == 'sub') {
			$('a',this).addClass('nav-on');
		} else {
			
		}
	},function() {
			$("ul#topnav a").removeClass('nav-on');
	});



});

