function calculation(o) {
        var location = $(o).offset();

        var top = location.top ;

        var left = location.left;

        $("#menurolloverbg").css({
            'position': 'absolute',
            'top': top + 2+ 'px',
            'left': left + 5 + 'px'
        });
    }


$(document).ready(function() {
var overcount = 0;
	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(); 
				});	
			};
		})(jQuery); 

		if ( $(this).find(".row").length > 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
			$(this).find(".sub").css({'width' : rowWidth});

		}
		if ($(this).width() < 100)
		{
			$('#menurolloverbg').css({width: $(this).width() + 20});
			calculation(this);
		}
		$('#menurolloverbg').show();
		overcount++;
	}

	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		 $(this).hide(); 
		
	  });
	 // setTimeout($('#menurolloverbg').hide(), 100000);
	  overcount--;
		 if (overcount == 0)
		 	$('#menurolloverbg').hide();
	}

	var config = {    
		 sensitivity: 2,		// 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)    
	};
	
	// Center the subnav under primary nav item
	$("ul#nav li").mouseover(function() {
		var the_width = $(this).find("a").width();
		var child_width = $(this).find("div").width();
		var width = parseInt((child_width - the_width)/2);
		$(this).find("div").css('left', -width);
	});

	$("ul#nav li .sub").css({'opacity':'0', 'z-index':'-100'});
	$("ul#nav li").hoverIntent(config);

});
