Carousel = Class.create(Abstract, {

	initialize: function (scroller, slides, controls, options) {

		this.scrolling	= false;
		this.scroller	= $(scroller);
		this.slides		= slides;
		this.controls	= controls;
		this.slideWidth = 45;
		this.first		= true;

		this.options    = Object.extend({
            duration:   1,
            frequency:  3,
            visibleSlides: 1,
            controlClassName: 'carousel-control',
            jumperClassName: 'carousel-jumper'
        }, options || {});

		this.slides.each(function(slide, index) {
			slide._index = index;
        });

		if (this.controls) {
			this.controls.invoke('observe', 'mousedown',	this.mousedown.bind(this))
						 .invoke('observe', 'mouseup',		this.release.bind(this))
						 .invoke('observe', 'mouseout',		this.release.bind(this))
						 .invoke('observe', 'click',		function (event) { event.stop(); });
        }

		if (this.options.initial) {
			var initialIndex = this.slides.indexOf(this.options.initial);
			if (initialIndex > (this.options.visibleSlides - 1)) {
				// this.controls.first().show().next('span.controls').hide();
                
				if (initialIndex > this.slides.length - (this.options.visibleSlides + 1)) {
					initialIndex = this.slides.length - this.options.visibleSlides;
				}

				if (initialIndex == this.slides.length - this.options.visibleSlides) {
					// this.controls.last().hide().next('span.controls').show();
				}

				this.moveTo(this.slides[initialIndex]);
			}
		}
	},

	mousedown: function (event) {
		var element = event.findElement('a');
		this.start(element.rel);
    },

	release: function (event) {
		this.stop();
	},

	moveTo: function (element) {
		if (this.options.beforeMove && (typeof this.options.beforeMove == 'function')) {
			this.options.beforeMove();
        }

		this.previous = this.current ? this.current : this.slides[0];
		this.current  = $(element);

		var scrollerOffset = this.scroller.cumulativeOffset();
		var elementOffset  = this.current.cumulativeOffset();

		if (this.scrolling) {
			this.scrolling.cancel();
		}

		this.scrolling = new Effect.SmoothScroll(this.scroller, {
				duration: this.options.duration,
				x: (elementOffset[0] - scrollerOffset[0]),
				y: (elementOffset[1] - scrollerOffset[1]),
				//queue: {position: 'end', limit: 1, scope: this.scroller.id},
				afterFinish: (function () {
					if (this.controls) {
						this.activateControls();
					}
				}).bind(this)});

		if (this.options.afterMove && (typeof this.options.afterMove == 'function')) {
			this.options.afterMove();
		}

		return false;

	},

	prev: function () {
		if (this.current) {
			var currentIndex = this.current._index;
			var prevIndex = (currentIndex == 0) ? 0 : currentIndex - 1;
		} else {
			var prevIndex = 0;
		}

		// this.controls.last().show().next('span.controls').hide();
		if (prevIndex == 0) {
			// this.controls.first().hide().next('span.controls').show();
		}

		this.moveTo(this.slides[prevIndex]);
	},

	next: function () {
		if (this.current) {
			var currentIndex = this.current._index;
			var nextIndex = (this.slides.length - 1 == currentIndex) ? currentIndex : currentIndex + 1;
			} else {
				var nextIndex = 1;
				}

		if (nextIndex > this.slides.length - (this.options.visibleSlides + 1)) {
			nextIndex = this.slides.length - this.options.visibleSlides;
		}

		// this.controls.first().show().next('span.controls').hide();
		if (nextIndex == this.slides.length - this.options.visibleSlides) {
			// this.controls.last().hide().next('span.controls').show();
		}

		this.moveTo(this.slides[nextIndex]);
	},

	first: function () {
		var firstIndex = 0;
		if (this.current) {
			var currentIndex = this.current._index;
        }

		this.moveTo(this.slides[firstIndex]);
    },

	last: function () {
		var lastIndex = (this.slides.length - 1);
		if (this.current) {
			var currentIndex = this.current._index;
        }

		this.moveTo(this.slides[lastIndex]);
    },

	toggle: function () {
		if (this.previous) {
			this.moveTo(this.slides[this.previous._index]);
        } else {
            return false;
        }
    },

	stop: function () {
		if (this.timer) {
			clearTimeout(this.timer);
		}
	},

	start: function (direction) { this.periodicallyUpdate(direction); },

	pause: function (event) {
		this.stop();
		this.activateControls();
    },

	resume: function (event) {
		if (event) {
			var related = event.relatedTarget || event.toElement;
			if (!related || (!this.slides.include(related) && !this.slides.any(function (slide) { return related.descendantOf(slide); }))) {
				this.start();
            }
        } else {
            this.start();
        }
    },

	periodicallyUpdate: function (direction) {
		if (this.timer != null) {
			clearTimeout(this.timer);
			switch (direction) {
				case 'next':
					this.next();
                break;
				case 'prev':
					this.prev();
                break
            }
		} else {
			if (this.first) {
				this.first = false;
				switch (direction) {
					case 'next':
						this.next();
						break;
					case 'prev':
						this.prev();
						break
				}
			}
		}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this, direction), this.options.frequency * 1000);
	},

	deactivateControls: function () {
		this.controls.invoke('addClassName', 'disabled');
    },

	activateControls: function () {
		this.controls.invoke('removeClassName', 'disabled');
    }

});

Effect.SmoothScroll = Class.create();

Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {

	initialize: function (element) {
		this.element = $(element);
		var options = Object.extend({ x: 0, y: 0, mode: 'absolute' } , arguments[1] || {});
		this.start(options);
    },

	setup: function () {
		if (this.options.continuous && !this.element._ext ) {
			this.element.cleanWhitespace();
			this.element._ext = true;
			this.element.appendChild(this.element.firstChild);
        }

		this.originalLeft = this.element.scrollLeft;
		this.originalTop = this.element.scrollTop;

		if (this.options.mode == 'absolute') {
			this.options.x -= this.originalLeft;
			this.options.y -= this.originalTop;
        }
    },

	update: function (position) {
		this.element.scrollLeft = this.options.x * position + this.originalLeft;
		this.element.scrollTop  = this.options.y * position + this.originalTop;
    }

});