// NOTE: Lots of this code is taken from:
//	http://13thparallel.org/archive/dhtml-scrollbars/example.htm
// which, in turn, takes a dragging library from:
//	http://boring.youngpup.net/2001/domdrag
// I added all the code that allows the up and down scroll buttons
//  to work, as well as using images (instead of shapes) for the
//  scroll lozenge and the scroll area background

/////////////////////////////////////////////////////////////////////////////////
// NOTE: scroller_1 and scroller_2 are supposed to be EXACTLY the same code
//  (with the obvious exception that items in the former object will have
//  '_1' suffixes while the latter will have '_2' suffixes
/////////////////////////////////////////////////////////////////////////////////

var upInterval, downInterval;
var intervalLength = 100;
var scrollButtonsHeight = 0;

var scroller_1 = {
	init:   function() {
		init_scroller(1);
	},
		
	scrollContent: function(x,y) {
		scroll_content(x,y,1)
	},

	scrollDown: function() {
		scroll_down(1);
	},

	scrollUp: function() {
		scroll_up(1);
	}
}


//We wrap all the code in an object so that it doesn't interfere with any other code
var scroller_2 = {
	init:   function() {
		init_scroller(2);
	},
		
	scrollContent: function(x,y) {
		scroll_content(x,y,2);
	},

	scrollDown: function() {
		scroll_down(2);
	},

	scrollUp: function() {
		scroll_up(2);
	}
}

function scrollers_init()
{
	init_scroller(1);
	// init_scroller(2);
}

function init_scroller(_num)
{
	var scroller = eval('scroller_' + _num);
	// var scroller = scrollers[_num];
	
	// docH is the total height of the content (regardless of how much is visible) 
	scroller.docH = $("scroll_content_" + _num).offsetHeight;
	
	// contH is the visible height of the content
	scroller.contH = $("container_" + _num).offsetHeight;

	// Only display the scroll bar if it's necessary and, if no scroll bar is
	//  needed, stretch the text box a little further now that we have the room
	if(scroller.contH > scroller.docH)
	{
		var extra_width = $("scrollArea_" + _num).offsetWidth;
		var original_height = $("scroll_content_" + _num).offsetHeight;
		$("scrollerMaster").removeChild($("scrollArea_" + _num));
		$("container_" + _num).style.width = ($("container_" + _num).offsetWidth + extra_width) + "px";
		$("scroll_content_" + _num).style.width = ($("scroll_content_" + _num).offsetWidth + extra_width) + "px";

		var new_height = $("scroll_content_" + _num).offsetHeight;
		var height_change = original_height - new_height;
		$("container_" + _num).style.height = ($("container_" + _num).offsetHeight - height_change) + "px";
	}
	else
	{
		$("scrollArea_" + _num).style.visibility = 'visible';
	}

	// scrollAreaH is the vertical area available to the lozenge

	// Skip this code if there are no scroll buttons
	if($("scroll_down_button_" + _num))
	{
		scrollButtonsHeight = $("scroll_down_button_" + _num).offsetHeight + $("scroll_up_button_" + _num).offsetHeight

		$("scroll_down_button_" + _num).onmousedown = scroller.scrollDown;
		$("scroll_down_button_" + _num).onmouseup = $("scroll_down_button_" + _num).onmouseout = function()
		{
			clearInterval(downInterval);
		}
	
		$("scroll_up_button_" + _num).onmousedown = scroller.scrollUp;
		$("scroll_up_button_" + _num).onmouseup = $("scroll_up_button_" + _num).onmouseout = function()
		{
			clearInterval(upInterval);
		}
	}
	
	// Skip this code if there is no scroll bar
	if($("scrollArea_" + _num))
	{
		scroller.scrollAreaH = $("scrollArea_" + _num).offsetHeight - (scrollButtonsHeight + 1);
		
		// Set the height of the scroll bar background image
		$('scroll_bgd').style.height = (scroller.scrollAreaH - 2) + "px";
		
		//calculate height of scroller and resize the scroller div
		//(however, we make sure that it isn't to small for long pages)
		if(dynamic_lozenge)
		{
			scroller.scrollH = (scroller.contH * scroller.scrollAreaH) / scroller.docH;
		}
		else
		{
			scroller.scrollH = lozenge_height;
		}

		// Change the height of the lozenge based on the height (amount) of the content
		$("scroller_" + _num).style.height = Math.round(scroller.scrollH) + "px";
		$("scroll_lozenge_" + _num).style.height = Math.round(scroller.scrollH) + "px";
		$("scroll_lozenge_" + _num).style.width = 15 + "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($("scroller_" + _num),null,0,0,0,scroller.scrollDist);
		
		// Add ondrag function
		$("scroller_" + _num).onDrag = scroller.scrollContent;
		
		// Make sure we begin with all the content scrolled to the top
		$("scroller_" + _num).style.top = "0px";
		$("scroller_" + _num).onDrag();
	}
}
	
function scroll_content(x,y,_num)
{
	var scroller = eval('scroller_' + _num);
	var scrollY = parseInt($("scroller_" + _num).style.top);
	var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist);
	$("scroll_content_" + _num).style.top = docY + "px";
}

function scroll_down(_num)
{
	clearInterval(downInterval);
	clearInterval(upInterval);
	downInterval = setInterval('scroll_down_interval(' + _num + ')',intervalLength);
}

function scroll_down_interval(_num)
{
	var scroller = eval('scroller_' + _num);
	var maxY = scroller.scrollAreaH - scroller.scrollH;
	var newY = Math.min(parseInt($("scroller_" + _num).style.top) + scrollAmount, maxY);
	$("scroller_" + _num).style.top = newY + "px";
	$("scroller_" + _num).onDrag();
}

function scroll_up(_num)
{
	clearInterval(downInterval);
	clearInterval(upInterval);
	upInterval = setInterval('scroll_up_interval(' + _num + ')',intervalLength);
}

function scroll_up_interval(_num)
{
	var newY = Math.max(parseInt($("scroller_" + _num).style.top) - scrollAmount, 0);
	$("scroller_" + _num).style.top = newY + "px";
	$("scroller_" + _num).onDrag();
}