/**
 * eFocus FX Base SlideShow class for MooTools 1.2.1
 * Reusable slideshow class.
 *
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @param arrSlides, intStartSlide, blnLoop, intInterval, blnAutoPlay, blnPlaying
 * @param strOutEffectProperty, intOutEffectStartValue, intOutEffectEndValue, intOutEffectDuration
 * @param strInEffectProperty, intInEffectStartValue, intInEffectEndValue, intInEffectDuration
 * @return EfxBaseSlideShow
 * @version 1.0
 */
var EfxBaseSlideShow = new Class({
	Implements: [Options, Events, Chain],
	options: {
		arrSlides: [],
		intStartSlide: 0,
		blnLoop: true,
		intInterval: 2500,
		blnAutoPlay: true,
		blnPlaying: false,
		
		strOutEffectProperty: 'opacity',
		intOutEffectStartValue: 1,
		intOutEffectEndValue: 0,
		intOutEffectDuration: 'long',
		
		strInEffectProperty: 'left',
		intInEffectStartValue: 690,
		intInEffectEndValue: 0,
		intInEffectDuration: 'long'
	},
	initialize: function(options) {
		var that = this;
		this.setOptions(options);
		this.addSlides(this.options.arrSlides);
		this.options.arrSlides.each(function(slide){
//			slide.setStyle('visibility', 'hidden');
		});
		this.showSlide(this.options.intStartSlide);
		if (this.options.blnAutoPlay == true && this.options.blnPlaying == false) {
			this.playSlideShow();
		}
		this.intSlideCount = 0;
	},
	arrSlideCollection: [],
	addSlides: function(arrSlides){
		arrSlides.each(function(slide){
			this.arrSlideCollection.include(slide);
		}, this);
	},
	addSlide: function(slide){
		this.addSlides($splat(slide));
	},
	cycleForwards: function(){
		if ($chk(this.intCurrentSlideNumber) && this.intCurrentSlideNumber < this.arrSlideCollection.length-1) {
			this.showSlide(this.intCurrentSlideNumber+1);
		} else if ((this.intCurrentSlideNumber) && this.options.blnLoop) {
			this.showSlide(0);
		} else if (!defined(this.intCurrentSlideNumber)) {
			this.showSlide(this.options.intStartSlide);
		}
	},
	cycleBackwards: function(){
		if (this.intCurrentSlideNumber > 0) {
			this.showSlide(this.intCurrentSlideNumber-1);
		} else if (this.options.blnLoop) {
			this.showSlide(this.arrSlideCollection.length-1);
		}
	},
	showSlide: function(intSlideNumberToShow){
		if (this.arrSlideCollection[intSlideNumberToShow]) {
			var that = this;
			if (this.arrSlideCollection[this.intCurrentSlideNumber]) {
				var intPreviousSlideNumber = this.intCurrentSlideNumber;
				if (that.options.strOutEffectProperty != that.options.strInEffectProperty) {
					this.arrSlideCollection[intSlideNumberToShow].setStyle(that.options.strOutEffectProperty, that.options.intOutEffectStartValue);
				}
				this.arrSlideCollection[this.intCurrentSlideNumber].get('tween', {property: that.options.strOutEffectProperty, duration: that.options.intOutEffectDuration}).start(that.options.intOutEffectStartValue, that.options.intOutEffectEndValue).chain(function(){
					that.arrSlideCollection[intPreviousSlideNumber].setStyle(that.options.strInEffectProperty, that.options.intInEffectStartValue);
				});
			}
			this.arrSlideCollection[intSlideNumberToShow].get('tween', {property: that.options.strInEffectProperty, duration: that.options.intInEffectDuration}).start(that.options.intInEffectStartValue, that.options.intInEffectEndValue);
			this.intCurrentSlideNumber = intSlideNumberToShow;
		}
		this.fireEvent('slideShown', this);
	},
	runSlideShow: function(){
		var that = this;
		
		if (that.intCurrentSlideNumber != this.intSlideCount) {
			this.intSlideCount = that.intCurrentSlideNumber;
		}
		this.intSlideCount++;
		if (this.intSlideCount >= that.arrSlideCollection.length) {
			this.intSlideCount = 0;
		}			
		this.showSlide(this.intSlideCount);
	},
	playSlideShow: function(){
		this.options.blnPlaying = true;
		this.slideShowTimer = this.runSlideShow.periodical(this.options.intInterval, this);
	},
	resetTimer: function(){
		if ($chk(this.slideShowTimer)) {
			this.slideShowTimer = $clear(this.slideShowTimer);
			this.playSlideShow();
		}
	},
	name: 'EfxBaseSlideShow'
});



