_.Module.define({path:"common/component/ImageUploader",requires:["common/component/image_exif"],sub:{_url:"",_xhr:null,_active:false,_successFilter:null,_variable:null,_MAX_SIZE:1024*1024*3,initial:function(c,a,b){this._url=c;this._successFilter=a||null;b=b||{};this._compressWidth=b.maxWidth||3000;this._compressHeight=b.maxHeight||3000},_createHXR:function(){var a=this,b=this._successFilter;this._xhr=new XMLHttpRequest();this._xhr.onload=function(d){var c;try{c=$.parseJSON(a._xhr.responseText)}catch(f){c=a._xhr.responseText}if(b){if(b(c)){a.trigger("success",a._xhr,c)}else{a.trigger("error",a._xhr,c)}}else{a.trigger("success",a._xhr,c)}};this._xhr.onerror=function(c){a.trigger("error",a._xhr)};this._xhr.onabort=function(c){a.trigger("abort",a._xhr)};this._xhr.upload.onprogress=function(c){a.trigger("progress",c.loaded,c.total)}},_dataURLtoBlob:function(b){var h=atob(b.split(",")[1]);var a=b.split(",")[0].split(":")[1].split(";")[0];var f=new ArrayBuffer(h.length);var c=new Uint8Array(f);for(var d=0;d<h.length;d++){c[d]=h.charCodeAt(d)}var e=(window.MozBlobBuilder||window.WebKitBlobBuilder||window.BlobBuilder);if(e){var g=new e();g.append(f);return g.getBlob(a)}else{if(Blob){return new Blob([f],{type:a})}else{return null}}},_compressFormDataURI:function(a,g){var d=this._dataURLtoBlob(a),f=d.size,c=this;if(f<=g){c._uploadBlob(d)}else{var b=new Image(),e=document.createElement("canvas");b.onload=function(){var h=c._getCompressSize(b.width,b.height);e.width=h.width;e.height=h.height;e.getContext("2d").drawImage(b,0,0,b.width,b.height,0,0,h.width,h.height);b.onload=null;b.src="about:blank";b=null;var i=e.toDataURL("image/jpeg",(g/f).toFixed(1));e.getContext("2d").clearRect(0,0,h.width,h.height);e.width=e.height=0;e=null;d=c._dataURLtoBlob(i);if(d.size>g){c.trigger("error")}else{c._uploadBlob(d)}};b.src=a}},_uploadBlob:function(a){var c=new FormData(),d=this._variables;if(d&&typeof d=="object"){for(var b in d){c.append(b,d[b])}}c.append("file",a);this._active=true;if(!this._xhr){this._createHXR()}this._xhr.open("POST",this._url,true);this._xhr.withCredentials=true;this._xhr.send(c);if(d&&d.html5stat){$.stats.sendRequest("fr=tb0&st_type=html5upload&st_value=send")}},_getCompressSize:function(f,a){var b=this._compressWidth,e=this._compressHeight,g=f/a,c={};if(b<f||e<a){if(g>b/e){c.width=b;c.height=b/g}else{c.height=e;c.width=e*g}}else{c={width:f,height:a}}return c},setVariable:function(a){this._variables=a},upload:function(e,f,a){if(!e){return}f=(typeof f==="undefined")?this._MAX_SIZE:f;var d,b=this;if(e.getContext){if(a){d=e.toDataURL(a)}else{d=e.toDataURL("image/jpeg",0.8)}e.getContext("2d").clearRect(0,0,e.width,e.height);e.width=e.height=0;e=null;b._compressFormDataURI(d,f)}else{if(e.size){var c=this.use("common/component/image_exif");c.bind("success",function(h,i){if(i.getContext){b.upload(i,f,a)}else{if(e.size>f){var g=new FileReader();g.onload=function(j){g.onload=null;d=j.target.result;b._compressFormDataURI(d,f)};g.readAsDataURL(i)}else{b._uploadBlob(i)}}});c.rotateByorientation(e)}}},abort:function(){if(this._active){this._xhr.abort();this._active=false}},distroy:function(){this.abort();this.unbind("success");this.unbind("error");this.unbind("abort");this.unbind("progress");this._xhr=null}}});