var $j = jQuery.noConflict();
$j(document).ready(function() {


	function megaHoverOver(){
		$j(this).addClass('hover');
		$j(this).find(".sub").stop().fadeTo('fast', 1).show();
		
		//Calculate width of all ul's
		(function($j) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				var nestedUL = $j(this).find("ul ul").length; // determines if there are nested lists
				
				var	col = null;
				if (nestedUL) { col = $j(this).find("ul:first").children("li"); }
				else { col = $j(this).find("ul"); }				
				$j(col).each(function() {
					rowWidth += $j(this).width();
					if (nestedUL) { 
						rowWidth += 40; // include padding for each row
						$j(this).addClass('column');
					}
				});
				
				if (nestedUL) $j(this).equalHeight();
			};
		})(jQuery);
		
		//Equal height columns
		(function($j) {
			jQuery.fn.equalHeight = function() {
				var tallest = 0;
				var col = $j(this).find("ul:first").children("li");
				$j(col).each(function() {
					var thisHeight = $j(this).height();
					if(thisHeight > tallest) {
						tallest = thisHeight;
					}
				});
				$j(col).height(tallest);
			};
		})(jQuery);
		
		if ( $j(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$j(this).find(".row").each(function() {							   
				$j(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$j(this).find(".sub").css({'width' :biggestRow});
			$j(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$j(this).calcSubWidth();
			//Set Width
			$j(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $j(this).removeClass('hover');
	  $j(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $j(this).hide(); 
	  });
	}


	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: 100, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$j("ul#topnav li .sub").css({'opacity':'0'});
	$j("ul#topnav").children("li").hoverIntent(config);

	// add "no-children" class
	$j("ul#topnav li").each(function() {
		if (!$j(this).find("li").length) $j(this).find("a").addClass("no-children");
	}); 
	
	// add rows
	var count = 3; // should = num of columns
	$j('ul#topnav .sub:eq(2)').find('ul:first').contents().unwrap(); // removes outermost ul from the 3rd sub menu (2 = 3)
	$j('ul#topnav .sub:eq(2)').find('li').each(function() { // loops through lis in 3rd sub menu
		var li = $j(this);		
		if ($j(li).find('h4').length) { // only top li's have h4 as child
			if (count % 3 == 0) { // 3 columns per row
				var nextsibling = $j(li).next();
				var nextsibling2 = $j(li).next().next();
				$j(li).wrap('<div class="row" />').wrap('<ul />'); // wrap the li in div and ul
				$j(nextsibling).appendTo($j(li).parent('ul')); // grap the next child
				$j(nextsibling2).appendTo($j(li).parent('ul')); // grap the third child
			}
			count++;
		}
	});
	
});



