var Slideshow = Class.create({
	
	currentImage: null,
	container: null,
	duration: 5,
	transitionLength: 1.5,
	_executer: null,
	images: null,
	
	initialize: function(container){
		this.container = $(container);
		this.images = this.container.childElements();
		this.currentImage = 0;
		this.run();
	},

	run: function(){
		this._executer = new PeriodicalExecuter(this._run.bindAsEventListener(this), this.duration);
	},
	
	_run: function(){
		if(this.images.length < 2){
			return;
		}
		var currentEl = this.images[this.currentImage];
		var c = this.currentImage+1;
		if(c == (this.images.length)){
			c = 0;
		}
		currentEl.fade({duration: this.transitionLength});
		this.currentImage = c;
		this.images[c].appear({duration: this.transitionLength});
	},
	
	deleteImage: function(e){
		var el = e.element();
		if(!confirm('Are you sure you want delete: '+el.up(1).siblings()[0].down('a').innerHTML+'?')){
			return false;
		}
		var image_id = el.id.split('-')[1];
		new Ajax.Request('slideshow.php', {
			method: 'get',
			parameters: {
				action: 'delete',
				image_id: image_id
			}
		});
		el.up('tr').fade();
		var img = $('image-'+image_id);
		if(img.visible()){
			this._run();
		}
		img.remove();
		return false;
	}
	
});


