/*
Script: mooCase.js
	mooCase - mooCase version 1.1

License:
	MIT-style license.

Copyright:
	Copyright 2008
	Riaan Los
	http://www.riaanlos.nl

Based on MooTools v1.2 Core
	[The MooTools production team](http://mootools.net/developers/).


Usage:
	window.addEvent('domready', function() {
		var example1 = new mooCase('example1');
		var example2 = new mooCase('example2',{
			elements:'photos',
			timer:3,
			speed:5,
			transition:Fx.Transitions.Quart.easeIn
		});
	});

Keep smiling ;)
*/

var mooCase = new Class({

	Implements: [Events, Options],

	options: {
		elements:'elements',
		activeClassName:'active',
		timer:5,
		speed:10,
		transition:Fx.Transitions.Quart.easeOut
	},

	initialize: function(element,options){
		if($(element)){
			this.setOptions(options);
			this.elements = $$('#' + element + ' .' + this.options.elements + ' li');
			if(this.elements[0]==null){
				this.log('Invalid elements ' + this.options.elements);
				return false;
			}
			this.elements.set('tween',{
				duration:(this.options.speed*100),
				transition:this.options.transition,
				link:'cancel'
			});
			this.current = 0;
			this.start();
		} else {
			this.log('The container ' + element + ' does not exist');
		}
	},
	
	start: function(){
		this.elements.each(function(el,i){
			if(i!=0) el.setStyle('opacity',0);
		});
		var periodical = this.slide.periodical(this.options.timer * 1000, this);
	},
	
	slide: function(){
		this.fader(this.elements[this.current],0);
		if(this.elements[this.current] == this.elements.getLast()) {
			this.current = 0;
		} else {
			this.current++;
		}
		this.fader(this.elements[this.current],1);
	},
	
	fader: function(element,opacity){
		element.get('tween').start('opacity',opacity);
	},
	
	log: function(text, args) {
		if (window.console) console.log(text.substitute(args || {}));
	}

});