Tn.Class({
	"Class" : "Tn.Fx.Slideshow",
	
	options : EMPTY,
	isRunning : NO,
	
	_preLoadImages : [],
	_missingImages : 0,
	_topElement : EMPTY,
	_bottomElement : EMPTY,
	_currIndex : 0,
	
	initialize: function( options ) {
		var self = this;
		this.options = options;
		this._topElement = $("<div>").addClass("slideshow-image").css("z-index",2);
		this._bottomElement = $("<div>").addClass("slideshow-image").css("z-index",1);
		options.preview.click(function() {
			self.start();
		});
	},
	
	start : function() {
		var data = this._getData();
		if( !this.isRunning ) {
			if( data['running'] ) {
				data['running'].stop();
			}
			this.options.destination.html("<img src='"+baseUrl+"images/ajax-loader.gif' /><br/>Einen Diashow wird geladen...");
			
			this.isRunning = YES;
			this._prepareSlidshow();
			
			data['running'] = this;
		}
	},
	
	_prepareSlidshow : function() {
		if( !this._prealoading ) {
			this._missingImages = this.options.images.length;
			this._prealoading = YES;
			var self = this;
			for( var i=0 ; i<this.options.images.length ; i++ ) {
				(function() {
					var index = i;
					self._preLoadImages[i] = new Image();
					self._preLoadImages[i].onload = function() {
						self._preLoadImages[index]._loaded = true;
						self._preloadFinished();
					}
					self._preLoadImages[i].src = self.options.path+self.options.images[i];
				})();
				
			}
		} else {
			if( this._missingImages == 0 ) {
				this._start();
			}
		}
	},
	
	_preloadFinished : function() {
		this._missingImages--;
		if( this._missingImages == 0 ) {
			this._start();
		}
	},
	
	_start :function ()  {
		this.options.destination.empty();
		this.options.destination.append(this._topElement);
		this.options.destination.append(this._bottomElement);
		this._currIndex = 0;
	 	this._nextImage();
		console.log("_start");
	},
	
	_nextImage : function() {
		if( this.isRunning ) {
			var self = this;
			var tmp;
			tmp = this._topElement;
			this._topElement = this._bottomElement;
			this._bottomElement = tmp;
			
			this._topElement.css("opacity",1);
			this._bottomElement.css("opacity",1);
			
			this._topElement.css(
							{
								"background-image":"url('"+this._preLoadImages[this._currIndex].src+"')",
								"z-index" : 2
							}
						);
					
			++this._currIndex;	
			if( this._currIndex >= this._preLoadImages.length ) {
				this.isRunning = NO;
				this._currIndex = 0;
			}
			this._bottomElement.css(
							{
								"background-image":"url('"+this._preLoadImages[this._currIndex].src+"')",
								"z-index" : 1
							}
			);
			
			
			this._topElement.animate(
									{"opacity":0,"easing":"linear"},
									800,
									function() { self._nextImage() });
		}
	},
	
	stop : function() {
		var data = this._getData();
		if( this.isRunning ) {
			
			data['running'] = null;
			this.isRunning = NO;
			this.options.destination.empty();
		}
	},
	
	_getData : function() {
		var tmpData = this.options.destination.data(this.getClassName());
		if( tmpData == null ) {
			this.options.destination.data(this.getClassName(),{});
			tmpData = this.options.destination.data(this.getClassName());
		}
		return tmpData;
	}
});
