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

	version: '0.1',
 
	options: {
		roarContainer: null,
		container: null,
		link: 'select-0',
		rootPath: '/',
		'url':'',
		typeFilter: {
			'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
		},
		autoActive: true,
		afterAll: null //function
	},
 
	
	initialize: function(options)
	{
		var obj = this;
		this.setOptions(options);
		
		// One Roar instance for our notofications, positioned in the top-right corner of our demo.
		this.log = new Roar({
			container: $(this.options.roarContainer),
			position: 'topRight',
			duration: 5000
		});
		
		//Creo la struttura
		var status = new Element('div', {'id':'upload-status'});
		var p = new Element('p');
		status.grab(p);
		var sfoglia = new Element('a', {'id':'upload-browse', 'html':'Sfoglia', 'href':'#'});
		p.grab(sfoglia);
		var start = new Element('a', {'id':'upload-upload', 'html':'Comincia l\'upload', 'href':'#'});
		p.grab(start);
		var clear = new Element('a', {'id':'upload-clear', 'html':'Svuota Lista', 'href':'#'});
		p.grab(clear);
		//p.set('html','<a href="#" id="upload-browse">Sfoglia</a> |	<a href="#" id="upload-upload">Comincia l\'upload</a> | <a href="#" id="upload-clear">Svuota Lista</a> | <a href="#" id="upload-annulla">Annulla</a>');
		var overall = new Element('div');
		overall.set('html', '<strong class="overall-title"></strong><br /><img src="'+this.options.rootPath+'scripts/fancyupload/assets/progress-bar/bar.gif" class="progress overall-progress" />');
		status.grab(overall);
		
		
		var current = new Element('div');
		current.set('html', '<strong class="current-title"></strong><br /><img src="'+this.options.rootPath+'scripts/fancyupload/assets/progress-bar/bar.gif" class="progress current-progress" />');
		status.grab(current);
		
		var currentTxt = new Element('div', {'class':'current-text'});
		status.grab(currentTxt);
		
		var list = new Element('ul', {'id':'upload-list'});
		status.grab(list);
		
		this.sfoglia = sfoglia;
		this.list = list;
		this.status = status;
		this.start = start;
		this.clear = clear;
		
		status.inject(this.options.container);
		
		if (this.options.autoActive) this.active();
			
	},
	
	active: function()
	{
		var obj = this;
		var up = new FancyUpload2(this.status, this.list, { // options object
			// we console.log infos, remove that in production!!
			verbose: true,
			url: obj.options.url,
			path: obj.options.rootPath+'scripts/fancyupload/source/Swiff.Uploader.swf',
			typeFilter: {
				'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
			},
			target: this.sfoglia,
			onLoad: function() {
				this.target.addEvents({
					click: function() {
						return false;
					},
					mouseenter: function() {
						this.addClass('hover');
					},
					mouseleave: function() {
						this.removeClass('hover');
						this.blur();
					},
					mousedown: function() {
						this.focus();
					}
				});

				// Interactions for the 2 other buttons

				obj.clear.addEvent('click', function() {
					up.remove(); // remove all files
					return false;
				});

				obj.start.addEvent('click', function() {
					up.start(); // start upload
					return false;
				});
			},

			// Edit the following lines, it is your custom event handling

			/**
			 * Is called when files were not added, "files" is an array of invalid File classes.
			 *
			 * This example creates a list of error elements directly in the file list, which
			 * hide on click.
			 */
			onSelectFail: function(files) {
				files.each(function(file) {
					new Element('li', {
						'class': 'validation-error',
						html: file.validationErrorMessage || file.validationError,
						title: MooTools.lang.get('FancyUpload', 'removeTitle'),
						events: {
							click: function() {
								this.destroy();
							}
						}
					}).inject(this.list, 'top');
				}, this);
			},

			/**
			 * This one was directly in FancyUpload2 before, the event makes it
			 * easier for you, to add your own response handling (you probably want
			 * to send something else than JSON or different items).
			 */
			onFileSuccess: function(file, response) {
	            var json = new Hash(JSON.decode(response, true) || {});

				if (json.get('status') == '1') {
					file.element.addClass('file-success');
					file.info.set('html', '<strong>Image was uploaded:</strong> ' + json.get('width') + ' x ' + json.get('height') + 'px, <em>' + json.get('mime') + '</em>)');
				} else {
					file.element.addClass('file-failed');
					file.info.set('html', '<strong>An error occured:</strong> ' + (json.get('error') ? (json.get('error') + ' #' + json.get('code')) : response));
				}

	            /**/
			},

			/**
			 * onFail is called when the Flash movie got bashed by some browser plugin
			 * like Adblock or Flashblock.
			 */
			onFail: function(error) {
				switch (error) {
					case 'hidden': // works after enabling the movie and clicking refresh
						alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
						break;
					case 'blocked': // This no *full* fail, it works after the user clicks the button
						alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
						break;
					case 'empty': // Oh oh, wrong path
						alert('A required file was not found, please be patient and we fix this.');
						break;
					case 'flash': // no flash 9+ :(
						alert('To enable the embedded uploader, install the latest Adobe Flash plugin.')
				}
			},
			
			onComplete: function()
			{
				if (obj.options.afterAll) (function(){obj.options.afterAll();}).delay(500);
	            else (function(){window.location.reload();}).delay(500);
			}

		});
	}
});
