////////
// File: Scroll.js
// Author: Luke Mallon
// Date: 09/02/2007
//
// This script is for scrolling the contents of a div up, down, left or right.
// This works fine in IE 5+ and Firefox.
//
// Use functions like this:
// onmouseover="hovering = true; upScroll(document.getElementById('divId'));"
// onmouseout="hovering = false;"
//
// onmousedown="press = true; upScroll(document.getElementById('divId'));"
// onmouseup="press = false;"
//
// divId needs to have a position of absolute or relative.
// it also needs to have an overflow of hidden.
////////

// default hovering and press to false.
// these are the variables that tell the script to scroll the content of the div.
// when set to true the div content scrolls.
var hovering = false;
var press = false;

// setting a variable to set the rate of the scrolling.
var scrollRate = 1;
var scrollAmount = 1;

// Scroll the div up
function upScroll(div)
{
   // we first have to get the position of the div.
   var topOfDiv = div.scrollTop;
   var heightOfDiv = div.scrollHeight;
     // for testing purposes we output the variables of div in an alert.
   //alert('Top: '+topOfDiv);
   //alert('Height: '+heightOfDiv);
   //alert('Div: '+div.id);
     // now we have to check that the div when scrolling up doesn't scroll past the topOfDiv value.
   if(topOfDiv > 0)
   {
       if(hovering)
       {
           div.scrollTop = div.scrollTop - (div.scrollAmount = scrollAmount);
           setTimeout("upScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
       else if(press)
       {
           div.scrollTop = div.scrollTop - (div.scrollAmount = scrollAmount);
           setTimeout("upScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
   }
}

// Scroll the div down
function downScroll(div)
{
   // we first have to get the position of the div.
   var topOfDiv = div.scrollTop;
   var heightOfDiv = div.scrollHeight;
     // for testing purposes we output the variables of div in an alert.
   //alert('Top: '+topOfDiv);
   //alert('Height: '+heightOfDiv);
   //alert('Div: '+div.id);
     // now we have to check that the div when scrolling up doesn't scroll past the topOfDiv value.
   if(topOfDiv < (heightOfDiv-div.clientHeight))
   {
       if(hovering)
       {
           div.scrollTop = div.scrollTop + (div.scrollAmount = scrollAmount);
           setTimeout("downScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
       else if(press)
       {
           div.scrollTop = div.scrollTop + (div.scrollAmount = scrollAmount);
           setTimeout("downScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
   }
}

// Scroll the div left
function leftScroll(div)
{
   // we first have to get the position of the div.
   var leftOfDiv = div.scrollLeft;
   var widthOfDiv = div.scrollWidth;
     // for testing purposes we output the variables of div in an alert.
   //alert('Left: '+leftOfDiv);
   //alert('Width: '+widthOfDiv);
   //alert('Div: '+div.id);
     // now we have to check that the div when scrolling up doesn't scroll past the topOfDiv value.
   if(leftOfDiv > 0)
   {
       if(hovering)
       {
           div.scrollLeft = div.scrollLeft - (div.scrollAmount = scrollAmount);
           setTimeout("leftScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
       else if(press)
       {
           div.scrollLeft = div.scrollLeft - (div.scrollAmount = scrollAmount);
           setTimeout("leftScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
   }
}

// Scroll the div right
function rightScroll(div)
{
   // we first have to get the position of the div.
   var leftOfDiv = div.scrollLeft;
   var widthOfDiv = div.scrollWidth;
     // for testing purposes we output the variables of div in an alert.
   //alert('Left: '+leftOfDiv);
   //alert('Width: '+widthOfDiv);
   //alert('Div: '+div.id);
     // now we have to check that the div when scrolling up doesn't scroll past the leftOfDiv value.
   if(leftOfDiv < (widthOfDiv-div.clientWidth))
   {
       if(hovering)
       {
           div.scrollLeft = div.scrollLeft + (div.scrollAmount = scrollAmount);
           setTimeout("rightScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
       else if(press)
       {
           div.scrollLeft = div.scrollLeft + (div.scrollAmount = scrollAmount);
           setTimeout("rightScroll(document.getElementById('"+div.id+"'))", scrollRate);
       }
   }
} 