
$(document).ready(function(){
	// the slideshow HTML element
	var slideshowElement = $("#slideshow");

	// the image files of the slideshow
	var imgArr = new Array('images/photos/slideshow/gill.jpg', 'images/photos/slideshow/walk1.jpg', 'images/photos/slideshow/walk3.jpg', 'images/photos/slideshow/zacandjemimah.jpg', 'images/photos/slideshow/beckyflute.jpg', 'images/photos/slideshow/sounddesk.jpg', 'images/photos/slideshow/bethan.jpg', 'images/photos/slideshow/tonyquiz.jpg', 'images/photos/slideshow/croftyetalfromabove.jpg', 'images/photos/slideshow/walk2.jpg', 'images/photos/slideshow/olifootball.jpg', 'images/photos/slideshow/fearnehoughs.jpg', 'images/photos/slideshow/claireandnoah.jpg');	

	// the array of image objects to be preloaded and cycled through
	var preloadArr = new Array();	

	// find the url of the initial photo 
	var currentImgFullUrl = slideshowElement.css("background-image");
	var currentImgUrl = currentImgFullUrl.substring(
							currentImgFullUrl.indexOf("images/photos/slideshow/"), 
							currentImgFullUrl.length-1);

	// put the initial photo at the start of the list
	var img = new Image();
	img.src = currentImgUrl;
	preloadArr.push(img);

	// add all the other photos to the list, taking care not to add the initial photo again
	for (var i=0; i < imgArr.length; i++) {
		if (imgArr[i] != currentImgUrl) {
			img = new Image();
			img.src = imgArr[i];
			preloadArr.push(img);
		}
	}
			
	// the current image being displayed in the slideshow, start at the 2nd in the list
	var currImg = 1;
	var intID = setInterval(changeImg, 5000);

	// image rotation
	function changeImg(){
		slideshowElement.animate({opacity: 0}, 1000, function(){
			$(this).css('background','url(' + preloadArr[currImg++%preloadArr.length].src +')');
		}).animate({opacity: 1}, 1000);
	}			
});
