var SimpleSlideShowDemo = new Class({
	Implements: [Options, Events],
	options: {
		slides: [],
		startIndex: 0,
		wrap: true,
		nextLink: false,
		prevLink: false,
		autoStart: false
		//onShow: $empty
	},
	initialize: function(options){
		this.setOptions(options)
		this.addSlides(this.options.slides);
		this.setUpNav();
		this.setCounters();
		if(this.slides.length) this.showSlide(this.options.startIndex);	
		
		//if autoStart is set to true auto cycle without buttons
		if (this.options.autoStart) {
		    this.cycleForward.bind(this).periodical(7000);			    
            }
	},
	
	slides: [],
	
	addSlides: function(slides){
		$$(slides).each(function(slide){
			this.slides.include($(slide));
			slide.addEvent('click', this.cycleForward.bind(this));
		}, this);
	},
	
	setCounters: function(){
			if ($(this.options.currentIndexContainer))$(this.options.currentIndexContainer).set('html', this.now+1);
			if ($(this.options.maxContainer))$(this.options.maxContainer).set('html', this.slides.length);
		},
	
	addSlide: function(slide){
		this.addSlides($splat($(slide)));
	},
	
	cycleForward: function(){
		if($chk(this.now) && this.now < this.slides.length-1) this.showSlide(this.now+1);
		else if ((this.now) && this.options.wrap) this.showSlide(0);
		else if(!$defined(this.now)) this.showSlide(this.options.startIndex);
	},
	
	cycleBack: function(){
		if(this.now > 0) this.showSlide(this.now-1);
		else if(this.options.wrap) this.showSlide(this.slides.length-1);
	},
	
	
	setUpNav: function(){	
			if ($(this.options.nextLink)) {
				$(this.options.nextLink).addEvent('click', function(){
					this.forward();
				}.bind(this));
			}
			if ($(this.options.prevLink)) {
				$(this.options.prevLink).addEvent('click', function(){
					this.back();
				}.bind(this));
			} 
		},
   
		
	disableLinks: function(now){
		if (this.options.wrap) return;
		now = $pick(now, this.now);
		var prev = $(this.options.prevLink);
		var next = $(this.options.nextLink);
		var dlc = this.options.disabledLinkClass;
		if (now > 0) {
			if (prev) prev.removeClass(dlc);
			if (now === this.slides.length-1 && next) next.addClass(dlc);
			else if (next) next.removeClass(dlc)
		}	else { //now is zero
			if (this.slides.length > 0 && next) next.removeClass(dlc);
			if (prev) prev.addClass(dlc);
		}
	},
	
	forward: function(){
		var fireEvent = false;
		if ($type(this.now) && this.now < this.slides.length-1) fireEvent = this.showSlide(this.now+1);
		else if ($type(this.now) && this.options.wrap) fireEvent = this.showSlide(0);
		else if (!$type(this.now)) fireEvent = this.show(this.options.startIndex);
		if (fireEvent) this.fireEvent('onNext');
		return this;
	},
	
	back: function(){
		if (this.now > 0) {
			this.showSlide(this.now-1);
			this.fireEvent('onPrev');
		} else if (this.options.wrap && this.slides.length > 1) {
			this.showSlide(this.slides.length-1);
			this.fireEvent('onPrev');
		}
		return this;
	},
	
	showSlide: function(iToShow){
	 
		if (this.fading) return;
		var now = this.now;		
		var currentSlide = this.slides[now];
		var slide = this.slides[iToShow];
		var fadeIn = function (s){
			this.fading = true;
			s.setStyles({
				display:'block',
				visibility: 'visible',
				opacity: 0
			});
			s.get('tween').start('opacity', 1).chain(function(){
				this.fading = false;
				this.fireEvent('onShow', [slide, iToShow]);
			}.bind(this));
		}.bind(this);
		
		if(slide) {
			if($chk(now) && now != iToShow){
				this.fading = true;				
				currentSlide.get('tween').start('opacity', 0).chain(function(){
					currentSlide.setStyle('display', 'none');
					fadeIn(slide);
				}.bind(this));
			} else fadeIn(slide);
			this.now = iToShow;
		}
	}
});

