﻿// change these paths for your images
var myImages = ['/img/logos/logos0001.jpg','/img/logos/logos0002.jpg','/img/logos/logos0003.jpg','/img/logos/logos0004.jpg','/img/logos/logos0005.jpg','/img/logos/logos0006.jpg','/img/logos/logos0007.jpg','/img/logos/logos0008.jpg','/img/logos/logos0009.jpg','/img/logos/logos0010.jpg','/img/logos/logos0011.jpg','/img/logos/logos0012.jpg','/img/logos/logos0013.jpg','/img/logos/logos0014.jpg','/img/logos/logos0015.jpg','/img/logos/logos0016.jpg','/img/logos/logos0017.jpg','/img/logos/logos0018.jpg','/img/logos/logos0019.jpg','/img/logos/logos0020.jpg','/img/logos/logos0021.jpg','/img/logos/logos0022.jpg','/img/logos/logos0023.jpg','/img/logos/logos0024.jpg'];

// how many times should the photo change per page load
var maxChanges = myImages.length * 2;

// 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 = 5;

// name of DIV to load photos into
var div_name = "loader";

var changes = 0;
var timer;
var thisImg = myImages.length - 1;

shuffle = function(o){ //v1.0
	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;
};

// shuffling is better than random because of less potential repetition
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("normal").remove();
	var largeImage = new Image();
	$(largeImage).load(function()
		{
			$(this).hide();
			$("#"+div_name).append(this).removeClass("loading");
							 
			$(this).fadeIn("slow");              
		});    
	$(largeImage).attr("src", src);                                                                               
}

function checkForLoaded () {
	if (document.getElementById(div_name) != null) {
		//alert("loaded");
		clearInterval(timer);
		changeImage();
		timer = setInterval(changeImage, (seconds_between_photos * 1000));
	}
}

// check every second to see if DIV exists, when it does, start photo changing timer
timer = setInterval(checkForLoaded, 500); 

