
/*!
 * JS Carousel build on MooTools 1.2.1 
 * Inspired by http://www.webdevlounge.com/javascript/creating-a-carousel-with-mootools/
 *
 * @author Tim Lund <tl@t3cms.dk>
 * @version 1.0
 * @since 20080127
 */

var Carousel = new Class({
	
	Implements:				[Options],
	animation:				null,
	maxWidth: 				0,
	autoSliderId:			null, 	
	currentPageNum:			1,
	sliderRunning:			false,
	
	options: {
		
		outerWrapper:		null,
		innerWrapper:		null,
		nextButton:			null,
		prevButton:			null,
		items: 				[],
		itemWidth:			0,
		duration:			2000,
		autoStart:			false,
		autoStartInterval:	2000,
		pageCurrent:		null,
		pageMax:			null,
		startSlide:			1
	},
	
	
	initialize: function(options) {
		
		this.setOptions(options);
			
		this.maxWidth = this.options.items.length * this.options.itemWidth - this.options.itemWidth;
		
		
		if(this.options.items.length < 2) {
			return;			
		}
				
		this.animation = new Fx.Tween(this.options.innerWrapper, {
			duration: this.options.duration ,
			transition: Fx.Transitions.Sine.easeInOut,
			fps: 40
		});
		
		this.animation.addEvents({
			'start': function() {
				this.sliderRunning = true;
			}.bind(this),
			
			'complete': function() {
				this.sliderRunning = false;
			}.bind(this)
		});
		
		
		$(this.options.nextButton).addEvent('click', function(){
			
			this.nextItem();
			return false;
		}.bind(this));
		
		$(this.options.prevButton).addEvent('click', function(){
			this.previousItem();
			return false;
		}.bind(this));
		
		
		if(this.options.startSlide > 1) {
			this.options.innerWrapper.setStyle('left', -(this.options.itemWidth * (this.options.startSlide - 1)) );
		}
		
		
		if(this.options.autoStart) {
			this.enableAutoSlider();
		}
		
		this.setCurrentPage();
		
		if($type(this.options.pageMax) == 'element') {
			this.options.pageMax.set('html', this.options.items.length);
		}
	},
	
	
	setCurrentPage: function() {
		
		if($type(this.options.pageCurrent) != 'element') {
			return this; 			
		}
		
		this.options.pageCurrent.set('html', 
			this.currentPageNum
		);
		
		return this;
	},
	
	
	/**
	 * Do not call this twice
	 */
	enableAutoSlider: function() {
			
		/*this.options.outerWrapper.addEvents({
			'mouseenter': function() {
				$clear(this.autoSliderId);
			}.bind(this),
			
			'mouseleave': function() {
				this.startAutoSlider();
			}.bind(this)
		});*/
		
		this.startAutoSlider();
		
		
	},
		
	
	
	startAutoSlider: function() {
		this.autoSliderId = this.nextItem.periodical(this.options.autoStartInterval, this);
		/* CS t3cms */
		this.autoSliderId = 3;
	},
	
	
	nextItem: function(){
		
		if(this.sliderRunning) {
			return this;
		}
		
		$clear(this.autoSliderId);
		
		var pos = parseInt(this.options.innerWrapper.getStyle('left'));
		
		if(pos == -this.maxWidth) {
			this.currentPageNum = 1;
			this.animation.start('left', 0);
		} else {
			this.currentPageNum++;
			this.animation.start('left', pos - this.options.itemWidth);
		}
		
		this.setCurrentPage();
	},
	
	previousItem: function(){		
		
		if(this.sliderRunning) {
			return this;
		}
		
		$clear(this.autoSliderId);
		
		var pos = parseInt(this.options.innerWrapper.getStyle('left'));
		
		if(pos == 0){
			this.currentPageNum = this.options.items.length;
			this.animation.start('left', -this.maxWidth);
		} else {
			this.currentPageNum--;
			this.animation.start('left', pos + this.options.itemWidth);
		}
		
		this.setCurrentPage();
	}
	
	
});
