var Carousel = new Class({
	initialize: function(element, options) {
		this.setOptions({
			autoStyle: false,
			idPrevious: null,
			idNext: null,
			visibleItems: 5,
			scrollAmount: 80,
			scrollInterval: 200,
			fxTransition: Fx.Transitions.quadOut
		}, options);
		
		if(!element) return;
		
		if(this.options.idPrevious) 
			$(this.options.idPrevious).addEvent("click", (function(e){ new Event(e).stop(); this.scrollLeft(); }).bindWithEvent(this));
		if(this.options.idNext) 
			$(this.options.idNext).addEvent("click", (function(e){ new Event(e).stop(); this.scrollRight(); }).bindWithEvent(this));
		
		this.mainElement = $(element);
		this.mainElements = this.mainElement.getChildren();

		this.index = 0;
		this.maxLength = this.mainElements.length;
				
		this.maxWidth = this.maxLength * this.options.scrollAmount;

		if(this.options.autoStyle) {
			this.mainElement.getParent().setStyles({
				width: (this.options.scrollAmount * this.options.visibleItems),
				display: "block",
				overflow: "hidden"
			});
			this.mainElement.setStyles({
				width: this.maxWidth,
				position: "relative",
				margin: "0px",
				padding:"0px"
			});
			this.mainElements.each(function(el){
				el.setStyles({
					position:"relative",
					overflow:"hidden",
					float:"left"
				});
			});
		}		

		this.checkScrollers(0);
			
		this.effect = new Fx.Style(
		this.mainElement, 
		"left", 
		{
			duration:this.options.scrollInterval,
			transition:this.options.fxTransition,
			wait:false
		});
		
	},
	
	checkScrollers: function(index) {
			if(this.options.idPrevious && this.options.idNext) {
				if(index == 0 || this.options.visibleItems > this.maxLength) {
					$(this.options.idPrevious).setStyle("visibility","hidden");
				} else {
					$(this.options.idPrevious).setStyle("visibility","visible");
				}
					
				if(index == (this.maxLength - this.options.visibleItems) || this.options.visibleItems > this.maxLength) {
					$(this.options.idNext).setStyle("visibility","hidden");
				} else {
					$(this.options.idNext).setStyle("visibility","visible");
				}
			}
	},
	
	calculateIndex: function(index) {
		this.checkScrollers(index);
	
		if(index < 0) {
			this.index = 0;
			return false;
		}
			
		if(index > (this.maxLength - this.options.visibleItems)) {
			this.index = this.maxLength - this.options.visibleItems;
			return false;
		}
		
		return true;
	},
	
	jumpTo: function(index) {
		if(this.calculateIndex(index)) {
			this.index = index;
		}
		
		if(this.options.visibleItems > this.maxLength) {
			return;
		}
					
		var current_x = this.mainElement.getStyle("left").toInt() || 0;
		var next_x = (this.index * this.options.scrollAmount);
		this.effect.start(current_x, -next_x);	
		this.checkScrollers(this.index);	
	},
	
	scrollLeft: function() {
		if(this.calculateIndex(--this.index)) {
			var current_x = this.mainElement.getStyle("left").toInt() || 0;
			var next_x = (this.index * this.options.scrollAmount);
			this.effect.start(current_x, -next_x);
		}
	},
	
	scrollRight: function() {
		if(this.calculateIndex(++this.index)) {
			var current_x = this.mainElement.getStyle("left").toInt() || 0;
			var next_x = (this.index * this.options.scrollAmount);
			this.effect.start(current_x, -next_x);
		}
	}

});
Carousel.implement(new Options);