$(document).ready(function() {

	//On Hover Over
	function megaHoverOver(){
		$(this).find(".massivedropdown").stop().delay('50').fadeTo('fast', 1).show(); //Find massive dropdown and fade it in
		(function($) {
			//Function to calculate total width of all ul's
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				rowCount = 0;
				//Calculate row
				rowCount = $(this).find("ul.level2").each(function() { //for each ul...
					rowWidth += $(this).width(); //Add each ul's width together
				}).size();
			};
		})(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
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});

			$(this).find(".massivedropdown").css({'width' :biggestRow}); //Set width
			$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
			if(rowCount > 4){
			    //var offset = -((rowCount - 4) * biggestRow);
			    //$(this).find(".massivedropdown").css({'left': offset+'px'});
			    var dd = $(this).find(".massivedropdown");
			    var left = $(widow).width()/2 - dd.width()/2;
			    dd.css({'left': left+'px'})
			}

		} else { //If row does not exist...

			$(this).calcSubWidth();  //Call function to calculate width of all ul's
			$(this).find(".massivedropdown").css({'width' : rowWidth}); //Set Width
			if(rowCount > 4){
			    //var offset = -(rowWidth/rowCount) * (rowCount - 4);
			    //$(this).find(".massivedropdown").css({'left': offset+'px'});
			    var dd = $(this).find(".massivedropdown");			    
			    var left = $(window).width()/2 - rowWidth/2;
			    if(!dd.data('offset')){
				dd.data('offset', dd.offset().left > left ? left - dd.offset().left : left);
			    }
			    dd.css({'left': dd.data('offset')+'px'})
			}
		}
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".massivedropdown").stop(true, false).fadeTo('fast', 0, function() { //Fade to 0 opactiy
		  $(this).hide();  //after fading, hide it
		  $(this).parent().hoverIntent(config);
	  }).end().unbind();
	}

	
	//Set custom configurations
	var config = {
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		 interval: 60, // 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#menu li .massivedropdown").css('display', 'block');
	
	$("ul#menu li .massivedropdown").css({'opacity':'0', 'display': 'none'}); //Fade sub nav to 0 opacity on default
	$("ul#menu li").hoverIntent(config); //Trigger Hover intent with custom configurations
	$("ul#menu li").mouseleave(megaHoverOut)
	$("ul#menu li a").click(function(event){
	    var mouseFromRightEdge = $(this).parent().offset().left + $(this).parent().width() - event.pageX;
	    if(mouseFromRightEdge < 19 && $(this).parent().hasClass('withsubitems')){
		if($(this).siblings('.massivedropdown:hidden').size() > 0){
		    megaHoverOver.call($(this).parent());
		}else if($(this).siblings('.massivedropdown:not(:animated)').size() > 0){
		    megaHoverOut.call($(this).parent());
		}
		return false;
	    }else{
		$(this).parent().unbind().find(".massivedropdown").stop(true, false).hide();
		location.href = $(this).attr('href');
	    }
	});


});
