/*
* need jquery
* by zy
*/
var upload = {
   uploadInfo : null,
   bar        : null,
   text       : null,
   content    : null,
   contextPath: null,
   width      : null,
   height     : null,
   
   refreshProgress : function(){
   		var self = this;   		
   		self.getUploadInfo(self.updateProgress);
   },

   updateProgress  : function(){
   		var self = this;
   		var uploadInfo = self.uploadInfo;
		if (uploadInfo.inProgress)
	    {
	        //document.getElementById('uploadbutton').disabled = true;
	
	        var fileIndex = uploadInfo.fileIndex;
	
	        var progressPercent = Math.ceil((uploadInfo.bytesRead / uploadInfo.totalSize) * 100);
			
			//alert(progressPercent +" "+ fileIndex+" "+uploadInfo.bytesRead+" "+uploadInfo.totalSize+" text:"+ self.text );
	        self.text.html( 'upload in progress: ' + progressPercent + '%');
	
	        self.content.width(parseInt(progressPercent * 3.5) + 'px');
	
	        window.setTimeout("upload.refreshProgress()", 1);
	    }	
	    return true;
   },

   startProgress   : function(bar, text , content , contextPath ){
   	 var self  = this; 
   	 self.bar  = $("#"+bar);
   	 self.text = $("#"+text);
   	 self.content = $("#"+content); 
   	 self.contextPath = contextPath;
   	 self.width       = $("#w").val();
   	 self.height      = $("#h").val();
	 self.bar.show();     
	 with(document.getElementById('fileUploadFrom')){	  
       if(document.getElementById("file").value){
        //alert(document.getElementById("file").fileSize);
        submit();
        window.setTimeout("upload.refreshProgress()", 1);
       } 
	 }
   },
   
   getUploadInfo : function(fn){
        var self = this;
		$.ajax({
   		type: "GET",
   		data: "asdf="+Math.random(),
   		url: self.contextPath+"/uploadInfo!uploadInfo.do",
   		success: function(res){   		  
   		  if(res != ""){
   			eval("upload.uploadInfo ="+res);
   			//alert(self.uploadInfo+" "+res);
    		fn.call(self);
   		  }else{
   		  	window.setTimeout("upload.refreshProgress()", 1);
   		  }			
   		}   		
	 });
   },
   uploadError : function(error){
     alert(error);
     window.close();
   },
   uploadCompleted : function(fileUrl,sFileUrl,bFileUrl){  
   		sFileUrl   = fileUrl;
   		bFileUrl   = fileUrl;
        var self   = this;
        var whStr  = "";       
        if(self.width && self.height)
        whStr =  " height='"+self.height+"' width='"+self.width+"'";
   		window.close();
   		window.opener.__EditorWindow.document.body.innerHTML = window.opener.__EditorWindow.document.body.innerHTML +
   		  "<img src='"+fileUrl+"' "+whStr+" id='"+sFileUrl+"'/>";
   		window.opener.document.getElementById("content").value = window.opener.__EditorWindow.document.body.innerHTML;  
   		window.opener.__AddEventHandler(window.opener.__EditorWindow.document.getElementById(sFileUrl),"mousedown", window.opener.__CheckImg_mouse);  
   		with(window.opener.__EditorWindow.document.body){
   			focus();
   		}
   		var translete = "translucent";
   		if(!window.opener.hasSelectedImg()){
   			translete = "nontransparent";
   		}
   		window.opener.document.getElementById("manage_list_pic").innerHTML=window.opener.document.getElementById("manage_list_pic").innerHTML + "<img onclick='changeClassName(this);' width='70' height='65' class='"+translete+"' id='"+sFileUrl+"' src='"+sFileUrl+"'/>";     		
   },
   uploadCompassCompleted : function(fileUrl){  
        var self   = this;     
   		window.close();   	
   		window.opener.document.getElementById("image").value = fileUrl;	
   		window.opener.document.getElementById("imgPreview").innerHTML="<img id='"+fileUrl+"' src='"+fileUrl+"'/>";     		
   },
   uploadMusicCompleted : function(fileUrl){  
        var self   = this;
  		window.close();
  		window.opener.__EditorWindow.document.body.innerHTML = window.opener.__EditorWindow.document.body.innerHTML +
  		  "<P><embed src='"+fileUrl+"' width='300' height='69' type='application/x-mplayer2' loop='-1' showcontrols='1' ShowDisplay='0' ShowStatusBar='1' autostart='0'></EMBED></P>";
  		window.opener.document.getElementById("content").value = window.opener.__EditorWindow.document.body.innerHTML;  
  },uploadVidioCompleted : function(fileUrl){  
        var self   = this;
 		window.close();
 		window.opener.__EditorWindow.document.body.innerHTML = window.opener.__EditorWindow.document.body.innerHTML +
 		  "<P><embed style='filter:Alpha(opacity=10,finishOpacity=100,style=2); WIDTH: 300px; HEIGHT: 300px' src='"+fileUrl+"' width='300' height='300' type='audio/mpeg' volume='100' loop='true' autostart='0' showstatusbar='1'></EMBED></P>";
 		window.opener.document.getElementById("content").value = window.opener.__EditorWindow.document.body.innerHTML;  
 }
};

