// slideshow.js

// Tom Auger, Zeitguys inc., www.zeitguys.com

// version history
// 1.0, Jan 2009 - automatic slideshow for Summerlee.com


var slideshowDelay = 7000; // number of millisecs between images
var imagesFolder = "images"; // the name of the folder that the images are stored in


// don't change below this
var lastImageIndex = 0;
var curImageIndex = 0;


var slideshowImages = new Array();
var slideshowStarted = false;
var imgTag;



// Run code when the page loads.
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
addLoadEvent(startSlideshow);


function startSlideshow(){

	imgTag = document.getElementById("slideshowImage");
	
	if (imgTag){
		// slideshow image was found. go ahead with the rest
		var nameMatch = document.location.href.match(/([^\/.]+)\.[^\/]+$/);
		
		if (nameMatch[1]){
			for (var i=1; i<10; i++){
				var preloadImage = new Image();
				preloadImage.arrayIndex = i;
				preloadImage.onload = function(eventObj){ 
					slideshowImages[this.arrayIndex] = this; 
					if (this.arrayIndex > lastImageIndex) lastImageIndex = this.arrayIndex;
					triggerSlideshow(this.arrayIndex);
				}
				preloadImage.src = imagesFolder + "/" + nameMatch[1] + "/" + i + ".jpg";
			}
		} 
	} 
}

function triggerSlideshow(imageIndex){
	if (!slideshowStarted){
		slideshowStarted = true;
		imgTag.src = slideshowImages[imageIndex].src;
		setTimeout("showSlide()", slideshowDelay);
		curImageIndex = imageIndex;
	}
}


function showSlide(){
	while(!slideshowImages[++curImageIndex]){
		if (curImageIndex > lastImageIndex) curImageIndex = 0;
	}
	
	imgTag.src = slideshowImages[curImageIndex].src;
	//alert (curImageIndex);
	setTimeout("showSlide()", slideshowDelay);
	
}