var CosMooUpload = new Class({
	Implements: [Options],
	

	version: '0.2',
 
	options: {
		roarContainer: null,
		target: null,
		link: 'select-0',
		rootPath: '/',
		'url':'',
		typeFilter: {
			'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
		},
		limitMB: 2,
		linkAfter: null,
		targetPath: '',
		multiple: false,
		fileListMax: 0
	},
 
	
	initialize: function(options)
	{
		if (options.typeFilter) this.options.typeFilter = null;
		this.setOptions(options);
		//this.swf=null;
		
		// One Roar instance for our notofications, positioned in the top-right corner of our demo.
		if(!this.log)
		{
			this.log = new Roar({
				container: $(this.options.roarContainer),
				position: 'topRight',
				duration: 5000
			});
		}
		
		var obj = this;
		var link = $(this.options.link);

		// Uploader instance
		this.createSwf(link);

		// Button state
		link.addEvents({
			click: function() {
				return false;
			},
			mouseenter: function() {
				this.addClass('hover');
				obj.swf.reposition();
			},
			mouseleave: function() {
				this.removeClass('hover');
				this.blur();
			},
			mousedown: function() {
				this.focus();
			}
		});
	},
	
	createSwf: function(link)
	{
		var obj = this;
		var linkIdle = this.options.linkAfter || link.get('html');
		
		function linkUpdate() {
			if (!obj.swf.uploading) return;
			var size = Swiff.Uploader.formatUnit(obj.swf.size, 'b');
			link.set('html', '<span class="small">' + obj.swf.percentLoaded + '% of ' + size + '</span>');
		}
		
		this.swf = new Swiff.Uploader({
			path: this.options.rootPath+'scripts/fancyupload/source/Swiff.Uploader.swf',
			url: this.options.url+'?url='+this.options.targetPath,
			verbose: false,
			queued: false,
			multiple: this.options.multiple,
			fileListMax: this.options.fileListMax,
			target: link,
			instantStart: true,
			typeFilter: this.options.typeFilter,
			fileSizeMax: this.options.limitMB * 1024 * 1024,
			onSelectSuccess: function(files) {
				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 ...');
				obj.log.alert('Starting Upload', 'Uploading <em>' + files[0].name + '</em> (' + Swiff.Uploader.formatUnit(files[0].size, 'b') + ')');
				this.setEnabled(false);
			},
			onSelectFail: function(files) {
				if(files[0].validationError=="fileListMax")
				{
					obj.log.alert('<em>' + files[0].name + '</em> non inserito!', 'Non puoi inserire piu di '+obj.options.fileListMax+' immagini.');
				}
				else if(files[0].validationError=="sizeLimitMax")
					obj.log.alert('<em>' + files[0].name + '</em> non inserito!', 'Per favore scegliere un\'immagine inferiore ai '+obj.options.limitMB+' Mb.');
			},
			appendCookieData: true,
			onQueue: linkUpdate,
			onFileComplete: function(file) {
				if (file.response.error) {
					obj.log.alert('Errore: '+file.response.error);
				} else {
					var filedata = JSON.decode(file.response.text).data;
					if (obj.options.target)
					{
						obj.options.target.value = filedata.file;
					}
					link.fireEvent('fileComplete', filedata);

				}
				
				file.remove();
				this.setEnabled(true);
			},
			onComplete: function() {
				link.set('html', linkIdle);
				link.fireEvent('opComplete');
			}
		});
	},
	
	disabilita: function()
	{
		this.swf.box.destroy();
	},
	
	abilita: function(fileListMax)
	{
		this.options.fileListMax = fileListMax;
		this.createSwf($(this.options.link));
	}
});
