//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("scrollcontent").offsetHeight;
	if (scroller.docH == 0){
		scroller.docH = 1;
	}
    scroller.contH = document.getElementById("scrollcontainer").offsetHeight;
    scroller.scrollAreaH = document.getElementById("scrollArea").offsetHeight;
	scroller.scrollPos = 0;
      
    //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);
     
	//nur anzeigen wenn gescrollt werden muss
	if(scroller.docH > 300) {
		document.getElementById("scroller").style.visibility = "visible";
		document.getElementById("scrollArea").style.visibility = "visible";
		document.getElementById("scrollbtnUp").style.visibility = "visible";
		document.getElementById("scrollbtnDown").style.visibility = "visible";
	}

    //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 = parseInt(0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist));
      document.getElementById("scrollcontent").style.top = docY + "px";
	  scroller.scrollPos = (scrollY);
	  
    }
	 

    document.getElementById("scrollbtnDown").onclick = function (evt) {
   		scroller.scrollPos = scroller.scrollPos + 30;
		if(scroller.scrollPos >= scroller.scrollDist) {
			scroller.scrollPos = scroller.scrollDist;
		}
    	document.getElementById("scroller").style.top = parseInt(scroller.scrollPos) + "px";
      	var scrollY = parseInt(document.getElementById("scroller").style.top);
      	var docY = parseInt(0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist));
      	document.getElementById("scrollcontent").style.top = docY + "px";
    }
	
    document.getElementById("scrollbtnUp").onclick = function (evt) {
		scroller.scrollPos = parseInt (scroller.scrollPos - 30);
		
		if(scroller.scrollPos <= 0) {
			scroller.scrollPos = 0;
		}
    	document.getElementById("scroller").style.top = parseInt(scroller.scrollPos) + "px";
      	var scrollY = parseInt(document.getElementById("scroller").style.top);
      	var docY = parseInt(0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist));
		
      	document.getElementById("scrollcontent").style.top = docY + "px";
		
    }	
  }
}
addEvent(window, 'load', scroller.init);

/*
document.onmousewheel = function(evt)
{
 evt=evt?evt:window.event;
 if (evt.wheelDelta) { ;
        //alert(evt.wheelDelta);
        document.getElementById("scrollcontent").style.top = evt.wheelDelta + "px";
 }
}
*/
