$.widget("ui.Sounds",{_create:function(){var e=this.element,n=this,i={};i[i18n("confirm_")]=function(){n.confirm()&&e.dialog("close")};i[i18n("cancel")]=function(){e.dialog("close")};e.dialog({width:500,height:290,modal:!0,buttons:i,beforeClose:function(){n.stopSound()},closeText:i18n("close_")});e.form({columns:[{label:i18n("name"),widget:"<input class = 'resource-dialog-input ui-widget-content ui-corner-all name'/>"},{label:i18n("sound"),widget:"<div class = 'sound'><div class = 'sounds'></div><button class = 'add-sound'>"+i18n("add_sound")+"</button><button class = 'delete-sound'>"+i18n("delete_sound")+"</button><button class = 'play-sound'>"+i18n("play_sound")+"</button><button class = 'stop-sound'>"+i18n("stop_sound")+"</button></div>"},{label:i18n("volume")+"(%)",widget:"<input class = 'resource-dialog-input ui-widget-content ui-corner-all volume'/>"},{label:"",widget:"<input type = 'checkbox' id = 'preload' name = 'preload'/><label for = 'preload'>"+i18n("preload")+"</label>"},{label:"",widget:"<input type = 'checkbox' id = 'is-background' name = 'is-background'/><label for = 'is-background'>"+i18n("is_background")+"</label>"}]});["add","delete","play","stop"].forEach(function(i){var t=i+"Sound";"add"===i&&(t="addSoundDialog");e.find("."+i+"-sound").button({text:!1,icons:{primary:"ui-icon-"+i+"-sound"}}).click(function(){n[t]()}).attr("disabled","add"!==i)});e.find("#preload, #is-background").customInput();e.find(".volume").spinner({min:0,stop:function(){n.volume(n.volume())}})},updateSound:function(e,n,i){this.callback=n;this.json=e;this.sounds(e.urls||[]);this.name(i||"");this.preload(!!e.preload);this.isBackground(!!e.background);this.volume(void 0===e.volume?1:e.volume)},name:function(e){if(void 0===e)return this.element.find(".name").val();this.element.find(".name").val(e);return void 0},sounds:function(e){var n=this,i=this.element.find(".sounds");if(void 0===e){e=[];i.find(".sound").each(function(n,i){var t=$(i).attr("url");e.indexOf(t)<0&&e.push(t)});return e}i.empty();e&&e.forEach&&e.forEach(function(e){n.addSound(e)})},addSoundDialog:function(){var e=this;$.utils.selectImage({fileType:"audio",multiple:!1,callback:function(n){return e.addSound(n)}})},addSound:function(e){var n=this,i=this.element.find(".sounds");if(i.find("[url = '"+e+"']").length){$.utils.alert(i18n("exist"));return!1}$("<div class = 'sound' url = '"+e+"'>"+e+"</div>").appendTo(i).click(function(){i.find(".selected").removeClass("selected");$(this).addClass("selected");n.element.find(".delete-sound").attr("disabled",!1)});this.element.find(".play-sound").attr("disabled",!1);return!0},deleteSound:function(){this.element.find(".delete-sound").attr("disabled",!0);var e=this.element.find(".sounds"),n=e.find(".selected");if(n.length){n.remove();if(!this.element.find(".sounds .sound").length){this.stopSound();this.element.find(".play-sound").attr("disabled",!0)}}else $.utils.alert(i18n("please_select_a_",i18n("sound")))},playSound:function(){this.stopSound();qyremote.deployProj($.user.userid+"/"+$.currentProj);var e=this.element,n=window.audioCodecs||[],i=!1,t=this.sounds();t.forEach(function(e,o){t[o]="/"===e[0]?e:"/"+$.user.userid+"/"+$.currentProj+"/"+e;var s=e.split(".").pop();i||(i=n[s])});if(i){this.howl=new Howl({urls:t,autoplay:!0,onplay:function(){e.find(".play-sound").addClass("playing")},onend:function(){e.find(".play-sound").removeClass("playing")},volume:this.volume()});this.element.find(".stop-sound").attr("disabled",!1)}else $.utils.alert(i18n("can_not_play_audio",Object.keys(n).join(", ")))},stopSound:function(){this.howl&&this.howl.stop();this.element.find(".play-sound").removeClass("playing");this.element.find(".stop-sound").attr("disabled",!0)},volume:function(e){if(void 0===e)return this.element.find(".volume").spinner("value")/100;e!==this.volume()&&this.element.find(".volume").spinner("value",100*e);this.howl&&this.howl.volume(e)},preload:function(e){if(void 0===e)return this.element.find("#preload").customInput("checked");this.element.find("#preload").customInput("checked",!!e);return void 0},isBackground:function(e){if(void 0===e)return this.element.find("#is-background").customInput("checked");this.element.find("#is-background").customInput("checked",!!e);return void 0},confirm:function(){var e=this,n=this.callback,i=this.name();if(!i||!$.utils.validId(i)){$.utils.alert(i18n("invalid_id",i18n("resource_name")));return!1}var t={urls:this.sounds(),preload:this.preload(),background:this.isBackground(),volume:this.volume(),durations:{}},o=0,s=t.urls;if(t.urls&&t.urls.length){s.forEach(function(d){var l=qyremote.getDuration("deployed",$.utils.getFileFullPath(d).slice(1));if(l&&l.success){t.durations[d]=l.result;o++;if(o>=s.length){var l=n&&n(t,i);l&&e.element.dialog("close")}}});return!1}$.utils.alert(i18n("please_select_a_",i18n("audio")))}});