//A very simple AJAX routine to create a picture slideshow
//written by Owen McAree on 29/08/2010


var timeDelay = 5000; 	// slideshow delay in millisecs
var pictures;			// array containing all the pictures
var i = 0;				//counter

//function to create ajax connection
function setupXMLHttp() {
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}


//function to load images
function loadImages() {
	//get ajax
	var ajax = setupXMLHttp();
	
	//create event listener
	ajax.onreadystatechange = function(){
		//when data is ready...
		if(ajax.readyState == 4){
			//parse returned string into array
			pictures = ajax.responseText.split("\n");
			//first call of function to set initial image
//			slideshow();
			//set interval to repeat slideshow indefinately
			setInterval("slideshow()", timeDelay);
		}
	}
	//open ajax connection
	ajax.open("GET","include/listImages.php",true);
	
	//send data
	ajax.send();
}

//function to preload images and update document
function slideshow() {
	//variable for next image
	var pic = new Image();
	
	//reset counter if end of list has been reached
	if (i == pictures.length-1) {
		i = 0;
	}
	
	//preload next image
	pic.src = "images/frontpage/" + pictures[i];
	
	//load next image
	document.getElementById("slideshow").src = pic.src;
	
	//increment counter
	i++;
}
