var IndexFader = new Class({

	initialize: function(images) {
		this.next = [];
		this.images = images;
		
		this.images.each(function(e) {
			e.set('tween', {duration: 1500});
			e.set({styles: {position: 'absolute', top: 0, left: 0}});
		});
		
		this.run('show', 'hide');
	},
	
	run: function(methodIn, methodOut) {
		var methodIn = methodIn || 'in';
		var methodOut = methodOut || 'out';
		while (true) {
			var i = this.images.pop();
			this.next.push(i);
			if (this.images.length == 0) {
				i.fade(methodIn);
				break;
			} else {
				i.fade(methodOut);
			}
		}
		this.images = this.next;
		this.next = [];
	}
});


var SlideShow = new Class({
	Implements: [Events, Options],

	options: {
		element: null,
		images: [],
		prevLink: 'prev',
		nextLink: 'next',
		current: 0,
		onEndReached: $empty,
		onBeginReached: $empty
	},
	
	initialize: function(options) {
		this.setOptions(options);
		
		this.options.element = $(this.options.element);
		this.options.prevLink = $(this.options.prevLink);
		this.options.nextLink = $(this.options.nextLink);
		
		this.options.current = (this.options.current == -1 ? 0 : this.options.current); 
		
		this.succ = {
			prev: {
				index: null, 
				image: null
			},
			next: {
				index: null,
				image: null
			}
		};
		
		this.move();
		
		this.options.prevLink.addEvent('click', this.reload.bindWithEvent(this, 'prev'));
		this.options.nextLink.addEvent('click', this.reload.bindWithEvent(this, 'next'));
		
		this.options.prevLink.set('tween', {duration: 150});
		this.options.nextLink.set('tween', {duration: 150});
	},
	
	reload: function(evt, dir) {
		evt.target.blur();
		if (this.succ[dir].index) {
			evt.stop();
			var offset = ({prev: -970, next: 970})[dir];
			this.succ[dir].image.injectAfter(this.options.element).setStyle('left', offset);

			this.options.element.set('tween', {duration: 600, transition: Fx.Transitions.Quad.easeInOut});
			this.succ[dir].image.set('tween', {duration: 600, transition: Fx.Transitions.Quad.easeInOut});
			
			this.options.prevLink.tween('opacity', 0);
			this.options.nextLink.tween('opacity', 0);
			(function() {
				this.options.element.tween('left', (-1 * offset));
				this.succ[dir].image.get('tween').start('left', 0).chain(function() {
					this.options.element = this.succ[dir].image;
					this.succ[dir].image = null;
					
					this.options.prevLink.tween('opacity', 1);
					this.options.nextLink.tween('opacity', 1);
					
					this.move(dir);
				}.bind(this));
			}).bind(this).delay(150);
		} else {
			this.fireEvent(({prev: 'onBeginReached', next: 'onEndReached'})[dir], evt);
		}
	},
	
	move: function(dir) {
		dir = dir || ''; 
		this.options.current += ({prev: -1, next: 1, '': 0})[dir];
		
		this.succ.prev.index = (this.options.current == 0 ? null : this.options.current - 1);
		this.succ.next.index = (this.options.current == this.options.images.length - 1 ? null : this.options.current + 1);
		
		['prev', 'next'].each(function(d) {
			if (this.succ[d].index) {
				this.succ[d].image = new Asset.image(this.options.images[this.succ[d].index]);
			}
		}.bind(this));		
	}
	
});


