// "Scroll div contents with Javascript." was written April 10th, 2006 by germ, and filed under javascript.
// http://germ.wordpress.com/2006/04/10/scroll-div-contents-with-javascript/

// gives up and down scroll buttons to images, spans, ... named up_name, down_name, respectively.
// will keep the default scroll_box's style overflow if it encounters errors (so make overflow: auto;)

// usage: put this after the scrollbox div:  var div_scroll1 = new TextScroll('div_scroll1', 'scroll_box');

function TextScroll(scrollname, div_name, up_name, down_name, contain_name)
{
    this.div_name = div_name;
    this.name = scrollname;
    this.scrollCursor = 0;
    this.speed = 5;
    this.timeoutID = 0;
    this.div_obj = null;
    this.contain = null;
    this.up_name = up_name;
    this.dn_name = down_name;
    this.content_ofh = 0;
    this.div_ofh = 0;
//    this.slider = slider_obj;
    this.indice = null;

{
        if (document.getElementById) {
            div_obj = document.getElementById(this.div_name);
            if (div_obj) {
                this.div_obj = div_obj;
                this.div_obj.style.overflow = 'hidden';
                this.contain = document.getElementById(contain_name);
                this.content_ofh = this.contain.offsetHeight;
                this.div_ofh = this.div_obj.offsetHeight;
                this.indice = this.content_ofh - this.div_ofh;
                
            }
            div_up_obj = document.getElementById(this.up_name);
            div_dn_obj = document.getElementById(this.dn_name);
            if (div_up_obj && div_dn_obj) {
						div_up_obj.onmouseover = function() { eval(scrollname + '.scrollUp();') };
						div_up_obj.onmouseout = function() { eval(scrollname + '.stopScroll();') };
						
						
						div_dn_obj.onmouseover = function() { eval(scrollname + '.scrollDown();') };
						div_dn_obj.onmouseout = function() { eval(scrollname + '.stopScroll();') };

/*						if (this.indice > 0)
						{
							div_up_obj.style.display = 'inline';
							div_dn_obj.style.display = 'inline';
						}
*/							
            }
        }
    }

this.stopScroll = function() {
        clearTimeout(this.timeoutID);
    }

this.scrollUp = function() {
        if (this.div_obj) {
            this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
            this.div_obj.scrollTop = this.scrollCursor;
            //this.slider.setValue(this.div_obj.scrollTop/this.indice);
            this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
        }
    }

this.scrollPara = function(onde) {
	var ondeReal = null;
	if (onde >= 0 && onde <= 1)
	{
		ondeReal = onde * this.indice;
    this.div_obj.scrollTop = this.scrollCursor = Math.round(ondeReal);
	}
}

this.scrollDown = function() {
        if (this.div_obj) {
            this.scrollCursor += this.speed;
            this.div_obj.scrollTop = this.scrollCursor;
            //this.slider.setValue(this.div_obj.scrollTop/this.indice);
	          if (this.div_obj.scrollTop == this.scrollCursor) {
            this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
						} else {
						this.scrollCursor = this.div_obj.scrollTop;
						}
        }
    }

this.resetScroll = function() {
        if (this.div_obj) {
            this.div_obj.scrollTop = 0;
            this.scrollCursor = 0;
        }
    }
}



function wheel(event){
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta) {
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta)
		handle(delta);
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}

/* Initialization code. */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;

function handle(delta) {
var t=0;
	if (delta < 0) {
		for(t=0;t>=Math.floor(delta);t--)
		{
			scrollAtual.scrollDown();
			scrollAtual.stopScroll();
		}
	}
	else {
		for(t=0;t<=Math.ceil(delta);t++)
		{
			scrollAtual.scrollUp();
			scrollAtual.stopScroll();
		}
	}
}

var scrollAtual = null;