(function(l){defaults={formDataKey:"files",buttonText:"Add Files",buttonClass:"file-preview-button",shadowClass:"file-preview-shadow",tableCss:"file-preview-table",tableRowClass:"file-preview-row",placeholderClass:"file-preview-placeholder",loadingCss:"file-preview-loading",previewTableContainer:"",ajaxDataType:"json",previewTableShow:true,tableTemplate:function(){return"<table class='table table-striped file-preview-table' id='file-preview-table'>"+"<tbody></tbody>"+"</table>"},rowTemplate:function(e){return"<tr class='"+config.tableRowClass+"'>"+"<td class='filethumb'>"+"<img src='"+e.src+"' class='"+e.placeholderCssClass+"' />"+"</td>"+"<td class='filename'>"+e.name+"</td>"+"<td class='filesize'>"+e.size+"</td>"+"<td class='remove-file'><button class='btn btn-danger'>&times;</button></td>"+"</tr>"},loadingTemplate:function(){return"<div id='file-preview-loading-container'>"+"<div id='"+config.loadingCss+"' class='loader-inner ball-clip-rotate-pulse no-show'>"+"<div></div>"+"<div></div>"+"</div>"+"</div>"}};var g;if(typeof App!="undefined"&&typeof App.formatBytes=="function"){g=App.formatBytes}else{if(typeof Humanize=="undefined"||typeof Humanize.filesize!="function"){l.getScript("https://cdnjs.cloudflare.com/ajax/libs/humanize-plus/1.5.0/humanize.min.js")}g=function(e){return Humanize.fileSize(e)}}var h=function(e){var t;t=function(){switch(true){case/video/.test(e):return"video";case/audio/.test(e):return"audio";case/pdf/.test(e):return"pdf";case/csv|excel/.test(e):return"spreadsheet";case/powerpoint/.test(e):return"powerpoint";case/msword|text/.test(e):return"document";case/zip/.test(e):return"zip";case/rar/.test(e):return"rar";default:return"default-filetype"}}();return defaults.placeholderClass+" "+t};l.fn.uploadPreviewer=function(e,c){var s=this;var n=l(this).prop("multiple");if(!e){e={}}config=l.extend({},defaults,e);var t,p,i,u,a,d=[];if(window.File&&window.FileReader&&window.FileList&&window.Blob){this.wrap("<span class='btn btn-primary "+config.shadowClass+"'></span>");t=this.parent("."+config.shadowClass);t.prepend("<span>"+config.buttonText+"</span>");t.wrap("<span class='"+config.buttonClass+"'></span>");if(config.previewTableShow){a=config.previewTable;if(!a){a="table."+config.tableCss;if(config.previewTableContainer){l(config.previewTableContainer).html(config.tableTemplate());var r=l(config.previewTableContainer).attr("id");if(r)a="#"+r+" "+a}else{l("span."+config.buttonClass).after(config.tableTemplate())}}i=l(a);i.addClass(config.tableCss);u=i.find("tbody");p=config.previewRowTemplate||config.rowTemplate;i.after(config.loadingTemplate());i.on("click",".remove-file",function(){var e=l(this).parent("tr");var t=e.find(".filename").text();s.removeFile(t);e.remove()});this.on("change",function(e){var o=l("#"+config.loadingCss);o.show();var t;var f=e.currentTarget.files.length;if(!n&&f>0){d=[];u.empty()}l.each(e.currentTarget.files,function(r,l){d.push(l);t=new FileReader;t.onload=function(e){var t,i,n,a,s;if(u){i=l.type;if(/image/.test(i)){s=e.target.result;a=config.placeholderClass+" image"}else{s="";a=h(i)}t=g(l.size);n=p({src:s,name:l.name,placeholderCssClass:a,size:t});u.append(n);if(r==f-1){o.hide()}}if(c){c(e)}};t.readAsDataURL(l)});l(s).trigger("file-preview:changed",d)})}else{this.on("change",function(e){var t=l("#"+config.loadingCss);t.show();var i=e.currentTarget.files.length;if(!n&&i>0){d=[]}l.each(e.currentTarget.files,function(e,t){d.push(t)});t.hide();l(s).trigger("file-preview:changed",d)})}this.fileList=function(){return d};this.removeFile=function(e){for(var t=0;t<d.length;t++){if(d[t].name==e){d.splice(t,1);break}}l(s).trigger("file-preview:removed",e)};this.clearFileList=function(){d=[];if(u&&u.length>0){u.empty()}};this.url=function(e){if(e!=undefined){config.url=e}else{return config.url}};this._onComplete=function(e){l(s).trigger("file-preview:submit:complete",e)};this.submit=function(n,a){if(config.url==undefined)throw"Please set the URL to which I shall post the files";if(d.length>0){var t=new FormData;d.forEach(function(e){t.append(config.formDataKey+"[]",e)});l.ajax({type:"POST",url:config.url,dataType:config.ajaxDataType,data:t,contentType:false,processData:false,xhr:function(){var e=new window.XMLHttpRequest;e.upload.addEventListener("progress",function(e){if(e.lengthComputable&&config.uploadProgress!=null&&typeof config.uploadProgress=="function"){config.uploadProgress(e.loaded/e.total)}},false);return e},success:function(e,t,i){if(typeof n=="function"){n(e,t,i)}if(!u||u.length<1){s.clearFileList()}s._onComplete({data:e,status:t,jqXHR:i})},error:function(e,t,i){if(typeof a=="function"){a(e,t,i)}if(!u||u.length<1){s.clearFileList()}s._onComplete({error:i,status:t,jqXHR:e})}})}else{console.log("There are no selected files, please select at least one file before submitting.");s._onComplete({status:"no-files"})}};return this}else{throw"The File APIs are not fully supported in this browser."}}})(jQuery);