C.ActiveFileUpload = Class.extend(Prado.WebUI.CallbackControl,{
	onInit:function(options){
		// set up variables
		this.options = options;
		
		this.input = $(options.inputID);
		this.flag = $(options.flagID);
		this.form = $(options.formID);
		
		this.indicator = $(options.indicatorID);
		this.complete = $(options.completeID);
		this.error = $(options.errorID);
		this.reflabel = $(options.reflabelID);

		// set up events
		Event.observe(this.input,"change",this.fileChanged.bind(this));
		EventController.addEventListener('onFileUploadFinish', this.finishUpload.bind(this));
		EventController.addEventListener('onFileUploadStart', this.startUpload.bind(this));
		
		fu = this;
	},
	
	fileChanged:function(){
		if (this.reflabel) {
			this.reflabel.value = this.input.value;
		}
	},

	startUpload:function(tid){
		// show the upload indicator, and hide the complete and error indicators (if they areSn't already).
		this.flag.value = '1';
		this.complete.style.display = 'none';
		this.error.style.display = 'none';
		this.indicator.style.display = 'none';
		
		// set the form to submit in the iframe, submit it, and then reset it.
		this.oldtargetID = this.form.target;
		this.form.target = this.options.targetID;
		this.form.submit();
		this.form.target = this.oldtargetID;
	},
	
	finishUpload:function(options){
		// hide the display indicator.
		this.flag.value = '';
		this.indicator.style.display = 'none';
		if (this.options.targetID == options.targetID){
			// show the complete indicator.
			if (options.errorCode == 0){
				this.complete.style.display = 'none';
 				Prado.Callback(this.options.EventTarget, options, null, this.options);
			} else {
 				this.error.style.display = 'none';
			}
		}
	}
});

