//We wrap all the code in an object so that it doesn't interfere with any other code
var scroller = {
  init:   function() {
  
  	scroller.downArrow = document.getElementById("scrolldown");

    //collect the variables
    scroller.docH = document.getElementById("content").offsetHeight;
    scroller.contH = document.getElementById("container").offsetHeight;
    scroller.scrollAreaH = document.getElementById("scrollArea").offsetHeight;
	scroller.top = parseInt(document.getElementById("container").style.top);
    scroller.scrollH = document.getElementById("scroller").offsetHeight
	scroller.arrowHeight = document.getElementById("scrolldown").offsetHeight;
	//
	//what is the effective scroll distance once the scoller's height has been taken into account
    scroller.scrollDist = scroller.scrollAreaH - scroller.arrowHeight - scroller.scrollH ;
	//Math.round(scroller.scrollAreaH-scroller.scrollH - document.getElementById("scroller").offsetHeight);
	//
	scroller.downArrow.style.top = (parseInt(document.getElementById("scrollArea").offsetHeight)-scroller.arrowHeight) + "px";
	document.getElementById("scroller").style.top = scroller.arrowHeight + "px";
	document.getElementById("scrollArea").style.left = document.getElementById("container").offsetWidth + "px";
    //
	//make the scroller div draggable
	Drag.init(document.getElementById("scroller"),null,0,0,scroller.arrowHeight,scroller.scrollDist);
    
    //add ondrag function
    document.getElementById("scroller").onDrag = function (x,y) {
      var scrollY = parseInt(document.getElementById("scroller").style.top)-scroller.arrowHeight;
      var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / (scroller.scrollDist-scroller.arrowHeight));
      document.getElementById("content").style.top = docY + "px";
	  
    }
  }
}

var scroll_timer;

function scrollManager(s)
{
    switch ( s )
    {
	case "goUp":
		Scroll(-1);
		clearInterval( scroll_timer );
		scroll_timer = setInterval("Scroll(-1)",130);
    	break;
	case "goDown":
		Scroll(1);
		clearInterval( scroll_timer );
		scroll_timer = setInterval("Scroll(1)",130);
    	break;
	case "all_stop":
		clearInterval( scroll_timer );
		break;
  	}
	
}		
		
function Scroll(dir)
{

	var maxscroll = scroller.contH - scroller.docH;
	
	//
	var y = parseInt(document.getElementById("scroller").style.top);
	
	if(dir == 1)
	{
		document.getElementById("scroller").style.top = findBounds( y-scroller.scrollDist*(2/maxscroll) ) + "px";
	}
	else
	{
		document.getElementById("scroller").style.top = findBounds( y+scroller.scrollDist*(2/maxscroll) ) + "px";
	}

	var scrollY = parseInt(document.getElementById("scroller").style.top)-scroller.arrowHeight;
	var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / (scroller.scrollDist-scroller.arrowHeight));
    	document.getElementById("content").style.top = docY + "px";
	document.getElementById("scroller").style.top =  Math.max( scroller.arrowHeight , Math.min((parseInt(document.getElementById("scroller").style.top) +dir),scroller.scrollDist))+ "px";
}

function findBounds(y) {
	return Math.min(Math.max(y, scroller.arrowHeight), scroller.scrollDist);
}

window.onload = scroller.init;