jQuery(document).ready(function($) {
	
	function megaHoverOver(){
		
		// show effect
		//$(this).find(".sub").stop().slideDown( 1 );
		//$(this).find(".sub").css( 'display', 'block' );
		$(this).find(".sub").stop().fadeIn( 'fast' );
		
		// render cufon on headings again (because it wasn't visible before)
		if (typeof Cufon == 'function') {
			Cufon.replace('#MainMenu h2');
		}
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul.mm-col").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 + 68;
				}
			});
			//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});
			
		}
		//Set height
		subHeight = $(this).find(".sub .mm-sub-middle").height();
		$(this).find(".sub .mm-sub-r").css({'height' : subHeight + 'px'});
		$(this).find(".sub .mm-sub-l").css({'height' : subHeight + 'px'});
		$(this).find(".sub .mm-sub-m").css({'height' : subHeight + 'px'});
		
		$( this ).find( '.sub .mm-sub-bottom' ).css( { 'top': (28 + subHeight ) + 'px' } );
	}
	
	function megaHoverOut(){ 
		//$(this).find(".sub").stop().slideUp('fast');
		$(this).find(".sub").css( 'display', 'none' );
		//$(this).find(".sub").stop().fadeOut('fast');
	}

	// initialize the menu
	$( "ul#MegaMenu li" ).each( function() {
		// add some necessary containers for styling
		if ( $( this ).find( ".sub .mm-sub-middle" ).length <= 0 ) {
			var oSubEl = $( this ).find( ".sub" );
			var sSub = oSubEl.html();
			
			oSubEl.html( '' );
			
			var sHtml = [
				'<div class="mm-sub-top" style="height:28px;line-height:28px;position:absolute;top:0px;left:0px;right:0px;">',
					'<div class="mm-sub-tr">&nbsp;</div>',
					'<div class="mm-sub-tl"></div>',
					'<div class="mm-sub-tm">&nbsp;</div>',
				'</div>',
				'<div style="position:absolute;top:28px;">',
					'<div class="mm-sub-middle" style="position:relative;">',
						'<div class="mm-sub-r"></div>',
						'<div class="mm-sub-l"></div>',
						'<div class="mm-sub-m" style="position:relative; top:0px;">',
							sSub + '<div style="clear:both;"></div>',
						'</div>',
					'</div>',
				'</div>',
				'<div class="mm-sub-bottom" style="position: absolute;bottom:0px;left:0px;right:0px;">',
					'<div class="mm-sub-br"></div>',
					'<div class="mm-sub-bl"></div>',
					'<div class="mm-sub-bm" style="">&nbsp;</div>',
				'</div>',
			].join( '' );
			
			oSubEl.html( sHtml );
		}
		// add a drop down arrow symbol
		
		if ($(this).find(".sub").length !== 0) {
			$(this).children('a:first').css('padding-right','28px').append('<span class="mm-arrow"></span>');
			$(this).find(".sub").css({
				 'display': 'none',
				 'visibility': 'visible'
			});
		}
	});

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

	$("ul#MegaMenu li").hoverIntent(MmConfig);
	

});

