myUpload = Class.create();
myUpload.prototype = {
	initialize: function(id,iframe,options) {
	
		this.options =  {
						url: '/admin/php/uploadProgress.php?progress_key=',
						idfilename: null,
						language: 'en',
						idstatus: null,
						progressBar : null,
						onComplete: null,
						onError : null,
						onCancel : null
						};
						
				
							
		Object.extend(this.options, options || {});	
		
		
		
		this.options.url =	this.options.url + $(id).value;
		this.filename   = null;
		this.uploadInfo = null;
		this.uploadFileName = null;
		this.uploadNewFileName = null;
	},
	
	
	start: function(){
		_getUploadInfo = this.getUploadInfo.bind(this);
		this.getUploadInfo();
		//setTimeout(_getUploadInfo,1000);
	},
	
	cancel: function(){
		alert(iframe)
	},
	
	getUploadInfo: function(){
		this.currentTime = new Date();
		this._onSuccessHandler = this.controller.bind(this);
		options = { 
					method: 'get',
					onSuccess: this._onSuccessHandler
				  };
				  
		new Ajax.Request(this.options.url+'&time='+this.currentTime.getTime()+'&file='+this.filename,options)
	},
	
	
	/*controllerJSON: function(responseRequest){
	
		this.uploadInfo = String(responseRequest.responseText).evalJSON();
		//alert('done : ' + this.uploadInfo.done);
		if (this.uploadInfo.done == 0){
			
			if(this.options.progressBar != null){
				//alert("current : " + this.uploadInfo.current + " tolal : " + this.uploadInfo.total)
				this.options.progressBar.setProgressBar(parseInt(this.uploadInfo.current),parseInt(this.uploadInfo.total));
			}
				
			if(this.options.idfilename != null){
				$(this.options.idfilename).innerHTML= '<strong>Uploading file</strong> ' + this.uploadInfo.filename
			}
			
			if(this.options.idstatus != null){
				$(this.options.idstatus).innerHTML= Math.round((this.uploadInfo.current/1024)) +'Ko sur '+ Math.round((this.uploadInfo.total/1024)) +' Ko<br>Please wait....'
			}
			
			if (this.uploadInfo.cancel_upload == 1){
				if(this.options.idstatus != null){	
					$(this.options.idstatus).setStyle({'textAlign':'center'});
					$(this.options.idstatus).innerHTML= '<strong>An error is occured :</strong><br>Max file size allowed 5 Mo<br>Please upload a new file !!!';	
					this.options.progressBar.progressBar.remove();
					$(this.options.idfilename).remove();
					if(this.options.onError != null){
						setTimeout(this.options.onError.bind(this), 1);
					}
				}
			}
			//setTimeout(this.getUploadInfo.bind(this), 1000);
			this.getUploadInfo();
		}else{
			if (this.uploadInfo.cancel_upload == 1){
				$(this.options.idstatus).setStyle({'textAlign':'center'});
				$(this.options.idstatus).innerHTML= '<strong>An error is occured :</strong><br>Max file size allowed 5 Mo<br>Please upload a new file !!!';	
				$(this.options.idfilename).remove();
				this.options.progressBar.progressBar.remove();
				if(this.options.onComplete != null){
					setTimeout(this.options.onError.bind(this), 1);
				}
				
				this.cancel();
				
			}else{
				
				if(this.options.progressBar != null)
					this.options.progressBar.setProgressBar(this.uploadInfo.current,this.uploadInfo.total);
				if(this.options.idfilename != null)
					$(this.options.idfilename).innerHTML= '<strong>Uploading file</strong> ' + this.uploadInfo.filename + ' '
				if(this.options.idstatus != null)
					$(this.options.idstatus).innerHTML=  Math.round((this.uploadInfo.current/1024)) +' ko of '+ Math.round((this.uploadInfo.total/1024)) +' uploaded! <br>Upload rate was '+ Math.round((this.uploadInfo.rate/1024))+' kops.'
				
				this.uploadFileName = this.uploadInfo.filename;
				this.uploadNewFileName = this.uploadInfo.new_file_name;	
				
				if(this.options.onComplete != null){
					setTimeout(this.options.onComplete.bind(this), 1);
				}
			}
		}	
	}*/
	
	controller: function(responseRequest){
		
		switch(this.options.language){
			case 'fr':
				uploadingFileStr = 'Upload du fichier';
				uploadingOnStr   = 'sur';
				uploadingRateStr = 'Vitesse de transfert:';
				uploadingSuccStr = 'Upload du fichier terminé';
				break;
			case 'nl':
				uploadingFileStr = 'Uploading file';
				uploadingOnStr   = 'op';
				uploadingRateStr = 'Upload rate:';
				uploadingSuccStr = 'Uploading file success';
				break;
			case 'en':
				uploadingFileStr = 'Uploading file';
				uploadingOnStr   = 'on';
				uploadingRateStr = 'Upload rate:';
				uploadingSuccStr = 'Uploading file success';
				break;			
		}
	
		this.uploadInfo = responseRequest.responseText
		//alert('done : ' + this.uploadInfo.done);
		if (this.uploadInfo != "done"){
			
			this.uploadInfo = this.uploadInfo.split("|");
			this.uploadInfoCurrentUploaded  = this.uploadInfo[3];
			this.uploadInfoCurrentTotalSize = this.uploadInfo[4];
			this.uploadInfoCurrentPourcent  = this.uploadInfo[5];
			this.uploadInfoCurrentRate	    = this.uploadInfo[2];
			
			
			if(this.options.progressBar != null){
				this.options.progressBar.setProgressBarASP(parseInt(this.uploadInfoCurrentPourcent));
			}
				
			if(this.options.idfilename != null){
				$(this.options.idfilename).innerHTML= '<strong>'+ uploadingFileStr +'</strong>';
			}
			
			if(this.options.idstatus != null){
				$(this.options.idstatus).innerHTML= this.uploadInfoCurrentUploaded +' '+ uploadingOnStr +' '+ this.uploadInfoCurrentTotalSize +' <br>'+ uploadingRateStr +' ' + this.uploadInfoCurrentRate;
			}
			
			/*if (this.uploadInfo.cancel_upload == 1){
				if(this.options.idstatus != null){	
					$(this.options.idstatus).setStyle({'textAlign':'center'});
					$(this.options.idstatus).innerHTML= '<strong>An error is occured :</strong><br>Max file size allowed 5 Mo<br>Please upload a new file !!!';	
					this.options.progressBar.progressBar.remove();
					$(this.options.idfilename).remove();
					if(this.options.onError != null){
						setTimeout(this.options.onError.bind(this), 1);
					}
				}
			}*/
			//setTimeout(this.getUploadInfo.bind(this), 1000);
			this.getUploadInfo();
		}else{
			if(this.options.progressBar != null){
				this.options.progressBar.setProgressBarASP(100);
			}
				
			if(this.options.idfilename != null){
				$(this.options.idfilename).innerHTML= '<strong>'+ uploadingSuccStr +'</strong>';
			}
			
			if(this.options.idstatus != null){
				
				//alert(this.uploadInfoCurrentTotalSize);
				
				if(this.uploadInfoCurrentTotalSize){
					$(this.options.idstatus).innerHTML = this.uploadInfoCurrentTotalSize +' '+ uploadingOnStr +' '+ this.uploadInfoCurrentTotalSize +' <br>'
				}else{
					$(this.options.idstatus).innerHTML = "<br>";
				}
			} 	
			
			if(this.options.onComplete != null){
				setTimeout(this.options.onComplete.bind(this), 1);
			}
		}	
	}
	
}