var Carousel = new Class({
	
	initialize: function(container, options){
		this.setOptions({
			display: 5000
		}, options);
		
		this.i = 0;
		
		this.container = container;
		
		this.start();
		
		this.container.getElements('.toggle a').each(function(toggle, i){
			toggle.addEvent('mouseover', function(e){
				new Event(e).stop();
				if (!this.stopped) {
					this.i = i;
					this.show(i);
				}
			}.bind(this)).addEvent('click', function(e){
				new Event(e).stop();
				this.i = i;
				this.show(i);
				this.stop();
			}.bind(this));
		}.bind(this));
	},
	
	start: function() {
		this.turner = (function(){ this.turn(); }.bind(this)).periodical(this.options.display);
		this.stopped = false;
	},
	
	stop: function() {
		$clear(this.turner);
		this.stopped = true;
	},
	
	turn: function() {
		this.i = (this.i+1 < this.container.getElements('.panel').length) ? this.i+1 : 0;
		this.show(this.i);
	},
	
	show: function(i) {
		this.container.getElements('.panel').each(function(panel, p){
			if (i == p) {
				panel.setStyle('display', 'block');
			} else {
				panel.setStyle('display', 'none');
			}
		});
		this.container.getElements('.toggle a').each(function(toggle, t){
			if (i == t) {
				toggle.addClass('selected');
			} else {
				toggle.removeClass('selected');
			}
		});
	}
	
});

Carousel.implement(new Events, new Options);