/* Swiff.Uploader 3.0, Fx.ProgressBar 1.1, FancyUpload 3.0, MIT License, Harald Kirschner <http://digitarald.de>, Valerio Proietti, <http://mad4milk.net> */
Swiff.Uploader=new Class({Extends:Swiff,Implements:Events,options:{path:"Swiff.Uploader.swf",target:null,zIndex:9999,height:30,width:100,callBacks:null,params:{wMode:"opaque",menu:"false",allowScriptAccess:"always"},typeFilter:null,multiple:true,queued:true,verbose:false,url:null,method:null,data:null,mergeData:true,fieldName:null,fileSizeMin:1,fileSizeMax:null,allowDuplicates:false,timeLimit:(Browser.Platform.linux)?0:30,buttonImage:null,policyFile:null,fileListMax:0,fileListSizeMax:0,instantStart:false,appendCookieData:false,fileClass:null},initialize:function(b){this.addEvent("load",this.initializeSwiff,true).addEvent("select",this.processFiles,true).addEvent("complete",this.update,true).addEvent("fileRemove",function(d){this.fileList.erase(d)
}.bind(this),true);this.setOptions(b);if(this.options.callBacks){Hash.each(this.options.callBacks,function(e,d){this.addEvent(d,e)},this)}this.options.callBacks={fireCallback:this.fireCallback.bind(this)};var c=this.options.path;if(!c.contains("?")){c+="?noCache="+$time()}this.options.container=this.box=new Element("span",{"class":"swiff-uploader-box"}).inject($(this.options.container)||document.body);
this.target=$(this.options.target);if(this.target){var a=window.getScroll();this.box.setStyles({position:"absolute",visibility:"visible",zIndex:this.options.zIndex,overflow:"hidden",height:1,width:1,top:a.y,left:a.x});this.parent(c,{params:{wMode:"transparent"},height:"100%",width:"100%"});this.target.addEvent("mouseenter",this.reposition.bind(this,[]));this.addEvents({buttonEnter:this.targetRelay.bind(this,["mouseenter"]),buttonLeave:this.targetRelay.bind(this,["mouseleave"]),buttonDown:this.targetRelay.bind(this,["mousedown"]),buttonDisable:this.targetRelay.bind(this,["disable"])});
this.reposition();window.addEvent("resize",this.reposition.bind(this,[]))}else{this.parent(c)}this.inject(this.box);this.fileList=[];this.size=this.uploading=this.bytesLoaded=this.percentLoaded=0;if(Browser.Plugins.Flash.version<9){this.fireEvent("fail",["flash"])}else{this.verifyLoad.delay(1000,this)}},verifyLoad:function(){if(this.loaded){return}if(!this.object.parentNode){this.fireEvent("fail",["disabled"])
}else{if(this.object.style.display=="none"){this.fireEvent("fail",["hidden"])}else{if(!this.object.offsetWidth){this.fireEvent("fail",["empty"])}}}},fireCallback:function(b,a){if(b.substr(0,4)=="file"){if(a.length>1){this.update(a[1])}var e=a[0];var c=this.findFile(e.id);this.fireEvent(b,c||e,5);if(c){var d=b.replace(/^file([A-Z])/,function(g,f){return f.toLowerCase()});c.update(e).fireEvent(d,[e],10)
}}else{this.fireEvent(b,a,5)}},update:function(a){$extend(this,a);this.fireEvent("queue",[this],10);return this},findFile:function(b){for(var a=0;a<this.fileList.length;a++){if(this.fileList[a].id==b){return this.fileList[a]}}return null},initializeSwiff:function(){this.remote("initialize",{width:this.options.width,height:this.options.height,typeFilter:this.options.typeFilter,multiple:this.options.multiple,queued:this.options.queued,url:this.options.url,method:this.options.method,data:this.options.data,mergeData:this.options.mergeData,fieldName:this.options.fieldName,verbose:this.options.verbose,fileSizeMin:this.options.fileSizeMin,fileSizeMax:this.options.fileSizeMax,allowDuplicates:this.options.allowDuplicates,timeLimit:this.options.timeLimit,buttonImage:this.options.buttonImage,policyFile:this.options.policyFile});
this.loaded=true;this.appendCookieData()},targetRelay:function(a){if(this.target){this.target.fireEvent(a)}},reposition:function(a){a=a||(this.target&&this.target.offsetHeight)?this.target.getCoordinates(this.box.getOffsetParent()):{top:window.getScrollTop(),left:0,width:40,height:40};this.box.setStyles(a);this.fireEvent("reposition",[a,this.box,this.target])},setOptions:function(a){if(a){if(a.url){a.url=Swiff.Uploader.qualifyPath(a.url)
}if(a.buttonImage){a.buttonImage=Swiff.Uploader.qualifyPath(a.buttonImage)}this.parent(a);if(this.loaded){this.remote("setOptions",a)}}return this},setEnabled:function(a){this.remote("setEnabled",a)},start:function(){this.fireEvent("beforeStart");this.remote("start")},stop:function(){this.fireEvent("beforeStop");this.remote("stop")},remove:function(){this.fireEvent("beforeRemove");this.remote("remove")
},fileStart:function(a){this.remote("fileStart",a.id)},fileStop:function(a){this.remote("fileStop",a.id)},fileRemove:function(a){this.remote("fileRemove",a.id)},fileRequeue:function(a){this.remote("fileRequeue",a.id)},appendCookieData:function(){var a=this.options.appendCookieData;if(!a){return}var c={};document.cookie.split(/;\s*/).each(function(d){d=d.split("=");if(d.length==2){c[decodeURIComponent(d[0])]=decodeURIComponent(d[1])
}});var b=this.options.data||{};if($type(a)=="string"){b[a]=c}else{$extend(b,c)}this.setOptions({data:b})},processFiles:function(f,d,a){var c=this.options.fileClass||Swiff.Uploader.File;var b=[],e=[];if(f){f.each(function(h){var g=new c(this,h);if(!g.validate()){g.remove.delay(10,g);b.push(g)}else{this.size+=h.size;this.fileList.push(g);e.push(g);g.render()}},this);this.fireEvent("selectSuccess",[e],10)
}if(d||b.length){b.extend((d)?d.map(function(g){return new c(this,g)},this):[]).each(function(g){g.invalidate().render()});this.fireEvent("selectFail",[b],10)}this.update(a);if(this.options.instantStart&&e.length){this.start()}}});$extend(Swiff.Uploader,{STATUS_QUEUED:0,STATUS_RUNNING:1,STATUS_ERROR:2,STATUS_COMPLETE:3,STATUS_STOPPED:4,log:function(){if(window.console&&console.info){console.info.apply(console,arguments)
}},unitLabels:{b:[{min:1,unit:"B"},{min:1024,unit:"kB"},{min:1048576,unit:"MB"},{min:1073741824,unit:"GB"}],s:[{min:1,unit:"s"},{min:60,unit:"m"},{min:3600,unit:"h"},{min:86400,unit:"d"}]},formatUnit:function(a,h,b){var f=Swiff.Uploader.unitLabels[(h=="bps")?"b":h];var c=(h=="bps")?"/s":"";var e,d=f.length,j;if(a<1){return"0 "+f[0].unit+c}if(h=="s"){var g=[];for(e=d-1;e>=0;e--){j=Math.floor(a/f[e].min);
if(j){g.push(j+" "+f[e].unit);a-=j*f[e].min;if(!a){break}}}return(b===false)?g:g.join(b||", ")}for(e=d-1;e>=0;e--){j=f[e].min;if(a>=j){break}}return(a/j).toFixed(1)+" "+f[e].unit+c}});Swiff.Uploader.qualifyPath=(function(){var a;return function(b){(a||(a=new Element("a"))).href=b;return a.href}})();Swiff.Uploader.File=new Class({Implements:Events,initialize:function(b,a){this.base=b;this.update(a)},update:function(a){return $extend(this,a)
},validate:function(){var a=this.base.options;if(a.fileListMax&&this.base.fileList.length>=a.fileListMax){this.validationError="fileListMax";return false}if(a.fileListSizeMax&&(this.base.size+this.size)>a.fileListSizeMax){this.validationError="fileListSizeMax";return false}return true},invalidate:function(){this.invalid=true;this.base.fireEvent("fileInvalid",this,10);return this.fireEvent("invalid",this,10)
},render:function(){return this},setOptions:function(a){if(a){if(a.url){a.url=Swiff.Uploader.qualifyPath(a.url)}this.base.remote("fileSetOptions",this.id,a);this.options=$merge(this.options,a)}return this},start:function(){this.base.fileStart(this);return this},stop:function(){this.base.fileStop(this);return this},remove:function(){this.base.fileRemove(this);return this},requeue:function(){this.base.fileRequeue(this)
}});Fx.ProgressBar=new Class({Extends:Fx,options:{text:null,url:null,transition:Fx.Transitions.Circ.easeOut,fit:true,link:"cancel"},initialize:function(c,b){this.element=$(c);this.parent(b);var a=this.options.url;if(a){this.element.setStyles({"background-image":"url("+a+")","background-repeat":"no-repeat"})}if(this.options.fit){a=a||this.element.getStyle("background-image").replace(/^url\(["']?|["']?\)$/g,"");
if(a){var d=new Image();d.onload=function(){this.fill=d.width;d=d.onload=null;this.set(this.now||0)}.bind(this);d.src=a;if(!this.fill&&d.width){d.onload()}}}else{this.set(0)}},start:function(b,a){return this.parent(this.now,(arguments.length==1)?b.limit(0,100):b/a*100)},set:function(c){this.now=c;var a=(this.fill)?(((this.fill/-2)+(c/100)*(this.element.width||1)||0).round()+"px"):((100-c)+"%");this.element.setStyle("backgroundPosition",a+" 0px").title=Math.round(c)+"%";
var b=$(this.options.text);if(b){b.set("text",Math.round(c)+"%")}return this}});var FancyUpload2=new Class({Extends:Swiff.Uploader,options:{queued:1,limitSize:0,limitFiles:0,validateFile:$lambda(true)},initialize:function(a,c,b){this.status=$(a);this.list=$(c);b.fileClass=b.fileClass||FancyUpload2.File;b.fileSizeMax=b.limitSize||b.fileSizeMax;b.fileListMax=b.limitFiles||b.fileListMax;this.parent(b);
this.addEvents({load:this.render,select:this.onSelect,cancel:this.onCancel,start:this.onStart,queue:this.onQueue,complete:this.onComplete})},render:function(){this.overallTitle=this.status.getElement(".overall-title");this.currentTitle=this.status.getElement(".current-title");this.currentText=this.status.getElement(".current-text");var a=this.status.getElement(".overall-progress");this.overallProgress=new Fx.ProgressBar(a,{text:new Element("span",{"class":"progress-text"}).inject(a,"after")});
a=this.status.getElement(".current-progress");this.currentProgress=new Fx.ProgressBar(a,{text:new Element("span",{"class":"progress-text"}).inject(a,"after")});this.updateOverall()},onSelect:function(){this.status.removeClass("status-browsing")},onCancel:function(){this.status.removeClass("file-browsing")},onStart:function(){this.status.addClass("file-uploading");this.overallProgress.set(0)},onQueue:function(){this.updateOverall()
},onComplete:function(){this.status.removeClass("file-uploading");if(this.size){this.overallProgress.start(100)}else{this.overallProgress.set(0);this.currentProgress.set(0)}},updateOverall:function(){this.overallTitle.set("html",MooTools.lang.get("FancyUpload","progressOverall").substitute({total:Swiff.Uploader.formatUnit(this.size,"b")}));if(!this.size){this.currentTitle.set("html",MooTools.lang.get("FancyUpload","currentTitle"));
this.currentText.set("html","")}},upload:function(){this.start()},removeFile:function(){return this.remove()}});FancyUpload2.File=new Class({Extends:Swiff.Uploader.File,render:function(){if(this.invalid){if(this.validationError){var a=MooTools.lang.get("FancyUpload","validationErrors")[this.validationError]||this.validationError;this.validationErrorMessage=a.substitute({name:this.name,size:Swiff.Uploader.formatUnit(this.size,"b"),fileSizeMin:Swiff.Uploader.formatUnit(this.base.options.fileSizeMin||0,"b"),fileSizeMax:Swiff.Uploader.formatUnit(this.base.options.fileSizeMax||0,"b"),fileListMax:this.base.options.fileListMax||0,fileListSizeMax:Swiff.Uploader.formatUnit(this.base.options.fileListSizeMax||0,"b")})
}this.remove();return}this.addEvents({start:this.onStart,progress:this.onProgress,complete:this.onComplete,error:this.onError,remove:this.onRemove});this.info=new Element("span",{"class":"file-info"});this.element=new Element("li",{"class":"file"}).adopt(new Element("span",{"class":"file-size",html:Swiff.Uploader.formatUnit(this.size,"b")}),new Element("a",{"class":"file-remove",href:"#",html:MooTools.lang.get("FancyUpload","remove"),title:MooTools.lang.get("FancyUpload","removeTitle"),events:{click:function(){this.remove();
return false}.bind(this)}}),new Element("span",{"class":"file-name",html:MooTools.lang.get("FancyUpload","fileName").substitute(this)}),this.info).inject(this.base.list)},validate:function(){return(this.parent()&&this.base.options.validateFile(this))},onStart:function(){this.element.addClass("file-uploading");this.base.currentProgress.cancel().set(0);this.base.currentTitle.set("html",MooTools.lang.get("FancyUpload","currentFile").substitute(this))
},onProgress:function(){this.base.overallProgress.start(this.base.percentLoaded);this.base.currentText.set("html",MooTools.lang.get("FancyUpload","currentProgress").substitute({rate:(this.progress.rate)?Swiff.Uploader.formatUnit(this.progress.rate,"bps"):"- B",bytesLoaded:Swiff.Uploader.formatUnit(this.progress.bytesLoaded,"b"),timeRemaining:(this.progress.timeRemaining)?Swiff.Uploader.formatUnit(this.progress.timeRemaining,"s"):"-"}));
this.base.currentProgress.start(this.progress.percentLoaded)},onComplete:function(){this.element.removeClass("file-uploading");this.base.currentText.set("html",MooTools.lang.get("FancyUpload","uploadCompleted"));this.base.currentProgress.start(100);if(this.response.error){var b=MooTools.lang.get("FancyUpload","errors")[this.response.error]||"{error} #{code}";this.errorMessage=b.substitute($extend({name:this.name,size:Swiff.Uploader.formatUnit(this.size,"b")},this.response));
var a=[this,this.errorMessage,this.response];this.fireEvent("error",a).base.fireEvent("fileError",a)}else{this.base.fireEvent("fileSuccess",[this,this.response.text||""])}},onError:function(){this.element.addClass("file-failed");var a=MooTools.lang.get("FancyUpload","fileError").substitute(this);this.info.set("html","<strong>"+a+":</strong> "+this.errorMessage)},onRemove:function(){this.element.getElements("a").setStyle("visibility","hidden");
this.element.fade("out").retrieve("tween").chain(Element.destroy.bind(Element,this.element))}});