/********************************************************************

	FOREX.COM - CAROUSEL
	
	=========================================
	Author: Chris Erwin (chris@teehanlax.com)
	Agency: teehan+lax
	Date: July 7, 2008
	=========================================
	
	Public Methods
	-------------------------------------------------------------
	initialize (constructor)
		Arguments:
			- carouselID (string) : the id of the carousel container element
			
	goTo
		Arguments:
			- slideNum (int) : the number of the slide you want to change to 
					(note: that it will not display the slides between the current slide and the slide you are changing to)
		Returns: null
	
	
	Internal 'Private' Methods
	-------------------------------------------------------------
	goNext 
		Arguments: none		
		Returns: false
	
	goPrevious
		Arguments: none		
		Returns: false
	
	
	Dependencies
	-------------------------------------------------------------
	- MooTools 1.2
	
	
	HTML Markup Sample
	-------------------------------------------------------------
	_widget_samples/carousel.html
	
*********************************************************************/

var Carousel = new Class({
    initialize: function(carouselID){
		this.carouselID = carouselID;
		this.carouselWindow = $(carouselID).getElement('.carousel_window');
		this.arrSlides = $(carouselID).getElements('.carousel_item');
		this.nextButton = $(carouselID).getElement('.next_button a');
		this.prevButton = $(carouselID).getElement('.prev_button a');
		
		this.currSlideNum = null;
		this.carouselWindowWidth = this.carouselWindow.getSize().x;
		
		this.arrSlides[0].setStyle('right', 0);
		this.arrSlides.set('morph', {duration: 900, transition: 'cubic:in:out'});

		this.currSlideNum = 0;
		this.nextButton.addEvent('click', this.goNext.bind(this));
		this.prevButton.addEvent('click', this.goPrevious.bind(this));		
    },
	
	goTo : function(slideNum) {
		if(! this.Animating) {			
			//slide in from right
			if(slideNum > this.currSlideNum) {
				currEndPos = this.carouselWindowWidth;
				nextStartPos = (this.carouselWindowWidth * -1);
				
				// make sure we're not passed the end
				if (slideNum > (this.arrSlides.length -1)) {
					slideNum = 0;	
				}
			}
			// slide in from left
			else {
				currEndPos = (this.carouselWindowWidth * -1);
				nextStartPos = this.carouselWindowWidth;
				
				// make sure we're not passed the start
				if (slideNum < 0) {
					slideNum = (this.arrSlides.length - 1);	
				}
			}
			
			// animate the slides
			this.arrSlides[slideNum].setStyles({
				right: nextStartPos,
				opacity: 0
			});
			this.arrSlides[this.currSlideNum].morph({right: currEndPos, opacity: 0});
			this.arrSlides[slideNum].morph({right: 0, opacity: 1});
			this.currSlideNum = slideNum;
		}	
	},
	
	goNext : function() {
		slideNum = this.currSlideNum + 1;
		this.goTo(slideNum);
		
		return false;
	},
	
	goPrevious : function() {
		slideNum = this.currSlideNum - 1;
		this.goTo(slideNum);
		
		return false;
	}	
});
