﻿// change these paths for your images
var myImages = ['slideshow/image1.jpg','slideshow/image2.jpg','slideshow/image3.jpg','slideshow/image4.jpg','slideshow/image5.jpg','slideshow/image6.jpg'];

// how many times should the photo change per page load
var maxChanges = myImages.length * 20;

// shuffle images so each time page loads, the photos show in different order
var do_shuffle = true;

// use simple randomness instead of shuffling (tends to repeat images too often)
var do_randomly = false;

// number of seconds between photo changes
var seconds_between_photos = 8;

// name of DIV to load photos into
var div_name = "slideshow";

var changes = 0;
var timer;
var thisImg = myImages.length - 1;

shuffle = function(o){ 
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};


if (do_shuffle) {
	myImages = shuffle(myImages);	
}

function nextImage () {
	
	var low = 0;
	var high = myImages.length - 1;
	var rand_no = Math.floor((high-(low - 1))*Math.random()) + low;
	
	thisImg++;
	changes++;
	if (thisImg==myImages.length) {
		thisImg = 0;
	}
	if (changes==maxChanges) {
		clearInterval(timer);
	}
	if (do_randomly) {
		thisImg = rand_no;
		return myImages[rand_no];
	} else {
		return myImages[thisImg];
	}
}

function changeImage () {
	
	var t = myImages[thisImg];
	var n = nextImage();
	
	if (t != n) {
		$("#"+div_name).addClass("loading");
		showImage(n);
	} else { 
		changeImage();
	}
}

function showImage(src)
{
	$("#"+div_name+" img").fadeOut(400);
	var largeImage = new Image();
	$(largeImage).load(function()
                        {
							$(this).hide();
                        	$("#"+div_name).append(this).removeClass("loading");
                                             
                       		$(this).fadeIn(1000);              
                        });    
	$(largeImage).attr("src", src);                                                                               
}

function checkForLoaded () {
	if (document.getElementById(div_name) != null) {
		//alert("loaded");
		clearInterval(timer);
		changeImage();
		timer = setInterval(changeImage, (seconds_between_photos * 1000));
	}
}

timer = setInterval(checkForLoaded, 500); 

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages("slideshow/image1.jpg", "slideshow/image2.jpg", "slideshow/image3.jpg", "slideshow/image4.jpg", "slideshow/image5.jpg", "slideshow/image6.jpg");
