var neteditAjaxUpload = new Class({
	Implements: Options,
	
	options: {
		onFileComplete:null,
		onSelectSuccess: null
    },
    
    initialize: function(options){

    	this.setOptions(options);
    	this.progressContainer = $('ajaxupload-progress');
    	if(this.progressContainer){
			var pb = new dwProgressBar({
				container: this.progressContainer,
				startPercentage: 0,
				speed:1000,
				timeLimit:0,
				boxID: 'dwProgressBox',
				percentageID: 'dwProgressPerc',
				displayID: 'dwProgressText',
				displayText: true
			});
			  
			this.pb = pb;
    	}
    	var links = $$('.ajaxupload-upload');
    	$each(links, function(link, index){
    		this.addEventSwiffUploader(link);
    	}.bind(this));
    },
    
    addEventSwiffUploader: function(linkUpload){
    	var log = new Roar({
			container: $('log'),
			position: 'topRight',
			duration: 5000
		});
    
		
    	var url = linkUpload.get('href').replace(/\&amp;/g,'&');
    	var linkUploadDef = linkUpload.get('html');
    	var parent = this;
    	var mySwiff = new Swiff.Uploader({
			path: 'js/libs/mootools-1.3/fancyupload/Swiff.Uploader.swf',
			url: url,
			verbose: true,
			queued: false,
			multiple: false,
			target: linkUpload,
			instantStart: true,
			fieldName: 'file_up',
			typeFilter: {},
			fileSizeMax: 500 * 1024 * 1024,
			onSelectSuccess: function(files) {
				if(parent.options.onSelectSuccess){
					parent.options.onSelectSuccess(this.target);
				}
				if (Browser.Platform.linux) window.alert('Warning: Due to a misbehaviour of Adobe Flash Player on Linux,\nthe browser will probably freeze during the upload process.\nSince you are prepared now, the upload will start right away ...');
				log.alert('Caricamento iniziato', 'Uploading <em>' + files[0].name + '</em> (' + Swiff.Uploader.formatUnit(files[0].size, 'b') + ')');
				this.setEnabled(false);
				parent.progressContainer.setStyle('display','block');
				parent.pb.set( 0 );
			},
			onSelectFail: function(files) {
				log.alert('<em>' + files[0].name + '</em> non aggiunto!', 'Seleziona un file pi� piccolo di 2 Mb. (Error: #' + files[0].validationError + ')');
			},
			appendCookieData: true,
			onQueue: function(){
				if (!this.uploading) return;
				var size = Swiff.Uploader.formatUnit(this.size, 'b');
				this.target.set('html', this.percentLoaded + '% di ' + size);
				parent.pb.set( this.percentLoaded );
				
			},
			onFileComplete: function(file) {
				try{
		 			var errorText = JSON.decode(file.response.text, true).error; // error
					if (file.response.error || errorText!='') {
						log.alert('Caricamento fallito', 'Uploading <em>' + this.fileList[0].name + '</em> fallito. (' + errorText + ')');
					} else {
						var md5 = JSON.decode(file.response.text, true).hash; // secure decode
						var path = JSON.decode(file.response.text, true).path;
						var name = JSON.decode(file.response.text, true).name;
		 
						log.alert('File caricato con successo', 'file: <code>'+name+'</code>');
		 
						if(parent.options.onFileComplete){
							parent.options.onFileComplete(file);
						}
						/*
						var inputs = this.target.getParent().getParent().getElements('.ajaximgupload-input input');
						$each(inputs, function(input, index){
							
							input.set('value', path+name);
							parent.updateImgByInput(input);
							parent.hideAllLink();
						});
						*/
					}
		 
					file.remove();
					this.setEnabled(true);
				}catch(e){
					alert(e);
				}
			},
			onComplete: function() {
				parent.progressContainer.setStyle('display','none');
				this.target.set('html', linkUploadDef);
			}
		}); 
    }
});

/*
var progress = new Element('img', {'class': 'file-progress', src: 'imgs/16x16/progress.gif'}).inject(this.ui.size, 'after');
this.ui.progress = new Fx.ProgressBar(progress, {
fit: true
}).set(0); 
*/
