////////
// 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; scrollUp(document.getElementById('divId'));"
// onmouseout="hovering = false;"
//
// onmousedown="press = true; scrollUp(document.getElementById('divId'));"
// onmouseup="press = false;"
////////

// 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 scrollUp(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("scrollUp(document.getElementById('"+div.id+"'))", scrollRate);
		}
		else if(press)
		{
			div.scrollTop = div.scrollTop - (div.scrollAmount = scrollAmount);
			setTimeout("scrollUp(document.getElementById('"+div.id+"'))", scrollRate);
		}
	}
}

// Scroll the div down
function scrollDown(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("scrollDown(document.getElementById('"+div.id+"'))", scrollRate);
		}
		else if(press)
		{
			div.scrollTop = div.scrollTop + (div.scrollAmount = scrollAmount);
			setTimeout("scrollDown(document.getElementById('"+div.id+"'))", scrollRate);
		}
	}
}

// Scroll the div left
function scrollLeft(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("scrollLeft(document.getElementById('"+div.id+"'))", scrollRate);
		}
		else if(press)
		{
			div.scrollLeft = div.scrollLeft - (div.scrollAmount = scrollAmount);
			setTimeout("scrollLeft(document.getElementById('"+div.id+"'))", scrollRate);
		}
	}
}

// Scroll the div right
function scrollRight(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("scrollRight(document.getElementById('"+div.id+"'))", scrollRate);
		}
		else if(press)
		{
			div.scrollLeft = div.scrollLeft + (div.scrollAmount = scrollAmount);
			setTimeout("scrollRight(document.getElementById('"+div.id+"'))", scrollRate);
		}
	}
}