// JavaScript Document
$(document).ready(function() {
 
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 100).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});
			
		}
	}
			
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
	
	
	// -- OPIEDIT - Set menu item images -- //
	
	setMenuMouseEvents("#menucars","#menucarssub","/images/global/megadropdown/cars.gif","/images/global/megadropdown/cars-roll.gif");
	setMenuMouseEvents("#menubikes","#menubikessub","/images/global/megadropdown/bikes.gif","/images/global/megadropdown/bikes-roll.gif");
	setMenuMouseEvents("#menuoils","#menuoilssub","/images/global/megadropdown/oils.gif","/images/global/megadropdown/oils-roll.gif");
	setMenuMouseEvents("#menufilters","#menufilterssub","/images/global/megadropdown/filters.gif","/images/global/megadropdown/filters-roll.gif");
	setMenuMouseEvents("#menudetailing","#menudetailingsub","/images/global/megadropdown/detailing.gif","/images/global/megadropdown/detailing-roll.gif");
	setMenuMouseEvents("#menuplugs","#menuplugssub","/images/global/megadropdown/plugs.gif","/images/global/megadropdown/plugs-roll.gif");
	setMenuMouseEvents("#menuaccessories","#menuaccessoriessub","/images/global/megadropdown/accessories.gif","/images/global/megadropdown/accessories-roll.gif");
	setMenuMouseEvents("#menushop-by-brand","#menushop-by-brandsub","/images/global/megadropdown/shop-by-brand.gif","/images/global/megadropdown/shop-by-brand-roll.gif");
	setMenuMouseEvents("#menuclearance","#menuclearancesub","/images/global/megadropdown/clearance.gif","/images/global/megadropdown/clearance-roll.gif");

	// -- OPIEDIT END - Set menu item images -- //

	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 120, // 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#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
 
 	// -- OPIEDIT - Set menu item images -- //

 	$('#topnav').mouseout(function() {		
 	  $("#menucars").css('background-image','url(/images/global/megadropdown/cars.gif)');
	  $("#menubikes").css('background-image','url(/images/global/megadropdown/bikes.gif)');
	  $("#menuoils").css('background-image','url(/images/global/megadropdown/oils.gif)');
	  $("#menufilters").css('background-image','url(/images/global/megadropdown/filters.gif)');	
	  $("#menudetailing").css('background-image','url(/images/global/megadropdown/detailing.gif)');
	  $("#menuplugs").css('background-image','url(/images/global/megadropdown/plugs.gif)');
	  $("#menuaccessories").css('background-image','url(/images/global/megadropdown/accessories.gif)');
	  $("#menushop-by-brand").css('background-image','url(/images/global/megadropdown/shop-by-brand.gif)');
	  $("#menuclearance").css('background-image','url(/images/global/megadropdown/clearance.gif)');
	});

	// -- OPIEDIT END - Set menu item images -- //
	
  function setMenuMouseEvents(menuId, subMenuId, imageNormal, imageRollover) {
	  
	$(menuId).mouseover(function() {
	    $(menuId).stop().fadeTo('fast', 100, function() {
			$(menuId).css('background-image','url(' + imageRollover + ')');
		});
	});				

	$(subMenuId).mouseover(function() {
		$(menuId).stop().fadeTo('fast', 10, function() {
			$(menuId).css('background-image','url(' + imageRollover + ')');
		});
	});				
 
 	$(menuId).mouseout(function() {
		$(menuId).stop().fadeTo('fast', 100, function() {
			$(menuId).css('background-image','url(' + imageNormal + ')');
		});	
	});	
	
	$(subMenuId).mouseout(function() {
		$(menuId).stop().fadeTo('fast', 10, function() {
			$(menuId).css('background-image','url(' + imageNormal + ')');
		});
	});	
	
  }
  
  });
