var scroller = {
  init:   function() {
    //collect the variables
    scroller.docH = document.getElementById("divText").offsetHeight;
    scroller.contH = document.getElementById("divScrollTextCont").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);
    
    //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("divText").style.top = docY + "px";
    }
  }
}

var distance = 5;
var speed = 240;
var timer = null;

function startScrollUp() {
	startScroll(-distance);
	timer = setTimeout(startScrollUp, speed);
}

function startScrollDown() {
	startScroll(distance);
	timer = setTimeout(startScrollDown, speed);
}

function startScroll(d) {
	var oScroll = document.getElementById("scroller");
	var oScrollArea = document.getElementById("scrollArea");
	var top;

	if(d > 0) {
		var maxTop = oScrollArea.offsetHeight - oScroll.offsetHeight;
		top = (maxTop > 0) ? Math.min(parseInt(oScroll.style.top) + d, maxTop) : 0;
	} else {
		var minTop = 0;
		top = Math.max(parseInt(oScroll.style.top) + d, minTop);
	}
	oScroll.style.top = top + "px";
	oScroll.onDrag(0,0);
}

function noScroll() { if(timer) clearTimeout(timer); timer = null; }
