window.jQuery&&function(b){b.fn.MultiFile=function(c){if(0==this.length)return this;if("string"==typeof arguments[0]){if(1<this.length){var g=arguments;return this.each(function(){b.fn.MultiFile.apply(b(this),g)})}b.fn.MultiFile[arguments[0]].apply(this,b.makeArray(arguments).slice(1)||[]);return this}c=b.extend({},b.fn.MultiFile.options,c||{});b("form").not("MultiFile-intercepted").addClass("MultiFile-intercepted").submit(b.fn.MultiFile.disableEmpty);b.fn.MultiFile.options.autoIntercept&&(b.fn.MultiFile.intercept(b.fn.MultiFile.options.autoIntercept),
b.fn.MultiFile.options.autoIntercept=null);this.not(".MultiFile-applied").addClass("MultiFile-applied").each(function(){window.MultiFile=(window.MultiFile||0)+1;var e=window.MultiFile,a={e:this,E:b(this),clone:b(this).clone()};"number"==typeof c&&(c={max:c});var d=b.extend({},b.fn.MultiFile.options,c||{},(b.metadata?a.E.metadata():b.meta?a.E.data():null)||{},{});0<d.max||(d.max=a.E.attr("maxlength"),0<d.max||(d.max=(String(a.e.className.match(/\b(max|limit)\-([0-9]+)\b/gi)||[""]).match(/[0-9]+/gi)||
[""])[0],d.max=0<d.max?String(d.max).match(/[0-9]+/gi)[0]:-1));d.max=new Number(d.max);d.accept=d.accept||a.E.attr("accept")||"";d.accept||(d.accept=a.e.className.match(/\b(accept\-[\w\|]+)\b/gi)||"",d.accept=(new String(d.accept)).replace(/^(accept|ext)\-/i,""));b.extend(a,d||{});a.STRING=b.extend({},b.fn.MultiFile.options.STRING,a.STRING);b.extend(a,{n:0,slaves:[],files:[],instanceKey:a.e.id||"MultiFile"+String(e),generateID:function(b){return a.instanceKey+(0<b?"_F"+String(b):"")},trigger:function(c,
d){var e=a[c],f=b(d).attr("value");return e&&(e=e(d,f,a),null!=e)?e:!0}});1<String(a.accept).length&&(a.accept=a.accept.replace(/\W+/g,"|").replace(/^\W|\W$/g,""),a.rxAccept=RegExp("\\.("+(a.accept?a.accept:"")+")$","gi"));a.wrapID=a.instanceKey+"_wrap";a.E.wrap('<div class="MultiFile-wrap" id="'+a.wrapID+'"></div>');a.wrapper=b("#"+a.wrapID+"");a.e.name=a.e.name||"file"+e+"[]";a.list||(a.wrapper.append('<div class="MultiFile-list" id="'+a.wrapID+'_list"></div>'),a.list=b("#"+a.wrapID+"_list"));a.list=
b(a.list);a.addSlave=function(c,d){a.n++;c.MultiFile=a;0<d&&(c.id=c.name="");0<d&&(c.id=a.generateID(d));c.name=String(a.namePattern.replace(/\$name/gi,b(a.clone).attr("name")).replace(/\$id/gi,b(a.clone).attr("id")).replace(/\$g/gi,e).replace(/\$i/gi,d));0<a.max&&a.n-1>a.max&&(c.disabled=!0);a.current=a.slaves[d]=c;c=b(c);c.val("").attr("value","")[0].value="";c.addClass("MultiFile-applied");c.change(function(){b(this).blur();if(!a.trigger("onFileSelect",this,a))return!1;var e="",f=String(this.value||
"");a.accept&&f&&!f.match(a.rxAccept)&&(e=a.STRING.denied.replace("$ext",String(f.match(/\.\w{1,4}$/gi))));for(var g in a.slaves)a.slaves[g]&&a.slaves[g]!=this&&a.slaves[g].value==f&&(e=a.STRING.duplicate.replace("$file",f.match(/[^\/\\]+$/gi)));f=b(a.clone).clone();f.addClass("MultiFile");if(""!=e)return a.error(e),a.n--,a.addSlave(f[0],d),c.parent().prepend(f),c.remove(),!1;b(this).css({position:"absolute",top:"-3000px"});c.after(f);a.addToList(this,d);a.addSlave(f[0],d+1);if(!a.trigger("afterFileSelect",
this,a))return!1});b(c).data("MultiFile",a)};a.addToList=function(c,d){if(!a.trigger("onFileAppend",c,a))return!1;var e=b('<div class="MultiFile-label"></div>'),f=String(c.value||""),f=b('<span class="MultiFile-title" title="'+a.STRING.selected.replace("$file",f)+'">'+a.STRING.file.replace("$file",f.match(/[^\/\\]+$/gi)[0])+"</span>"),g=b('<a class="MultiFile-remove" href="#'+a.wrapID+'">'+a.STRING.remove+"</a>");a.list.append(e.append(g," ",f));g.click(function(){if(!a.trigger("onFileRemove",c,a))return!1;
a.n--;a.current.disabled=!1;a.slaves[d]=null;b(c).remove();b(this).parent().remove();b(a.current).css({position:"",top:""});b(a.current).reset().val("").attr("value","")[0].value="";a.trigger("afterFileRemove",c,a);return!1});if(!a.trigger("afterFileAppend",c,a))return!1};a.MultiFile||a.addSlave(a.e,0);a.n++;a.E.data("MultiFile",a)})};b.extend(b.fn.MultiFile,{reset:function(){var c=b(this).data("MultiFile");c&&c.list.find("a.MultiFile-remove").click();return b(this)},disableEmpty:function(c){c=("string"==
typeof c?c:"")||"mfD";var g=[];b("input:file.MultiFile").each(function(){""==b(this).val()&&(g[g.length]=this)});return b(g).each(function(){this.disabled=!0}).addClass(c)},reEnableEmpty:function(c){c=("string"==typeof c?c:"")||"mfD";return b("input:file."+c).removeClass(c).each(function(){this.disabled=!1})},intercepted:{},intercept:function(c,g,e){var a;e=e||[];0>e.constructor.toString().indexOf("Array")&&(e=[e]);if("function"==typeof c)return b.fn.MultiFile.disableEmpty(),a=c.apply(g||window,e),
setTimeout(function(){b.fn.MultiFile.reEnableEmpty()},1E3),a;0>c.constructor.toString().indexOf("Array")&&(c=[c]);for(e=0;e<c.length;e++)(g=c[e]+"")&&function(c){b.fn.MultiFile.intercepted[c]=b.fn[c]||function(){};b.fn[c]=function(){b.fn.MultiFile.disableEmpty();a=b.fn.MultiFile.intercepted[c].apply(this,arguments);setTimeout(function(){b.fn.MultiFile.reEnableEmpty()},1E3);return a}}(g)}});b.fn.MultiFile.options={accept:"",max:-1,namePattern:"$name",STRING:{remove:"x",denied:"You cannot select a $ext file.\nTry again...",
file:"$file",selected:"File selected: $file",duplicate:"This file has already been selected:\n$file"},autoIntercept:["submit","ajaxSubmit","ajaxForm","validate","valid"],error:function(b){alert(b)}};b.fn.reset=function(){return this.each(function(){try{this.reset()}catch(b){}})};b(function(){b("input[type=file].multi").MultiFile()})}(jQuery);
