﻿function Rolagem(conteudo, bt_up, bt_down, areaVisivel) {
    this.ie = /IE/.test(navigator.userAgent);
    this.Conteudo;
    this.Up;
    this.Dw;
    this.Altura;
    this.Margem = 0;
    this.Intervalo = 0;
    this.AreaVisivel = areaVisivel;
    scrl = this;

    onload = function() {
        scrl.Conteudo = document.getElementById(conteudo);
        scrl.Up = document.getElementById(bt_up);
        scrl.Dw = document.getElementById(bt_down);
        scrl.init();
    }

    this.init = function() {
        this.Up.style.cursor = this.Dw.style.cursor = "pointer";
        if (this.Up.addEventListener) {
            this.Up.addEventListener('mousedown', this.sobe, false);
            //this.Up.addEventListener('mouseup', this.para, false);

            this.Dw.addEventListener('mousedown', this.desce, false);
            //this.Dw.addEventListener('mouseup', this.para, false);
            document.body.addEventListener('mouseup', this.para, false);
        } else {
            this.Up.attachEvent("onmousedown", this.sobe);
            //this.Up.attachEvent("onmouseup", this.para);
            this.Dw.attachEvent("onmousedown", this.desce);
            //this.Dw.attachEvent("onmouseup", this.para);
            document.body.attachEvent("onmouseup", this.para);
        }
        if (!this.ie)
            this.Altura = document.defaultView.getComputedStyle(this.Conteudo, null).height;
        else
            this.Altura = this.Conteudo.currentStyle["height"];
        this.Altura = parseInt(this.Altura.replace("px", ""));
    }

    this.sobe = function() {
        scrl.Intervalo = setInterval(function() {
            if (scrl.Margem < 0) {
                scrl.Margem += 10;
                if (scrl.Margem > 0)
                    scrl.Margem = 0;
                scrl.Conteudo.style.marginTop = scrl.Margem + "px";
            }
        }, 50);
    }

    this.desce = function() {
        scrl.Intervalo = setInterval(function() {
            var dif = -(scrl.Altura - scrl.AreaVisivel);
            if (scrl.Margem > dif) {
                scrl.Margem -= 10;
                if (scrl.Margem < dif)
                    scrl.Margem = dif;
                scrl.Conteudo.style.marginTop = scrl.Margem + "px";
            }
        }, 50);
    }

    this.para = function() {
        clearInterval(scrl.Intervalo);
        scrl.Intervalo = 0;
    }

    this.obterMargem = function() {
        var margem = scrl.Conteudo.style.marginTop;
        margem = margem.replace("px", "");
        if (scrl.ie) {
        } else {
            alert(scrl.Conteudo);
        }
    }
    
    
}