var currentVideo = null;
var videoRPC = null;

function getVideo( videoid, req )
{
	var url = "getvideo.php?id=" + videoid + "&r=" + req;

	if( videoRPC )
		videoRPC.abort();

	videoRPC = GetXmlHttpObject( handleVideo );

	if( videoRPC )
	{
		document.getElementById( 'videoItem' ).innerHTML = '<div class="loading"></div>';

		if( videoRPC != null )
			videoRPC.open("GET", url, true);
	
		if( videoRPC != null )
			videoRPC.send(null);
	}
}


function handleVideo()
{
	if( videoRPC == null ) return;

	if( videoRPC.readyState == 4 || videoRPC.readyState=="complete" )
	{
		if( videoRPC.status == 200 )
		{ 
			var response = videoRPC.responseText;
			var result = null;
			
			videoRPC = null;
			
			try { result = response.parseJSON(); }
			catch( ex )
			{
			        alert( 'Error while parsing: \"'+ response + '\".' );
			        return;
			}

			if( result.success )
			{
				showElement( 'uitzending', true );

				currentVideo = result.result;

				for( i = 0; i < 7; ++i )
					showElement( 'source' + i, false );
	
				if( currentVideo.sources.length == 1 )
				{
					playCurrentVideoType( currentVideo.sources[0].type );
				}
				else if( currentVideo.sources.length > 1 )
				{
					showElement( 'source0', true );

					for( i = 0; i < currentVideo.sources.length; ++i )
						showElement( 'source' + currentVideo.sources[i].type, true );
					
					playCurrentVideoType( currentVideo.sources[0].type );
				}
			}
			else
			{
				alert( 'Error retrieving video: ' + result.error );

				showElement( 'uitzending', false );
				return;
			}
		}
		else
		{
			videoRPC = null;
			showElement( 'uitzending', false );
			alert( 'Fout bij het ophalen van de gegevens, probeer het aub opnieuw...' );
			return;
		}
	}
}

function playCurrentVideoType( type )
{
	if( currentVideo == null )
		return;

	for( i = 0; i < currentVideo.sources.length; ++i )
	{
		var source = currentVideo.sources[i];

		if( type == source.type )
		{
			playVideoInTargetWithTitle( source.url, 'videoItem', source.type, true, currentVideo.title );
			return;
		}
	}
	
	alert( 'No video found for selected source type' );	
};

var lastLoggedPlay = -1;
var statsRPC = null;

function getUpdate(typ,pr1,pr2,swf)
{
	switch( typ )
	{
		case 'state':
			if( pr1 == 2 && lastLoggedPlay != currentVideo.id )
			{
				lastLoggedPlay = currentVideo.id;
				updateVideoStats( currentVideo.id );
			}
			// else
			  	// alert('the current state is: ' + pr1 );
			  	
		  	break;
		default:
			break;
	}
};

function onYouTubePlayerReady( pid )
{
	console.log( 'onYouTubePlayerReady(', pid, ');' );
	var ytplayer = document.getElementById("YouTubePlayer");
	ytplayer.addEventListener("onStateChange", "ytPlayerStateChange");
	console.log( 'Added YouTube event listener to ', ytplayer );
};

function ytPlayerStateChange( state )
{
	console.log( 'Youtube player state changed to ', state );
	
	if( state == 1 && lastLoggedPlay != currentVideo.id )
	{
		console.log( 'Updating video stats for ', currentVideo.id );
		lastLoggedPlay = currentVideo.id;
		updateVideoStats( currentVideo.id );
	}
};

function updateVideoStats( videoid )
{
	var url = "videostats.php?id=" + videoid + "&a=play";

	if( statsRPC )
		statsRPC.abort();

	statsRPC = GetXmlHttpObject( function(){} );

	if( statsRPC )
	{
		if( statsRPC != null )
			statsRPC.open("GET", url, true);
	
		if( statsRPC != null )
			statsRPC.send(null);
	}
};
