$(document).ready(function() {
	$('.slideshow').css("display", "block");	   
    $('.slideshow').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		random: 1
	});
	$("#partsRequest").validate();

		function megaHoverOver(){
	  $(this).find("#homenb").addClass('currentmenu');
	  $(this).find("#ccancnb").addClass('currentmenu');
	  $(this).find("#productsnb").addClass('currentmenu');
	  $(this).find("#drapernb").addClass('currentmenu');
	  $(this).find("#vehiclecomponentsnb").addClass('currentmenu');
	  $(this).find("#vehiclesnb").addClass('currentmenu');
			
		$(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});
			//$(this).find(".sub").css({'width' : 650});
			$(this).find("#ccancsub").css({'width' : 640});
			$(this).find("#drapersub").css({'width' : 540});
			$(this).find("#componentssub").css({'width' : 790});
			$(this).find("#vehiclesub").css({'width' : 790});
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find("#homenb").removeClass('currentmenu');
	  $(this).find("#ccancnb").removeClass('currentmenu');
	  $(this).find("#drapernb").removeClass('currentmenu');
	  $(this).find("#vehiclecomponentsnb").removeClass('currentmenu');
	  $(this).find("#vehiclesnb").removeClass('currentmenu');
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


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

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

			if($.browser.msie){
				$("#viewport").css("position","relative"); 
				$("#viewport ul").css("position","relative"); 
				$("#viewpoint").mouseover(function() {
						 $("#viewport").css("position","absolute"); 
						 $("#viewport ul").css("position","static"); 
				});
				$("#viewpoint").mouseout(function() {
						 $("#viewport").css("position","relative"); 
						 $("#viewport ul").css("position","relative"); 
				});
			}
 
	 $("a.prestone-video").click(function(){
			window.open(this.href, "Prestone", "width=720,height=375");
			return false;
		});
	 $("a.banner-batteries").click(function(){
			window.open(this.href, "BannerBatteries", "width=890,height=375");
			return false;
		});
	 $("a.ringmovie").click(function(){
			window.open(this.href, "RingChargerMovie", "width=710,height=315");
			return false;
		});
	 
	  $("#moreImages img.i1").click(function(){
			var t = $("#moreImages img.i1").attr('alt');
			$("#mainImage").attr('src', 'http://images.thehappymotorist.co.uk/product-images/big/'+t);
		});
	  $("#moreImages img.i2").click(function(){
			var t = $("#moreImages img.i2").attr('alt');
			$("#mainImage").attr('src', 'http://images.thehappymotorist.co.uk/product-images/big/'+t);
		});
	  $("#moreImages img.i3").click(function(){
			var t = $("#moreImages img.i3").attr('alt');
			$("#mainImage").attr('src', 'http://images.thehappymotorist.co.uk/product-images/big/'+t);
		});
		
	  $('ul#topnav .sub ul li:even').removeClass('newColour');
	  $('ul#topnav .sub ul li:even').removeClass('newColour2');
	  $('ul#topnav .sub ul li:even').removeClass('newColour3');
	  $('ul#topnav .sub ul li:even').addClass('stripe');

});

