var myPlayer =
{
	isInit: false,
	file: false,
	playerWidth: 360,
	playerHeight: 203,
	init: function()
	{
		this.container.init();
		jwplayer( 'videoPlaceHolder' ).setup
		({
			controlbar: 'over',
			file: this.file,
			flashplayer: 'wordpress/wp-content/themes/rivet/swf/player.swf',
			width: this.playerWidth,
			height: this.playerHeight,
			wmode: 'transparent'
		});
		this.isInit = true;
	},
	container:
	{
		$obj: false,
		isVisible: false,
		init: function()
		{
			$closeLink = $( '<a />' ).text( 'Close' ).click( function(){ myPlayer.stop() });
			this.$obj =
				$( '<div />' ).attr({ id: 'videoContainer' })
					.append( $( '<div />' ).attr({ id: 'videoPlaceHolder' }) )
					.append( $closeLink )
					.css({ height: 0 }).insertAfter( '#latestNews h2' );
		},
		show: function()
		{
			if( this.isVisible ) return;
			this.$obj.stop().animate({ height: myPlayer.playerHeight });
			this.isVisible = true;
		},
		hide: function()
		{
			if( !this.isVisible ) return;
			this.$obj.stop().animate({ height: 0 });
			this.isVisible = false;
		}
	},
	play: function( file )
	{
		fileChanged = false;
		
		if( !this.isInit || this.file != file )
		{
			fileChanged = true;
			this.file = file;
		}
		
		if( !this.isInit ) this.init();

		if( fileChanged )
		{
			jwplayer().play( false );
			jwplayer().load( this.file );
		}
		
		this.container.show();
		
		if( jwplayer().getState() != "PLAYING" );
			jwplayer().play( true );
	},
	stop: function()
	{
		jwplayer().play( false );
		this.container.hide();	
	}
}

$( document ).ready( function()
{
	$( '#latestNews .news-video a' ).click( function( e )
	{
		e.preventDefault();
		
		var file = $.ajax
		({
			data: { postID: $( this ).parents( 'li' ).attr( 'id' ).split( '-' )[ 1 ] },
			async: false
		}).responseText;
		
		if( file ) myPlayer.play( file );
		
		return false;
	});
});

