
/* SLideShow Script */
/* Pedro Costa */

var cache = [];

var numPhotos = 19;
var numCachedImages = 0;
var photo = Math.floor(Math.random() * numPhotos + 1);
var imagesRootPath =  "/images/";



function nextSlide() {
	if(photo >= numPhotos) {
		photo = 1;
			
	} else {
		photo++;
	}

	if(photo >= numPhotos) {	
		preloadImage(getImagePath(1));
	} else {
		preloadImage(getImagePath(photo+1));
	}

	return  getImagePath(photo);
}


function getImagePath(numImage) {
	return  imagesRootPath + numImage + ".jpg";
}


function loadImage() {
	$("#slide").fadeOut('1000', function() {
		$("#slide").attr("src", nextSlide());
		$("#slide").fadeIn('slow');
	});
}

function preloadImage(imageSrc){

	if (numCachedImages<numPhotos) {	
		var cacheImage = document.createElement('img');
     		cacheImage.src = imageSrc;
      		cache.push(cacheImage);
		numCachedImages++;
	}

}

$(document).ready(function() {
	preloadImage(getImagePath(photo+1));
	window.setInterval('loadImage()', 5000);
});

