$(document).ready(function() {
	
	for(i=0; i<images.length; i++) { 
  		var image_preload = new Image();
  		image_preload.src = images[i];
	}
	
	var firstland = 5000;
	
	var testimonial = 1;
	
	var nextimage=0;
	
	if(images.length > 0) {
		doSlideshow();
	}
	
	function doSlideshow()
	{
		if($('.backgroundImage').length!=0)
		{
			$('.backgroundImage').fadeOut(500,function(){slideshowFadeIn();$(this).remove();});
		}
		else
		{
			slideshowFadeIn();
		}
	}
	
	function slideshowFadeIn()
	{
		if(nextimage>=images.length) 
			nextimage=0;
		$(".straightToTestimonial").fadeOut();
		$('.homeLeadContent').prepend($('<img class="backgroundImage" src="'+images[nextimage]+'" style="display:none;">').fadeIn(500,function() {
			setTimeout(doSlideshow,firstland);
			$('.homeLeadContent').css("background-image","url("+images[nextimage++]+")");
			
			//Change the URL of the testimonial link if it exists
			if($('.straightToTestimonial').length>0) {
				$(".straightToTestimonial").attr("href","/testimonials/?id=" + $('.homeLeadContent').css("background-image").replace(/\D/g,''));
				if ($('.homeLeadContent').css("background-image").replace(/\D/g,'') == 1) {
					$(".straightToTestimonial").text("Read Joanne Davies and husband Sam Orton's full story").fadeIn();
				}
				else if ($('.homeLeadContent').css("background-image").replace(/\D/g,'') == 11) {
					$(".straightToTestimonial").text("Read Nigel and Stephanie Collins's full story").fadeIn();
				}
				else if ($('.homeLeadContent').css("background-image").replace(/\D/g,'') == 12) {
					$(".straightToTestimonial").text("Read Keith and Jennifer McCartney's full story").fadeIn();
				}
			}
			
			firstland = 8000;
		}));
	}
	
});
