
var FileUploader = new Class({
	Implements:[Options,Events],
	el:null,
	uploadFrame:null,
	iframe:null,	
	options: {
		retry:'Recommencer',
		charging:'En chargement'
	},
	initialize: function(el,options){
		this.el = el;
		this.setOptions(options);
		this.iframe = new IFrame({
			src:'uploader.inc.php?m=1',
			scrolling:'no',
			frameborder:0,
			styles:{
				border:'none',
				width:this.el.getCoordinates().width,
				height:this.el.getCoordinates().height
			},
			events:{
				load:function(){
					this.uploadFrame = new IFrame(this.getDom().getElement('iframe'),{
								events:{
									load:function(){
										this.manageComplete();
									}.bind(this)
								}
						});
					this.input();
				}.bind(this)				
			}			
		});
		if(this.el.getElement('input')){
			this.newInput = new Element('input',{'type':'hidden','name':this.el.getElement('input').get('name')});
			this.newInput.inject(this.el,'after');
			this.gainControl();
			if(this.el.getElement('input').hasClass('req'))
				this.releaseControl();
		}
		
		this.iframe.replaces(this.el);
	},
	
	getDom:function(){
		return this.iframe.contentWindow.document;
	},	
	input:function(){
		this.empty().set('html','<input name="uploadInput" id="uploadInput" class="req" type="file" />');
		this.getDom().getElement('input').addEvent('change',function(){
			this.uploading();
		}.bind(this));
		this.iframe.tween('height',this.getDom().getElement('body').getStyle('height'));
	},	
	empty:function(){
		return this.getDom().getElement('div.uploadFile').empty().removeClass('fileUploadErreur').removeClass('fileUploadDone');	
	},
	browse:function(){
		this.empty().adopt(new Element('input',{'name':'uploadInput','id':'uploadInput','class':'req','type':'file'}));
		return this.iframe.tween('height',this.getDom().getElement('body').getStyle('height'));
	},
	uploading:function(){
		this.fname = this.getDom().getElement('input').value;
		this.getDom().getElement('form').submit();
		this.empty().set('html','<strong>'+this.options.charging+'</strong> ('+this.fname+') | <a href="#" class="cancelLink">Cancel</a>');
		this.getDom().getElement('a.cancelLink').addEvent('click',function(e){
			e.preventDefault();
			this.uploadFrame.set('src','');
			this.uploadFrame.addClass('cancelled');
			this.input();			
		}.bind(this));
		this.iframe.tween('height',this.getDom().getElement('body').getStyle('height'));
	},
	complete:function(f){
		this.empty().addClass('fileUploadDone').set('text',this.fname+' | '+f.filesize);
		this.newInput.set('value',f.filename);
		this.iframe.tween('height',this.getDom().getElement('body').getStyle('height'));
		this.gainControl();
	},
	error:function(msg){
		this.empty().addClass('fileUploadErreur').set('html',msg + ' | <a href="#" class="cancelLink">'+this.options.retry+'</a>');
		this.getDom().getElement('a.cancelLink').addEvent('click',function(e){
			e.preventDefault();
			this.uploadFrame.set('src','');
			this.uploadFrame.addClass('cancelled');
			this.input();			
		}.bind(this));
		this.iframe.tween('height',this.getDom().getElement('body').getStyle('height'));
	},
	manageComplete:function(){
		if(this.uploadFrame.hasClass('cancelled'))return this.uploadFrame.removeClass('cancelled');
		response = JSON.decode(this.uploadFrame.contentWindow.document.getElement('p.response').get('text'));
		if(response.success==true) return this.complete(response);	
		this.error(response.msg);
	},
	releaseControl:function(){
		$('btnActive').setStyle('display','none');
		$('btnInActive').setStyle('display','block');
	},
	gainControl:function(){
		$('btnActive').setStyle('display','block');
		$('btnInActive').setStyle('display','none');	
	}
});
