var Crossfader = Class.create({
	initialize: function(element, fadeDuration, fadePause, autostart, autogrow) {
		if(Object.isUndefined(fadeDuration)) fadeDuration = 1;
		if(Object.isUndefined(fadePause)) fadePause = 1;
		this.autogrow = (autogrow)? true : false;
		this.container = $(element);
		this.imgs = $(element).select('img');
		this.reset();
		this.index = 0;
		this.imgs[this.index].xOpacity = 1;
		this.imgs[this.index].setOpacity(this.imgs[this.index].xOpacity).show();
		if(this.autogrow) {
			this.container.setStyle({ height: this.imgs[this.index].getHeight() + 'px' });
		}	
		this.fadeTimeout = fadeDuration * 50;
		this.pauseTimeout = fadePause * 1000;
		this.paused = (autostart)? false : true;
		this.direction = 1;
		this.timeout = null;
		if(!this.paused) this.timeout = window.setTimeout(this.fade.bind(this), this.pauseTimeout);
	},
	
	fade: function() {
		if(this.timeout != null) {
			window.clearTimeout(this.timeout);
			this.timeout = null;
		}
		
		if(this.runningIndex == null) {
			this.runningIndex = this.index;
		}
		if(this.runningDirection == null) {
			this.runningDirection = this.direction;
		}
		var newIndex = (this.runningIndex + this.runningDirection + this.imgs.length) % this.imgs.length;
		
		if(this.autogrow) {
			if(this.container.getHeight() < this.imgs[newIndex].getHeight()) {
				this.container.setStyle({ height: this.imgs[newIndex].getHeight() + 'px' });
			}
		}
		
		this.imgs[this.runningIndex].xOpacity -= .05;
		this.imgs[newIndex].xOpacity += .05;
		
		this.imgs[this.runningIndex].xOpacity = Math.max(Math.min(this.imgs[this.runningIndex].xOpacity, 1), 0);
		this.imgs[newIndex].xOpacity = Math.max(Math.min(this.imgs[newIndex].xOpacity, 1), 0);
		
		this.imgs[newIndex].show();
		this.imgs[this.runningIndex].setOpacity(this.imgs[this.runningIndex].xOpacity);
		this.imgs[newIndex].setOpacity(this.imgs[newIndex].xOpacity);
	
		var func = this.fade.bind(this);
		if(this.imgs[this.runningIndex].xOpacity <= 0) {
			this.imgs[this.runningIndex].hide();
			this.index = newIndex;
			if(this.autogrow) this.container.setStyle({ height: this.imgs[this.index].getHeight() + 'px' });
			this.runningIndex = null;
			this.runningDirection = null;
			if(!this.paused) {
				this.timeout = window.setTimeout(func, this.pauseTimeout);
			}
		} else {
			this.timeout = window.setTimeout(func, this.fadeTimeout);
		}
	},
	
	reset: function() {
		this.pause();
		if(this.timeout != null) {
			window.clearTimeout(this.timeout);
			this.timeout = null;
		}
		this.imgs.each(function(img) {
			img.xOpacity = 0;
			img.setOpacity(img.xOpacity).hide();
		});
		this.runningDirection = null;
		this.runningIndex = null;
	},
	
	toggle: function() {
		if(this.paused) {
			this.resume();
		} else {
			this.pause();
		}
	},
	
	resume: function() {
		this.paused = false;
		this.fade();
	},
	
	pause: function() {
		this.paused = true;
		if(this.timeout != null && this.runningIndex == null) {
			window.clearTimeout(this.timeout);
			this.timeout = null;
		}
	},
	
	setDirection: function(direction) {
		this.direction = (parseInt(direction) > 0)? 1 : -1;
		if(this.paused) this.resume();
	},
	
	show: function(index) {
		this.reset();
		this.index = this.imgs[index]? index : 0;
		if(this.autogrow) this.container.setStyle({ height: this.imgs[this.index].getHeight() + 'px' });
		this.imgs[this.index].xOpacity = 1;
		this.imgs[this.index].setOpacity(this.imgs[this.index].xOpacity).show();
	}
});