/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.form.FileUploader"]){
dojo._hasResource["dojox.form.FileUploader"]=true;
dojo.provide("dojox.form.FileUploader");
dojo.experimental("dojox.form.FileUploader");
var swfPath=dojo.config.uploaderPath||dojo.moduleUrl("dojox.form","resources/uploader.swf");
dojo.require("dojox.embed.Flash");
dojo.require("dojo.io.iframe");
dojo.require("dojox.html.styles");
dojo.declare("dojox.form.FileUploader",null,{isDebug:false,devMode:false,id:"",uploadUrl:"",button:null,uploadOnChange:false,selectMultipleFiles:true,htmlFieldName:"uploadedfile",flashFieldName:"flashUploadFiles",fileMask:[],force:"",postData:null,swfPath:swfPath,minFlashVersion:9,uploaderType:"",flashObject:null,flashMovie:null,flashDiv:null,domNode:null,constructor:function(_1){
this.init(_1);
},log:function(){
if(this.isDebug){
console.log.apply(console,arguments);
}
},init:function(_2){
dojo.mixin(this,_2);
console.warn("isdebug:",this.isDebug,_2.isDebug,this.id);
this.id=this.id||dijit.getUniqueId("uploader");
dijit.registry.add(this);
this.log("init Flash:",(dojox.embed.Flash.available>=this.minFlashVersion||this.force=="flash"),dojox.embed.Flash.available>=this.minFlashVersion,this.force=="flash");
this.fileList=[];
this._subs=[];
this._cons=[];
if((dojox.embed.Flash.available>=this.minFlashVersion||this.force=="flash")&&this.force!="html"){
this.uploaderType="flash";
this.createFlashUploader();
}else{
this.uploaderType="html";
this.fileInputs=[];
this.fileCount=0;
if(dojo.isIE&&dojo.isIE<7){
setTimeout(dojo.hitch(this,"createHtmlUploader"),1);
}else{
this.createHtmlUploader();
}
}
},onMouseDown:function(_3){
},onMouseUp:function(_4){
},onMouseOver:function(_5){
if(this.button.domNode){
dojo.addClass(this.button.domNode,"dijitButtonHover dijitHover");
}
},onMouseOut:function(_6){
if(this.button.domNode){
dojo.removeClass(this.button.domNode,"dijitButtonHover dijitHover");
}
},onChange:function(_7){
},onProgress:function(_8){
},onComplete:function(_9){
},onCancel:function(){
this.log("Upload Canceled");
},onError:function(_a){
console.warn("FLASH/ERROR "+_a.type.toUpperCase()+":",_a);
},upload:function(_b){
if(_b){
this.postData=_b;
}
this.log("UPLOAD.TYPE:",this.uploaderType,"postData:",this.postData);
if(this.uploaderType=="flash"){
this.flashMovie.doUpload(this.postData);
}else{
dojo.io.iframe.send({url:this.uploadUrl,form:this._formNode,handleAs:"json",handle:dojo.hitch(this,function(_c,_d,_e){
this._complete([_c]);
})});
}
},setPosition:function(){
if(this.uploaderType=="flash"){
this.setFlashPosition();
}else{
this.setHtmlPosition();
}
},hide:function(){
dojo.style(this.domNode,"display","none");
},show:function(){
dojo.style(this.domNode,"display","");
},disable:function(_f){
if(_f){
this.hide();
}else{
this.show();
}
},destroyAll:function(){
if(this.button.destroy){
this.button.destroy();
}else{
dojo.destroy(this.button);
}
this.destroy();
},destroy:function(){
if(this.uploaderType=="flash"&&!this.flashMovie){
this._cons.push(dojo.connect(this,"onLoad",this,"destroy"));
return;
}
dojo.forEach(this._subs,function(s){
dojo.unsubscribe(s);
});
dojo.forEach(this._cons,function(c){
dojo.disconnect(c);
});
if(this.uploaderType=="flash"){
this.flashObject.destroy();
dojo.destroy(this.flashDiv);
}
},createFlashUploader:function(){
this.log("FLASH");
var _12=this.uploadUrl.toLowerCase();
if(_12.indexOf("http")<0&&_12.indexOf("/")!=0){
var loc=window.location.href.split("/");
loc.pop();
loc=loc.join("/")+"/";
this.uploadUrl=loc+this.uploadUrl;
}else{
}
var dim=this.getFakeButtonSize();
var w="100%";
var h="100%";
var _17={path:this.swfPath.uri||this.swfPath,width:w,height:h,allowScriptAccess:"always",allowNetworking:"all",vars:{uploadDataFieldName:this.flashFieldName,uploadUrl:this.uploadUrl,uploadOnSelect:this.uploadOnChange,selectMultipleFiles:this.selectMultipleFiles,id:this.id,isDebug:this.isDebug,devMode:this.devMode},params:{wmode:"transparent"}};
if(_17.vars.isDebug&&window.console&&window.console.dir){
window.passthrough=function(){
console.log.apply(console,arguments);
};
window.passthrough("Flash trace enabled.");
}else{
window.passthrough=function(){
};
}
this.flashDiv=dojo.doc.createElement("div");
this.domNode=this.flashDiv;
dojo.body().appendChild(this.flashDiv);
this._connectFlash();
this.setPosition();
this.flashObject=new dojox.embed.Flash(_17,this.flashDiv);
this.flashObject.onLoad=dojo.hitch(this,function(mov){
this.log("ONLOAD",mov);
this.flashMovie=mov;
this.setFlashVars();
});
},setFlashVars:function(){

try{
this.flashMovie.setFileMask(this.fileMask);
if(this.postData){
this.flashMovie.setPostData(this.postData);
}
}
catch(e){
setTimeout(dojo.hitch(this,"setFlashVars"),100);
}
},createHtmlUploader:function(){
if(!this.button.id){
this.button.id=dijit.getUniqueId("btn");
}
var _19;
if(this.button.domNode){
_19=dojo.byId(this.button.id).parentNode.parentNode;
_19.parentNode.onmousedown=function(){
};
}else{
_19=this.button.parentNode;
}
this._buildForm(_19);
this._buildFileInput(_19);
this.setPosition();
this._connectInput();
},setFlashPosition:function(){
var dim=this.getFakeButtonSize();
setTimeout(dojo.hitch(this,function(){
dojo.style(this.flashDiv,{position:"absolute",top:dim.y+"px",left:dim.x+"px",width:dim.w+"px",height:dim.h+"px",zIndex:2001});
this.log("this.flashDiv:",this.flashDiv);
}),100);
},setHtmlPosition:function(){
var _1b=this.getFakeButtonSize();
var _1c=dojo.marginBox(this._fileInput);
var _1d="rect(0px "+_1c.w+"px "+_1b.h+"px "+(_1c.w-_1b.w)+"px)";
this._fileInput.style.clip=_1d;
this._fileInput.style.left=(_1b.x+_1b.w-_1c.w)+"px";
this._fileInput.style.top=_1b.y+"px";
this._fileInput.style.zIndex=2001;
},_connectFlash:function(){
this._doSub("/filesSelected","_change");
this._doSub("/filesUploaded","_complete");
this._doSub("/filesProgress","_progress");
this._doSub("/filesError","_error");
this._doSub("/filesCanceled","onCancel");
this._doSub("/up","onMouseUp");
this._doSub("/down","onMouseDown");
this._doSub("/over","onMouseOver");
this._doSub("/out","onMouseOut");
this._connectCommon();
},_doSub:function(_1e,_1f){
this._subs.push(dojo.subscribe(this.id+_1e,this,_1f));
},_connectInput:function(){
this._disconnect();
this._cons.push(dojo.connect(this._fileInput,"mouseover",this,function(evt){
this.onMouseOver(evt);
}));
this._cons.push(dojo.connect(this._fileInput,"mouseout",this,function(evt){
this.onMouseOut(evt);
this._checkHtmlCancel("off");
}));
this._cons.push(dojo.connect(this._fileInput,"mousedown",this,function(evt){
this.onMouseDown(evt);
}));
this._cons.push(dojo.connect(this._fileInput,"mouseup",this,function(evt){
this.onMouseUp(evt);
this._checkHtmlCancel("up");
}));
this._cons.push(dojo.connect(this._fileInput,"change",this,function(){

this._checkHtmlCancel("change");
this._change([{name:this._fileInput.value,type:"",size:0}]);
}));
this._connectCommon();
},_checkHtmlCancel:function(_24){
if(_24=="change"){
this.dialogIsOpen=false;
}
if(_24=="up"){
this.dialogIsOpen=true;
}
if(_24=="off"){
this.dialogIsOpen=false;
this.onCancel();
}
},_connectCommon:function(){
this._cons.push(dojo.connect(window,"resize",this,"setPosition"));
var _25=this._dialogParent();
if(_25){
this._cons.push(dojo.connect(_25,"show",this,function(){
this.show();
this.setPosition();
}));
this._cons.push(dojo.connect(_25,"hide",this,"hide"),dojo.connect(_25,"destroy",this,"destroy"));
this._subs.push(dojo.subscribe("/dnd/move/stop",this,"setPosition"));
}
if(this.button.domNode){
this._cons.push(dojo.connect(this.button,"_setDisabledAttr",this,"disable"));
}
setTimeout(dojo.hitch(this,"setPosition"),500);
},_error:function(evt){
this.onError(evt);
},_change:function(_27){
this.fileList=this.fileList.concat(_27);
this.onChange(_27);
if(this.uploadOnChange){
this.upload();
}
},_complete:function(_28){
for(var i=0;i<this.fileList.length;i++){
this.fileList[i].percent=100;
}
this._progress(this.fileList);
this.fileList=[];
this.onComplete(_28);
},_progress:function(_2a){
for(var i=0;i<this.fileList.length;i++){
var f=this.fileList[i];
if(f.name==_2a.name){
f.bytesLoaded=_2a.bytesLoaded;
f.bytesTotal=_2a.bytesTotal;
f.percent=Math.ceil(f.bytesLoaded/f.bytesTotal*100);
}else{
if(!f.percent){
f.bytesLoaded=0;
f.bytesTotal=0;
f.percent=0;
}
}
}
this.onProgress(this.fileList);
},_dialogParent:function(){
var _2d;
var _2e=this.button.domNode||this.button;
for(var i=0;i<50;i++){
if(_2e.tagName.toLowerCase()=="body"){
_2e=null;
break;
}
if(_2e.tagName&&_2e.tagName.toLowerCase()=="div"&&(dojo.attr(_2e,"widgetId")||dojo.attr(_2e,"widgetid"))){
_2d=dijit.byNode(_2e);
if(_2d.titleBar&&_2d.titleNode){
break;
}else{
_2d=null;
}
}
_2e=_2e.parentNode;
}
return _2d;
},_disconnect:function(){
dojo.forEach(this._cons,function(c){
dojo.disconnect(c);
});
},_buildFileInput:function(_31){
if(this._fileInput){
this._disconnect();
dojo.style(this._fileInput,"display","none");
}
this._fileInput=document.createElement("input");
this.domNode=this._fileInput;
this._fileInput.setAttribute("type","file");
this.fileInputs.push(this._fileInput);
var nm=this.htmlFieldName;
var _id=this.id;
if(this.selectMultipleFiles){
nm+=this.fileCount;
_id+=this.fileCount;
this.fileCount++;
}
this.log("NAME:",nm,this.htmlFieldName,this.fileCount);
this._fileInput.setAttribute("id",this.id);
this._fileInput.setAttribute("name",nm);
dojo.addClass(this._fileInput,"dijitFileInputReal");
if(this.devMode){
dojo.style(this._fileInput,"opacity",1);
}
this._formNode.appendChild(this._fileInput);
},_removeFileInput:function(){
dojo.forEach(this.fileInputs,function(inp){
inp.parentNode.removeChild(inp);
});
this.fileInputs=[];
this.fileCount=0;
},_buildForm:function(_35){
if(this._formNode){
return;
}
if(dojo.isIE){
this._formNode=document.createElement("<form enctype=\"multipart/form-data\" method=\"post\">");
this._formNode.encoding="multipart/form-data";
}else{
this._formNode=document.createElement("form");
this._formNode.setAttribute("enctype","multipart/form-data");
}
this._formNode.id=dijit.getUniqueId("form");
if(_35&&dojo.style(_35,"display").indexOf("inline")>-1){
document.body.appendChild(this._formNode);
}else{
_35.appendChild(this._formNode);
}
this._setHtmlPostData();
this._setFormStyle();
},_setHtmlPostData:function(){
if(this.postData){
for(var nm in this.postData){
var f=document.createElement("input");
dojo.attr(f,"type","hidden");
dojo.attr(f,"name",nm);
dojo.attr(f,"value",this.postData[nm]);
this._formNode.appendChild(f);
}
}
},_setFormStyle:function(){
var _38=this.getFakeButtonSize();
var _39=Math.max(2,Math.max(Math.ceil(_38.w/60),Math.ceil(_38.h/15)));
dojox.html.insertCssRule("#"+this._formNode.id+" input","font-size:"+_39+"em");
},getFakeButtonSize:function(){
var _3a=(this.button.domNode)?dojo.byId(this.button.id).parentNode:dojo.byId(this.button.id)||this.button;
if(_3a.tagName.toLowerCase()=="span"){
_3a=dojo.byId(this.button.id);
}
var _3b=dojo.coords(_3a,true);
_3b.w=(dojo.style(_3a,"display")=="block")?dojo.style(_3a,"width"):_3b.w;
var p=_3a.parentNode.parentNode;
if(p&&dojo.style(p,"position")=="relative"){
_3b.x=dojo.style(p,"left");
_3b.y=dojo.style(p,"top");
}
if(p&&dojo.style(p,"position")=="absolute"){
_3b.x=0;
_3b.y=0;
}
var s=3;
_3b.x-=s;
_3b.y-=s;
_3b.w+=s*2;
_3b.h+=s*2;
return _3b;
}});
}
