/*
* Scroller.js
* by Mário Araújo. v1.1
* Copyright (c) 2006 Declarativa. All Rights Reserved.
* http://www.declarativa.com
*/

function Scroller(id, element, type, clipSize, scrollSize, jump, rotate, onEnd, onMove){
	this.element = element;
	this.type = type;
	this.jump = jump;
	this.position = 0;
	this.by = 0;
	this.clipSize = clipSize;
	this.scrollSize = scrollSize;
	this.currentScrollWidth = 0;
	this.rotate = rotate;
	this.element.scroller = this;
	this.onEnd = onEnd;
	this.onMove = onMove;
	this._startPixel = null;
	this._endPixel = null;
	this._pixelDif = null;
	this._currentPixel = 0;
	this._timerId = null;
	this._lr = 0;
	this.pagesScrolled = 1;
	this.id = id;
	this.direction;
	
	this.scrollLeft = this.scrollUp = function (end, nPages){
			if (typeof nPages == "undefined") nPages = 1; this.pagesScrolled = nPages;
			// --- If it is scrolling, wait until it stops
			if (this._timerId != null) return;
			this._startPixel = Math.abs(this.position) + this.scrollSize;
			if (end == true) this._endPixel = this.element[this.type[1]];
			else { this._endPixel = this._startPixel + this.scrollSize * nPages; this.direction = Scroller.DIRECTION.LEFT; }
			if (this._endPixel > this.element[this.type[1]]) this._endPixel = this.element[this.type[1]];
			this._pixelDif = this._endPixel - this._startPixel;
			if (this._pixelDif <= 1){ if (this.rotate) this.scrollRight(true); return; }
			// --- Set move direction
			this._lr = -1;
			this._timerId = window.setInterval("__scroll('"+this.element.id+"');", 50);
		}

	this.scrollRight = this.scrollDown = function (begin, nPages){
			if (typeof nPages == "undefined") nPages = 1; this.pagesScrolled = nPages;
			// --- If it is scrolling, wait until it stops
			if (this._timerId != null) return;
			this._startPixel = Math.abs(this.position);
			if (begin == true) this._endPixel = 0;
			else { this._endPixel = this._startPixel - this.scrollSize * nPages; this.direction = Scroller.DIRECTION.RIGHT; }
			if (this._endPixel < 0) this._endPixel = 0;
			this._pixelDif = this._startPixel - this._endPixel;
			if (this._pixelDif <= 1){ if (this.rotate) this.scrollLeft(true); return; }
			// --- Set move direction
			this._lr = 1;
			this._timerId = window.setInterval("__scroll('"+this.element.id+"');", 50);
		}

	this.getCurrentPage = function (){
		return Math.ceil(Math.abs(this.position)/this.scrollSize);
	}

	this._move = function(){
			if (this._currentPixel < this._pixelDif / 2) var inc = Math.ceil(this._currentPixel / this.jump + 0.1);
			else var inc = Math.ceil((this._pixelDif - this._currentPixel) / this.jump - 0.1);
			this._currentPixel += inc;
			this.position += this.by = inc*this._lr;
			this.element.style[this.type[0]] = this.position + "px";
			if (typeof this.onMove == "function") this.onMove();
			if (this._currentPixel == this._pixelDif){
				this.currentScrollWidth = this._currentPixel;
				this._clearInterval();
				if (typeof this.onEnd == "function") this.onEnd();
			}
		}
	
	this._clearInterval = function(){
			window.clearInterval(this._timerId);
			this._timerId = null;
			this._currentPixel = 0;
		}
	
	this.initialize = function(){
			this.position = 0;
			this.element.style[this.type[0]] = "0px";
		}
}
Scroller.VERTICAL = ["top", "offsetHeight"];
Scroller.HORIZONTAL = ["left", "offsetWidth"];
Scroller.DIRECTION = { LEFT:1, RIGHT:2, UP:1, DOWN:2 };
Scroller._count = 0;

function __scroll(elementId){
	document.getElementById(elementId).scroller._move();
}
