var ss = Class.create({
	initialize: function(element, options) {		
		this.options = {
      		Duration: 2,
			Delay: 7.5,
			Random: false,
			Slideshow:true,
			Controls:true
    	}
		Object.extend(this.options, options || {});

    	this.element        = $(element);
		this.slides			= this.element.childElements();
		this.num_slides		= this.slides.length;		
		this.current_slide 	= (this.options.Random) ? (Math.floor(Math.random()*this.num_slides)) : 0;
		this.end_slide		= this.num_slides - 1;
		
		this.slides.invoke('hide');
		this.slides[this.current_slide].show();
				
		if (this.options.Slideshow) { 
			this.startSlideshow();
		}				
		if (this.options.Controls) {
			this.addControls();
		}		
	},
	
	addControls: function() {

$('con').select('.back').invoke('observe', 'click', this.moveToPrevious.bindAsEventListener(this));
$('con').select('.next').invoke('observe', 'click', this.moveToNext.bindAsEventListener(this));
$('con').select('.star').invoke('observe', 'click', this.startSlideshow.bindAsEventListener(this));
$('con').select('.stop').invoke('observe', 'click', this.stopSlideshow.bindAsEventListener(this));
//old
	//$('ctrl').select('div.back').invoke('observe', 'click', this.moveToPrevious.bindAsEventListener(this));
	//$('ctrl').select('div.next').invoke('observe', 'click', this.moveToNext.bindAsEventListener(this));
	//$('ctrl').select('div.star').invoke('observe', 'click', this.startSlideshow.bindAsEventListener(this));
	//$('ctrl').select('div.stop').invoke('observe', 'click', this.stopSlideshow.bindAsEventListener(this));
},

	startSlideshow: function() {
		if (!this.running)	{
			this.executer = new PeriodicalExecuter(function(){
	  			this.updateSlide(this.current_slide+1);
	 		}.bind(this),this.options.Delay);
			this.running=true;
		}
	},
	
	stopSlideshow: function() {	
		if (this.executer) { 
			this.executer.stop();
			this.running=false;
		}	 
	},

	moveToPrevious: function (event) {
		this.stopSlideshow();
  		this.updateSlide(this.current_slide-1);
		//this.startSlideshow();//added
	},

	moveToNext: function (event) {
		this.stopSlideshow();
  		this.updateSlide(this.current_slide+1);
		//this.startSlideshow();//added
	},
	
	updateSlide: function(next_slide) {
		
		if (next_slide > this.end_slide) { 
				next_slide = 0; 
		} 
		else if ( next_slide == -1 ) {
				next_slide = this.end_slide;
		}
		
		this.fadeInOut(next_slide, this.current_slide);		
	},

 	fadeInOut: function (next, current) {		
		new Effect.Parallel([
			new Effect.F(this.slides[current], { sync: true }),
			new Effect.A(this.slides[next], { sync: true }) 
  		], { duration: this.options.Duration });
		
		this.current_slide = next;		
	}

});