/*
 * Scrollbox
 * =========
 *
 * Description:	Used to create and control a scrollbox
 */


// Creates a Scrollbox structure
function Scrollbox( id, width, window_size, scroll_speed, timer_interval, timer_speed )
{
	this.id 			= id;				// The ID of the scroll box containing the images
	
	var parent = document.getElementById( id );
	var objs = parent.getElementsByTagName( "img" );
	
	/*
	var width = objs.length * padding;
	
	//alert( objs.length );
	
	for( var i = 0; i < objs.length; i++ )
	{
		var obj = objs[ i ];
		width += scrollbox_get_width( obj );
		//alert( width );
	}
	*/
	
	// width -= scrollbox_get_width( parent.parentNode );
	

	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.scrollable		= true;
	
	// The end position of the last image in the scrollbox
	this.end_pos		= ( width - window_size ) * -1;
	
	// alert( this.end_pos );

	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)
	
	//parent.style.width = width; // 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 scrollbox_get_width( obj )
{
	var result = 0;
	if( obj.offsetWidth )
	{
		result = obj.offsetWidth;	
	}
	else if( obj.style.pixelWidth )
	{
		result = obj.style.pixelWidth;	
	}
	return result;
	
}


window.scrollbox = new Object(); 
