var SLIDESHOW = new Class({
	initialize: function(container, images, player, handle) {
		if(typeof container == 'object') this.container = container;
		else if($(container.toString())) this.container = $(container.toString());
		if(typeof player == 'object') this.player = player;
		else if($(player.toString())) this.player = $(player.toString());
		if(typeof handle == 'object') this.handle = handle;
		else if($(handle.toString())) this.handle = $(handle.toString());
		this.playerStart = parseInt(this.player.getStyle('left').replace(/px/g, ''));
		this.playerStep = Math.abs(this.playerStart) / images.length;
		this.images = images;
		this.imagePointer = new Array();
		this.currentImage = 0;
		this.fxImage = null;
		this.fxPlayer = null;
		this.timer = null;
		this.totalFxTime = 0;
		this.playerDragEnabled = false;
	},

	show: function() {
		var _self = this;
		
		var images = new Array();
		this.totalFxTime = 0;
		for(var i = 0; i < this.images.length; i++) {
			images.push(this.images[i].image);
			this.totalFxTime += this.images[i].delay;
		}
		
		new Asset.images(images, {
			onComplete: function() {
				for(var i = 0; i < _self.images.length; i++) {
					_self.imagePointer[i] = new Element('img');
					_self.imagePointer[i].setStyles({
						'opacity':0,
						'position':'absolute',
						'z-index':5
					});
					_self.imagePointer[i].src = _self.images[i].image;
					_self.container.grab(_self.imagePointer[i]);
				}
				_self.showFirstImage();_self.startPlayer();
			}
		});
	},

	showFirstImage: function() {
		var _self = this;
		if(this.images.length > 0) {
			this.fxImage = new Fx.Morph(this.imagePointer[this.currentImage], {duration:this.images[this.currentImage].fxTime}).start({'opacity':[0, 1]});
			this.timer = window.setTimeout(function() { _self.next(); }, this.images[this.currentImage].fxTime + this.images[this.currentImage].delay);
		}
	},
	
	startPlayer: function() {
		var _self = this;
		this.fxPlayer = new Fx.Morph(this.player, {duration:this.totalFxTime, transition:Fx.Transitions.linear}).start({'left':[this.playerStart, this.playerStart + this.images.length * this.playerStep]});
		if(!this.playerDragEnabled) {
			this.playerDragEnabled = true;
			new Drag(this.player, {
				grid:_self.playerStep,
				handle: _self.handle,
				modifiers:{x:'left', y:false},
				onStart: function(player) {
					_self.fxImage.cancel();
					_self.fxPlayer.cancel();
					window.clearTimeout(_self.timer);
				},
				onDrag: function(player) {
					var left = parseInt(player.getStyle('left').replace(/px/g, ''));
					if(left < (_self.playerStart + 1)) left = _self.playerStart + 1;
					else if(left > -_self.playerStep) left = -_self.playerStep;
					player.setStyle('left', left);
				},
				onComplete: function(player) {
					var left = parseInt(Math.abs(player.getStyle('left').replace(/px/g, '')));
					var indx = parseInt((Math.abs(_self.playerStart) - left) / _self.playerStep);
					_self.showImage(indx);
				}
			});
		}
	},

	showImage: function(indx) {
		var _self = this;
		this.currentImage = indx;
		for(var i = 0; i < this.imagePointer.length; i++) {
			this.imagePointer[i].setStyles({
				'opacity':0,
				'z-index':5
			});
		}
		this.fxImage = new Fx.Morph(this.imagePointer[this.currentImage], {duration:this.images[this.currentImage].fxTime}).start({'opacity':[0, 1]});
		var nextImage = this.currentImage;
		if(nextImage >= this.images.length) nextImage = 0;
		this.currentImage = nextImage;
		
		var tmpFxTime = 0;
		for(var i = indx; i < this.images.length; i++) tmpFxTime += this.images[i].delay;
		this.fxPlayer = new Fx.Morph(this.player, {duration:tmpFxTime, transition:Fx.Transitions.linear}).start({'left':this.playerStart + (this.images.length * this.playerStep)});
		this.timer = window.setTimeout(function() {
			if(_self.currentImage == _self.images.length - 1) _self.startPlayer();
			_self.next();
		}, this.images[this.currentImage].delay);
	},

	next: function() {
		var _self = this;
		var prevImage = this.currentImage - 1;
		if(prevImage < 0) prevImage = this.images.length - 1;
		this.imagePointer[prevImage].setStyle('z-index', 5);
		this.imagePointer[this.currentImage].setStyle('z-index', 10);
		var nextImage = this.currentImage + 1;
		if(nextImage >= this.images.length) nextImage = 0;
		this.imagePointer[nextImage].setStyle('opacity', 1);

		this.fxImage = new Fx.Morph(this.imagePointer[this.currentImage], {duration:this.images[this.currentImage].fxTime}).start({'opacity':[1, 0]});
		this.currentImage = nextImage;
		this.timer = window.setTimeout(function() {
			if(_self.currentImage == _self.images.length - 1) _self.startPlayer();
			_self.next();
		}, this.images[this.currentImage].delay);
	}
});