// File's Multicharged by Edgar Cervantes Olivera - México :JavaScript Document
/* eval and insert the elements */
var load_method = (window.ie ? 'load' : 'domready');
window.addEvent(load_method, function(){
	/* styles */
	var daDivCss = {
		'position':'relative',
		'padding-bottom':36,
		'overflow':'hidden'
	}
	var daImagesToUploadStoreCss = {
		'position':'absolute',
		'top':'-100%',
		'right':0,
		'z-index':999
	}
	var daSpanButtonsAreaCss = {
		'position':'absolute',
		/*'bottom': (window.ie6 ? 36 : 12),*/
		'z-index':0
	}
	var daInputTypeButtonCss = {
		'height':24,
		'margin':0,
		'padding':0,
		'text-align':'center',
		'background':'#eee',
		'color':'#333',
		'border-left':'1px solid #ccc',
		'border-top':'1px solid #ccc',
		'border-right':'1px solid #999',
		'border-bottom':'1px solid #999'
	}
	var daInputTypeFileCss = {
		'height':24,
		'margin':0,
		'padding':0,
		'opacity':.01,
		'position':'absolute',
		'right':0,
		'z-index':99 
	}
	var daPFileSelectedCss = {
		'position':'relative'
	}
	var daSpanInfoBoxCss = {
		'display':'block',
		'width':38,
		'height':34,
		'margin-right':6,
		'float':'left',
		'background':'#eee',
		'color':'#333',
		'border':'2px solid #ccc',
		'border-bottom':'6px solid #ccc',
		'text-align':'center',
		'line-height':34,
		'font-family': 'Arial, Helvetica, FreeSans',
		'font-size': 11
	}
	var daSpanInfoTextCss = {
		'font-family': 'Arial, Helvetica, FreeSans',
		'font-size': 12
	}
	var daSpanSpacer = {
		'display':'block',
		'clear':'left'
	}
	var daARemoveCss = {
		'font-family': 'Arial, Helvetica, FreeSans',
		'font-size': 11,
		'color':'#3399CC',
		'text-decoration':'none',
		'cursor':'pointer'
	}
	/* tool functions */
	var daFileName = function(name){
		var justName = (name.lastIndexOf('\\')>0 ? name.split('\\') : name.split('/'));
		return justName.pop().toString();
	}
	var daFileExt = function(name){
		var justExt = name.split('.');
		return (justExt!=null && justExt.length>1 ? '.'+justExt.pop().toString().toLowerCase() : '');
	}
	var daFilePath = function(name){
		var justName = (name.lastIndexOf('\\')>0 ? name.split('\\') : name.split('/'));
		return justName.slice(0,justName.length-2).join('/').toString();
	}
	/* start procedure */
	var daimages = document.getElement('daimages');
	if(daimages!=null){
		var propertyNameForInputTypeFile = daimages.getProperty('name');
		var daDiv = new Element('div',{
			'styles':daDivCss
		});
		//daimages.replaceWith(daDiv);
		daDiv.injectBefore(daimages);
		var daImagesToUploadStore = new Element('div',{
			'id':'daImagesToUploadStore',
			'styles':daImagesToUploadStoreCss
		}).injectInside(daDiv);
		var daImagesToUploadList = new Element('div',{
			'id':'daImagesToUploadList'
		}).injectInside(daDiv);
		var daSpanButtonsArea = new Element('span',{
			'styles':daSpanButtonsAreaCss
		}).injectInside(daDiv);
		/* buttons for select files */
		var daInputTypeButton = new Element('input',{
			'type':'button',
			'value':'Examinar...',
			'styles':daInputTypeButtonCss
		}).injectInside(daSpanButtonsArea);
		var daInputTypeFile = new Element('input',{
			'type':'file',
			'name':propertyNameForInputTypeFile,
			'styles':daInputTypeFileCss
		}).addEvent('change',function(){
			/* open a new input element for select other new file */
			var daNewInputTypeFile = new Element('input',{
				'type':'file',
				'name':propertyNameForInputTypeFile,
				'styles':daInputTypeFileCss
			}).cloneEvents(this,'change').injectBefore(this);
			/* eval if the selected file no was used before */
			var fileSelected = this.value;
			var appendFile = true;
			var filesInStore = $$('#daImagesToUploadStore input');
			for(i in filesInStore){
				if(filesInStore[i].value==fileSelected){
					alert(daFileName(fileSelected)+' ya fue seleccionado anteriormente.');
					this.remove();
					return;
				}
			}
			/* insert a reference to the selected file before for visual control */
			var ID = new Date();
				ID = ID.getTime().toString();
			var daPFileSelected = new Element('p',{
				'styles':daPFileSelectedCss
			}).injectInside(daImagesToUploadList);
			new Element('span',{
				'styles':daSpanInfoBoxCss
			}).setText(daFileExt(this.value)).injectInside(daPFileSelected);
			new Element('span',{
				'styles':daSpanInfoTextCss
			}).setText('Cargar el archivo "'+daFileName(this.value)+'"').injectInside(daPFileSelected);
			new Element('br').injectInside(daPFileSelected);
			new Element('span',{
				'styles':daSpanInfoTextCss
			}).setText('desde "'+daFilePath(this.value)+'"').injectInside(daPFileSelected);
			new Element('br').injectInside(daPFileSelected);
			new Element('a',{
				'href':'javascript:void(0)',
				'styles':daARemoveCss,
				'events':{
					'click':function(e){
						var e= new Event(e).stop();
						$(ID).remove();
						this.getParent().remove();
					}
				}
			}).setText('(-) Remover de la lista').injectInside(daPFileSelected);
			new Element('span',{
				'styles':daSpanSpacer
			}).injectInside(daPFileSelected);
			/* store the current input with the file selected */
			this.setStyles({ 'opacity':1, 'position':'static' });
			new Element('span',{
				'id':ID,
				'styles':{
					'display':'block'
				}
			}).adopt(this).injectInside('daImagesToUploadStore');
		}).injectInside(daSpanButtonsArea);
	}
});
