Ext.namespace("web.base.video.player");

// see http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12540/javascript-api-reference#Function
web.base.video.player.jwplayer = Ext.extend(Ext.Container, {
	initComponent: function(){
		this.autoEl = "div";
		this.id = this.id || Ext.id();
		this.playerConfig = this.playerConfig || {};
		
		Ext.applyIf(this.playerConfig, {
			flashplayer: "/northpointcoc/videoplayer/jw_player.swf", 
			height: 144, 
			//modes: [{type: "html5"}, {type: "flash", src: "/northpointcoc/videoplayer/jw_player.swf" }, {type: "download"}],			
			width: 176 
		});
		/*
{ 
			flashplayer: "jw_player.swf", 
			height: 270, 
			width: 480, 
			image: "/thumbs/video.jpg", 
			levels: [ 
				{ bitrate: 300, file: "videos/video_300k.mp4", width: 320 }, 
				{ bitrate: 600, file: "videos/video_600k.mp4", width: 480 }, 
				{ bitrate: 900, file: "videos/video_900k.mp4", width: 720 } 
			], 
			provider: "rtmp", 
			streamer: "rtmp://rtmp.example.com/application/" }
		*/
		web.base.video.player.jwplayer.superclass.initComponent.call(this);
		this.on("afterrender", this.initializePlayer, this);		
	},
	initializePlayer: function(){
		jwplayer(this.ownerId).setup(this.playerConfig);
		this.isSetup = true;
	},
	/**
	 * An array of playlist items, or a playlist item object:
	 *	[
	 *	 { duration: 32, file: '/uploads/video.mp4', image: '/uploads/video.jpg' },
	 *	 { title: 'cool video', file: '/uploads/bbb.mp4' },
	 *	 { duration: 542, file: '/uploads/ed.mp4', start: 129 }
	 *	]
	 */	
	loadPlaylist: function(playlist){
		// if(!this.isSetup){
		this.initializePlayer();
		// }
		jwplayer(this.ownerId).load(playlist);
	},
	pauseVideo: function(){
		jwplayer(this.ownerId).play(false);
	},
	/**
	 * Calls the jwplayer's play method.
	 * [file] - optional video file parameter.  If passed
	 *   in the file is first loaded, then play is called.
	 */
	playVideo: function(){
		if(arguments.length > 0){
			this.playerConfig.file = arguments[0];
			this.initializePlayer();
			//this.loadPlaylist(arguments[0]);
		}
		
		jwplayer(this.ownerId).play(true);
	},
	/**
	 * Cleanup/destroy code.  Only call this when you are ready to remove
	 * this player from the DOM.
	 */
	removePlayerFromDom: function(){
		jwplayer(this.ownerId).remove();
	},
	stopVideo: function(){
		jwplayer(this.ownerId).stop();
	}
});

Ext.reg("video.jwplayer", web.base.video.player.jwplayer);
