/**
 * @author ArKa jk
 */
isIE = document.all;
isNN = !document.all && document.getElementById;
isN4 = document.layers;
isHot = false;
var scrollbarsloaded = new Array();

AKscrollbar = function(){
    document.onmousedown = this.ddInit;
    document.onmouseup = Function("ddEnabled=false;");
}

AKscrollbar.prototype.sufijo;

AKscrollbar.prototype.ddInit = function(e){
    var sufijoin = getsufijo(e.target.id);
    topDog = isIE ? "BODY" : "HTML";
    whichDog = isIE ? document.all.scrinside : document.getElementById("scrinside" + sufijoin);
    hotDog = isIE ? event.srcElement : e.target;
    while (hotDog.id != "scrollbr" + sufijoin && hotDog.tagName != topDog) {
        hotDog = isIE ? hotDog.parentElement : hotDog.parentNode;
    }
    
    if (hotDog.id == "scrollbr" + sufijoin) {
        offsetx = isIE ? event.clientX : e.clientX;
        offsety = isIE ? event.clientY : e.clientY;
        nowX = parseInt(whichDog.style.left);
        nowY = parseInt(whichDog.style.top);
        ddEnabled = true;
        
        document.onmousemove = dd;
    }
    
    
}

function getsufijo(cadena){
    var cadenasplit = cadena.split("_");
    return "_" + cadenasplit[1];
}

function dd(e){
    if (!ddEnabled) 
        return;
    whichDog.style.top = isIE ? nowY + event.clientY - offsety : nowY + e.clientY - offsety; //comentar esto para mover solo horizontalmente
    var altura = whichDog.style.top;
    var sufijoin = getsufijo(e.target.id);
    var scrollactual = findscr(sufijoin);
    scrollactual.alturadef = altura.replace("px", "")
    scrollactual.alturadef = scrollactual.alturadef.replace("pt", "")
    
    if (scrollactual.alturadef <= 0) {
        document.getElementById('scrinside' + sufijoin).style.top = 0 + "px";
        scrollactual.alturadef = 0;
    }
    else 
        if (scrollactual.alturadef * scrollactual.factor >= scrollactual.alturadiferencial) {
        
            document.getElementById('scrinside' + sufijoin).style.top = (scrollactual.alturadiferencial / scrollactual.factor) - 1 + "px";
            scrollactual.alturadef = scrollactual.alturadiferencial / scrollactual.factor;
            
        }
    document.getElementById('divContent' + sufijoin).style.top = -scrollactual.alturadef * scrollactual.factor + "px";
    scrollactual.alturadef = -scrollactual.alturadef * scrollactual.factor;
    
    return false;
    
}

