var HoverCarousel = new Class({
	initialize: function(element, options) {
		this.setOptions({
			autoStyle: false,
			idPrevious: null,
			idNext: null,
			visibleItems: 5,
			itemWidth: 80,
			scrollInterval: 200,
			fxTransition: Fx.Transitions.linear
		}, options);
		
		if(!element) return;
		
		this.rightScrollInterval = this.options.scrollInterval;
		this.leftScrollInterval = 0;
		
		if(this.options.idNext) 
			$(this.options.idNext).addEvent("mouseover", (function(e){ new Event(e).stop(); this.scrollRight(); }).bindWithEvent(this));
		if(this.options.idNext) 
			$(this.options.idNext).addEvent("mouseout", (function(e){ new Event(e).stop(); this.effect.stop(); this.recalculateScrollInterval(); }).bindWithEvent(this));
			
		if(this.options.idPrevious) 
			$(this.options.idPrevious).addEvent("mouseover", (function(e){ new Event(e).stop(); this.scrollLeft(); }).bindWithEvent(this));
		if(this.options.idPrevious) 
			$(this.options.idPrevious).addEvent("mouseout", (function(e){ new Event(e).stop(); this.effect.stop(); this.recalculateScrollInterval(); }).bindWithEvent(this));
		
		this.mainElement = $(element);
		this.mainElements = this.mainElement.getChildren();

		this.index = 0;
		this.maxLength = this.mainElements.length;
				
		this.maxWidth = this.maxLength * this.options.itemWidth;

		if(this.options.autoStyle) {
			this.mainElement.getParent().setStyles({
				width: (this.options.itemWidth * 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.effect = new Fx.Style(
		this.mainElement, 
		"left", 
		{
			duration:this.options.scrollInterval,
			transition:this.options.fxTransition,
			wait:false
		});
		
	},
	
	setScrollDuration: function(newDuration) {
		
		this.effect = new Fx.Style(
			this.mainElement, 
			"left", 
			{
				duration:newDuration,
				transition:this.options.fxTransition,
				wait:false
			}
		);
		
	},
	
	scrollLeft: function() {
		
		var current_x = this.mainElement.getStyle("left").toInt() || 0;
		
		if (current_x < 0) {
			this.setScrollDuration(this.leftScrollInterval);
			this.effect.start(current_x, 0);
		}
		
	},
	
	scrollRight: function() {
		
		var mainWidth = this.mainElement.getStyle("width").toInt() || 0;
		var mainLeft = this.mainElement.getStyle("left").toInt() || 0;
		
		var leftLimit = -(mainWidth / 2);
		
		if (mainLeft == 0) {
			var doScroll = true;
		} else {
			var doScroll = (mainWidth / -mainLeft > 2);
		}
		
		if (doScroll) {
	
			var current_x = mainLeft;
			
			this.setScrollDuration(this.rightScrollInterval);
			this.effect.start(current_x, leftLimit);
		
		}
		
	},
	
	recalculateScrollInterval: function() {
		
		var mainWidth = this.mainElement.getStyle("width").toInt() || 0;
		var mainLeft = this.mainElement.getStyle("left").toInt() || 0;
		var amountScrolled = -(this.mainElement.getStyle("left").toInt() || 0);
		
		var leftLimit = -(mainWidth / 2);
		var widthToScroll = -leftLimit;
		
		if (amountScrolled == 0 || amountScrolled == leftLimit) {
			this.rightScrollInterval = this.options.scrollInterval;
			this.leftScrollInterval = 0;
		} else {
			
			var msPerPixel = this.options.scrollInterval / widthToScroll;
			var rightScrollRemaining = widthToScroll - amountScrolled;
			
			this.rightScrollInterval = msPerPixel * rightScrollRemaining;
			this.leftScrollInterval = msPerPixel * amountScrolled;
		
		}
		
	}

});
HoverCarousel.implement(new Options);
