/** Simple Ajax Uploader v1.11 Copyright 2012-2013 LPology, LLC MIT license https://github.com/LPology/Simple-Ajax-Uploader */
(function(f,h,a){var m=f.ss||{},l=/^\s+/,i=/\s+$/,b=/[xy]/g,c=/.*(\/|\\)/,e=/.*[.]/,d=/[\t\r\n]/g,g=Object.prototype.toString.call(f.HTMLElement).indexOf("Constructor")>0,j=h.createElement("input"),k;j.type="file";k=("multiple" in j&&typeof File!=="undefined"&&typeof(new XMLHttpRequest()).upload!=="undefined");m.obj2string=function(q,p){var r=[];for(var s in q){if(q.hasOwnProperty(s)){var o=p?p+"["+s+"]":s,n=q[s];r.push(typeof n==="object"?m.obj2string(n,o):encodeURIComponent(o)+"="+encodeURIComponent(n))}}return r.join("&")};m.extendObj=function(o,n){for(var p in n){if(n.hasOwnProperty(p)){o[p]=n[p]}}};m.contains=function(p,o){var n=p.length;while(n--){if(p[n]===o){return true}}return false};m.removeItem=function(p,o){var n=p.length;while(n--){if(p[n]===o){p.splice(n,1);break}}};m.addEvent=function(p,o,n){if(p.addEventListener){p.addEventListener(o,n,false)}else{p.attachEvent("on"+o,n)}return function(){m.removeEvent(p,o,n)}};m.removeEvent=function(p,o,n){if(p.removeEventListener){p.removeEventListener(o,n,false)}else{p.detachEvent("on"+o,n)}};m.newXHR=function(){if(typeof XMLHttpRequest!=="undefined"){return new f.XMLHttpRequest()}else{if(f.ActiveXObject){try{return new f.ActiveXObject("Microsoft.XMLHTTP")}catch(n){return false}}}};m.parseJSON=function(o){if(!o){return false}o=m.trim(o+"");if(f.JSON&&f.JSON.parse){try{return f.JSON.parse(o+"")}catch(n){return false}}var p=/(,)|(\[|{)|(}|])|"(?:[^"\\\r\n]|\\["\\\/bfnrt]|\\u[\da-fA-F]{4})*"\s*:?|true|false|null|-?(?!0\d)\d+(?:\.\d+|)(?:[eE][+-]?\d+|)/g,q=null,r;if(o&&!m.trim(o.replace(p,function(u,s,t,v){if(r&&s){q=0}if(q===0){return u}r=t||s;q+=!v-!t;return""}))){return(new Function("return "+o))()}return false};m.getBox=function(p){var o,n,r=0,q=0;if(p.getBoundingClientRect){o=p.getBoundingClientRect();n=h.documentElement;r=o.top+(f.pageYOffset||n.scrollTop)-(n.clientTop||0);q=o.left+(f.pageXOffset||n.scrollLeft)-(n.clientLeft||0)}else{do{q+=p.offsetLeft;r+=p.offsetTop}while((p=p.offsetParent))}return{top:Math.round(r),left:Math.round(q)}};m.addStyles=function(p,o){for(var n in o){if(o.hasOwnProperty(n)){p.style[n]=o[n]}}};m.copyLayout=function(p,o){var n=m.getBox(p);m.addStyles(o,{position:"absolute",left:n.left+"px",top:n.top+"px",width:p.offsetWidth+"px",height:p.offsetHeight+"px"})};m.getUID=function(){return"axxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(b,function(p){var o=Math.random()*16|0,n=p=="x"?o:(o&3|8);return n.toString(16)})};m.trim=function(n){return n.toString().replace(l,"").replace(i,"")};m.getFilename=function(n){return n.replace(c,"")};m.getExt=function(n){return(-1!==n.indexOf("."))?n.replace(e,""):""};m.hasClass=function(o,n){return(" "+o.className+" ").replace(d," ").indexOf(" "+n+" ")>=0};m.addClass=function(o,n){if(!n||n===""){return false}if(!m.hasClass(o,n)){o.className+=" "+n}};m.removeClass=(function(){var n={};return function(p,o){if(!n[o]){n[o]=new RegExp("(?:^|\\s)"+o+"(?!\\S)")}p.className=p.className.replace(n[o],"")}})();m.purge=function(r){var p=r.attributes,q,o,s;if(p){for(q=p.length-1;q>=0;q-=1){s=p[q].name;if(typeof r[s]==="function"){r[s]=null}}}p=r.childNodes;if(p){o=p.length;for(q=0;q<o;q+=1){m.purge(r.childNodes[q])}}};m.remove=function(n){if(n.parentNode){m.purge(n);n.parentNode.removeChild(n)}n=null};m.verifyElem=function(n){if(typeof jQuery!=="undefined"&&n instanceof jQuery){n=n[0]}else{if(typeof n==="string"){if(n.charAt(0)=="#"){n=n.substr(1)}n=h.getElementById(n)}}if(!n||n.nodeType!==1){return false}if(n.nodeName.toUpperCase()=="A"){n.style.cursor="pointer";m.addEvent(n,"click",function(o){if(o&&o.preventDefault){o.preventDefault()}else{if(f.event){f.event.returnValue=false}}})}return n};m.SimpleUpload=function(o){var q,n,p;this._opts={button:"",url:"",cors:false,progressUrl:false,sessionProgressUrl:false,nginxProgressUrl:false,multiple:false,maxUploads:3,queue:true,checkProgressInterval:50,keyParamName:"APC_UPLOAD_PROGRESS",sessionProgressName:"PHP_SESSION_UPLOAD_PROGRESS",nginxProgressHeader:"X-Progress-ID",corsInputName:"XHR_CORS_TARGETORIGIN",allowedExtensions:[],accept:"",maxSize:false,name:"",data:{},autoSubmit:true,multipart:false,method:"POST",responseType:"",debug:false,hoverClass:"",focusClass:"",disabledClass:"",customHeaders:{},onAbort:function(r,s){},onChange:function(r,t,s){},onSubmit:function(r,t,s){},onProgress:function(r){},onUpdateFileSize:function(r){},onComplete:function(s,r,t){},onExtError:function(r,s){},onSizeError:function(s,r){},onError:function(t,v,r,w,s,u){},startXHR:function(s,r,t){},endXHR:function(s,r,t){},startNonXHR:function(r,s){},endNonXHR:function(r,s){}};m.extendObj(this._opts,o);o=null;this._btns=[];if(this._opts.button instanceof Array){n=this._opts.button.length;for(q=0;q<n;q++){p=m.verifyElem(this._opts.button[q]);if(p!==false){this._btns.push(this.rerouteClicks(p))}else{this.log("Button with array index "+q+" is invalid")}}}else{p=m.verifyElem(this._opts.button);if(p!==false){this._btns.push(this.rerouteClicks(p))}}delete this._opts.button;if(this._btns.length<1||this._btns[0]===false){throw new Error("Invalid button. Make sure the element you're passing exists.")}if(this._opts.multiple===false){this._opts.maxUploads=1}this._queue=[];this._active=0;this._disabled=false;this._progKeys=[];this._maxFails=10;if(!k){this._sizeFlags={}}this._createInput();this.enable()};m.SimpleUpload.prototype={destroy:function(){var n=this._btns.length;while(n--){if(this._btns[n].off){this._btns[n].off()}m.removeClass(this._btns[n],this._opts.hoverClass);m.removeClass(this._btns[n],this._opts.focusClass);m.removeClass(this._btns[n],this._opts.disabledClass);this._btns[n].disabled=false}m.remove(this._input.parentNode);for(var o in this){if(this.hasOwnProperty(o)){delete this.prop}}},log:function(n){if(this._opts.debug&&f.console){console.log("[uploader] "+n)}},setData:function(n){this._opts.data=n},setOptions:function(n){m.extendObj(this._opts,n)},setProgressBar:function(n){this._progBar=m.verifyElem(n)},setPctBox:function(n){this._pctBox=m.verifyElem(n)},setFileSizeBox:function(n){this._sizeBox=m.verifyElem(n)},setProgressContainer:function(n){this._progBox=m.verifyElem(n)},setAbortBtn:function(o,n){this._abortBtn=m.verifyElem(o);this._removeAbort=false;if(n){this._removeAbort=true}},getQueueSize:function(){return this._queue.length},_cycleQueue:function(){if(this._queue.length>0&&this._opts.autoSubmit){this.submit()}},removeCurrent:function(){var n=this._queue.length;while(n--){if(this._queue[n].file===this._file){this._queue.splice(n,1);break}}delete this._file;this._cycleQueue()},clearQueue:function(){this._queue=[]},disable:function(){var n=this._btns.length,o;this._disabled=true;while(n--){o=this._btns[n].nodeName.toUpperCase();m.addClass(this._btns[n],this._opts.disabledClass);if(o=="INPUT"||o=="BUTTON"){this._btns[n].disabled=true}}if(this._input&&this._input.parentNode){this._input.parentNode.style.visibility="hidden"}},enable:function(){var n=this._btns.length;this._disabled=false;while(n--){m.removeClass(this._btns[n],this._opts.disabledClass);this._btns[n].disabled=false}},_getHost:function(o){var n=h.createElement("a");n.href=o;if(n.hostname){return n.hostname.toLowerCase()}return o},_createInput:function(){var n=this,o=h.createElement("div");this._input=h.createElement("input");this._input.type="file";this._input.name=this._opts.name;if(k&&!g&&this._opts.multiple){this._input.multiple=true}if("accept" in this._input&&this._opts.accept!==""){this._input.accept=this._opts.accept}m.addStyles(o,{display:"block",position:"absolute",overflow:"hidden",margin:0,padding:0,opacity:0,direction:"ltr",zIndex:2147483583});m.addStyles(this._input,{position:"absolute",right:0,margin:0,padding:0,fontSize:"480px",fontFamily:"sans-serif",cursor:"pointer"});if(o.style.opacity!=="0"){o.style.filter="alpha(opacity=0)"}m.addEvent(this._input,"change",function(){var r=n._overBtn,p,s,t,q;if(!n._input||n._input.value===""){return}if(!k){p=m.getFilename(n._input.value);s=m.getExt(p);if(false===n._opts.onChange.call(n,p,s,r)){return}n._queue.push({file:n._input,btn:r})}else{p=m.getFilename(n._input.files[0].name);s=m.getExt(p);if(false===n._opts.onChange.call(n,p,s,r)){return}t=n._input.files.length;if(!n._opts.multiple){t=1}for(q=0;q<t;q++){n._queue.push({file:n._input.files[q],btn:r})}}m.removeClass(n._overBtn,n._opts.hoverClass);m.removeClass(n._overBtn,n._opts.focusClass);m.remove(n._input.parentNode);delete n._input;n._createInput();if(n._opts.autoSubmit){n.submit()}});m.addEvent(this._input,"mouseover",function(){m.addClass(n._overBtn,n._opts.hoverClass)});m.addEvent(this._input,"mouseout",function(){m.removeClass(n._overBtn,n._opts.hoverClass);m.removeClass(n._overBtn,n._opts.focusClass);n._input.parentNode.style.visibility="hidden"});m.addEvent(this._input,"focus",function(){m.addClass(n._overBtn,n._opts.focusClass)});m.addEvent(this._input,"blur",function(){m.removeClass(n._overBtn,n._opts.focusClass)});o.appendChild(this._input);h.body.appendChild(o)},rerouteClicks:function(o){var n=this;o.off=m.addEvent(o,"mouseover",function(){if(n._disabled){return}if(!n._input){n._createInput()}n._overBtn=o;m.copyLayout(o,n._input.parentNode);n._input.parentNode.style.visibility="visible"});return o},_getFrame:function(){var o=m.getUID(),n;if(navigator.userAgent.indexOf("MSIE 7")>-1){n=h.createElement('<iframe src="javascript:false;" name="'+o+'">')}else{n=h.createElement("iframe");n.src="javascript:false;";n.name=o}n.style.display="none";n.id=o;h.body.appendChild(n);return n},_getForm:function(p,o){var q=h.createElement("form"),n=this._opts.url;q.method="post";q.encoding="multipart/form-data";q.enctype="multipart/form-data";q.style.display="none";if(this._opts.nginxProgressUrl){n=n+((n.indexOf("?")>-1)?"&":"?")+encodeURIComponent(this._opts.nginxProgressHeader)+"="+encodeURIComponent(o)}q.action=n;q.target=p.name;h.body.appendChild(q);return q},_getHidden:function(o,p){var n=h.createElement("input");n.type="hidden";n.name=o;n.value=p;return n},_last:function(p,o,r,q,n){if(p){p.innerHTML=""}if(o){m.remove(o)}if(r){r.innerHTML=""}if(q&&n){m.remove(q)}this._active--;p=o=r=q=n=null;if(this._disabled){this.enable()}this._cycleQueue()},_errorFinish:function(s,p,r,q,o,n,t,x,u,w,v){this.log("Upload failed: "+s+" "+p);r=m.parseJSON(r);this._opts.onError.call(this,o,q,s,p,r,v);this._last(n,t,x,u,w);s=p=r=q=o=n=t=x=u=w=v=null},_finish:function(r,p,q,o,n,s,w,t,v,u){this.log("Server response: "+q);if(this._opts.responseType.toLowerCase()=="json"){q=m.parseJSON(q);if(q===false){this._errorFinish(r,p,false,"parseerror",o,n,s,t,v,u);return}}this._opts.onComplete.call(this,o,q,u);this._last(n,s,w,t,v);r=p=q=o=n=s=w=t=v=u=null},_uploadXhr:function(D,z,o,C,v,E,n,r,t){var A=this,x=this._opts,u=m.newXHR(),F={},w,s,q;F[x.name]=D;m.extendObj(F,x.data);w=x.url+((x.url.indexOf("?")>-1)?"&":"?")+m.obj2string(F);if(o){o.innerHTML=z+"K"}if(E){E.innerHTML="0%"}if(C){C.style.width="0%"}x.onProgress.call(this,0);s=function(I,H){var G,K;try{if(s&&(H||u.readyState===4)){s=a;u.onreadystatechange=function(){};if(H){if(u.readyState!==4){u.abort()}A._last(o,v,E,n,r);x.onAbort.call(A,D,t)}else{G=u.status;try{K=u.statusText}catch(J){K=""}if(G>=200&&G<300){x.endXHR.call(A,D,z,t);A._finish(G,K,u.responseText,D,o,v,E,n,r,t)}else{A._errorFinish(G,K,u.responseText,"error",D,o,v,E,n,r,t)}}}}catch(J){if(!H){A._errorFinish(-1,J.message,false,"error",D,o,v,E,n,r,t)}}};if(n){q=function(){m.removeEvent(n,"click",q);if(s){s(a,true)}};m.addEvent(n,"click",q)}u.onreadystatechange=s;u.open(x.method.toUpperCase(),w,true);m.addEvent(u.upload,"progress",function(G){if(G.lengthComputable){var H=Math.round((G.loaded/G.total)*100);x.onProgress.call(A,H);if(E){E.innerHTML=H+"%"}if(C){C.style.width=H+"%"}}});u.setRequestHeader("X-Requested-With","XMLHttpRequest");u.setRequestHeader("X-File-Name",encodeURIComponent(D));if(x.responseType.toLowerCase()=="json"){u.setRequestHeader("Accept","application/json, text/javascript, */*; q=0.01")}for(var y in x.customHeaders){if(x.customHeaders.hasOwnProperty(y)){u.setRequestHeader(y,x.customHeaders[y])}}if(x.multipart===true){var B=new FormData();for(var p in x.data){if(x.data.hasOwnProperty(p)){B.append(p,x.data[p])}}B.append(x.name,this._file);this.log("Commencing upload using multipart form");u.send(B)}else{u.setRequestHeader("Content-Type","application/octet-stream");this.log("Commencing upload using binary stream");u.send(this._file)}this.removeCurrent()},_uploadIframe:function(q,p,x,u,B,y){var A=this,o=this._opts,z=m.getUID(),t=this._getFrame(),s=this._getForm(t,z),n=false,v,w;if(o.sessionProgressUrl){s.appendChild(this._getHidden(o.sessionProgressName,z))}else{if(o.progressUrl){s.appendChild(this._getHidden(o.keyParamName,z))}}for(var r in o.data){if(o.data.hasOwnProperty(r)){s.appendChild(this._getHidden(r,o.data[r]))}}if(o.cors){s.appendChild(this._getHidden(o.corsInputName,f.location.href))}s.appendChild(this._file);o.onProgress.call(this,0);if(B){B.innerHTML="0%"}if(x){x.style.width="0%"}if(o.cors){v=m.addEvent(f,"message",function(C){if(A._getHost(C.origin)!=A._getHost(o.url)){A.log("Non-matching origin: "+C.origin);return}n=true;v();o.endNonXHR.call(A,q,y);A._finish("","",C.data,q,p,u,B,a,a,y)})}w=m.addEvent(t,"load",function(){if(!t.parentNode){return}w();m.removeItem(A._progKeys,z);if(A._sizeFlags[z]){delete A._sizeFlags.key}if(o.cors){f.setTimeout(function(){m.remove(t);if(!n){A._errorFinish("","",false,"error",q,p,u,B,a,a,y)}o=z=t=p=u=B=y=null},800)}else{try{var E=t.contentDocument?t.contentDocument:t.contentWindow.document,C=E.body.innerHTML;o.endNonXHR.call(A,q,y);A._finish("","",C,q,p,u,B,a,a,y)}catch(D){A._errorFinish("",D.message,false,"error",q,p,u,B,a,a,y)}m.remove(t);o=z=t=p=u=B=y=null}});A.log("Commencing upload using iframe");s.submit();m.remove(s);s=null;if(o.progressUrl||o.nginxProgressUrl||o.sessionProgressUrl){this._progKeys.push(z);f.setTimeout(function(){A._getProg(z,x,p,B,1);x=p=B=null},o.checkProgressInterval)}this.removeCurrent()},_getProg:function(v,u,p,z,q){var y=this,o=this._opts,s=new Date().getTime(),x,r,w;if(!v){return}if(o.nginxProgressUrl){r=o.nginxProgressUrl+"?"+encodeURIComponent(o.nginxProgressHeader)+"="+encodeURIComponent(v)+"&_="+s}else{if(o.sessionProgressUrl){r=o.sessionProgressUrl}else{if(o.progressUrl){r=o.progressUrl+"?progresskey="+encodeURIComponent(v)+"&_="+s}}}w=function(){var B,C,D,A,F;try{if(w&&(o.cors||x.readyState===4)){w=a;x.onreadystatechange=function(){};try{F=x.statusText;A=x.status}catch(E){F="";A=""}if(o.cors||(A>=200&&A<300)){B=m.parseJSON(x.responseText);q++;if(B===false){y.log("Error parsing progress response (expecting JSON)");return}if(o.nginxProgressUrl){if(B.state=="uploading"){C=B.size;if(C>0){D=Math.round((B.received/C)*100);C=Math.round(C/1024)}}else{if(B.state=="done"){D=100}else{if(B.state=="error"){y.log("Error requesting upload progress: "+B.status);return}}}}else{if(o.sessionProgressUrl||o.progressUrl){if(B.success===true){C=B.size;D=B.pct}}}if(D){if(z){z.innerHTML=D+"%"}if(u){u.style.width=D+"%"}o.onProgress.call(y,D)}if(C&&!y._sizeFlags[v]){y._sizeFlags[v]=1;if(p){p.innerHTML=C+"K"}o.onUpdateFileSize.call(y,C)}if(!D&&!C&&q>=y._maxFails){y.log("Failed progress request limit reached");return}if(D<100&&m.contains(y._progKeys,v)){f.setTimeout(function(){y._getProg(v,u,p,z,q);v=u=p=z=q=null},o.checkProgressInterval)}}else{m.removeItem(y._progKeys,v);y.log("Error requesting upload progress: "+A+" "+F)}x=C=D=A=F=B=null}}catch(E){y.log("Error requesting upload progress: "+E.message)}};if(o.cors&&!o.sessionProgressUrl){if(typeof XDomainRequest!=="undefined"){x=new f.XDomainRequest();x.open("GET",r,true);x.onprogress=x.ontimeout=function(){};x.onload=w;x.onerror=function(){m.removeItem(y._progKeys,v);v=null;y.log("Error requesting upload progress")}}else{return}}else{var n=!o.sessionProgressUrl?"GET":"POST",t;x=m.newXHR();x.onreadystatechange=w;x.open(n,r,true);if(o.sessionProgressUrl){t=encodeURIComponent(o.sessionProgressName)+"="+encodeURIComponent(v);x.setRequestHeader("Content-type","application/x-www-form-urlencoded")}if(o.nginxProgressUrl){x.setRequestHeader(o.nginxProgressHeader,v)}x.setRequestHeader("X-Requested-With","XMLHttpRequest");x.setRequestHeader("Accept","application/json, text/javascript, */*; q=0.01");x.send((o.sessionProgressUrl&&t)||null)}},_checkFile:function(n,q,p){var s=this._opts.allowedExtensions,o=s.length,r=false;if(o>0){q=q.toLowerCase();while(o--){if(s[o].toLowerCase()==q){r=true;break}}if(!r){this.removeCurrent();this.log("File extension not permitted");this._opts.onExtError.call(this,n,q);return false}}if(p&&this._opts.maxSize!==false&&p>this._opts.maxSize){this.removeCurrent();this.log(n+" exceeds "+this._opts.maxSize+"K limit");this._opts.onSizeError.call(this,n,p);return false}return true},submit:function(){var n,p,o;if(this._disabled||this._active>=this._opts.maxUploads||this._queue.length<1){return}this._file=this._queue[0].file;if(k){n=m.getFilename(this._file.name);o=Math.round(this._file.size/1024)}else{n=m.getFilename(this._file.value)}p=m.getExt(n);if(!this._checkFile(n,p,o)){return}if(false===this._opts.onSubmit.call(this,n,p,this._queue[0].btn)){return}this._active++;if(this._opts.multiple===false||this._opts.queue===false&&this._active>=this._opts.maxUploads){this.disable()}if(k){if(false===this._opts.startXHR.call(this,n,o,this._queue[0].btn)){if(this._disabled){this.enable()}this._active--;return}this._uploadXhr(n,o,this._sizeBox,this._progBar,this._progBox,this._pctBox,this._abortBtn,this._removeAbort,this._queue[0].btn)}else{if(false===this._opts.startNonXHR.call(this,n,this._queue[0].btn)){if(this._disabled){this.enable()}this._active--;return}this._uploadIframe(n,this._sizeBox,this._progBar,this._progBox,this._pctBox,this._queue[0].btn)}this._sizeBox=this._progBar=this._progBox=this._pctBox=this._abortBtn=this._removeAbort=null}};f.ss=m})(window,document);