// slideShow ///////////////////////////////////////////////////////////////
// by www.markuslerner.com
// 2008-01-24


function SlideShow(slideshowDivID, slideshowImageID, slideshowLinkID) {
	this.slideshowDivID = slideshowDivID;
	this.slideshowImageID = slideshowImageID;
	this.slideshowLinkID = slideshowLinkID;
	
	this.preloadFlag = false;
	this.currentImageID = 0;
	this.nextImageID = 1;
	
	this.slideDuration = 2000;
	this.transitionDuration = 1000;
	this.random = false;
}

SlideShow.prototype.setRandom = function(random) {
	this.random = random;
}

SlideShow.prototype.setSlideDuration = function(slideDuration) {
	this.slideDuration = slideDuration;
}

SlideShow.prototype.setTransitionDuration = function(transitionDuration) {
	this.transitionDuration = transitionDuration;
}

SlideShow.prototype.setImages = function() {
	if (document.images) {
		this.images = new Array();
		for (var i = 0; i < this.setImages.arguments.length; i++) {
			this.images[i] = this.createImage(this.setImages.arguments[i]);
		}
		this.preloadFlag = true;
	}
}

SlideShow.prototype.setLinks = function() {
	this.links = new Array();
	for (var i = 0; i < this.setLinks.arguments.length; i++) {
		this.links[i] = this.setLinks.arguments[i];
	}
}

SlideShow.prototype.createImage = function(url) {
	if (document.images) {
		var img = new Image();
		img.src = url;
		return img;
	}
}

SlideShow.prototype.sineInOut = function(pos) {
	// EASE IN AND OUT by position
	// pos = 0 ... 1
	// returns float(0 ... 1)
	return (1 - Math.cos(pos * Math.PI)) / 2;
}

SlideShow.prototype.loop = function() {
	var currentTime = new Date().getTime();
	this.position = (currentTime - this.startTime) / this.transitionDuration;
	if(this.position > 0.99) {
		this.position = 0.99;
		clearInterval(this.interval);
	}
	var opacity = this.sineInOut(this.position);	
	this.setOpacity(this.objectStyle, opacity);
}

SlideShow.prototype.setOpacity = function(objectStyle, opacity) {
	// set the opacity for different browsers
	objectStyle.opacity = (opacity);
	objectStyle.MozOpacity = (opacity);
	objectStyle.KhtmlOpacity = (opacity);
	objectStyle.filter = "alpha(opacity=" + opacity * 100 + ")";
}

SlideShow.prototype.startTransition = function(currentImage, nextImage) {
	// set the current image as background
	document.getElementById(this.slideshowDivID).style.backgroundImage = "url(" + currentImage + ")";
	
	this.objectStyle = document.getElementById(this.slideshowImageID).style;
	this.setOpacity(this.objectStyle, 0);
	
	this.nextImage = nextImage;
	
	var self = this;
	window.setTimeout(function(){ self.startInterval(); }, 10);
}

SlideShow.prototype.setLink = function(url) {
	if(this.slideshowLinkID != '') {
		document.getElementById(this.slideshowLinkID).href = url;
	}
}

SlideShow.prototype.startInterval = function() {
	// set the new image as the foreground image
	document.getElementById(this.slideshowImageID).src = this.nextImage;	
	this.startTime = new Date().getTime();
	var self = this;
	this.interval = setInterval(function(){ self.loop(); }, 10);
}

SlideShow.prototype.showNextImage = function() {
	if(document.images && this.preloadFlag) {
		if(this.random) {
			this.nextImageID = Math.round(Math.random() * (this.images.length - 1));
		} else {
			this.nextImageID = this.currentImageID + 1;
		}
		if(this.nextImageID > this.images.length - 1) {
			this.nextImageID = 0;
		}
		if(this.links != null) {
			this.setLink(this.links[this.nextImageID]);
		}
		this.startTransition(this.images[this.currentImageID].src, this.images[this.nextImageID].src);
		this.currentImageID = this.nextImageID;
	}
	var self = this;
	window.setTimeout(function(){ self.showNextImage(); }, this.slideDuration);
}

SlideShow.prototype.start = function() {
	var self = this;
	window.setTimeout(function(){ self.showNextImage(); }, this.slideDuration);
}
