// JavaScript Document

Slideshow = new Class({

  Implements: [Options],
	
	options: {
		slide: '.slide',
		duration: 500,
		wait: 7000
	},
	
	initialize: function(container, options){
		this.container = $(container);
		this.setOptions(options);
		
		this.slides = this.container.getElements(this.options.slide);
		this.current = 0;
		this.prev = 0;
		this.count = this.slides.length;
		this.slideFx = new Array();
		
		// exit 
		if (this.count < 2) return this;
		
		// slideFx
		this.slides.each(function(slide, index){
		  this.slideFx[index] = new Fx.Tween(slide, { property: 'opacity', duration: this.options.duration, link: 'chain' });
			this.slideFx[index].set(0);
		}, this);
		
		// initialize first slide
		this.slideFx[this.current].set(1);
		
		// run slideshow
		this.repeater = this.next.periodical(this.options.wait, this);
		
		
	},
	
	next: function(){
    this.prev = this.current;
		this.current = (this.current + 1)%this.count;
		this.slideFx[this.prev].start(0);
		this.slideFx[this.current].start(1);
	}

});

window.addEvent('domready', function(){

  if ($('news')){
		new Slideshow('news', { slide: '.item' });
	}
});