
timer = null;
initTop = 0;

//We wrap all the code in an object so that it doesn't interfere with any other code
var scroller = {
  init:   function() {

    //collect the variables
    scroller.docH = document.getElementById("content").offsetHeight;
    scroller.contH = document.getElementById("container").offsetHeight;
    scroller.scrollAreaH = document.getElementById("scrollArea").offsetHeight;
      
    //calculate height of scroller and resize the scroller div
    //(however, we make sure that it isn't to small for long pages)
    scroller.scrollH = (scroller.contH * scroller.scrollAreaH) / scroller.docH;
    //if(scroller.scrollH < 15) scroller.scrollH = 15;
    //document.getElementById("scroller").style.height = Math.round(scroller.scrollH) + "px";
    
    //what is the effective scroll distance once the scoller's height has been taken into account
    //scroller.scrollDist = Math.round(scroller.scrollAreaH-scroller.scrollH);
    scroller.scrollDist = Math.round(scroller.scrollAreaH-60);
    
    //make the scroller div draggable
    Drag.init(document.getElementById("scroller"),null,0,0,-1,scroller.scrollDist);
    
    //add ondrag function
    document.getElementById("scroller").onDrag = function (x,y) {
      var scrollY = parseInt(document.getElementById("scroller").style.top);
      var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist);
      document.getElementById("content").style.top = docY + "px";
    }
  },
  
  goDown:   function() {
    var scrollY = parseInt(document.getElementById("scroller").style.top);
    if(initTop == 0) initTop = scrollY;
    if(scrollY<initTop + scroller.scrollDist) {
        scrollY++;
        document.getElementById("scroller").style.top = scrollY + "px";
        var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist);
        document.getElementById("content").style.top = docY + "px";
        timer = setTimeout("scroller.goDown()",5);
    }
  },

  goUp:   function() {
    var scrollY = parseInt(document.getElementById("scroller").style.top);
    if(scrollY>initTop) {
        scrollY--;
        document.getElementById("scroller").style.top = scrollY + "px";
        var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist);
        document.getElementById("content").style.top = docY + "px";
        timer = setTimeout("scroller.goUp()",5);
    }
  },

  release: function() {
    if(timer) {
        clearTimeout(timer);
    }
  }
  
};

onload = scroller.init;
onresize = scroller.init;

function go(sec,idx) {
    window.frames.media.location = 'images.php?seccion='+sec+'&id_item='+ idx;
    window.frames.text.location = 'content.php?seccion='+sec+'&id_item='+ idx;
}

function Toggle(what) {
    el = document.getElementById(what);
    if(el.style.display == 'none') {
        el.style.display = 'block';
    } else {
        el.style.display = 'none';
    }
    scroller.init();
}