var activarscroll;
AKscrollbar.prototype.handleMouseWheel = function(e){
    var sufijoin = getsufijo(this.id);
    var scrollactual = findscr(sufijoin);
    if (!e) 
        e = window.event;
    if (activarscroll == 0) {
    
        return false;
    }
    {
        if (e.wheelDelta <= 0 || e.detail > 0) {
            if (scrollactual.alturadef - 30 <= -scrollactual.alturadiferencial) {
                scrollactual.alturadef = -scrollactual.alturadiferencial;
                document.getElementById('divContent' + sufijoin).style.top = scrollactual.alturadef + "px";
                document.getElementById('scrinside' + sufijoin).style.top = (-scrollactual.alturadef / scrollactual.factor) - 1 + "px";
                
            }
            else {
                scrollactual.alturadef = scrollactual.alturadef - 30;
                document.getElementById('divContent' + sufijoin).style.top = scrollactual.alturadef + "px";
                document.getElementById('scrinside' + sufijoin).style.top = (-scrollactual.alturadef / scrollactual.factor) - 1 + "px";
                
                //document.getElementById('posicionx').value=-scrollactual.alturadiferencial;
            }
        }
        else {
        
            if (scrollactual.alturadef + 30 >= 0) {
                scrollactual.alturadef = 0;
                document.getElementById('divContent' + sufijoin).style.top = 0 + "px";
                document.getElementById('scrinside' + sufijoin).style.top = (-scrollactual.alturadef / scrollactual.factor) + 0 + "px";
            }
            else {
                scrollactual.alturadef = scrollactual.alturadef + 30;
                document.getElementById('divContent' + sufijoin).style.top = scrollactual.alturadef + "px";
                document.getElementById('scrinside' + sufijoin).style.top = (-scrollactual.alturadef / scrollactual.factor) + 0 + "px";
            }
        }
    }
    if (!e) 
        e = window.event;
    
    if (e.preventDefault) 
        e.preventDefault();
    else 
        e.returnValue = false;
    
}
AKscrollbar.prototype.alturadef = 0;
AKscrollbar.prototype.alturacontenedor = null;
AKscrollbar.prototype.alturadiferencial;
AKscrollbar.prototype.factor;
AKscrollbar.prototype.alturascroll;
AKscrollbar.prototype.hallarbarra = function(sufijo){
    this.sufijo = sufijo;
    document.getElementById('divContent' + this.sufijo).style.top = 0;
    document.getElementById('scrinside' + this.sufijo).style.top = 0;
    this.alturadef = 0;
    
    this.alturacontenedor = document.getElementById('divContainer' + this.sufijo).offsetHeight;
    
    var barrascroll = document.getElementById('divContent' + this.sufijo).offsetHeight / this.alturacontenedor;
    var valorbarra = document.getElementById('Layerscroll' + this.sufijo).offsetHeight / barrascroll;
    document.getElementById("divContainer" + this.sufijo).style.overflow = "hidden";
    if (valorbarra >= document.getElementById("Layerscroll" + this.sufijo).clientHeight) {
        document.getElementById('Layerscroll' + this.sufijo).style.position = "absolute";
        document.getElementById('Layerscroll' + this.sufijo).style.top = -1000 + "px";
        document.getElementById('Layerscroll' + this.sufijo).style.left = -1000 + "px";
        document.getElementById('Layerscroll' + this.sufijo).style.display = "none";
        //document.getElementById('divContainer').style.width="350px";
        activarscroll = 0;
        valorbarra = 0;
    }
    else {
        document.getElementById('Layerscroll' + this.sufijo).style.position = "relative";
        document.getElementById('Layerscroll' + this.sufijo).style.top = 0 + "px";
        document.getElementById('Layerscroll' + this.sufijo).style.left = 0 + "px";
        document.getElementById('Layerscroll' + this.sufijo).style.display = "block";
        
        //document.getElementById('divContainer').style.width="373px";
        activarscroll = 1;
    }
    document.getElementById('scrollbr' + this.sufijo).style.height = valorbarra + "px";
    
    this.alturascroll = document.getElementById('Layerscroll' + this.sufijo).offsetHeight - document.getElementById('scrinside' + this.sufijo).offsetHeight;
    this.alturadiferencial = document.getElementById('divContent' + this.sufijo).offsetHeight - this.alturacontenedor;
    this.factor = this.alturadiferencial / this.alturascroll;
    
    if (window.addEventListener) {
    
    
        var mapElem = document.getElementById('divContent' + this.sufijo);
        mapElem.addEventListener("DOMMouseScroll", this.handleMouseWheel, true);
    }
    else {
    
        document.getElementById('divContent' + this.sufijo).onmousewheel = this.handleMouseWheel;
    }
    scrollbarsloaded.push(this);
    
}

function dnbtn(){
    AKscrollbar.prototype.alturadef -= 15;
    if (AKscrollbar.prototype.alturadef <= -AKscrollbar.prototype.alturadiferencial) {
        AKscrollbar.prototype.alturadef = -AKscrollbar.prototype.alturadiferencial;
    }
    document.getElementById('divContent' + this.sufijo).style.top = AKscrollbar.prototype.alturadef + "px";
    document.getElementById('scrinside' + this.sufijo).style.top = (-AKscrollbar.prototype.alturadef / AKscrollbar.prototype.factor) - 1 + "px";
}

function upbtn(){
    AKscrollbar.prototype.alturadef += 15;
    if (AKscrollbar.prototype.alturadef >= 15) {
        AKscrollbar.prototype.alturadef = 3;
    }
    document.getElementById('divContent' + this.sufijo).style.top = AKscrollbar.prototype.alturadef + "px";
    document.getElementById('scrinside' + this.sufijo).style.top = (-AKscrollbar.prototype.alturadef / AKscrollbar.prototype.factor) - 1 + "px";
}

function findscr(sufijo){
    for (var i = 0; i < scrollbarsloaded.length; i++) {
        if (scrollbarsloaded[i].sufijo == sufijo) {
            return scrollbarsloaded[i];
        }
    }
}


