var celciusSlideshow = new Class({
    options: {
	showDuration: '',
	legende: '',
	legendeClass: '',
	thumbs: '',
	controls: ''
    },
    
    Implements: [Options,Events],
    
    initialize: function(container,elements,options) {
    	this.setOptions(options);
	this.container = $(container);
	this.elements = $$(elements);
	this.currentIndex = 0;
	this.interval = '';
	this.showDuration = this.options.showDuration;
	this.legende = this.options.legende;
	this.legendeClass = this.options.legendeClass;
	this.thumbs = this.options.thumbs;
	this.controls = $(this.options.controls);
	this.childAmount = 0;
	this.childWidth = 0;
	this.childMargin = 0;
	
	this.elements.each(function(el,i){
	    if(i > 0) el.set('opacity',0);
	},this);

	if( this.legende != "" && this.legendeClass ){
		$$(this.legendeClass).each( function( el ){
			el.addEvent( 'click', function(){
				this.stop(); 
				this.show( (el.id).replace( this.legende, "" ) );
			}.bind(this));
		}.bind(this));
	}
	
	if( this.thumbs != "" ){
		this.childAmount = ($(this.thumbs).getChildren().length).toInt();
		this.childMargin = ($(this.thumbs).getChildren()[1].getStyle('marginLeft')).toInt();	
		this.childWidth  = ($(this.thumbs).getChildren()[0].getSize().x).toInt();		
		$(this.thumbs).setStyle( 'width', ( this.childAmount * ( this.childWidth + this.childMargin ) ) - this.childMargin );
	}
	
	if( this.controls ){
		(this.controls.getChildren('img'))[0].addEvent( 'click', function(){ this.prev(); }.bind( this ) );
		(this.controls.getChildren('img'))[1].addEvent( 'click', function(){ this.next(); }.bind( this ) );		
	}
    },
    
    show: function(to) {
	this.elements[this.currentIndex].fade('out');
	this.setLegende( this.currentIndex, 'off' );
	this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex + 1 : 0));
	this.elements[this.currentIndex].fade('in');
	this.setLegende( this.currentIndex, 'on' );
	if( this.thumbs != "" ){
		myFx = new Fx.Scroll( $(this.thumbs).getParent().get('id') ).start( ( ( this.childWidth + this.childMargin ) * this.currentIndex ), 0 );
	}
    },
    
    start: function() {
	this.interval = this.show.bind(this).periodical(this.showDuration);
    },
    
    stop: function() {
	$clear(this.interval);
    },
    
    next: function() {
    	this.stop();
    	this.show();
    },
    
    prev: function() {
	this.stop();
	this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
    },
    
    setLegende: function( i, state ){
    	if( this.legende != "" ){
		target = this.legende + i;
		newSRC = $(target).getProperty( 'src' ).replace( "_on", "_STATE" ).replace( "_off", "_STATE" ).replace( "_STATE", "_" + state );
		$(target).setProperty( 'src', newSRC );
	}
    }
});
