FileController = require './file'
class VideoController extends FileController
	className:'browser-video'
	events:
		'click .browser-entry-info':'togglePlay'

	added:->
		@delay =>
			@preview.append el = $ "<video controls='false' id='browser-video_#{@el.index()}'>"
			(@player = videojs el[0], preload: true).addClass('browser-video-player').on('play',@status).on('pause',@status).on('ended',@status)
			@player.on 'firstplay', @firstplay
		super

	status: (e)=> @el.toggleClass 'playing', e.type is 'play'

	togglePlay: =>
		@el.addClass 'loading' if !@played

		withurl = (url)=>
			@item.url = url
			@player.src url
			@togglePlay()
		if @item.url
			unless @hasSrc
				@player.src @item.url if @player.paused()
				@hasSrc = true
			@player[if @player.paused() then 'play' else 'pause']()
		else
			@$trigger 'getURL', withurl

	release:->
		try @player.dispose()
		super

	firstplay: =>
		@played = @el.addClass('played').removeClass 'loading'

	refresh: =>
		@player?.pause()

module.exports = VideoController