/*       _    _ _  __  _____  _      _____ 
   ____ | |  | | |/ / |  __ \| |    / ____|
  / __ \| |  | | ' /  | |__) | |   | |     
 / / _` | |  | |  <   |  ___/| |   | |     
| | (_| | |__| | . \  | |    | |___| |____ 
 \ \__,_|\____/|_|\_\ |_|    |______\_____|
  \____/
*/

/**
 * Very simple div fade out/in effects using Mootools 1.2
 * @class
 * @version 0.1
 * @author Damien Churchill
 **/
var Rotater = new Class({
	Implements: [Options,Events],
	
	options: {
		period: 10000, //time set
		startDiv: null
	},
	
	initialize: function(divs, tabs, options) {
		this.setOptions(options);
		this.divs = divs;
		this.tabs = tabs;
		this.currentDiv = 0;
		this.rotating = null;
		this.divs.each(function(div, index) {
			var tab = this.tabs[index];
			tab.addEvent('mouseenter', this.onMouseEnter.bindWithEvent(this, div))
			tab.addEvent('mouseleave', this.onMouseLeave.bindWithEvent(this, div));
			if (index != this.currentDiv) div.fade(0);
		}, this);
	},
	
	/**
	 *
	 */
	rotate: function(index) {
		this.divs[this.currentDiv].fade(0);
		this.tabs[this.currentDiv].removeClass('active');
		this.currentDiv =  (this.currentDiv + 1 < this.divs.length) ? this.currentDiv + 1 : 0;
		this.divs[this.currentDiv].fade(1);
		this.tabs[this.currentDiv].addClass('active');
	},
	
	start: function() {
		this.rotating = this.rotate.periodical(this.options.period, this);
	},
	
	stop: function() {
		$clear(this.rotating);
	},
	
	onMouseEnter: function(e, div) {
		this.stop();
		this.divs.each(function(d, index) {
			if (d == div) {
				d.fade(1)
				this.tabs[index].addClass('active');
				this.currentDiv = index;
			} else {
				d.fade(0);
				this.tabs[index].removeClass('active');
			}
		}, this);
	},
	
	onMouseLeave: function(e) {
		this.start();
	}
});

window.addEvent('domready', function() {
	if (!$('slideshow')) return;
	var slides = $$('#slideshow .slide');
	slides.reverse();
	var rotater = new Rotater(slides, $$('#slideshow #tabs li'));
	rotater.start();
})
