function MultiUpload(inputsTarget, listTarget, form, submitButton, filesInForm){

	this.listTarget = listTarget;
	this.inputsTarget = inputsTarget;
	this.submitButton = submitButton;
	this.filesInForm = filesInForm;
	this.form = form;
	this.count = 0;
	this.callbackFunc = null;

	this.setCallback = function(func) {
		this.callbackFunc = func;
	};
	
	this.setCallbackFinish = function(funcfinish) {
		this.callbackFuncFinish = funcfinish;
	};
	
	this.addElement = function(element) {

		element.ref = this;
		element.onchange = function(){
			
			low=this.value.toLowerCase();
			ext1=low.substring((low.length)-4,low.length);
			ext2=low.substring((low.length)-5,low.length);
			if (ext1=='.png' || ext1=='.jpg' || ext1=='.gif' || ext2=='.jpeg') 
			{
				// New file input
				var new_element = document.createElement('input');
				new_element.type = 'file';
				
				// Add new empty file input
				this.parentNode.insertBefore(new_element, this);
				
				this.id = "file"+this.ref.count;
				this.ref.moveInput(this);
				this.ref.addListRow(this,this.ref.count);
				
				// Apply 'update' to element
				this.ref.addElement(new_element);
			}
			else 
			{
				alert("D\351sol\351, seuls les formats jpg, png, gif sont accept\351s");
			};

		};

		this.count++;

	};
	
	this.moveInput = function(element) {
		this.inputsTarget.appendChild(element);
		this.inputsTarget.insertBefore(element,this.inputsTarget.firstChild);
	}

	this.addListRow = function(element,id){
		html = '<li id="listElement'+id+'"><div class="placel">' + element.value + '</div><div class="placer" id="listElementRight'+id+'"><a href="#" id="listElementDeleter'+id+'" onclick="javascript:listElementDeleter'+id+';">Supprimer</a></div></li>';
		new Insertion.Top(this.listTarget.id,html);

		$("listElementDeleter"+id).onclick = function() {
			Element.remove("file"+id);
			Element.remove("listElement"+id);
		}
	};
	
	this.reloadIframe = function() {
		if ($("iframeUpload") != null)
			Element.remove("iframeUpload");
			
		new Insertion.Bottom(this.listTarget.id,'<iframe style="width:0px;height:0px;border:0;padding:0;margin:0;" src="" name="iframeUpload" id="iframeUpload"></iframe>');
	};
	
	this.filesToUpload = 0;
	this.fileUploading = 0;
	this.fileIdUploading = -1;
	this.filenameUploading = "";

	this.startUpload = function() {
		this.submitButton.disabled = "true";
		var elements = this.findElements(this.inputsTarget.id,"input");
		this.filesToUpload = elements.length;
		this.fileUploading = 0;
		this.nextElement();
	};
	
	this.updatePercent = function() {
		if (this.callbackFunc != null)
			this.callbackFunc(this.fileUploading,this.filesToUpload,this.fileUploading / this.filesToUpload * 100);
	};


	this.nextElement = function() {
		this.fileUploading++;
		if (this.fileUploading <= this.filesToUpload) {
			var elements = this.findElements(this.inputsTarget.id,"input");
			if (elements != null) {
				for(i=0;i<elements.length;i++) {
					e = elements[i];
					if (e.type == "file") {
						this.reloadIframe();
						if ($("actualFile") != null)
							Element.remove("actualFile");
							
						this.fileIdUploading = e.id.substring(4);
						e.id = "actualFile";
						e.name = "photo";
						this.filenameUploading = e.value;
						this.updatePercent();
						this.filesInForm.appendChild(e);
						this.form.target = "iframeUpload";
						this.form.submit();
						break;
					}
				}
			}		
		}
		else {
			this.submitButton.disabled = "";
			if (this.callbackFuncFinish != null)
				this.callbackFuncFinish();
		}
	};
	
	this.findElements = function(element, tag) {
	    element = $(element);
	    if(!element.hasChildNodes()) return null;
	    var elements = [];
	    $A(element.childNodes).each( function(e) {
	      if(e.tagName && e.tagName.toUpperCase()==tag.toUpperCase())
	          elements.push(e);
	    });
	    return (elements.length>0 ? elements.flatten() : null);
	};

};