//<![CDATA[
/*
	name : HorizontalTweenSlider
	author : gregory tomlinson
	© 2008 AOL LLC
    modified by Melwyn Furtado
	Date: 15 December 2008
	////////////////////////////////
	////////////////////////////////	
	dependencies : mootools 1.2
	////////////////////////////////
	////////////////////////////////		
*/
var HorizontalTweenSlider = new Class({
	
	Implements: [Options, Events],
	
	slides : [],
	currIndex : 0,
	autoPlayId : null,	
	slideObj : null,
	slideWidth : 0,
	sOffSet : 0,
	currPos : 0,
	carouselWidth : 0,
	
	options : {
		slides : [],
		wrapperElement : null, // the area to hide within
		innerElement : null, // the actual container to slide around
		prevBttn : null,
		nextBttn : null,
		duration : 1500,
		timerLength : 8000,		
		slideTransition : Fx.Transitions.Quart.easeInOut,
		autoPlay : true,
		shownSlideCount : 2,
		adjustDirection : 'left',
		shuffle : false
	},

	initialize : function (options)
	{
		this.setOptions(options);
		
		if( this.options.prevBttn ) this.addPrevBttn( this.options.prevBttn );
		if( this.options.nextBttn ) this.addNextBttn( this.options.nextBttn );	

		if( this.options.slides.length > this.options.shownSlideCount ) {
			if( this.options.shuffle ) this.shuffleSlides( this.options.slides );
			this.addSlides( this.options.slides );
			this.setSlideWidth();
			this.setOffSet();
		}
		this.carouselIllusion(); 
		this.createTransition();		
		this.startAutoPlay(); // start if true
	},
	
	carouselIllusion : function()
	{
		if( this.slides.length > this.options.shownSlideCount )
		{
			// make extras to create illusion of carousel
			this.addSet(this.options.innerElement);
			this.addSet(this.options.innerElement);	
			this.resetSlider();					
		}
		else
		{
			this.options.prevBttn.setStyle('display', 'none');
			this.options.nextBttn.setStyle('display', 'none');
		}
	},
	
	addSet : function( iElm )
	{
		this.slides.each( function( el ) {
			var c = el.clone().inject( iElm );
		});		
	},

	resetSlider : function()
	{
		// this centers the slides in the viewport
		this.currPos = -(this.slides.length * this.slideWidth) + this.sOffSet;
		this.currIndex = 0;
		this.options.innerElement.setStyle( 'left', this.currPos);
		this.carouselWidth = ((this.slideWidth*this.options.shownSlideCount) * (this.slides.length+2)) + (-this.currPos);
		this.options.innerElement.setStyle( 'width', this.carouselWidth);
	},
	
	setSlideWidth : function()
	{
		this.slideWidth = this.slides[this.currIndex+1].getPosition().x - this.slides[this.currIndex].getPosition().x; 		
	},
		
	setOffSet : function()
	{
		var pOffSet = this.options.wrapperElement.getPosition();
		var offSet = this.options.innerElement.getPosition();
		this.sOffSet =  offSet.x - pOffSet.x;
	},

	shuffleSlides : function ( slides )
	{
		//shuffles the html elements
		var randomSlide = slides.getRandom();
		var indx = slides.indexOf(randomSlide);
		var tmp = slides[0].innerHTML;
		slides[0].innerHTML = randomSlide.innerHTML;
		slides[indx].innerHTML = tmp;
	},	

	addSlides : function( slides )
	{	
		// expects an array of html elements or ids
		$$(slides).each( function(slide) {
			this.slides.include( $(slide) );
		}, this );
	},
		
	addPrevBttn : function( element )
	{
		$(element).addEvent('click', this.cycleBackward.bind(this) );
	},
	
	addNextBttn : function( element )
	{
		$(element).addEvent('click', this.cycleForward.bind(this) );
	},	
	checkLoop : function( event )
	{
		this.startAutoPlay();
		if( this.currIndex <= (this.slides.length*-1) || (this.currIndex >= this.slides.length) ) this.resetSlider();
	},
	createTransition : function()
	{
		var method = this.checkLoop;
		var timer = this.clearAutoPlay;
		var dur = this.options.duration;
		var trans = this.options.slideTransition;
		var dir = this.options.adjustDirection;
		
		this.slideObj = new Fx.Tween(this.options.innerElement, {
			property: dir,
			duration: dur, 
			transition: trans,
			onComplete: method.bind(this),
			onStart : timer.bind(this)
		});		
	},
	
	startAutoPlay : function()
	{
		if( ! this.options.autoPlay ) return false;
		this.autoPlayId = this.cycleForward.periodical( this.options.timerLength, this );
	},
	clearAutoPlay : function()
	{
		if( this.autoPlayId )$clear( this.autoPlayId )
	},
	
	cycleBackward : function()
	{
		this.slideObj.start(this.currPos + this.slideWidth );
		this.currPos += this.slideWidth;
		this.currIndex--
		return false;		

	},
	cycleForward : function()
	{
		this.slideObj.start(this.currPos - this.slideWidth );
		this.currPos -= this.slideWidth;
		this.currIndex++
		return false;
	}

});

//]]>
