define(["dialogHelper","connectionManager","dom","loading","layoutManager","globalize","require","emby-button","emby-select","formDialogStyle","css!./style","emby-scroller"],function(dialogHelper,connectionManager,dom,loading,layoutManager,globalize,require){"use strict";var currentItem,currentFile,hasChanges=!1;function onFileReaderError(evt){switch(loading.hide(),evt.target.error.code){case evt.target.error.NOT_FOUND_ERR:require(["toast"],function(toast){toast(globalize.translate("MessageFileReadError"))});break;case evt.target.error.ABORT_ERR:break;default:require(["toast"],function(toast){toast(globalize.translate("MessageFileReadError"))})}}function onSubmit(e){var file=currentFile;if(!file)return!1;if("image/png"!==file.type&&"image/jpeg"!==file.type&&"image/jpeg"!==file.type)return!1;loading.show();var dlg=this.closest(".dialog"),imageType=dlg.querySelector("#selectImageType").value;return connectionManager.getApiClient(currentItem).uploadItemImage(currentItem.Id,imageType,file).then(function(){dlg.querySelector("#uploadImage").value="",loading.hide(),hasChanges=!0,dialogHelper.close(dlg)}),e.preventDefault(),!1}function initEditor(page){page.querySelector("form").addEventListener("submit",onSubmit),page.querySelector("#uploadImage").addEventListener("change",function(){!function(page,files){var file=files[0];if(!file||!file.type.match("image.*"))return page.querySelector("#imageOutput").innerHTML="",page.querySelector("#fldUpload").classList.add("hide"),currentFile=null;currentFile=file;var theFile,reader=new FileReader;reader.onerror=onFileReaderError,reader.onloadstart=function(){page.querySelector("#fldUpload").classList.add("hide")},reader.onabort=function(){loading.hide(),console.log("File read cancelled")},reader.onload=(theFile=file,function(e){var html=['<img style="max-width:100%;max-height:100%;" src="',e.target.result,'" title="',escape(theFile.name),'"/>'].join("");page.querySelector("#imageOutput").innerHTML=html,page.querySelector("#fldUpload").classList.remove("hide")}),reader.readAsDataURL(file)}(page,this.files)}),page.querySelector(".btnBrowse").addEventListener("click",function(){page.querySelector("#uploadImage").click()})}function removeImageType(dlg,type){var opt=dlg.querySelector('option[value="'+type+'"]');opt&&opt.parentNode.removeChild(opt)}return{show:function(options){return new Promise(function(resolve,reject){hasChanges=!1,function(options,resolve){options=options||{},require(["text!./imageuploader.template.html"],function(template){currentItem=options.item;var dialogOptions={removeOnClose:!0};layoutManager.tv?dialogOptions.size="fullscreen":dialogOptions.size="fullscreen-border";var dlg=dialogHelper.createDialog(dialogOptions);dlg.classList.add("formDialog"),dlg.innerHTML=globalize.translateDocument(template,"sharedcomponents"),"TvChannel"!==currentItem.Type&&(removeImageType(dlg,"LogoLight"),removeImageType(dlg,"LogoLightColor")),dlg.addEventListener("close",function(){loading.hide(),resolve(hasChanges)}),options.imageType||dlg.querySelector(".fldSelectImageType").classList.remove("hide"),dialogHelper.open(dlg),initEditor(dlg),dlg.querySelector("#selectImageType").value=options.imageType||"Primary",dlg.querySelector(".btnCancel").addEventListener("click",function(){dialogHelper.close(dlg)})})}(options,resolve)})}}});