/*
 * Scrollbox
 * =========
 *
 * Description:	Used to create and control a scrollbox
 */


// Creates a Scrollbox structure
function Scrollbox( id, total_nodes, image_width, padding, scroll_speed, timer_interval, timer_speed )
{
	this.id 			= id;				// The ID of the scroll box containing the images
	this.total_nodes 	= total_nodes;		// The total number of images in the scrollbox
	this.start_timer 	= null;				// The timer that runs when the scrolling begins
	this.end_timer 		= null;				// The timer that runs when the scrolling ends
	this.image_width	= image_width;		// The width of the image (including padding and borders)
	this.padding		= padding;
	this.scrollable		= true;
	
	// The end position of the last image in the scrollbox
	this.end_pos		= ( ( ( image_width + padding ) * total_nodes ) - document.getElementById( id ).parentNode.offsetWidth - padding ) * -1;
	
	
	
	this.scroll_speed	= scroll_speed;		// The speed at which the image scroll (in pixels)
	this.timer_interval	= timer_interval;	// The time delay between updating the scrollbox (in milliseconds)
	this.timer_max 		= timer_interval;	// The maximum time between scrollbox updates (in milliseconds)
	this.timer_speed	= timer_speed;		// The speed of at which the timer interval changes (in milliseconds)
	
	document.getElementById( id ).style.width = image_width * total_nodes; // set the width of the container
} 


// Called when the mouse button is held down, causes the scrolling to accelerate and run at a continuous speed
function start_scroll( index, direction )
{
	var sb = window.scrollbox[ index ];
	
	if( sb.scrollable )
	{
		scrollbox_scroll( index, direction ); // perform the scroll
		sb.timer_interval -= sb.timer_speed;  // ammend the timer interval (speed it up)
		if( sb.timer_interval < 0 ) sb.timer_interval = 0; // check to see whether we are running at full pelt
		sb.start_timer = setTimeout( 'start_scroll(' + index + ', ' + direction + ')', sb.timer_interval );
	}
}

// Called when the mouse button is released, causes scrolling to slow down to a stop
function end_scroll( index, direction )
{
	var sb = window.scrollbox[ index ];
	// check whether either timer is set, otherwise things will start jumping about on mouse out
	// jumping about like pixies!
	if( sb.start_timer || sb.end_timer )
	{
		// First things first, stop the start timer (rem this if statement if you want a laugh ;)
		if( sb.start_timer )
		{
			clearTimeout( sb.start_timer );
			sb.start_timer = null;
		}
		scrollbox_scroll( index, direction ); // perform the scroll
		sb.timer_interval += sb.timer_speed; // ammend the timer interval (slow it down)
		
		// Once the scrolling has come to a halt kill the timer
		if( sb.timer_interval >= sb.timer_max )
		{
			sb.timer_interval = sb.timer_max;
			clearTimeout( sb.end_timer );
			sb.end_timer = null;
		}
		else
		{	
			sb.end_timer = setTimeout( 'end_scroll(' + index + ', ' + direction + ')', sb.timer_interval );
		}
	}
}

function scrollbox_scroll( index, direction )
{
	var sb = window.scrollbox[ index ];
	var parent_obj  = document.getElementById( sb.id );
	if( !parent_obj.style.left ) parent_obj.style.left = 0;
	var position = sb.scroll_speed * direction;
	
	// alert( 'parent width: ' + parent_obj.style.width + ', sb end pos:' + sb.end_pos );
	
	var parent_width = parent_obj.style.width.replace( "px", "" );
	
	if( parent_width > sb.end_pos )
	{
		if( parseInt(parent_obj.style.left) + position > 0 ) 
		{
			parent_obj.style.left = '0px';
		}
		else if( parseInt(parent_obj.style.left) + position < sb.end_pos ) 
		{
			parent_obj.style.left = sb.end_pos + 'px';
		}
		else 
		{
			parent_obj.style.left = parseInt(parent_obj.style.left) + position + 'px';
		}
		//alert( window.scrollbox[ index ].scroll_speed * direction );
	}
}

function resize_scrollbox()
{	
	for( index in window.scrollbox )
	{
		var sb = window.scrollbox[ index ];
		var id = sb.id;
		var obj_parent = document.getElementById( sb.id );
		var width = obj_parent.offsetWidth;
		
		var a = ( sb.image_width * sb.total_nodes );
		var b = document.getElementById( id ).parentNode.parentNode.offsetWidth;
		
		sb.end_pos = ( a - b ) * -1;
		//alert( a + " " + b );
		
		if( b >= a ) sb.scrollable = false;
		else sb.scrollable = true;
	}
}



window.scrollbox = new Object(); 