// JavaScript Document
var oFichaAdjunto = new function FichaAdjunto(){
	this.file = document.getElementById('adjuntoArchivos');
	this.imgCerrar = document.getElementById('adjuntoCerrar');
	this.iframe = document.getElementById('adjuntoIframe');
	
	this.info = oAdjunto.o;
	this.files = new Array;
	this.esperar = false;
	this.cargado = false;
	
	this.req = new Request();
	this.req.listener = function(){
		var i = 0, o = this.req.respuestaXML, e = new Object;
		this.esperar = false;
		
		if(!o){
			//alert(this.req.respuestaHTML);
			return false;
		}
		
		if(o.childNodes.length == 0) this.info.innerHTML = 'No existen archivos adjuntos';
		else{
			this.cargado = true;
			for(i; i < o.childNodes.length; i++){
				e.i = o.childNodes[i].getAttribute('id');
				e.t = o.childNodes[i].firstChild.data;
				this.nuevo(e);
			}
		}
		i = o = e = null;
	}.closure(this);
	
	this.abrir = function(){
		if(!this.cargado){
			this.cargar();
			this.info.innerHTML = 'Cargando...';
		}
		
		popupAbrir(oAdjunto);
	}.closure(this);
	
	this.cerrar = function(){
		popupCerrar(oAdjunto);
	}.closure(this);
	
	this.cargar = function(){
		var v = '';
		if(this.esperar) return false;
		this.esperar = true;
		
		v += 'elemento|=|'+this.info.e+'|&|';
		v += 'seccion|=|'+this.info.s+'|&|';
		this.req.pedir(DIR_RAIZ_WEB+'xmlHttpRequest/adjuntos.php', v, 'POST');
	}
	
	this.descargar = function(a){
		if(Nav.esIE6) document.location = DIR_RAIZ_WEB+'xmlHttpRequest/adjunto.php'+'?seccion='+this.info.s+'&elemento='+this.info.e+'&adjunto='+a.i;
		else this.iframe.src = DIR_RAIZ_WEB+'xmlHttpRequest/adjunto.php'+'?seccion='+this.info.s+'&elemento='+this.info.e+'&adjunto='+a.i;
	}.closure(this);
	
	this.nuevo = function(i){
		var o = new Object;
		
		o.descargar = this.descargar;
		
		o.e = document.createElement('div');
		o.e.innerHTML = i.t;
		AddEvent(o.e.childNodes[0], 'click', function(){ this.descargar(this); }.closure(o));
		AddEvent(o.e.childNodes[1], 'click', function(){ this.descargar(this); }.closure(o));
		
		o.i = i.i;
		
		this.file.appendChild(o.e);
		this.files.push(o);
		
		o = null;
	}
	
	this.getTotal = function(){
		return this.info.t;
	}.closure(this);
	
	//
	AddEvent(this.imgCerrar, 'click', this.cerrar);
}
