var fade = new Class({
    initialize: function(element, options)
	{
		this.setOptions({
		fadeInDuration: 1000,
		fadeOutDuration: 1000,
		showDuration: 4000,
		random: true
		}, options);
		
		this.element = element;
		this.images = this.element.getChildren();
		this.currentImage = 0;
		
		if(this.options.random) this.images.sort(this.randomSort);

		if(this.images.length > 1)
		{
			this.hide();
			this.fadeIn();
		}
    }
	,
	hide: function()
	{
		this.images.each(function(img){
		img.setStyle('display', 'none');
		img.setOpacity(0);
		});
	}
	,
	showImage: function()
	{
		this.fadeOut.delay(this.options.showDuration, this);
	}
	,
	nextImage: function()
	{
		this.images[this.currentImage].setStyle('display', 'none');
		if(this.currentImage < (this.images.length-1)) this.currentImage++; else this.currentImage = 0;
		this.fadeIn();
	}
	,
	fadeIn: function()
	{
		this.images[this.currentImage].setStyle('display', 'block');
		this.images[this.currentImage].effect('opacity', {
											  duration: this.options.fadeInDuration,
											  onComplete: this.showImage.bind(this)
											  }).start(0, 1);
	}
	,
	fadeOut: function()
	{
		this.images[this.currentImage].effect('opacity', {
											  duration: this.options.fadeOutDuration,
											  onComplete: this.nextImage.bind(this)
											  }).start(1, 0);
	},
	randomSort: function()
	{
		return (Math.round(Math.random())-0.5);
	}
});
fade.implement(new Events);
fade.implement(new Options);