/* ---------- ARRAY FUER SLIDESHOW TIMER ---------------------------- */
var _slideshows = Array();
function _autoplay(id){
	if(_slideshows[id].autoplay){
		_slideshows[id].next();
		window.setTimeout("_autoplay('"+id+"')", _slideshows[id].milliSecondsPerSlide);
	}
}

/* ---------- SLIDESHOW --------------------------------------------- */
function slideshow(id) {
	_slideshows[id] = this;

	/* ---------- MEMBERS ----------------------------------------------- */
    this.id           = id;
    this.slides       = null;
    this.visibleSlide = 0;

	/* ---------- METHODEN ---------------------------------------------- */
	this.init  = function(){
		var container = document.getElementById(this.id);

		/* alles slides aufsammeln und merken */
		this.slides   = new Array();
		for(i=0; i<container.childNodes.length; i++){
			if(container.childNodes[i].nodeName == 'DIV'){
				this.slides[this.slides.length] = container.childNodes[i];
			}
		}
		/* alle unsichtbar machen */
		this.hideAll();

		/* die erste slide slide sichtbar machen */
		this.visibleSlide = 0;
		this.slides[this.visibleSlide].style.display = 'block';
	}

	this.hideAll = function(){
		for(i=0; i<this.slides.length; i++){
			this.slides[i].style.display = 'none';
		}
	}

	this.first = function(){
		if(this.maySwap()){
			var lastVisibleSlide = this.visibleSlide;
			this.visibleSlide=0;
			this.swapStart(this.slides[lastVisibleSlide], this.slides[this.visibleSlide]);
		}

	}

	this.last = function(){
		if(this.maySwap()){
			var lastVisibleSlide = this.visibleSlide;
			this.visibleSlide=this.slides.length-1;
			this.swapStart(this.slides[lastVisibleSlide], this.slides[this.visibleSlide]);
		}
	}

	this.next = function(){
		if(this.maySwap()){
			if(this.visibleSlide < this.slides.length-1){
				var lastVisibleSlide = this.visibleSlide;
				this.visibleSlide++;
				this.swapStart(this.slides[lastVisibleSlide], this.slides[this.visibleSlide]);
			} else if(this.rotate) {
				this.first();
			}
		}
	}

	this.prev = function(){
		if(this.maySwap()){
			if(this.visibleSlide > 0){
				var lastVisibleSlide = this.visibleSlide;
				this.visibleSlide--;
				this.swapStart(this.slides[lastVisibleSlide], this.slides[this.visibleSlide]);
			} else if(this.rotate) {
				this.last();
			}
		}
	}

	/* ---------- AUTO PLAY ------------------------------------------ */
	this.autoplay = false;
	this.milliSecondsPerSlide = 5000;

	this.play = function(milliSecondsPerSlide){
		this.autoplay = true;
		if(milliSecondsPerSlide){
			this.milliSecondsPerSlide = milliSecondsPerSlide;
		}
		_autoplay(this.id);
	}

	this.stop = function(){
		this.autoplay = false;
	}

	/* ---------- SWAPPING ------------------------------------------- */
	this.swapRun  = false;
	this.swapFrom = null;
	this.swapTo   = null;
	this.opacity  = 0;
	this.opacStep = 0.06;
	this.swapDelay = 50;
	this.rotate    = true;

	this.maySwap = function(){
		return this.swapRun == false;
	}

	this.swapStart = function(from, to){
		if(this.swapRun == false){
			this.swapRun = true;
			window[this.id] = this;
			this.swapFrom = from;
			this.swapTo   = to;
			window.setTimeout("_gSwap('"+this.id+"')", this.swapDelay);
		}
	}
	this.swap = function(){
		if(this.opacity<1){
			this.opacity += this.opacStep;
			var opacityFrom = 1 - this.opacity;
			var opacityTo   =     this.opacity;

			this.swapFrom.style.display = 'block';
			this.swapFrom.style.filter  = 'alpha(opacity=' + (opacityFrom*100) +')';
			this.swapFrom.style.opacity = "" + opacityFrom;

			this.swapTo.style.display = 'block'
			this.swapTo.style.filter  = 'alpha(opacity=' + (opacityTo*100) +')';
			this.swapTo.style.opacity = "" + opacityTo;

			window.setTimeout("_gSwap('"+this.id+"')", this.swapDelay);
		} else {
			this.swapFrom.style.display = 'none';
			this.swapFrom.style.filter  = 'alpha(opacity=100)';
			this.swapFrom.style.opacity = "1";

			this.swapTo.style.display = 'block'
			this.swapTo.style.filter  = 'alpha(opacity=100)';
			this.swapTo.style.opacity = "1";

			this.opacity = 0;
			this.swapRun = false;
		}
	}

	/* ---------- INIT AUFRUF ------------------------------------------- */
	this.init();
}
function _gSwap(id){
	window[id].swap();
}

