var OwnFancyUpload = new Class({

	Extends: FancyUpload2,
	
	options: {
		limitFiles: 100,
		params: {
			wmode: "transparent"
		}
	},
	
	initialize: function(status, options) {
		this.uploadContainers = [];
		if (Browser.Plugins.Flash.version < 9 || (Browser.Plugins.Flash.version == 9 && Browser.Plugins.Flash.build < 16)) return false;
		
		if (!options.container) {
			options.container = new Element('div', {
				'id': 'upload'
			});
			$('pageContent').appendChild(options.container);
		}
		
		this.parent(status, null, options);
		this.overallProgress = new Fx.ProgressBar(new Element('div'));
	},
	
	updateOverall: function(bytesTotal) {
		this.bytesTotal = bytesTotal;
	},
	
	browse: function(container, fileList) {
		if (container.file != null) {
			this.removeFile(container.file);
		}
		this.parent(fileList);
	},
	
	addUploadElement: function(element) {
		var container = element.getParent();
		
		for (var i = this.uploadContainers.length-1; i >= 0; i--) {
			if (this.uploadContainers[i] == container) {
				return;
			}
		}
		
		if (!container.hasClass('ajax')) {
			return;
		}
		
		this.uploadContainers.push(container);
		container.uploader = this;
		container.file = null;
		container.fieldName = container.get('id');
		
		container.uploadList = new Element('ul', {
			'class': 'files_to_upload'
		});
		container.uploadList.container = container;
		
		var uploadDiv = new Element('div', {
			'class': 'ajax_upload'
		}).adopt(container.uploadList);
		
		var selectButton = new Element('a', {
			'class': 'select',
			'href': '#'
		}).set('text', selectImage).addEvent('click', function(e) {
			var uploader = this.container.uploader;
			uploader.list = this.container.uploadList;
			uploader.browse(this.container);
			return false;
			
		}).addEvent('mouseover', function() {
			this.container.uploader.options.target = this;
			this.container.uploader.target = this;
			this.container.uploader.reposition();
		});
		this.options.target = selectButton;
		this.target = selectButton;
		selectButton.container = container;
	
		uploadDiv.replaces(element).adopt(selectButton);
		
		this.reposition();
	},
	
	fileCreate: function(file) {
	
		if (!this.list) {
			this.list = this.options.target.container.uploadList;
		}
		
		if (this.list.container.file != null) {
			this.removeFile(this.list.container.file);
		}
	
		this.list.container.file = file;
		file.container = this.list.container;
		
		
		file.info = new Element('span', {'class': 'file-info'});
		file.element = new Element('li', {'class': 'file'}).adopt(
			new Element('span', {'class': 'file-size', 'html': this.sizeToKB(file.size)}),
			new Element('a', {
				'class': 'file-remove',
				'href': '#',
				'html': removeImage,
				'events': {
					'click': function() {
						this.removeFile(file);
						return false;
					}.bind(this)
				}
			}),
			new Element('span', {'class': 'file-name', 'html': file.name}),
			file.info
		).inject(this.list);
		
		
		var progress = new Element('div', {
			'class': 'progress current-progress'
		}).adopt(new Element('div', {
			'class': 'bar'
		}));
		file.element.adopt(progress);
		file.progress = new Fx.ProgressBar(progress, {
			//text: new Element('span', {'class': 'progress-text'}).inject(progress, 'after')
		});
		this.list = null;
		
		this.reposition();
	},
	
	render: function() {
	},
	
	onOpen: function(file, overall) {
		file = this.getFile(file);
		file.element.addClass('file-uploading');
		file.progress.cancel().set(0);
	},

	upload: function(options) {
		options.fileNames = [];
		for (var i = 0, j = this.files.length; i < j; i++) {
			if (!this.files[i].finished) {
				options.fileNames.push(this.files[i].container.fieldName);
			}
		}
		
		var ret = this.parent(options);
		if (ret !== true) {
			this.log('Upload in progress or nothing to upload.');
			if (ret) alert(ret);
		} else {
			this.log('Upload started.');
			this.status.addClass('file-uploading');
			this.overallProgress.set(0);
		}
	},

	onAllComplete: function(current) {
		this.parent(current);
		
		for (var i = this.files.length-1; i >= 0; i--) {
			if (!this.files[i].finished) {
				this.log('not all finished');
				this.log(this.files[i].name);
				return;
			}
		}
		
		this.removeFile();
		
		this.fireEvent('allFilesUploaded');
	},

	removeFile: function(file) {
		this.parent(file);
		this.reposition();
	},

	onComplete: function(file, response) {
		this.log('Completed upload "' + file.name + '".', arguments);
		file = this.getFile(file);
		file.progress.start(100);
		(this.options.fileComplete || this.fileComplete).call(this, this.finishFile(file), response);
	}, 
	
	addUploadElements: function(parent) {
		parent.getElements('input.file').each((function(fi){
			this.addUploadElement(fi);
		}).bind(this));
	},
	
	removeUploadElements: function(parent) {
		parent.getElements('input.file').each((function(fi){
		
			var container = fi.getParent();
			
			var found = -1
			for (var i = this.uploadContainers.length-1; i >= 0; i--) {
				if (this.uploadContainers[i] == container) {
					found = i;
					break;
				}
			}
			if (found == -1) {
				return;
			}
			
			this.uploadContainers.splice(found, 1);
			this.removeFile(container.file);
		
		}).bind(this));
	},

	onProgress: function(file, current, overall) {
		file = this.getFile(file);
		file.progress.start(current.bytesLoaded, current.bytesTotal);
	}
	
});

function prepareFileUploads() {

	if (Browser.Plugins.Flash.version >= 9) {
		var form = $('pwf_form');
		
		/**
		 * @todo at the moment it's not possible to select the same file in multiple input fields. To make that possible it would be necessary to identify a file not only by its name and size
		 */
		form.swiffy = new OwnFancyUpload(form, {
			'path': jsDir + 'pwf/upload/Swiff.Uploader.swf',
			queued: false,
			'onLoad': function() {
				this.addUploadElements(this.status);
			}
		});
	 
	 	/*
		$('demo-browse-images').addEvent('click', function() {
			swiffy.browse({'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'});
			return false;
		});*/
	}
}
	
	
window.addEvent('domready', function() {
	prepareFileUploads();
});
