/** 
 * @description		scroller plugin for prototype.js
 * @author		Victor Stanciu; contact [at] victorstanciu [dot] ro; http://www.victorstanciu.ro/ 
 * @version		0.8
 * @date		21/03/08
 * @requires		prototype.js 1.6
*/

Scroller = Class.create({

	initialize: function (scroller, controls, options) {

		this.timers = {nw: null, up: null, ne: null, right: null, se: null, down: null, sw: null, left: null};
		this.scroller = scroller;
		this.controls = controls;
		this.cacheStep = null;

		this.options = Object.extend({
									frequency: 0.1, 
									step: 2, 
									stepModifier: 3
									}, options || {});

		this.controls.invoke('observe', 'mouseover', this.mouseover.bind(this));
		this.controls.invoke('observe', 'mouseout', this.mouseout.bind(this));
		this.controls.invoke('observe', 'mousedown', this.mousedown.bind(this));
		this.controls.invoke('observe', 'mouseup', this.mouseup.bind(this));
		this.controls.invoke('observe', 'click', this.click.bind(this));

		},
		
	mouseover: function (event) {
		control = event.element();
		eval('this.move' + control.rel + '()');
		},

	mouseout: function (event) {
		event.element().blur();
		this.stop();
		},

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

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

	click: function (event) {
		event.stop();
		},
	
	stop: function () {
		$H(this.timers).values().each(function (timer) {
			if (timer != null) {
				clearTimeout(timer);
				}
			});
		},
	
	moveleft: function () {
		this.scroller.scrollLeft -= this.options.step;
		this.timers.left = setTimeout(this.moveleft.bind(this), this.options.frequency);
		},
	
	moveright: function (event) {
		temp = this.scroller.scrollLeft;
		this.scroller.scrollLeft += this.options.step;
		this.timers.right = setTimeout(this.moveright.bind(this), this.options.frequency);
		},	

	moveup: function () {
		this.scroller.scrollTop -= this.options.step;
		this.timers.up = setTimeout(this.moveup.bind(this), this.options.frequency);
		},
	
	movedown: function () {
		this.scroller.scrollTop += this.options.step;
		this.timers.down = setTimeout(this.movedown.bind(this), this.options.frequency);
		},

	movenw: function () {	
		this.moveup();
		this.moveleft();
		},

	movene: function () {		
		this.moveup();
		this.moveright();
		},

	movese: function () {		
		this.moveright();
		this.movedown();
		},

	movesw: function () {		
		this.movedown();
		this.moveleft();
		}

	});
