/**
 * Class : mooFader
 * ----------------
 * Author : Chris Sewell
 * Created on : 12/5/11
 * Description : fades between a list of elements
 */
var mooFader = new Class({
	Implements: [Events,Options],
	options: {
		duration: 3000
	},
	current_slide: 0,
	/**
	 * Initialises the class
	 */
  initialize: function(container, options) {
  	// Create error console for IE output
  	this.error_console = new Element('div', {id:'error_console'});
  	this.error_console.inject($(document.body), 'bottom');
  	// set options and standard elems
		this.setOptions(options);
  	this.container = container;
  	this.slides = container.getChildren();
  	
  	// hide all slides apart from the first one
  	Array.each(this.slides, function(value, key){
  		if(key>0){
  			var zindex = 1000 + key;
  			this.slides[key].setStyles({
  				'visibility': 'hidden',
  				'z-index': zindex
  			});
  		}
  	}, this);
  	this.animation = this.show_next.periodical(this.options.duration, this);
	},
	
	/**
	 * Stop the slideshow
	 */
	stop: function(){
		clearInterval(this.animation);
	},
	/**
	 * Tag an error to the bottom of the error console
	 */
	add_error: function(err){
		var p = new Element('p', {'html': err});
		p.inject(this.error_console, 'bottom');
	},
	
	/**
	 * Shows the next slide then increments the counter.
	 */
	show_next: function(){
		var prev = this.current_slide;
		this.current_slide = this.current_slide < this.slides.length - 1 ? this.current_slide + 1 : 0 ;
		this.slides[this.current_slide].setStyles({
			'visibility': 'visible',
			'opacity': 0
		});
		this.slides[prev].tween('opacity', 1, 0);
		this.slides[this.current_slide].tween('opacity', 0, 1);
	}
});
