var ball_left = new Array();
var ball_top = new Array();
var ball_impact = new Array();
var target_url = new Array();
var trail_offset;
var ball_to_move;
var ball_to_move_id;
var ball_timer = 0;
var ball_move_stage;
var ball_drop_rate;
var ball_speed = 50;
var ball_rebound_speed;
var gravity = 1;
var trail_element;
var next_z_index = 1;

var ticker_text = '<p id="ticker0"></p>';
var ticker_text_count = 1;
var ticker_timer = 0;
var ticker_scroll_timer = 0;
var current_text = 0;
var new_text;
var ticker_scroll_step;

function position_ball( ball_id, left, top, impact, url )
{
	ball_element = document.getElementById( ball_id );

	ball_element.style.top = top;
	ball_element.style.left = left;
	ball_element.style.visibility = 'visible';
	ball_element.style.zIndex = next_z_index;
	next_z_index++;

	ball_left[ ball_id ] = left;
	ball_top[ ball_id ] = top;
	ball_impact[ ball_id ] = impact;
	target_url[ ball_id ] = url;
}

function click_ball( ball_id )
{
	ball_to_move_id = ball_id;
	ball_to_move = document.getElementById( ball_id );
	ball_move_stage = 1;

	if ( ball_timer == 0 )
	{
		ball_timer = window.setInterval( 'move_ball()', 25 );
	}
}

function move_ball()
{
	if ( ball_move_stage == 1 )
	{
		ball_left[ ball_to_move_id ] -= ball_speed;

		if ( ( ball_impact[ ball_to_move_id ] != 0 ) && ( ball_left[ ball_to_move_id ] <= ball_impact[ ball_to_move_id ] ) )
		{
			ball_left[ ball_to_move_id ] = ball_impact[ ball_to_move_id ];
			ball_move_stage = 2;
			trail_offset = 0;
			trail_element = document.getElementById( ball_to_move_id + '_trail' );

			if ( ball_top[ ball_to_move_id ] >= 400 )
			{
				ball_drop_rate = 0;
				ball_rebound_speed = 10;
			}
			else
			{
				ball_drop_rate = -20;
				ball_rebound_speed = -10;
			}
		}

		ball_to_move.style.left = ball_left[ ball_to_move_id ];

		if ( ball_left[ ball_to_move_id ] < -500 )
		{
			window.clearInterval( ball_timer );
			ball_timer = 0;

			window.location.href = target_url[ ball_to_move_id ];
		}
	}
	else if ( ball_move_stage == 2 )
	{
		ball_drop_rate += gravity;
		ball_top[ ball_to_move_id ] += ball_drop_rate;
		ball_left[ ball_to_move_id ] += ball_rebound_speed;
		trail_offset -= ( ball_speed + ball_rebound_speed );

		ball_to_move.style.top = ball_top[ ball_to_move_id ];
		ball_to_move.style.left = ball_left[ ball_to_move_id ];

		if ( trail_offset <= -414 )
		{
			trail_element.style.visibility = 'hidden';
		}
		else
		{
			trail_element.style.left = trail_offset;
		}

		if ( ball_top[ ball_to_move_id ] > 750 )
		{
			window.clearInterval( ball_timer );
			ball_timer = 0;

			window.location.href = target_url[ ball_to_move_id ];
		}
	}
}

function ball_over( ball_id )
{
	document.getElementById( ball_id + '_text' ).className = 'over';
}

function ball_out( ball_id )
{
	document.getElementById( ball_id + '_text' ).className = '';
}

function add_ticker_text( text )
{
	ticker_text += '<p id="ticker' + ticker_text_count + '"><img src="images/ball_ticker.png" />' + text 
				+ '<img src="images/ball_ticker.png" /></p>';

	ticker_text_count++;
}

function start_ticker()
{
	document.getElementById( 'ticker' ).innerHTML = ticker_text;

	start_ticker_scroll();

	ticker_timer = window.setInterval( 'start_ticker_scroll()', 5000 );
}

function start_ticker_scroll()
{
	new_text = current_text + 1;

	if ( new_text >= ticker_text_count )
	{
		new_text = 1;
	}

	ticker_scroll_step = 0;

	stop_timer( ticker_scroll_timer );
	ticker_scroll_timer = window.setInterval( 'scroll_ticker()', 25 );
}

function stop_timer( timer )
{
	if ( timer != 0 )
	{
		window.clearInterval( timer );
	}
}

function scroll_ticker()
{
	current_element = document.getElementById( 'ticker' + current_text );
	new_element = document.getElementById( 'ticker' + new_text );

	ticker_scroll_step++;

	current_element.style.top = 6 - ticker_scroll_step;
	new_element.style.top = 36 - ticker_scroll_step;

	if ( ticker_scroll_step >= 30 )
	{
		stop_timer( ticker_scroll_timer );
		current_text = new_text;
	}
}


