//GENERIC ADDLOAD EVENT FUNCTION
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
//////////////////////////////////

//Images and alt text - needs to be the same number in each
var images = new Array('graphics/room_photo.jpg','graphics/room_photo2.jpg','graphics/room_photo3.jpg');
var alttext = new Array('Completed interior design','Completed interior design','Completed interior design');

//Store current image number
var currentIndex = 0;

//Delay in milliseconds
var timeOutDelay = 5000;


//Initialise slideshow if the objects exist for it to work
function startSlideshow() {
	if (!document.getElementsByTagName || !document.getElementById || !document.getElementById('slideshow')) {
		return false;
	}
	counter();
}

//Recursive function to loop through images array and call the swapImage function after every timeOutDelay
function counter() { 
	if (currentIndex >= images.length) {
		//Reached the end so go back to beginning
		currentIndex = 0;
	}
	if ( currentIndex == 0 && images.length == 1 ) {
		//Swap first image
		swapImage(0);
	} else {
		//Swap next image
		swapImage(currentIndex++);
		//Start the delay before calling counter again
		window.setTimeout("counter()",timeOutDelay); 
	}		
}	

//function to swap the images src's
function swapImage(index) {
	//Find target image
	var target = document.getElementById('slideshow');
	//Get target images current src
	var currentsrc = 	target.getAttribute('src');
	//Get new src
	var newSrc = images[index];
	//if nextimage is different
	if (currentsrc != newSrc) {
		//Change src to swap image
		target.setAttribute('src',newSrc);
		//Change alt text of the image
		target.setAttribute('alt',alttext[index]);
	}
}

//Add onload event to page
addLoadEvent(startSlideshow);