var imageSlideshow = new Class({
	Implements: Options,
	
	options: {
		duration: 1000,
		wait: 4000,
		changeEmltHeight: false
	},
	
	current: null,
	count: 0,	
	fx: {},
	
	initialize: function(elmt,options){
		this.setOptions(options);
		
		this.elmt = elmt;
		this.fx['emlt'] = new Fx.Tween(this.elmt);
		
		this.images = this.elmt.getElements('img');
		this.count = this.images.length;
		
		if (this.count > 1) {
			this.elmt.setStyles({
				'overflow': 'hidden',
				'width': this.images[0].width,
				'height': this.images[0].height
			});
			this.images.setStyles({
				'position': 'absolute',
				'opacity': 0
			});
			
			this.setImage(0);
			this.changeImage.periodical(this.options.wait, this);
		}
	},
	
	nextI: function(){
		this.current++;
		if(this.current >= this.count){
			this.current = 0;
		}
		return this.current;
	},
	
	setImage: function(i){
		if(i < this.count){			
			if($type(this.images[this.current]) == 'element'){
				this.images[this.current].setStyle('opacity',0);
			}
			this.getFx(i).start('opacity',1);
			if (this.options.changeEmltHeight) {
				this.elmt.setStyle('height', this.images[i].height+'px');
			}
			this.current = i;
		}
	},
	
	getFx: function(i){
		if(!this.fx[i]){
			this.fx[i] = new Fx.Tween(this.images[i],{duration:this.options.duration});
		}
		return this.fx[i];
	},
	
	changeImage: function(i){
		if (this.current !== null) {
			this.getFx(this.current).start('opacity', 0);
		}		
		i = this.nextI();
		if (this.options.changeEmltHeight) {
			this.getFx('emlt').start('height', this.images[i].height);
		}		
		this.getFx(i).start('opacity',1);
	}
	
});

Element.implement({
	imageSlideshow: function (options){
		new imageSlideshow(this,options);
		return this;
	}
});



