//	slideshow.js
//	Original Code by T.K.Egan - 4 April 2009
//	License: none
//		Use as you will but I'd appreciate an
//		email at tkegan@greenneondesign.com if 
//		you find a way to improve the code

function SlideShow(container, path, max) {
	var self = this;
	this.container = container;
	this.path = path;
	this.max = max;
	this.delay = 8;
	this.framerate = 150;
	this.currentSlide = null;
	this.nextSlideData = new Image();
	this.nextSlideData.onload = function() { self.advance(); };
}

SlideShow.prototype.play = function() {
	this.loadNext();
	var self = this;
	this.thread = setInterval(function() { self.loadNext(); }, this.delay*1000);
}

SlideShow.prototype.stop = function() {
	clearInterval(this.thread);
}

SlideShow.prototype.animate = function(step) {
	if(step > 9) {	// done
		this.nextSlide.style.opacity = 1.0;
		if(null != this.currentSlide) {
			this.currentSlide.style.display = 'none';
			this.container.removeChild(this.currentSlide);
		}
		this.currentSlide = this.nextSlide;
		this.currentSlide.style.zIndex = 2;
	} else {
		step++;
		this.nextSlide.style.opacity = step / 10.0;
		
		var self = this;
		setTimeout(function() { self.animate(step); }, this.framerate);
	}
}

SlideShow.prototype.advance = function() {
	this.nextSlide = document.createElement('img');
	this.nextSlide.setAttribute('src', this.nextSlideData.src);
	this.nextSlide.style.opacity = 0.1;
	this.nextSlide.style.zIndex = 3;
	this.container.appendChild(this.nextSlide);
	
	var self = this;
	setTimeout(function() { self.animate(1); }, this.framerate	);
}

SlideShow.prototype.loadNext = function() {
	var numNextSlide = Math.ceil(this.max * Math.random());
	this.nextSlideData.src = this.path + numNextSlide + '.jpg';
}