String.implement({shorten:function(a,b){a=a||20;b=b||12;if(this.length>a){return this.substring(0,a-b-3)+"... "+this.substring(this.length-b+1)}return this}});(function(e){function b(k){var l=this.options.filetype.split(/[^a-z0-9]/i);if(!this.aborted&&l.length>0){this.aborted=!new RegExp("(."+l.join(")$|(.")+"$)","i").test(k.file)}if(this.aborted){this.message="unauthorized file type"}}var h="umo",d="upl:tr",g=this,f=!!new Element("input",{type:"file"}).files,c=this.uploadManager={xmlhttpupload:f,uploads:{},actives:{},events:{},queue:{},attachDragEvents:function(l,k){if(document.id(l)){if($type(l)!=="element"){l=e(l)}if(!l.retrieve(h)){new Element("div",{style:"display:none",text:"Drop files here"}).inject(e(l).addEvents(i).store(h,k),"top")}}return this},detachDragEvents:function(k){k=e(k);if(k.retrieve(h)){e(k).removeEvents(i).eliminate(h).getFirst().destroy()}return this},addEvent:function(k,m,l){this.events[k].addEvent(m,l);return this},fireEvent:function(k,n,m,l){this.events[k].fireEvent(n,m,l);return this},upload:function(n,k){var o=$merge({limit:0},n),m=o.container,l;if($chk(k)&&k===true){this.uploads[m]=null}if(!this.uploads[m]){this.events[m]=new Events();this.actives[m]=[];this.uploads[m]=[]}if(o.limit>0&&this.uploads[m].length>=o.limit){return null}if(o.onAllComplete){this.addEvent(o.container,"onAllComplete",o.onAllComplete);delete o.onAllComplete}o.base=o.base||"/scripts/php/upload.php";o.id=o.name.replace(/[^a-z0-9]/gi,"")+new Date().getTime();l=f?new a(o):new j(o);this.uploads[m].push(l);return l.fireEvent("create",o.id)},enqueue:function(k,l){this.queue[k]=this.queue[k]||[];this.queue[k].push(l);return this.load(k)},load:function(k){var l=this.queue[k].shift();if(l){l()}return this},get:function(k){return e(k).retrieve(d)},getSize:function(k,m){var l=0;(this.uploads[k]||[]).each(function(n){l+=n.filesize});return m?this.format(l):l},getTransfers:function(k){var l=this.uploads[k]||[];return l.map(function(m){return m})},format:function(k){if(k==0){return k}if(k<1024){return k+" Bytes"}if(k<1024*1024){return(k/1024)+" Kb"}if(k<1024*1024*1024){return(k/(1024*1024))+" Mb"}return(k/(1024*1024*1024))+" Gb"}},i={dragenter:function(l){l.stop();var k=this;co=k.getCoordinates();k.getFirst().setStyles({left:co.left,top:co.top,width:co.width,height:24,backgroundColor:"#E1F1FD",textAlign:"center",display:"block",zIndex:10}).tween("backgroundColor","#1096E6")},dragexit:function(k){k.stop()},mouseleave:function(k){this.getFirst().style.display="none"},dragover:function(k){k.stop()},drop:function(m){m.stop();var l=this,k=l.retrieve(h);l.getFirst().style.display="none";if(m.event.dataTransfer){$A(m.event.dataTransfer.files).each(function(n){c.upload(k).load(n)})}}},j=new Class({filesize:0,complete:false,initialize:function(l){if(l.filetype){this.addEvent("load",b.bind(this))}var m,k=l.container;this.addEvents({success:function(o){this.filesize=o.size;this.complete=true;c.actives[k].erase(this);var q=l.id,n=e(q+"_lfile").set({checked:true,value:o.path}),p=function(){n.checked=this.checked};e(q).set({checked:true,value:o.file,events:{change:p,click:p}}).style.display=""},load:function(){c.actives[k].push(this)},cancel:function(){c.uploads[k].erase(this);c.actives[k].erase(this)},complete:function(){if(c.actives[k].length==0){c.fireEvent(k,"allComplete",k)}}}).setOptions(l);m=this.createElement();m.getElement("#"+l.id).store(d,this);m.getElement("a").addEvent("click",function(n){n.stop();this.cancel()}.bind(this))},createElement:function(){var k=this.options;this.element=new Element("div",{"class":"upload-container",html:'<iframe id="'+k.id+'_iframe" src="'+k.base+(k.base.indexOf("?")==-1?"?":"&")+k.id+'" frameborder="0" scrolling="no" style="border:0;overflow:hidden;padding:0;display:block;float:left;height:20px;width:228px; "></iframe><input type="checkbox" style="display:none" name="'+k.name+'" id="'+k.id+'"/><input type="checkbox" style="display:none" name="file_'+k.name+'" id="'+k.id+'_lfile"/><span class="upload-span" id="'+k.id+'_label"><a href="'+k.base+'">Remove</a></span>'}).inject(k.container);return this.element},toElement:function(){return this.element},load:function(k){this.aborted=false;this.fireEvent("load",{element:this.element,file:k,size:0,transfer:this});if(this.aborted){this.fireEvent("abort",{file:k,message:this.message||"",transfer:this})}return this},cancel:function(){var k=this.running;this.fireEvent("cancel",this);if(k){this.fireEvent("complete",this)}this.element.destroy()},Implements:[Options,Events]}),a=new Class({Extends:j,running:false,ready:false,reader:!!g.FileReader,initialize:function(l){this.addEvents({success:function(o){var n=o.remove;delete o.remove;this.addEvent("cancel",function(){var p=new XMLHttpRequest();p.open("GET",n,true);p.setRequestHeader("Sender","XMLHttpRequest");p.send()})},cancel:function(){if(this.running){this.xhr.abort();this.xhr.onreadystatechange=function(){};this.xhr=new XMLHttpRequest();this.running=false}}}).parent(l);var m=this.xhr=new XMLHttpRequest();this.binary=!!m.sendAsBinary;m.onreadystatechange=this.onStateChange.bind(this);this.add(m.upload,"progress",function(o){if(o.lengthComputable){var n={width:o.loaded*this.width/o.total};this.progress.start({0:n,1:n})}}).add(m,"load",function(){var n={width:this.width};this.progress.start({0:n,1:n}).chain(function(){(function(){}.bind(this)).delay(50)}.bind(this))}).add(m,"error",function(){this.span.style.display="none";this.fields.getElement("label").set("text",this.filename+"(Failed)")}.bind(this));if(this.reader){var k=this.reader=new FileReader();this.add(k,"load",function(n){this.bin=n.target.result;this.ready=true})}},createElement:function(){var l=this.options,k;this.element=new Element("div",{"class":"upload-container",html:'<div style="display:inline-block;padding:3px"><span style="display:none">&nbsp;</span><span class="first" ><input id="'+l.id+'_input" type="file" name="'+l.id+'_input"'+(l.multiple?' multiple="multiple"':"")+'/><input type="checkbox" style="display:none" name="'+l.name+'" id="'+l.id+'"/><input type="checkbox" style="display:none" name="file_'+l.name+'" id="'+l.id+'_lfile"/><label for="'+l.id+'"></label></span></div><a class="noUploadPhoto" href="'+l.base+'"></a>'}).inject(l.container);k=this.element.getElement("input[type=file]").addEvent("change",function(o){var n=$A(o.target.files),m=this.options;this.load(n.shift());n.each(function(p){c.upload(m).load(p)})}.bind(this));return this.addEvent("abort",function(){k.value=""}).element},add:function(m,l,k){k=k.bind(this);if(m.addEventListener){m.addEventListener(l,k,false)}else{m["on"+l]=k}return this},onStateChange:function(){if(this.xhr.readyState!=4){return}var k,l,m="success";this.running=false;try{k=this.xhr.status}catch(n){}this.xhr.onreadystatechange=function(){};if(k>=200&&k<300){try{l=JSON.decode(this.xhr.responseText);l.transfer=this;l.element=this.element;if(l.size!=this.size){m="failure"}}catch(n){m="failure"}}else{m="failure"}this.fireEvent(m,m=="failure"?this:l).fireEvent("complete",this)},load:function(m){this.aborted=false;this.fireEvent("load",{element:this.element,file:m.name,size:m.size,transfer:this});if(this.aborted){this.fireEvent("abort",{file:m,message:this.message||"",transfer:this});this.cancel();return this}this.file=m;this.size=m.size;this.filename=m.name;var q=this.element.getFirst(),p=this.width=q.offsetWidth,k=m.name.shorten(),o="position:absolute;display:inline-block;margin:0 auto;left:0;top:0;height:30px;",n=this.span=q.getElement("span").set({"class":"progressBar",style:"width:"+p+"px;height:28px;position:relative;display:inline-block;text-align:center;",title:m.name}).adopt(new Element("span",{style:"z-index:1;width:"+p+"px;margin:0 auto;color:#aaa;"+o,text:k,"class":"z1"})).adopt(new Element("span",{style:"z-index:2;width:1px;"+o,"class":"z2"}).adopt(new Element("span",{style:"width:"+p+"px;margin:0 auto;color:#fff;display:inline-block",text:k,"class":"z22"}))).adopt(new Element("span",{html:"&nbsp;",style:"width:1px;background:#aaa;"+o,"class":"z3"})),l=n.getChildren();q.setStyle("width",q.offsetWidth);this.progress=new Fx.Elements([l.pop(),l.pop()],{link:"cancel"});this.fields=n.getNext().setStyle("display","none");this.fields.getFirst().style.display="none";c.enqueue(this.options.container,this.upload.bind(this));if(this.reader){this.reader.readAsBinaryString(m)}return this},initUpload:function(){var k=this.xhr;this.running=true;k.open("POST",this.options.base,true);k.setRequestHeader("Filename",this.filename);k.setRequestHeader("Sender","XMLHttpRequest");if(this.binary){k.sendAsBinary(this.bin)}else{k.send(this.file)}},upload:function(){if(this.reader){if(this.ready){this.initUpload()}else{setTimeout(this.upload.bind(this,1000))}}else{this.initUpload()}}});$extend(Element.NativeEvents,{dragenter:2,dragexit:2,dragover:2,drop:2})})(document.id);
