/* ---------------------------------------------------- */
/* JQuery												*/
/* ---------------------------------------------------- */
$(document).ready(function() 
{
	
	/* ---------------------------------------------------- */
	/* Image preloader									    */
	/* ---------------------------------------------------- */
	
	/* Preload the images for the rotation, to avoid flickering as they load */
	$jQuery.preloadImages = function()
	{
		for(var i = 0; i<arguments.length; i++)
		{
			$("<img>").attr("src", arguments[i]);
		}
	}
	$jQuery.preloadImages(
						"images/vw_used_car_of_week181.jpg", 
						"images/vw_used_car_of_week182.jpg", 
						"images/vw_used_car_of_week183.jpg", 
						"images/audi_used_car_of_week84.jpg",
						"images/audi_used_car_of_week85.jpg",
						"images/audi_used_car_of_week86.jpg",
						"images/skoda_used_car_of_week25.jpg",
						"images/skoda_used_car_of_week26.jpg",
						"images/skoda_used_car_of_week27.jpg"
					);

	/* ---------------------------------------------------- */
	/* Does the FadIn and fadeOut on Images 	    */
	/* ---------------------------------------------------- */
	$jQuery("#home_box1").hoverShade();
	$jQuery("#home_box2").hoverShade();
	$jQuery("#home_box3").hoverShade();
	
	
	/* ---------------------------------------------------- */
	/* Used cars of the week								*/
	/* ---------------------------------------------------- */
	/* specify the time time delay between the image fades */
	var time_delay = 3000;	
	/* run the VW image changing function, passing in the time delay, the image number to change to, the first image number and the last image number */
	change_vw_image(time_delay, 181, 182, 183);
	/* run the Audi image changing function, passing in the time delay, the image number to, the first image number and the last image number */
	change_audi_image(time_delay, 84, 85, 86);
	/* run the Skoda image changing function, passing in the time delay, the image number to, the first image number and the last image number */
	change_skoda_image(time_delay, 25, 26, 27);

});


/* ---------------------------------------------------- */
/* A recursive function which cycles through images     */
/* ---------------------------------------------------- */
function change_vw_image(time_delay, first_image_number, img_num, maximum_image_number) {
	
	window.setTimeout(function() {
		$jQuery("img#vw_car_image").fadeOut('slow', function() {
			$jQuery("img#vw_car_image").attr( "src", "images/vw_used_car_of_week" + img_num + ".jpg" );
			$jQuery("img#vw_car_image").fadeIn('slow');
		
			/* if the current image processed is not the final image, then add one to it and continue, else reset it to the 1st vehicle number (ie start again) */
			if (img_num != maximum_image_number) { img_num = img_num + 1; } else { img_num = first_image_number;}
			/* run the function recursively with the next image */
			change_vw_image(time_delay, first_image_number, img_num, maximum_image_number);
			return;		
		});
	}, time_delay);
	
}

/* ---------------------------------------------------- */
/* A recursive function which cycles through images     */
/* ---------------------------------------------------- */
function change_audi_image(time_delay, first_image_number, img_num, maximum_image_number) {
	
	window.setTimeout(function() {
		$jQuery("img#audi_car_image").fadeOut('slow', function() {
			$jQuery("img#audi_car_image").attr( "src", "images/audi_used_car_of_week" + img_num + ".jpg" );
			$jQuery("img#audi_car_image").fadeIn('slow');
		
			/* if the current image processed is not the final image, then add one to it and continue, else reset it to the 1st vehicle number (ie start again) */
			if (img_num != maximum_image_number) { img_num = img_num + 1; } else { img_num = first_image_number;}
			/* run the function recursively with the next image */
			change_audi_image(time_delay, first_image_number, img_num, maximum_image_number);
			return;		
		});
	}, time_delay);
	
}

/* ---------------------------------------------------- */
/* A recursive function which cycles through images     */
/* ---------------------------------------------------- */
function change_skoda_image(time_delay, first_image_number, img_num, maximum_image_number) {
	
	window.setTimeout(function() {
		$jQuery("img#skoda_car_image").fadeOut('slow', function() {
			$jQuery("img#skoda_car_image").attr( "src", "images/skoda_used_car_of_week" + img_num + ".jpg" );
			$jQuery("img#skoda_car_image").fadeIn('slow');
		
			/* if the current image processed is not the final image, then add one to it and continue, else reset it to the 1st vehicle number (ie start again) */
			if (img_num != maximum_image_number) { img_num = img_num + 1; } else { img_num = first_image_number;}
			/* run the function recursively with the next image */
			change_skoda_image(time_delay, first_image_number, img_num, maximum_image_number);
			return;		
		});
	}, time_delay);
	
}
