/**
 * carrousel.class.js
 * creates a carrousel
 * 
 * @version  1.0
 * @author Rocco
 * @since 05/08/2009
 * @copyright Rocco Janse
 * @package Engine JS Lib
 * @subpackage Carrousel
 * @uses MooTools 1.2.3 More
 */

 var Carrousel = new Class({
	
	Implements: Options,
	
	options: {
		'viewport': false,
		'slidesContainer': false,
		'linkNext': false,
		'linkPrevious': false,
		'slidesToShift': 1,
		'slidesToShow': 3		
	},
	
	initialize: function(options) {
		
		this.setOptions(options);
		
		if (!this.options.viewport) return;
		this.viewport = this.options.viewport;
		
		this.slides = [];
		this.slidesToShift = this.options.slidesToShift.toInt();
		this.slidesToShow = this.options.slidesToShow.toInt();
		
		this.slidesContainer = this.options.slidesContainer;
		if (!this.slidesContainer) return;
		
		this.initSlidesContainer();
		this.initSlides();
		
		this.linkNext = this.options.linkNext;
		this.linkPrevious = this.options.linkPrevious;
		
		if (!this.linkNext || !this.linkPrevious) return;

		this.initLinks();
	},
	
	initSlides: function() {
		
		// fetch original slides
		this.slides = this.slidesContainer.getElements('li');
		
		// remove them from the dom
		this.slides.each(function(slide, index) {
			slide.dispose();
		}.bind(this));
		
		// (re)place the first items
		for (i = 0; i < this.slidesToShow; i++) {
			this.slides[i].inject(this.slidesContainer);
		}
	},
	
	initLinks: function() {
		this.linkNext.addEvents({
			'click': function() {
				this.gotoNextPosition();	
			}.bind(this)
		})
		this.linkPrevious.addEvents({
			'click': function() {
				this.gotoPreviousPosition();
			}.bind(this)
		})
	},
	
	initSlidesContainer: function() {
		this.viewport.setStyles({
			'position': 'relative',
			'overflow': 'hidden'
		});
		this.slidesContainer.setStyles({
			'position': 'absolute',
			'top': 0,
			'left': 0
		});
	},

	fetchNextSlides: function() {
		
		// fetch next items
		var current = this.slides.indexOf(this.slidesContainer.getLast('li'));
		this.startPos = 0;
		
		for (i = 1; i <= this.slidesToShift; ++i) {
			
			if (current + i > this.slides.length-1) {
				index = (current + i) - this.slides.length;
			} else {
				index = current + i;
			}				

			this.slides[index].inject(this.slidesContainer , 'bottom');
			this.startPos += this.slides[index].getWidth();
		}	
	},

	gotoNextPosition: function() {
	
		// fetch next items
		var current = this.slides.indexOf(this.slidesContainer.getLast('li'));
		var startPos = 0;
		
		for (i = 1; i <= this.slidesToShift; ++i) {
			
			if (current + i > this.slides.length-1) {
				index = (current + i) - this.slides.length;
			} else {
				index = current + i;
			}				
			this.slides[index].inject(this.slidesContainer , 'bottom');
			startPos += this.slides[index].getWidth();
		}

		var myFx = new Fx.Tween(this.slidesContainer);
	
		myFx.start('left', 0, -startPos).chain(function() {

			slides = this.slidesContainer.getElements('li');
			for (i = 0; i < this.slidesToShift; i++) {
				width = slides[i].getWidth();
				slides[i].dispose();
			}
			this.slidesContainer.setStyle('left', 0);
			
		}.bind(this));
	},
	
	gotoPreviousPosition: function() {
	
		// fetch next items
		var current = this.slides.indexOf(this.slidesContainer.getFirst('li'));
		var startPos = 0;
		
		for (i = 1; i <= this.slidesToShift; ++i) {
			
			if (current - i < 0) {
				index = this.slides.length + (current - i);
			} else {
				index = current - i;
			}			
			
			this.slides[index].inject(this.slidesContainer, 'top');
			startPos += this.slides[index].getWidth();
			
		}
		this.slidesContainer.setStyle('left',-startPos);
		
		var myFx = new Fx.Tween(this.slidesContainer);
		myFx.start('left', -startPos, 0).chain(function() {

			slides = this.slidesContainer.getElements('li');
			for (i = 0; i < this.slidesToShift; i++) {
				var index = (slides.length-1) - i;
				slides[index].dispose();
			}
			
		}.bind(this));
	}
});