Array.implement({
    shuffle:function() {
        this.sort(function (x,y) { return Math.floor(Math.random()*3)-1; });
        return this;
    }

});

var BannerSlideshow = new Class({
	options: {
		showDuration: 6000
	},
	Implements: [Options,Events],
	initialize: function(container,elements,options) {
		//settings
		this.container = $(container);
		this.elements = $$(elements);
		this.currentIndex = 0;
		this.interval = '';

		//If we don't find the container do nothing.
		if(!this.container)
			return;
                this.elements.shuffle();
		//assign
		this.elements.each(function(el,i){
			if(i > 0) el.set('opacity',0);
		},this);

		//events
		this.container.addEvents({
			mouseenter: function() { this.stop(); }.bind(this),
			mouseleave: function() { this.start(); }.bind(this)
		});
	},
	show: function(to) {
		this.elements[this.currentIndex].fade('out');
		this.elements[this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].fade('in');
	},
	start: function() {
		this.interval = this.show.bind(this).periodical(this.options.showDuration);
	},
	stop: function() {
		$clear(this.interval);
	}

});


