var Slideshow = new Class({
	Implements: [Options, Chain],
	options: {
		id: 'slideshow',
		startimg: 1,
		endimg: 5,
		duration: 2000,
		el: null,
		img: null,
		currimg: null,
		imgs: [null, null],
		fx: null
	},
	initialize: function(options){
		this.setOptions(options);
		this.options.el = $(this.options.id);
		this.options.currimg = this.options.startimg;
	},
	start: function(){
		if (this.set_elements())
		{
			setTimeout(this.progress.bind(this), this.options.duration);
		}
	},
	progress: function()
	{
		if (this.options.currimg == this.options.endimg) this.options.currimg = this.options.startimg - 1;
		this.options.currimg++;
		var i = this.options.currimg;
		if (i < 10) i = '0' + i;
		var img = new Asset.images(['/images/ss/' + i + '.jpg'], {onComplete: function(){
			this.chain(function(){this.options.imgs[1].src = img[0].src;});
			this.chain(function(){this.options.fx.start('opacity', 1, 0);});
			this.chain(function(){this.options.fx.set('opacity', 1);});
			this.chain(function() {this.options.imgs[0].src = this.options.imgs[1].src;});
			this.chain(function() {this.progress();});
			//this.chain(function(){setTimeout(this.progress.bind(this), this.options.duration);});
			this.callChain();
			this.callChain.delay(3000, this);
			this.callChain();
			this.callChain();
			this.callChain.delay(this.options.duration + (3000 + 1000), this);
		}.bind(this)});
	},
	do_switch: function()
	{
		this.options.imgs[0].fade(0);
		this.options.currimg++;
	},
	set_elements: function()
	{
		if (this.options.el == null) return this.error('Missing HTML container element!');
		// get first img el
		this.options.imgs[0] = this.options.el.getFirst();
		if (this.options.imgs[0] == null) return this.error('Missing container\'s image element');
		// create second image element
		this.options.imgs[1] = new Element('img', {'src': '', 'alt': '', 'width': 987, 'height': 544});
		this.options.el.grab(this.options.imgs[1]);
		this.set_styles();
		this.options.fx = new Fx.Tween(this.options.imgs[0], {duration: 3000});
		return true;
	},
	set_styles: function()
	{
		if (this.options.imgs[0] != null && this.options.imgs[1] != null)
		{
			this.options.imgs[0].setStyle('position', 'absolute');
			this.options.imgs[1].setStyle('position', 'absolute');
			this.options.imgs[0].setStyle('z-index', 1000);
		}
	},
	error: function(msg)
	{
		alert(msg);
		return false;
	}
});