var S3FlashUpload=new Class({Implements:[Options,Events],options:{form:null,progress:null,file:null,fakeFile:null,text:null,browse:null,s3BucketUri:null,s3Form:null,s3ContentType:null,s3FilePrefix:null,name:"flashUpload",fieldName:"fileName",maxUploadSize:67108864,path:"swiff.uploader.swf",messageEmptyUploadSize:"Please upload only non-empty files.",messageMaxUploadSize:"Please upload only files smaller than {maxUploadSize}.",messageUploadFailed:'Upload "{file}" failed. The upload will switch to the Basic uploader.',onLoad:Class.empty,onSubmit:Class.empty,onUpload:Class.empty,onSelect:Class.empty,onSelectError:Class.empty,onError:Class.empty,onComplete:Class.empty,debug:false},initialize:function(A){this.setOptions(A);if(!A.onSelectError){this.addEvent("onSelectError",this.onSelectError)}if(!A.onError){this.addEvent("onError",this.onError)}this.form=$(this.options.form);this.progress=$(this.options.progress);this.file=$(this.options.file);this.fakeFile=$(this.options.fakeFile);this.text=$(this.options.text);this.browse=$(this.options.browse);this.s3Form=$(this.options.s3Form);this.s3ContentType=$(this.options.s3ContentType);this.maxUploadSizeKB=this.sizeToKB(this.options.maxUploadSize);this.loaded=false;this.isStandardUploader=true;this.isFlashUploader=false;if(this.form&&this.progress&&this.file&&this.fakeFile&&this.text&&this.browse&&this.s3Form&&this.s3ContentType){this.activated=true;this.fileId=this.file.getProperty("id");this.fakeFile.setProperty("value","");this.text.setProperty("value","");this.text.setProperty("autocomplete","off");if(false&&Browser.Plugins.Flash.version>=9&&!(Browser.Engine.webkit419||Browser.Engine.presto925)){this.swiffy=new Uploader(this.progress,{url:this.s3Form.getProperty("action"),id:this.options.name+"Swiffy",fieldName:"file",limitSize:this.options.maxUploadSize,path:this.options.path,target:this.browse,debug:this.options.debug,onLoad:function(){this.loaded=true;this.switchToFlashUploader();this.fireEvent("onLoad")}.bind(this),onSelect:function(B){if(B.size>this.options.maxUploadSize||B.size==0){this.fireEvent("onSelectError",B);return false}else{var C=B.name.trim();this.text.setProperty("value",C);this.s3ContentType.value=MIMEtype.getType(C);this.fireEvent("onSelect",B);return true}}.bind(this),fileError:this.uploadError.bind(this),fileUpload:function(C,B){this.progress.removeClass("hidden");this.fireEvent("onUpload",C)}.bind(this),fileComplete:function(E,D){var C=null;if(D=="201"){C=this.options.s3BucketUri+encodeURIComponent(this.options.s3FilePrefix+E.name.trim())}else{if(Browser.Engine.trident){var G=new ActiveXObject("Microsoft.XMLDOM");G.async=false;G.loadXML(D)}else{var G=new DOMParser().parseFromString(D,"text/xml")}var H=G.getElementsByTagName("Location");if(H.length){C=H[0].childNodes[0].nodeValue}else{var B=G.getElementsByTagName("Message");if(B.length){var F=B[0].childNodes[0].nodeValue}else{var F=""}this.uploadError(E,F,"")}}if(C){this.fakeFile.setProperty("value",C);this.fireEvent("onComplete",{file:E,response:D});this.form.submit()}}.bind(this)});this.browse.addEvent("click",function(B){B.stop();this.swiffy.browse()}.bind(this))}else{this.swiffy=null}}else{this.activated=false}},delaySubmit:function(){if(this.activated){this.form.submit()}},submit:function(){if(this.activated){if(this.isFlashUploader&&this.swiffy.files.length){this.swiffy.upload({data:this.s3Form})}else{this.fireEvent("onSubmit");this.delaySubmit.delay(100,this)}}},switchToStandardUploader:function(){if(this.activated&&this.isFlashUploader){this.text.removeProperty("id");this.fakeFile.removeProperty("name");this.file.setProperty("id",this.fileId);this.file.setProperty("name",this.options.fieldName);this.text.addClass("hidden");this.browse.addClass("hidden");this.file.removeClass("hidden");this.isStandardUploader=true;this.isFlashUploader=false;this.reposition()}},switchToFlashUploader:function(){if(this.loaded&&this.isStandardUploader){this.file.removeProperty("id");this.file.removeProperty("name");this.text.setProperty("id",this.fileId);this.fakeFile.setProperty("name",this.options.fieldName);this.file.addClass("hidden");this.text.removeClass("hidden");this.browse.removeClass("hidden");this.isStandardUploader=false;this.isFlashUploader=true;this.reposition()}},reposition:function(){if(this.loaded){this.swiffy.reposition()}},sizeToKB:function(A){var B="B";if((A/1048576)>1){B="MB";A/=1048576}else{if((A/1024)>1){B="kB";A/=1024}}return A.round(1)+" "+B},uploadError:function(B,A,C){this.progress.addClass("hidden");this.text.setProperty("value","");this.fireEvent("onError",[B,A,C])},onSelectError:function(A){if(A.size==0){var B=this.options.messageEmptyUploadSize}else{var B=this.options.messageMaxUploadSize.substitute({maxUploadSize:this.maxUploadSizeKB})}alert(B)},onError:function(B,A,D){this.switchToStandardUploader();var C={file:B.name.trim()};alert(this.options.messageUploadFailed.substitute(C))}});