var slideShows = new Array();

function changeSlide(index) {
	slideShows[index].changeSlide();
}

function fadeSlide(index) {
	slideShows[index].fadeSlide();
}

function SlideShow(imageId, timeOut, smoothTransition, transitionTime, transitionSteps) {
	this.imageObject = document.getElementById(imageId);
	this.imageInfo = new Array();
	this.timeOut = timeOut;
	this.smoothTransition = (typeof smoothTransition == 'undefined') ? false : smoothTransition;
	this.transitionTime = (typeof transitionTime == 'undefined') ? 1 : transitionTime;
	this.transitionSteps = (typeof transitionSteps == 'undefined') ? 20 : transitionSteps;
	this.imageClickHandler = null;

	slideShows.push(this);
	this.slideIndex = slideShows.length - 1;
}

SlideShow.prototype.changeSlide = function() {
	this.currentImageData = this.imageInfo.shift();
	this.imageInfo.push(this.currentImageData);

	if (this.smoothTransition)
		this.startSlideFade();
	else
		this.changeImage();
}

SlideShow.prototype.addImage = function (imagePath, url) {
	this.imageInfo.push({'imagePath' : imagePath, 'url' : url});
}

SlideShow.prototype.start = function() {
	setInterval('changeSlide(' + this.slideIndex +')', this.timeOut * 1000);
}

SlideShow.prototype.startSlideFade = function() {

	this.transitionTimerInterval = (this.transitionTime / 2 / this.transitionSteps) * 1000;
	this.opacity = 1;
	this.turningOff = true;
	this.stepCounter = 0;

	if (!navigator.platform || navigator.platform.toLowerCase().indexOf("linux") >= 0)
		this.domType = 'unknown';  // opacity on Linux is very very slow
	else if (typeof this.imageObject.style.opacity != 'undefined')
		this.domType = 'w3c';
	else if (typeof this.imageObject.style.MozOpacity != 'undefined')
		this.domType = 'mozilla';
	else if (typeof this.imageObject.style.KhtmlOpacity != 'undefined')
		this.domType = 'khtml';
	else if (typeof this.imageObject.filters == 'object') {
		this.domType = (this.imageObject.filters.length > 0 && typeof this.imageObject.filters.alpha == 'object' && typeof this.imageObject.filters.alpha.opacity == 'number') ? 'ie' : 'unknown';
	}
	else
		this.domType = 'unknown';

	if (this.domType == 'unknown')
		this.changeImage();
	else {
		if (this.timer != null)
			clearInterval(this.timer);
		this.timer = setInterval('fadeSlide(' + this.slideIndex + ')', this.transitionTimerInterval);
	}
}

SlideShow.prototype.fadeSlide = function() {
	
	this.stepCounter++;

	if (this.stepCounter >= this.transitionSteps) {
		clearInterval(this.timer);
		this.timer = null;

		if (this.turningOff) {
			this.turningOff = false;
			this.stepCounter = 0;
			this.changeImage();
			this.timer = setInterval('fadeSlide(' + this.slideIndex + ')', this.transitionTimerInterval);
		}
	} else {

		this.opacity *= (this.turningOff) ? 0.9 : 1/0.9;
		if (this.opacity > 1.0)
			this.opacity = 1.0;

		switch(this.domType)
		{
			case 'ie' :
				this.imageObject.filters.alpha.opacity = this.opacity * 100;
				break;
			case 'khtml' :
				this.imageObject.style.KhtmlOpacity = this.opacity;
				break;
			case 'mozilla' :
				this.imageObject.style.MozOpacity = this.opacity;
				break;
			default :
				this.imageObject.style.opacity = this.opacity;
		}
	}
}

SlideShow.prototype.changeImage = function() {
	this.imageObject.src = this.currentImageData.imagePath;

	if (this.imageClickHandler == null) {
		this.imageClickHandler = new LocationChanger();

		if (this.imageObject.addEventListener)
			this.imageObject.addEventListener('click', this.imageClickHandler.handleEvent, false);
		else if (this.imageObject.attachEvent)
			this.imageObject.attachEvent('onclick', this.imageClickHandler.handleEvent);
	}

	this.imageClickHandler.changeLocation(this.currentImageData.url);
}

function LocationChanger() {
	var newLocation = null;

	this.changeLocation = function(url) {
		newLocation = url 
	}

	this.handleEvent = function() {
		location.href = newLocation;
	}
}

//function debugOut(message) {
//	var div = document.createElement("div");
//	div.innerHTML = message;
//	document.getElementById("debug").appendChild(div);
//}
