HorizontalScroller = Class.create();

Object.extend(HorizontalScroller.prototype, {

	initialize: function (scroller, controls, options) {

		this.timer = null;
		this.scroller = scroller;
		this.controls = controls;

		this.options = Object.extend({
									frequency: 0.1, 
									step: 1, 
									oldStep: null, 
									stepModifier: 3, 
									controlOpacity: 1, 
									deactivateControl: true, 
									activeControlClass: 'active', 
									fasterControlClass: false
									}, options || {});

		this.events = {
					mouseoverLeft: this.mouseoverLeft.bind(this),  
					mouseoutLeft: this.mouseoutLeft.bind(this), 
					mousedownLeft: this.mousedownLeft.bind(this),
					mouseupLeft: this.mouseupLeft.bind(this), 

					mouseoverRight: this.mouseoverRight.bind(this),
					mouseoutRight: this.mouseoutRight.bind(this), 
					mousedownRight: this.mousedownRight.bind(this), 
					mouseupRight: this.mouseupRight.bind(this), 

					click: this.click.bind(this) 
					};

		this.bindObservers(this.controls);

		this.controls.left.active = true;
		this.controls.right.active = true;

		this.controls.left.setOpacity(0);
		this.controls.right.setOpacity(0);

		if (this.options.deactivateControl) {
			this.controls.left.active = false;
			this.controls.right.addClassName(this.options.activeControlClass);
			}
	
		},
	

	bindObservers: function (controls) {
		controls.left.observe('mouseover', this.events.mouseoverLeft);
		controls.left.observe('mouseout', this.events.mouseoutLeft);
		controls.left.observe('mousedown', this.events.mousedownLeft);
		controls.left.observe('mouseup', this.events.mouseupLeft);
		controls.left.observe('click', this.events.click);

		controls.right.observe('mouseover', this.events.mouseoverRight);
		controls.right.observe('mouseout', this.events.mouseoutRight);
		controls.right.observe('mousedown', this.events.mousedownRight);
		controls.right.observe('mouseup', this.events.mouseupRight);
		controls.right.observe('click', this.events.click);
		},


	mouseoverLeft: function (event) {
		this.controls.left.addClassName('hover');
		this.controls.left.setOpacity(this.options.controlOpacity);
		if ($('content-hints')) {
			$('content-hints').hide();
			}
		if ($('scroll-left-hint')) {
			$('scroll-left-hint').show();
			}
		this.moveLeft();
		},


	mouseoutLeft: function (event) {
		this.controls.left.removeClassName('hover');
		this.controls.left.setOpacity(0);
		if ($('content-hints')) {
			$('content-hints').show();
			}
		if ($('scroll-left-hint')) {
			$('scroll-left-hint').hide();
			}
		this.stop();
		},


	mouseoverRight: function (event) {
		this.controls.right.addClassName('hover');
		this.controls.right.setOpacity(this.options.controlOpacity);
		if ($('content-hints')) {
			$('content-hints').hide();
			}
		if ($('scroll-right-hint')) {
			$('scroll-right-hint').show();
			}
		this.moveRight();
		},

	
	mouseoutRight: function (event) {
		this.controls.right.removeClassName('hover');
		this.controls.right.setOpacity(0);
		if ($('content-hints')) {
			$('content-hints').show();
			}
		if ($('scroll-right-hint')) {
			$('scroll-right-hint').hide();
			}
		this.stop();
		},


	mousedown: function () {
		if (this.options.oldStep == null) {
			this.options.oldStep = this.options.step;
			this.options.step = this.options.step * this.options.stepModifier;
			}
		},


	mouseup: function () {
		if (this.options.oldStep != null) {
			this.options.step = this.options.oldStep;
			this.options.oldStep = null;
			}
		},


	mousedownLeft: function (event) {
		this.mousedown();
		if (this.options.fasterControlClass) {
			this.controls.left.addClassName(this.options.fasterControlClass);
			}
		},


	mouseupLeft: function () {
		this.mouseup();
		if (this.options.fasterControlClass) {
			this.controls.left.removeClassName(this.options.fasterControlClass);
			}
		},


	mousedownRight: function (event) {
		this.mousedown();
		if (this.options.fasterControlClass) {
			this.controls.right.addClassName(this.options.fasterControlClass);
			}
		},


	mouseupRight: function () {
		this.mouseup();
		if (this.options.fasterControlClass) {
			this.controls.right.removeClassName(this.options.fasterControlClass);
			}
		},


	click: function (event) {
		event.stop();
		},

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

	
	moveLeft: function () {
		this.activateRightControl();
		this.scroller.scrollLeft -= this.options.step;
		if (this.options.deactivateControl && this.scroller.scrollLeft == 0) {
			this.deactivateLeftControl();
			}
		this.timer = setTimeout(this.moveLeft.bind(this), this.options.frequency);
		},

	
	moveRight: function () {
		this.activateLeftControl();
		temp = this.scroller.scrollLeft;
		this.scroller.scrollLeft += this.options.step;
		if (this.options.deactivateControl && this.scroller.scrollLeft == temp) {
			this.deactivateRightControl();
			}
		this.timer = setTimeout(this.moveRight.bind(this), this.options.frequency);
		},

	
	activateLeftControl: function () {
		if (!this.controls.left.active) {
			this.controls.left.active = true;
			if (this.options.activeControlChild) {
				this.controls.left.down(this.options.activeControlChild).show();
				}
			this.controls.left.addClassName(this.options.activeControlClass);
			this.controls.left.observe('mouseover', this.events.mouseoverLeft);
			}
		},

	
	activateRightControl: function () {
		if (!this.controls.right.active) {
			this.controls.right.active = true;
			if (this.options.activeControlChild) {
				this.controls.right.down(this.options.activeControlChild).show();
				}
			this.controls.right.addClassName(this.options.activeControlClass);
			this.controls.right.observe('mouseover', this.events.mouseoverRight);
			}
		},

	
	deactivateLeftControl: function () {
		this.controls.left.active = false;
		this.controls.left.removeClassName(this.options.activeControlClass);
		this.controls.left.stopObserving('mouseover', this.events.mouseoverLeft);
		this.mouseup();
		},

	
	deactivateRightControl: function () {
		this.controls.right.active = false;
		this.controls.right.removeClassName(this.options.activeControlClass);
		this.controls.right.stopObserving('mouseover', this.events.mouseoverRight);
		this.mouseup();
		}

	});
