

Slideshow = new Class({
	Implements: Options,
	options: {
		container: null,
		delay: 6000,
		duration: 1500,
		slides: []
	},

	slides: 	[],
	container:	null,
	image:		null,

	_timer: 	null,
	_tween: 	null,
	_index: 	0,
	_run_flag: 	false,
	_pause_flag: 	false,
	_solo_flag:	false,

	initialize: function(options) {
		this.setOptions(options);

		this.container = $(this.options.container); if(!this.container) return false;
		this.image = this.container.getElement('.slideshow-image-container'); if(!this.image) return false;

		for(var i=0; i<this.options.slides.length; ++i) {
			this.slides.push( new Slideshow_Slide(this.options.slides[i]) );

			new Element('div', { 'class': 'ss-bullet ss-bullet-'+i })
			.store('slideshow', this)
			.store('slideshow-index', i)
			.addEvents({
				mouseover: function() { this.addClass('over'); },
				mouseout: function() { this.removeClass('over'); },
				click: function() { this.retrieve('slideshow').go(this.retrieve('slideshow-index')) }
			})
			.inject( this.container.getElement('.slideshow-nav') );
		}
		if(this.slides.length < 1) return;
		else if(this.slides.length == 1) this._solo_flag = true

		this.image.setStyles({
			backgroundImage: 'url('+this.slides[0].image.src+')',
			backgroundPosition: 'center center'
		});
		this._update(100);

		this.container.addEvents({
			mouseenter: this.pause.bind(this),
			mouseleave: this.resume.bind(this)
		});

		this.start();
	},

	stop: function() {
		this._clear_timer();
		this._run_flag = false;
	},

	start: function() {
		if(this._solo_flag) this.stop();
		this._run_flag = true;
		this._set_timer();
	},

	pause: function() {
		this._clear_timer();
		this._pause_flag = true;
	},

	resume: function() {
		this._pause_flag = false;
		this._set_timer();
	},

	prev: function() {
		this._clear_timer();
		--this._index;
		if(this._index < 0) this._index = this.slides.length - 1;
		this._transition();
	},

	next: function() {
		this._clear_timer();
		++this._index;
		if(this._index >= this.slides.length) this._index = 0;
		this._transition();
	},

	go: function(n) {
		if(this.slides[n]) this._index = n;
		this._transition();
	},

	_set_timer: function() {
		if(this._run_flag && !this._pause_flag) {
			this._clear_timer();
			this._timer = setTimeout(function() {
				this.next();
			}.bind(this), this.options.delay);
		}
	},

	_clear_timer: function() {
		clearTimeout(this._timer);
	},

	_transition: function() {
		if(this._solo_flag) this.stop();

		this._clear_timer();
		try { this._tween.cancel() } catch(e) {};
		this.image.empty();

		var nimg = this.image.clone().setStyles({
			backgroundImage: 'url('+this.slides[this._index].image.src+')',
			opacity: 0,
			float: 'none',
			margin: 0
		}).inject(this.image);

		this._tween = new Fx.Tween(nimg, {
			duration: this.options.duration,
			link: 'ignore',
			onStart: function() { this._update() }.bind(this),
			onComplete: function() {
				this.image.setStyle('backgroundImage', nimg.getStyle('backgroundImage'));
				nimg.destroy();
				if(this._run_flag && !this._pause_flag) this._set_timer();
			}.bind(this),
			onCancel: function() { this.fireEvent('complete') }
		}).start('opacity', 1);
	},

	_update: function(setdur) {
		var dur = this.options.duration / 2;
		if(setdur) dur = setdur;

		var c = new Chain();
		c.chain(function() {
			var sel = [
				'#'+this.container.id+' .slideshow-caption',
				'#'+this.container.id+' .slideshow-text',
				'#'+this.container.id+' .slideshow-link'
			];

			new Fx.Elements( $$( sel.join(',') ), {
				duration: dur,
				onComplete: function() {
					c.callChain();
				}
			}).start({  0: { opacity: 0 }, 1: { opacity: 0 }, 2: { opacity: 0 }  });
		}.bind(this)).chain(function() {

			var curslide = this.slides[this._index];
			this.container.getElements('.slideshow-caption').set('html', curslide.caption);
			this.container.getElements('.slideshow-text').set('html', curslide.text);

			this.container.getElements('.slideshow-link')
				.set('html', (curslide.link) ? curslide.link + ' &raquo; ' : '')
				.store('href', curslide.href)
				.addEvent('click', function() {
					if(this.retrieve('href')) document.redirect(this.retrieve('href'))
				});

			new Fx.Elements( $$( '#'+this.container.id+' .slideshow-caption, #'+this.container.id+' .slideshow-text, #'+this.container.id+' .slideshow-link'), {
				duration: dur,
				onComplete: function() {
					c.callChain();
				}
			}).start({  0: { opacity: 1 }, 1: { opacity: 1 }, 2: { opacity: 1 }  });

		}.bind(this)).callChain();

		if(this._solo_flag) this.container.getElements('.slideshow-nav .ss-bullet').setStyle('display', 'none');
		else {
			this.container.getElements('.slideshow-nav .ss-bullet').removeClass('on');
			this.container.getElements('.slideshow-nav .ss-bullet-'+this._index).addClass('on');
		}
	}

});

Slideshow_Slide = new Class({
	initialize: function(slide) {
		this.src	= slide.src;
		this.caption	= slide.caption;
		this.text	= slide.text;
		this.link	= slide.link;
		this.href	= slide.href;

		this.image = new Image();
		this.image.src = this.src;
	}
});




