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

	constructor:->
	 	super
	 	@deflt={volume:100,onpause:@status,onstop:@status,onresume:@status,onfinish:@status,onplay:@status,fromController:'browser-audio'}

	status  : =>
		return unless @sound
		@el.toggleClass 'playing',!@sound.paused and @sound.playState is 1

	togglePlay:->
		@el.toggleClass 'loading', !@sound
		withurl = (url)=>
			@item.url = url
			@sound = soundManager.createSound($.extend {url}, @deflt) or null
			@togglePlay()
		if @sound
			unless @el.is('.playing')
				sound.stop().setPosition(0) for id, sound of soundManager.sounds when sound.instanceOptions.fromController is 'browser-audio' and sound.id isnt @sound.id
			@sound.togglePause()
			# if @
		else
			@$trigger 'getURL', withurl
			
	release:->
		try 
			@sound.destruct()
		@sound = null
		super
	
	refresh: =>
		@sound?.pause()

module.exports = AudioController