//////////////////////////////////


//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( //'img/img_pyjamas_01.jpg',
					   // 'img/img_pyjamas_02.jpg',
					   'gx/I/photo_1.jpg',
					   'gx/I/photo_2.jpg',
					   'gx/I/photo_3.jpg',
					   'gx/I/photo_4.jpg',
					   'gx/I/photo_5.jpg',
					   'gx/I/photo_6.jpg',
					   'gx/I/photo_7.jpg',
					   'gx/I/photo_8.jpg',
					   'gx/I/photo_9.jpg',
					   'gx/I/photo_10.jpg',
					   'gx/I/photo_11.jpg',
					   'gx/I/photo_12.jpg',
					   'gx/I/photo_13.jpg',
					   'gx/I/photo_14.jpg',
					   'gx/I/photo_15.jpg',
					   'gx/I/photo_16.jpg',
					   'gx/I/photo_17.jpg');
					   //'gx/i/photo_22.jpg',
					   //'gx/i/photo_23.jpg',
					   //'gx/i/photo_24.jpg',
					   //'gx/i/photo_25.jpg',
					   //'gx/i/photo_26.jpg',
					   //'gx/i/photo_27.jpg');
						
var alttext = new Array('');  

//Store current image number
var currentIndex = 0;

//Delay in milliseconds
var timeOutDelay = 2000;


//Initialise slideshow if the objects exist for it to work
function startSlideshow() {
	if (!document.getElementsByTagName || !document.getElementById || !document.getElementById('slideshowIndex')) {
		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('slideshowIndex');
	//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);