/*
	Slideshow v0.1 for Reduce the Risk (www.reducetherisk.co.nz)
	Based on Slideshow v0.1 for Living Roofs (www.livingroofs.org.nz)
	Requires MooTools 1.2
	
	Author: Andrew Ferri <andrew@blacksheepcreative.co.nz>
	Date: 24 November 2009
*/

var Slideshow = new Class({
	Implements: [Options, Events],
	
	options: {
		transition: Fx.Transitions.Sine.easeOut,
		pauseLength: 7000,
		imageTransitionSpeed: 900,
		textTransitionSpeed: 400
	},
	
	initialize: function(containers,options)
	{
		this.setOptions(options);
		containers.each(function(ul){
			this.createSlideshow(ul);
		}.bind(this));
	},
	
	createSlideshow: function(ul)
	{
		ul.getElements('li').each(function(el){ this.prepLi(el); }.bind(this));
		ul.store('currentLi',ul.getElement('li:first-child'));
		this.setActive(ul.retrieve('currentLi'));
		var func = function(){
			this.runSlideshow(ul);
		};
		func.periodical(this.options.pauseLength,this);
	},
	
	runSlideshow: function(ul)
	{
		if (ul.retrieve('currentLi').getNext())
		{
			var nextLi = ul.retrieve('currentLi').getNext();
		} else
		{
			var nextLi = ul.getElement('li:first-child');
		}
		
		ul.retrieve('currentLi').hide();
		nextLi.show();
		ul.store('currentLi',nextLi);
	},
	prepLi: function(li)
	{
		li.addClass('active');
		li.setStyles({
		'opacity':	0,
		'z-index':	10
		});
		//li.getElement('p').setStyle('bottom',-40);
		var fx = new Fx.Morph(li, {duration: this.options.imageTransitionSpeed, transition: this.options.transition});
		var textFx = new Fx.Morph(li.getElement('p'), {duration: this.options.imageTransitionSpeed, transition: this.options.transition});
		li.hide = function(){
			fx.start({'opacity':0});
			//textFx.start({'bottom':-40});
		};
		li.show = function(){
			fx.start({'opacity':1});
			//textFx.start({'bottom':0});
		};
	},
	
	setActive: function(li)
	{
		li.setStyles({
		'opacity':	1,
		'z-index':	5
		});
		li.getElement('p').setStyle('bottom',0);
	}
});
window.addEvent('domready',function(){ var slideshow = new Slideshow($('slideshow').getElements('ul')); });
