var now_playing_show = '';
var now_playing_title = '';
var now_showing = '';

var switch_direction = '';
var switch_to_text = '';
var switch_length;
var switch_interval;

function reloadTitleCallback( text, statusText)
{
	now_playing_show = text.split( "- (" )[1].split(')')[0];
	now_playing_title = text.split( "- (" )[0];
	$('#now_playing_show').empty().append( '<span>'+now_playing_show+'</span>' );
	$('#now_playing_title').empty().append( '<span>'+now_playing_title+'</span>' );
}

function reloadTitle()
{
	jQuery.get( '/cms/r21toweb/titel.php', {}, reloadTitleCallback, 'text' );
}

function doSwitch()
{
	if (switch_direction == 'right')
	{
		var text = $('#now_playing').text();
		var cut = 1;
		while ( cut < text.length && text.substr( text.length - cut, 1 ) == ' ' )
			++cut;
		$('#now_playing').text( text.substr( 0, text.length - 1 )  );
		if ( cut >= text.length )
		{
			switch_direction = 'left';
			switch_length = 0;
		}
	}
	else
	{
		++switch_length;
		$('#now_playing').text( switch_to_text.substr( 0, switch_length ) );
		if (switch_length == switch_to_text.length)
		{
			window.clearInterval( switch_interval );
			switch_direction = 'right';
		}
	}
}

function switchShowShow()
{
	$('#now_playing_title').hide();
	$('#now_playing_show').fadeIn( "slow" );
}

function switchShowTitle()
{
	$('#now_playing_show').hide();
	$('#now_playing_title').fadeIn( "slow" );
}

function switchShowAndTitle()
{
	if (now_showing == 'title')
	{
		$('#now_playing_title').fadeOut( "slow", switchShowShow );
		$('#now_playing').animate( { width: $('#now_playing_show').width() }, "slow" );
		now_showing = 'show';
	}
	else
	{
		$('#now_playing_show').fadeOut( "slow", switchShowTitle );
		$('#now_playing').animate( { width: $('#now_playing_title').width() }, "slow" );
		now_showing = 'title';
	}
}

$(document).ready(function(){
	window.setInterval( "reloadTitle()", 30000 );

	now_playing_title = $('#now_playing').text();
	now_playing_show = now_playing_title.split( "- (" )[1].split(')')[0];
	now_playing_title = now_playing_title.split( "- (" )[0];

	now_showing = 'title';

	$('#now_playing').empty().append( '<div id="now_playing_title"><span>'+now_playing_title+'</span></div><div id="now_playing_show"><span>'+now_playing_show+'</span></div>' );
	$('#now_playing_show').hide();

	window.setInterval( "switchShowAndTitle()", 5000 );

});