// usage: put this after the scrollbox div:  var div_scroll1 = new Scroll('div_scroll1', 'scroll_box');

function Scroller(scrollname, div_id, direction, speed) {
    if (div_id == null) return false;
	this.name = scrollname;
    this.div_id = div_id;
	this.direction = direction;
	this.state = direction;
    this.speed = speed;
    this.scrollCursor = 0;
    this.timeoutID = 0;
    this.div_obj = null;
	this.ctrlUp = null;
	this.ctrlDown = null;
	this.ctrlLeft = null;
	this.ctrlRight = null;
	this.ctrlPause = null;
	this.ctrlStop = null;
	this.contentDiv = null;
	
	this.pauseScroll = _pauseScroll;
	this.stopScroll = _stopScroll;
	this.doScroll = _doScroll;
	this.resetScroll = _resetScroll;
	
	this.scrollUp = _scrollUp;
	this.scrollDown = _scrollDown;
	this.scrollLeft = _scrollLeft;
	this.scrollRight = _scrollRight;
	
	this.setControls = _setControls;
	
	this.resizeContentDiv = _resizeContentDiv;
	
	this.init();
}
// initialize dummy oForm object so that NS will initialize the prototype object
new Scroller(null, null, null);

function _init(){
	if( !this.name ) return false;
    if (document.getElementById) {
        div_obj = document.getElementById(this.div_id);
        if (div_obj) {
            this.div_obj = div_obj;
            if (document.all) {
				this.div_obj.style.position = 'absolute';
			}
			this.div_obj.style.overflow = 'hidden';
			eval('this.div_obj.onmouseover = function() {'+this.name+'.pauseScroll();};');
			eval('this.div_obj.onmouseout = function() {'+this.name+'.doScroll();};');
			
			this.contentDiv = this.div_obj.getElementsByTagName('div')[0];
			this.contentDiv.style.position = 'relative';

			this.resizeContentDiv();
			
			this.scrollCursor = 0;
			
			switch (this.direction) {
				case 'up':
					this.scrollUp();
					break;
				case 'right':
					this.scrollRight();
					break;
				case 'left':
					this.scrollLeft();
					break;
				case 'down':
					this.scrollDown();
					break;
				default:
					break;
			}
			
        }
    }
}
Scroller.prototype.init = _init;	

function _pauseScroll() {
    clearTimeout(this.timeoutID);
}

function _stopScroll() {
    clearTimeout(this.timeoutID);
	this.state = 'stoped';
}

function _doScroll() {
	//alert(this.state);
	switch (this.state) {
		case 'left': 
			eval(this.name + ".scrollLeft();");
			break;
		case 'right': 
			eval(this.name + ".scrollRight();");
			break;
		default:
			clearTimeout(this.timeoutID);
			break;
	}
}
	
function _scrollUp() {
    clearTimeout(this.timeoutID);
    if (this.div_obj) {
		this.state = 'up';
        this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
        this.div_obj.scrollTop = this.scrollCursor;

	
        this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
    }
}

function _scrollDown() {
    clearTimeout(this.timeoutID);
    if (this.div_obj) {
	    this.state = 'down';
        this.scrollCursor += this.speed;
        this.div_obj.scrollTop = this.scrollCursor;


        this.timeoutID = setTimeout(this.name + ".scrollDown)", 60);
    }
}

function _scrollRight() {
    clearTimeout(this.timeoutID);
	if (this.div_obj) {
        this.state = 'right';
		
		var index;
		for (var i=this.contentDiv.childNodes.length-1; i>0; i--){
			if (this.contentDiv.childNodes[i].nodeType == 1){
				index = i;
				break;
			}
		}
		
		var el = this.contentDiv.childNodes[index];
		
		if (parseInt(this.contentDiv.style.left) >= 0){
			this.scrollCursor = parseInt(this.contentDiv.style.left) - el.offsetWidth;
			this.contentDiv.insertBefore(el, this.contentDiv.firstChild)
		}
		this.scrollCursor += this.speed;
		this.contentDiv.style.left = this.scrollCursor + 'px';
		
		this.timeoutID = setTimeout(this.name + ".scrollRight()", 60);
    }
}


function _scrollLeft() {
	clearTimeout(this.timeoutID);
	if (this.div_obj) {
	    this.state = 'left';
	
		var index;
		for (var i=0; i<this.contentDiv.childNodes.length; i++){
			if (this.contentDiv.childNodes[i].nodeType == 1){
				index = i;
				break;
			}
		}

		var el = this.contentDiv.childNodes[index];
		
		if (parseInt(this.contentDiv.style.left) <= (el.offsetWidth * -1)){
			this.contentDiv.appendChild(el);
			this.scrollCursor = 0;
		}
		this.scrollCursor -= this.speed;
		this.contentDiv.style.left = this.scrollCursor + 'px';

		this.timeoutID = setTimeout(this.name + ".scrollLeft()", 60);
	}
}
//Scroller.prototype.scrollLeft = _scrollLeft;

function _setControls(upID, rightID, downID, leftID, pauseID, stopID) {
	if (upID) {
		this.ctrlUp = document.getElementById(upID);
		this.ctrlUp.style.cursor = 'pointer';
		eval('this.ctrlUp.onclick = function() {'+this.name+'.scrollUp();};');
		// O formato de código abaixo funciona no FF mas não no IE
		//this.ctrlUp['onclick'] = eval('function() {'+this.name+'.scrollUp();};');
	}
	if (downID) {
		this.ctrlDown = document.getElementById(downID);
		this.ctrlDown.style.cursor = 'pointer';
		eval('this.ctrlDown.onclick = function() {'+this.name+'.scrollDown();};');
	}
	if (leftID) {
		this.ctrlLeft = document.getElementById(leftID);
		this.ctrlLeft.style.cursor = 'pointer';
		eval('this.ctrlLeft.onclick = function() {'+this.name+'.scrollLeft();}');
	}
	if (rightID) {
		this.ctrlRight = document.getElementById(rightID);
		this.ctrlRight.style.cursor = 'pointer';
		eval('this.ctrlRight.onclick = function() {'+this.name+'.scrollRight();};');
	}
	if (pauseID) {
		this.ctrlPause = document.getElementById(pauseID);
		this.ctrlPause.style.cursor = 'pointer';
		eval('this.ctrlPause.onclick = function() {'+this.name+'.pauseScroll();};');
	}
	if (stopID) {
		this.ctrlStop = document.getElementById(stopID);
		this.ctrlStop.style.cursor = 'pointer';
		eval('this.ctrlStop.onclick = function() {'+this.name+'.stopScroll();};');
	}
}
//Scroller.prototype.setControl = _setControl;
	
	
function _resetScroll() {
    if (this.div_obj) {
        this.div_obj.scrollTop = 0;
        this.scrollCursor = 0;
    }
}

function _resizeContentDiv(){	
	var newWidth = 0;
	for (var i=0; i<this.contentDiv.childNodes.length; i++) {
		if (this.contentDiv.childNodes[i].nodeType == 1){
			newWidth += parseInt(this.contentDiv.childNodes[i].offsetWidth)
			if (document.all) {newWidth += 1;} // +1 para o IE
		}
	}
	this.contentDiv.style.width = newWidth + 'px';
}
