var Carousel = new Class({
	Implements: Options,
	options: {
		
		// id of the carousel element
		elementId: 'carousel',
		
		// id of the previous button element
		prevBtnElementId: 'carousel-previous',
		
		// id of the next button element
		nextBtnElementId: 'carousel-next',
		
		// if the carousel loops
		isLoop: false,
		
		// duration of the animation
		slideDuration: 500,
		
		// numbers of item to slide
		nbItemsSlide: 1
	},
	initialize: function(options) {
		
		this.setOptions(options);
		
		this.carouselElement 	= $(this.options.elementId);
		this.contentElement 	= this.carouselElement.getElement('ul');
		this.itemElements 		= this.contentElement.getElements('li');
		
		this.prevBtnElement 	= $(this.options.prevBtnElementId);
		this.nextBtnElement 	= $(this.options.nextBtnElementId);
		
		if((this.carouselElement != null) && (this.contentElement != null) && (this.itemElements.length != 0) && (this.prevBtnElement != null) && (this.nextBtnElement != null)) {
			this.start();
		}
    },
	start: function() {
		this.tracer('start()');
		
		// -- PROPERTIES --
		
		this.itemElement_width 		= (this.itemElements[0].getSize().x + this.itemElements[0].getStyle('margin-left').toInt() + this.itemElements[0].getStyle('margin-right').toInt());
		this.itemElement_nbVisible 	= (this.carouselElement.getSize().x / this.itemElement_width);
		
		var nbMoves = Math.ceil((this.itemElements.length - this.itemElement_nbVisible) / this.options.nbItemsSlide);
		
		this.min_posX = -1 * (nbMoves * this.options.nbItemsSlide) * this.itemElement_width;
		this.max_posX = 0;
		
		// movement
		this.contentAnimation = new Fx.Tween(this.contentElement, {duration: this.options.slideDuration});
		
		// -- STYLES --
		
		this.carouselElement.setStyles({
			'overflow': 'hidden',
			'position': 'relative'
		});
		
		this.contentElement.setStyles({
			'width': (this.itemElement_width * this.itemElements.length) +'px',
			'padding': '0px',
			'margin': '0px',
			'position': 'absolute',
			'top': '0px',
			'left': '0px'
		});
		
		this.itemElements.setStyles({
			'float': 'left',
			'display': 'inline',
			'text-align': 'center'
		});
		
		if(this.itemElements.length <= this.itemElement_nbVisible) {
			this.prevBtnElement.setStyles({
			'opacity': '0'
			});
			this.nextBtnElement.setStyles({
			'opacity': '0'
			});
		}
		
		// -- EVENTS --
		
		if(this.itemElements.length > this.itemElement_nbVisible) {
			
			// Sets up the previous button
			this.prevBtnElement.addEvent('click', this.gotoPrevItem.bind(this));
			
			// Sets up the next button
			this.nextBtnElement.addEvent('click', this.gotoNextItem.bind(this));
		}
	},
	gotoPrevItem: function() {
		this.tracer('gotoPrevItem()');
		
		var position = parseInt(this.contentElement.getStyle('left'));
		
		if(position < this.max_posX){
			
			var newposition = position + (this.itemElement_width * this.options.nbItemsSlide);
			this.contentAnimation.start('left', newposition);
			
		} else if(this.options.isLoop == true) { 
			
			var newposition = this.min_posX;
			this.contentAnimation.start('left', newposition);
			
		}
	},
	gotoNextItem: function() {
		this.tracer('gotoNextItem()');
		
		var position = parseInt(this.contentElement.getStyle('left'));
		
		if(position > this.min_posX) {
			
			var newposition = position - (this.itemElement_width * this.options.nbItemsSlide);
			this.contentAnimation.start('left', newposition);
			
		} else if(this.options.isLoop == true) {
			
			var newposition = this.max_posX;
			this.contentAnimation.start('left', newposition);
			
		}
	},
	tracer: function(toTrace) {
		//console.log(toTrace);
	}
});

/*
window.addEvent('domready', function() { 
	
	var myCarousel = new Carousel({elementId: 'carousel', prevBtnElementId: 'carousel-previous', nextBtnElementId: 'carousel-next', isLoop: false, slideDuration: 500, nbItemsSlide: 1});
	
});
*/
