/*
 * Slideshow
 * 
 */
 

function SlideShow() {
	this.slideDuration = 5;
	this.fadeDuration = 3;
	this.autoplay = true;
	this.slides = [];
	this.captions = [];
	this.currentSlideId = -1;
	this.currentFadeCnt = 0;
	this.fadeResolution = 40;
	this.lastMoveDirection = -1;
	this.elCaption = null;
	this.log_scale = 3;
	this.show_controls = false;
	this.id = '';
	this.slideDirection = 1;
	this.isPlaying = false;
	this.fadeInOnStart = false;
}
 
SlideShow.prototype = {
 
	/*
	 * wie lange soll ein slide angezeigt werden, bevor der nächste fade beginnt
	 */
	setSlideDuration: function(seconds) {
		this.slideDuration = seconds;
	},
 
	/*
	 * wie lange soll ein fade dauern
	 */
	setFadeDuration: function(seconds) {
		this.fadeDuration = seconds;
	},
 
	/*
	 * fügt ein neuen slide hinzu
	 */
	addSlide: function(elementId, caption) {
		this.slides.push(elementId);
		this.captions.push(caption);
	},
	
	/*
	 * fügt ein neuen slide hinzu
	 */
	start: function() {
		if (this.slides.length < 2) {
			// fade in?
			
			// hide controls
			this.show_controls = false;
			if (this.fadeInOnStart) {
				if ($(this.slides[0])) $(this.slides[0]).setOpacity(0);
				this.currentSlideId = 0;
				this.nextSlide();
			}
			return false;
		} else {
			this.currentSlideId = this.slides.length - 1;
			
			var oInstance = this;
			if (this.show_controls) this.initControls();
			this.instantFade = false;
			
			
			
			for (var c=0; c<this.slides.length; c++) {
				if (c != this.currentSlideId) $(this.slides[c]).setOpacity(0);
			}
			if (this.fadeInOnStart) {
				$(this.slides[this.currentSlideId]).setOpacity(0);
				if (this.autoplay) {
					this.isPlaying = true;
					window.setTimeout(function() { oInstance.nextSlide(); }, 0);
				}
			} else {
				if (this.autoplay) {
					this.isPlaying = true;
					window.setTimeout(function() { oInstance.nextSlide(); }, oInstance.slideDuration * 1000);
				}
			}
		}
	},
 
	/*
	 * 
	 */
	initControls: function() {
		$(this.id+'_playpause').show();
		
		var oInstance = this;
		
		// binding error
		if (this.autoplay) {
			$(this.id+'_playpause').onclick = this.pause.bind(oInstance);
		} else {
			$(this.id+'_playpause').onclick = this.play.bind(oInstance);
		}
		
		
		
		$(this.id+'_next').onclick = function() {
			if (oInstance.nextSlideTimer != null) {
				window.clearInterval(oInstance.nextSlideTimer);
				oInstance.nextSlideTimer = null;
			}
			oInstance.instantFade = true;
			if (oInstance.fadeTimer == null) {
				oInstance.lastMoveDirection = -1;
				oInstance.nextSlide();
			}
			return false;
		}
		$(this.id+'_prev').onclick = function() {
			if (oInstance.nextSlideTimer != null) {
				window.clearInterval(oInstance.nextSlideTimer);
				oInstance.nextSlideTimer = null;
			}
			oInstance.instantFade = true;
			oInstance.slideDirection = -1;
			oInstance.lastMoveDirection = -1;
			oInstance.nextSlide();
			return false;
		}
	},
	
	play: function() {
		if (this.isPlaying) return false;
		this.isPlaying = true;
		$(this.id+'_playpause').removeClassName('play');
		$(this.id+'_playpause').addClassName('pause');
		$(this.id+'_playpause').onclick = this.pause.bind(this);
		this.nextSlide();
		return false;
	},
	
	pause: function() {
		if (!this.isPlaying) return false;
		this.isPlaying = false;
		
		$(this.id+'_playpause').removeClassName('pause');
		$(this.id+'_playpause').addClassName('play');
		$(this.id+'_playpause').onclick = this.play.bind(this);
		
		if (this.nextSlideTimer != null) {
			window.clearInterval(this.nextSlideTimer);
			this.nextSlideTimer = null;
		}
		if (this.fadeTimer != null) this.instantFade = true;
		
		return false;
	},
 
	nextSlide: function() {
		window.clearInterval(this.fadeTimer);
		this.fadeTimer = null;
		window.clearInterval(this.nextSlideTimer);
		this.nextSlideTimer = null;
		
		
		var nextSlideId = (this.slides.length + this.currentSlideId + this.slideDirection)%this.slides.length;
 
		var lastSlideId = this.currentSlideId;
		var currentSlide = $(this.slides[this.currentSlideId]);
		var nextSlide = $(this.slides[nextSlideId]);
 
		if (nextSlide) {
			nextSlide.setOpacity(0);
			nextSlide.style.zIndex = (currentSlide.style.zIndex * 1.0) + 1.0;
 
			if (nextSlide.style.zIndex > this.slides.length) {
				for (var c=0; c<this.slides.length; c++) {
					$(this.slides[c]).style.zIndex--;
				}
			}
		}
 
		this.currentSlideId = nextSlideId;
		this.currentFadeCnt = 0;
		
		this.lastMoveDirection = this.lastMoveDirection >= 0 ? Math.floor(Math.random()*5) : 0;
		
		this.xPosStart = 0;
		this.xPosEnd = 0;
		this.yPosStart = 0;
		this.yPosEnd = 0;
		switch (this.lastMoveDirection) {
			case 0: 
				this.xPosStart = -25;
				break; 
			case 1: 
				this.xPosEnd = -25;
				break;
			case 2: 
				this.yPosStart = -25;
				break;
			case 3: 
				this.yPosEnd = 0;
				break;
			case 4: 
				break;
		}
				
		var oInstance = this;
 
		this.fadeTimer = window.setInterval(function() {
			oInstance.currentFadeCnt++;
			var duration = oInstance.instantFade ? 0.5 : oInstance.fadeDuration;
			
			var newOpacity = oInstance.currentFadeCnt * (oInstance.fadeResolution / (duration * 1000)); 
			
			if ($(oInstance.slides[oInstance.currentSlideId])) {
				$(oInstance.slides[oInstance.currentSlideId]).style.left = Math.round(oInstance.xPosStart + (oInstance.xPosEnd - oInstance.xPosStart)*oInstance.easeOut(newOpacity))+'px';
				$(oInstance.slides[oInstance.currentSlideId]).style.top = Math.round(oInstance.yPosStart + (oInstance.yPosEnd - oInstance.yPosStart)*oInstance.easeOut(newOpacity))+'px';
				$(oInstance.slides[oInstance.currentSlideId]).setOpacity(newOpacity);
			}
			
			if (newOpacity >= 1) {
				oInstance.instantFade = false;
				oInstance.slideDirection = 1;
				
				if ($(oInstance.elCaption)) {
					$(oInstance.elCaption).update(oInstance.captions[oInstance.currentSlideId]);
					$(oInstance.elCaption).addClassName('highlight');
					window.setTimeout(function() { $(oInstance.elCaption).removeClassName('highlight'); }, 700);
				}
				window.clearInterval(oInstance.fadeTimer);
				oInstance.fadeTimer = null;
				if (oInstance.isPlaying) oInstance.nextSlideTimer = window.setTimeout(function() { oInstance.nextSlide(); }, oInstance.slideDuration * 1000);
			}
		}, oInstance.fadeResolution);
	},
	
	easeOut: function(x) {
		return Math.log(x*this.log_scale+1)/Math.log(this.log_scale+1);
	}
}