var ArchivosJS;
function InicializaUpload(){
	ArchivosJS=new MultiFiles(document.getElementById('fila_fondo'));
}
function MuestraUpload(){
	c=document.getElementById('input_cell');
	if(ArchivosJS==null) InicializaUpload();
	var new_element = document.createElement('input');
	new_element.type = 'file';
	new_element.className = 'finput';
	new_element.style.width = '440px';
	new_element.style.display = 'block';
	c.appendChild(new_element);
	ArchivosJS.addElement(new_element);
}
function MultiFiles( list_target, max ){
	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	this.addElement = function( element ){
		if( element.tagName == 'INPUT' && element.type == 'file' ){
			element.name = 'imagen_' + this.id++;
			element.ArchivosJS = this;
			element.onchange = function(){
				this.ArchivosJS.addListRow( this );
				this.style.position = 'absolute';
				this.style.left = '-1000px';
			};
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};
			this.count++;
			this.current_element = element;

		} else {
			alert( 'Error: not a file input element' );
		};
	};
	this.addListRow = function( element ){
		var new_row = document.createElement( 'tr' );
		var new_cell1 = document.createElement( 'td' );
		var new_cell2 = document.createElement( 'td' );
		var new_cell3 = document.createElement( 'td' );
		var new_cell4 = document.createElement( 'td' );
		var new_row_button = document.createElement( 'button' );
		new_row_button.value = '<img src="images/del.png" border="0">';
		new_row_button.style.width = '16px';
		new_row_button.style.height = '16px';
		new_row_button.style.border = '0px';
		new_cell1.element = element;
		new_row_button.onclick= function(){
			this.parentNode.parentNode.element.element.parentNode.removeChild( this.parentNode.parentNode.element.element);
			this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode );
			this.parentNode.parentNode.element.element.ArchivosJS.count--;
			this.parentNode.parentNode.element.element.ArchivosJS.current_element.disabled = false;
			return false;
		};
		img_cnt= this.count;
		if(img_cnt<10) img_cnt="0"+img_cnt;
		new_cell1.innerHTML = "Img "+img_cnt+": ";
		txtt=(element.value.length>70)?"..."+element.value.substr(element.value.length-50):element.value;
		new_cell2.innerHTML = txtt+"&nbsp;";
		new_cell3.appendChild( new_row_button );
		new_row.element=new_cell1;
		new_row.appendChild(new_cell1);new_row.appendChild(new_cell2);new_row.appendChild(new_cell3);
		this.list_target.parentNode.insertBefore(new_row,this.list_target);
		//this.list_target.insertBefore( new_row,antes );
	};
};
