(function(e){"use strict";if(typeof define==="function"&&define.amd){define(["jquery","tmpl","./jquery.fileupload-image.min","./jquery.fileupload-audio.min","./jquery.fileupload-video.min","./jquery.fileupload-validate.min"],e)}else{e(window.jQuery,window.tmpl)}})(function(l,t){"use strict";l.blueimp.fileupload.prototype._specialOptions.push("filesContainer","uploadTemplateId","downloadTemplateId");l.widget("blueimp.fileupload",l.blueimp.fileupload,{options:{autoUpload:false,uploadTemplateId:"template-upload",downloadTemplateId:"template-download",filesContainer:undefined,prependFiles:false,dataType:"json",getNumberOfFiles:function(){return this.filesContainer.children().not(".processing").length},getFilesFromResponse:function(e){if(e.result&&l.isArray(e.result.files)){return e.result.files}return[]},add:function(e,i){if(e.isDefaultPrevented()){return false}var t=l(this),n=t.data("blueimp-fileupload")||t.data("fileupload"),r=n.options;i.context=n._renderUpload(i.files).data("data",i).addClass("processing");r.filesContainer[r.prependFiles?"prepend":"append"](i.context);n._forceReflow(i.context);l.when(n._transition(i.context),i.process(function(){return t.fileupload("process",i)})).always(function(){i.context.each(function(e){l(this).find(".size").text(n._formatFileSize(i.files[e].size))}).removeClass("processing");n._renderPreviews(i)}).done(function(){i.context.find(".start").prop("disabled",false);if(n._trigger("added",e,i)!==false&&(r.autoUpload||i.autoUpload)&&i.autoUpload!==false){i.submit()}}).fail(function(){if(i.files.error){i.context.each(function(e){var t=i.files[e].error;if(t){l(this).find(".error").text(t)}})}})},send:function(e,t){if(e.isDefaultPrevented()){return false}var i=l(this).data("blueimp-fileupload")||l(this).data("fileupload");if(t.context&&t.dataType&&t.dataType.substr(0,6)==="iframe"){t.context.find(".progress").addClass(!l.support.transition&&"progress-animated").attr("aria-valuenow",100).children().first().css("width","100%")}return i._trigger("sent",e,t)},done:function(i,n){if(i.isDefaultPrevented()){return false}var r=l(this).data("blueimp-fileupload")||l(this).data("fileupload"),e=n.getFilesFromResponse||r.options.getFilesFromResponse,o=e(n),a,s;if(n.context){n.context.each(function(e){var t=o[e]||{error:"Empty file upload result"};s=r._addFinishedDeferreds();r._transition(l(this)).done(function(){var e=l(this);a=r._renderDownload([t]).replaceAll(e);r._forceReflow(a);r._transition(a).done(function(){n.context=l(this);r._trigger("completed",i,n);r._trigger("finished",i,n);s.resolve()})})})}else{a=r._renderDownload(o)[r.options.prependFiles?"prependTo":"appendTo"](r.options.filesContainer);r._forceReflow(a);s=r._addFinishedDeferreds();r._transition(a).done(function(){n.context=l(this);r._trigger("completed",i,n);r._trigger("finished",i,n);s.resolve()})}},fail:function(i,n){if(i.isDefaultPrevented()){return false}var r=l(this).data("blueimp-fileupload")||l(this).data("fileupload"),o,a;if(n.context){n.context.each(function(e){if(n.errorThrown!=="abort"){var t=n.files[e];t.error=t.error||n.errorThrown||true;a=r._addFinishedDeferreds();r._transition(l(this)).done(function(){var e=l(this);o=r._renderDownload([t]).replaceAll(e);r._forceReflow(o);r._transition(o).done(function(){n.context=l(this);r._trigger("failed",i,n);r._trigger("finished",i,n);a.resolve()})})}else{a=r._addFinishedDeferreds();r._transition(l(this)).done(function(){l(this).remove();r._trigger("failed",i,n);r._trigger("finished",i,n);a.resolve()})}})}else if(n.errorThrown!=="abort"){n.context=r._renderUpload(n.files)[r.options.prependFiles?"prependTo":"appendTo"](r.options.filesContainer).data("data",n);r._forceReflow(n.context);a=r._addFinishedDeferreds();r._transition(n.context).done(function(){n.context=l(this);r._trigger("failed",i,n);r._trigger("finished",i,n);a.resolve()})}else{r._trigger("failed",i,n);r._trigger("finished",i,n);r._addFinishedDeferreds().resolve()}},progress:function(e,t){if(e.isDefaultPrevented()){return false}var i=Math.floor(t.loaded/t.total*100);if(t.context){t.context.each(function(){l(this).find(".progress").attr("aria-valuenow",i).children().first().css("width",i+"%")})}},progressall:function(e,t){if(e.isDefaultPrevented()){return false}var i=l(this),n=Math.floor(t.loaded/t.total*100),r=i.find(".fileupload-progress"),o=r.find(".progress-extended");if(o.length){o.html((i.data("blueimp-fileupload")||i.data("fileupload"))._renderExtendedProgress(t))}r.find(".progress").attr("aria-valuenow",n).children().first().css("width",n+"%")},start:function(e){if(e.isDefaultPrevented()){return false}var t=l(this).data("blueimp-fileupload")||l(this).data("fileupload");t._resetFinishedDeferreds();t._transition(l(this).find(".fileupload-progress")).done(function(){t._trigger("started",e)})},stop:function(e){if(e.isDefaultPrevented()){return false}var t=l(this).data("blueimp-fileupload")||l(this).data("fileupload"),i=t._addFinishedDeferreds();l.when.apply(l,t._getFinishedDeferreds()).done(function(){t._trigger("stopped",e)});t._transition(l(this).find(".fileupload-progress")).done(function(){l(this).find(".progress").attr("aria-valuenow","0").children().first().css("width","0%");l(this).find(".progress-extended").html("&nbsp;");i.resolve()})},processstart:function(e){if(e.isDefaultPrevented()){return false}l(this).addClass("fileupload-processing")},processstop:function(e){if(e.isDefaultPrevented()){return false}l(this).removeClass("fileupload-processing")},destroy:function(e,t){if(e.isDefaultPrevented()){return false}var i=l(this).data("blueimp-fileupload")||l(this).data("fileupload"),n=function(){i._transition(t.context).done(function(){l(this).remove();i._trigger("destroyed",e,t)})};if(t.url){t.dataType=t.dataType||i.options.dataType;l.ajax(t).done(n).fail(function(){i._trigger("destroyfailed",e,t)})}else{n()}}},_resetFinishedDeferreds:function(){this._finishedUploads=[]},_addFinishedDeferreds:function(e){if(!e){e=l.Deferred()}this._finishedUploads.push(e);return e},_getFinishedDeferreds:function(){return this._finishedUploads},_enableDragToDesktop:function(){var e=l(this),t=e.prop("href"),i=e.prop("download"),n="application/octet-stream";e.bind("dragstart",function(e){try{e.originalEvent.dataTransfer.setData("DownloadURL",[n,i,t].join(":"))}catch(e){}})},_formatFileSize:function(e){if(typeof e!=="number"){return""}if(e>=1e9){return(e/1e9).toFixed(2)+" GB"}if(e>=1e6){return(e/1e6).toFixed(2)+" MB"}return(e/1e3).toFixed(2)+" KB"},_formatBitrate:function(e){if(typeof e!=="number"){return""}if(e>=1e9){return(e/1e9).toFixed(2)+" Gbit/s"}if(e>=1e6){return(e/1e6).toFixed(2)+" Mbit/s"}if(e>=1e3){return(e/1e3).toFixed(2)+" kbit/s"}return e.toFixed(2)+" bit/s"},_formatTime:function(e){var t=new Date(e*1e3),i=Math.floor(e/86400);i=i?i+"d ":"";return i+("0"+t.getUTCHours()).slice(-2)+":"+("0"+t.getUTCMinutes()).slice(-2)+":"+("0"+t.getUTCSeconds()).slice(-2)},_formatPercentage:function(e){return(e*100).toFixed(2)+" %"},_renderExtendedProgress:function(e){return this._formatBitrate(e.bitrate)+" | "+this._formatTime((e.total-e.loaded)*8/e.bitrate)+" | "+this._formatPercentage(e.loaded/e.total)+" | "+this._formatFileSize(e.loaded)+" / "+this._formatFileSize(e.total)},_renderTemplate:function(e,t){if(!e){return l()}var i=e({files:t,formatFileSize:this._formatFileSize,options:this.options});if(i instanceof l){return i}return l(this.options.templatesContainer).html(i).children()},_renderPreviews:function(i){i.context.find(".preview").each(function(e,t){l(t).append(i.files[e].preview)})},_renderUpload:function(e){return this._renderTemplate(this.options.uploadTemplate,e)},_renderDownload:function(e){return this._renderTemplate(this.options.downloadTemplate,e).find("a[download]").each(this._enableDragToDesktop).end()},_startHandler:function(e){e.preventDefault();var t=l(e.currentTarget),i=t.closest(".template-upload"),n=i.data("data");t.prop("disabled",true);if(n&&n.submit){n.submit()}},_cancelHandler:function(e){e.preventDefault();var t=l(e.currentTarget).closest(".template-upload,.template-download"),i=t.data("data")||{};i.context=i.context||t;if(i.abort){i.abort()}else{i.errorThrown="abort";this._trigger("fail",e,i)}},_deleteHandler:function(e){e.preventDefault();var t=l(e.currentTarget);this._trigger("destroy",e,l.extend({context:t.closest(".template-download"),type:"DELETE"},t.data()))},_forceReflow:function(e){return l.support.transition&&e.length&&e[0].offsetWidth},_transition:function(t){var i=l.Deferred();if(l.support.transition&&t.hasClass("fade")&&t.is(":visible")){t.bind(l.support.transition.end,function(e){if(e.target===t[0]){t.unbind(l.support.transition.end);i.resolveWith(t)}}).toggleClass("in")}else{t.toggleClass("in");i.resolveWith(t)}return i},_initButtonBarEventHandlers:function(){var t=this.element.find(".fileupload-buttonbar"),i=this.options.filesContainer;this._on(t.find(".start"),{click:function(e){e.preventDefault();i.find(".start").click()}});this._on(t.find(".cancel"),{click:function(e){e.preventDefault();i.find(".cancel").click()}});this._on(t.find(".delete"),{click:function(e){e.preventDefault();i.find(".toggle:checked").closest(".template-download").find(".delete").click();t.find(".toggle").prop("checked",false)}});this._on(t.find(".toggle"),{change:function(e){i.find(".toggle").prop("checked",l(e.currentTarget).is(":checked"))}})},_destroyButtonBarEventHandlers:function(){this._off(this.element.find(".fileupload-buttonbar").find(".start, .cancel, .delete"),"click");this._off(this.element.find(".fileupload-buttonbar .toggle"),"change.")},_initEventHandlers:function(){this._super();this._on(this.options.filesContainer,{"click .start":this._startHandler,"click .cancel":this._cancelHandler,"click .delete":this._deleteHandler});this._initButtonBarEventHandlers()},_destroyEventHandlers:function(){this._destroyButtonBarEventHandlers();this._off(this.options.filesContainer,"click");this._super()},_enableFileInputButton:function(){this.element.find(".fileinput-button input").prop("disabled",false).parent().removeClass("disabled")},_disableFileInputButton:function(){this.element.find(".fileinput-button input").prop("disabled",true).parent().addClass("disabled")},_initTemplates:function(){var e=this.options;e.templatesContainer=this.document[0].createElement(e.filesContainer.prop("nodeName"));if(t){if(e.uploadTemplateId){e.uploadTemplate=t(e.uploadTemplateId)}if(e.downloadTemplateId){e.downloadTemplate=t(e.downloadTemplateId)}}},_initFilesContainer:function(){var e=this.options;if(e.filesContainer===undefined){e.filesContainer=this.element.find(".files")}else if(!(e.filesContainer instanceof l)){e.filesContainer=l(e.filesContainer)}},_initSpecialOptions:function(){this._super();this._initFilesContainer();this._initTemplates()},_create:function(){this._super();this._resetFinishedDeferreds();if(!l.support.fileInput){this._disableFileInputButton()}},enable:function(){var e=false;if(this.options.disabled){e=true}this._super();if(e){this.element.find("input, button").prop("disabled",false);this._enableFileInputButton()}},disable:function(){if(!this.options.disabled){this.element.find("input, button").prop("disabled",true);this._disableFileInputButton()}this._super()}})});