function elm( id )
{
	return document.getElementById( id );
}

function dbg( str )
{
	elm( "dbg" ).innerHTML = str;
}

var SCROLL_SIZE = 800;
var scrolling = false;
var cur_img = 0;

function scroll_img_pane( side , type )
{
	if ( scrolling )
		return;

	if ( type == "img" )
	{
	
		if ( side == "left" && cur_img > 0 )
		{
			cur_img = cur_img - 1;
			grad_scroll_left( img_pos_x[ cur_img ] );
		}
		else if ( side == "right" && cur_img < img_pos_x.length - 1 &&
				 ( img_pos_x[ cur_img ] < elm("proje-img-pane").scrollWidth - SCROLL_SIZE ) )
		{
			cur_img = cur_img + 1;
			grad_scroll_right( img_pos_x[ cur_img ] );
		}
		
	}
	else
	{
		
		if ( side == "left" && elm("proje-img-pane").scrollLeft > 0 )
		{
			grad_scroll_left( elm("proje-img-pane").scrollLeft - SCROLL_SIZE );
		}
		else if ( side == "right" && elm("proje-img-pane").scrollLeft < elm("proje-img-pane").scrollWidth )
		{
			grad_scroll_right( elm("proje-img-pane").scrollLeft + SCROLL_SIZE );
		}
		
	}
	
}

function grad_scroll_left( new_pos )
{
	var target = Math.max( 0 , new_pos );
	
	if ( elm("proje-img-pane").scrollLeft > target )
	{
		elm("proje-img-pane").scrollLeft = Math.max( target , elm("proje-img-pane").scrollLeft - 25 );
		scrolling = true;
		setTimeout( "grad_scroll_left(" + new_pos + ")" , 25 );
	}
	else
	{
		scrolling = false;
	}
	
}

function grad_scroll_right( new_pos )
{
	var target = Math.min( new_pos , elm("proje-img-pane").scrollWidth - SCROLL_SIZE );
	
	if ( elm("proje-img-pane").scrollLeft < target )
	{
		elm("proje-img-pane").scrollLeft = Math.min( target , elm("proje-img-pane").scrollLeft + 25 );
		scrolling = true;
		setTimeout( "grad_scroll_right(" + new_pos + ")" , 25 );
	}
	else
	{
		scrolling = false;
	}
	
}

var TXT_SCROLL_SIZE = 210;

function scroll_text( eid , side , txtScrollSize )
{
	TXT_SCROLL_SIZE = txtScrollSize;
	
	if ( scrolling )
		return;

	if ( side == "up" && elm( eid ).scrollTop > 0 )
	{
		grad_scroll_up( eid , elm( eid ).scrollTop - TXT_SCROLL_SIZE );
	}
	else if ( side == "down" && elm( eid ).scrollTop < elm( eid ).scrollHeight )
	{
		grad_scroll_down( eid , elm( eid ).scrollTop + TXT_SCROLL_SIZE );
	}
}

function grad_scroll_up( eid , new_pos )
{
	var target = Math.max( 0 , new_pos );
	
	if ( elm( eid ).scrollTop > target )
	{
		var oldPos = elm( eid ).scrollTop;
		elm( eid ).scrollTop = elm( eid ).scrollTop - Math.ceil( ( elm( eid ).scrollTop - target ) / 20 );
		// fail:
		if ( elm( eid ).scrollTop != oldPos )
		{
			scrolling = true;
			setTimeout( "grad_scroll_up( '" + eid + "', " + new_pos + ")" , 20 );
		}
		else
			scrolling = false;
	}
	else
	{
		scrolling = false;
	}
	
}

function grad_scroll_down( eid , new_pos )
{
	var target = Math.min( new_pos , elm( eid ).scrollHeight - TXT_SCROLL_SIZE );
	
	if ( elm( eid ).scrollTop < target )
	{
		var oldPos = elm( eid ).scrollTop;
		elm( eid ).scrollTop = elm( eid ).scrollTop + Math.ceil( ( target - elm( eid ).scrollTop ) / 20 );
		// fail:
		if ( elm( eid ).scrollTop != oldPos )
		{
			scrolling = true;
			setTimeout( "grad_scroll_down( '" + eid + "', " + new_pos + ")" , 20 );
		}
		else
			scrolling = false;
	}
	else
	{
		scrolling = false;
	}
	
}


