// JavaScript Document
var oDescarga = new function(){
	var oEle = null;
	var accion = "";
	//OBJs
	var req = new Request();
	var contenedorFichas = new Array();
	var ficha = null;
	var blockeador = null;
	var oClase = this;
	var iframe = null;
	req.listener = function(){
		var d = req.respuestaXML;
		switch(accion){
			case "categoria":
				$('categoriaFichaLoader').style.display = 'none';
				if(!d) alert(req.respuestaHTML);
				//
				else $('categoriaFichaCont').innerHTML = (Nav.esIE)? d.firstChild.text : d.firstChild.textContent;
				//
			break;
			case "html":
				ficha.innerHTML = req.respuestaXML.firstChild.data;
				contenedorFichas[oClase.idDescarga] = new Object();
				contenedorFichas[oClase.idDescarga].html = req.respuestaXML.firstChild.data;
				oClase.centrarContenido(ficha);
				ficha.style.visibility = "visible";
				mostrarBlockeador(true);
			break;
		}
		
	}.closure(this);
	
	//METs
	var setEle = function(i){
		if(!!oEle) oEle.className = '';
		oEle = $('categoriaDescarga' + i);
		if(oEle)oEle.className = 'noticiasCategoriaOn';
	}

	var setEleFicha = function(i){
		oRec.idRegistro = i;
		if(oEle)oEle.style.display = "block";
		oEle = $('relacionada'+i);
		if(oEle)oEle.style.display = "none";
	}

	this.verLista = function(i, e,pagina){
		setEle(i);
		//
		accion = "categoria";
		if(e == 'init') return true;
		//
		$('categoriaFichaLoader').style.display = 'block';
		var anex = "";
		if(pagina)anex = "&pagina="+pagina;
		req.pedir('requests/cargar.php?idC='+i+anex, 'file|=|descargasCategorias.php|&|');
		//
		if(e) StopEvent(e);
	}
	
	this.verFichaCompleta = function(i,e){
		setEleFicha(i);
		accion = "ficha";
		if(e == 'init') return true;
		//
		$('FichaLoader').style.display = 'block';
		var anex = "";
		req.pedir('requests/cargar.php', 'file|=|../noticias/' + i + 'Ficha.html|&|');
		//
		if(e) StopEvent(e);
	}
	

	this.centrarContenido = function(objeto){
		var pos = getScrollPos();
		var scrn = getBodyWHAvaible();
		var dim = getBodyWHAvaible();
		objeto.style.top = (Math.round((dim.h - objeto.offsetHeight)/2)+pos.y)+"px";
		objeto.style.left = (Math.round((scrn.w - objeto.offsetWidth)/2)+pos.x)+"px";
	}
	var mostrarBlockeador = function(valor){
		if(valor){
			var scrn = getBodyDims();
			var pant = getBodyWHAvaible();
			var dim = getWindowDims();
			var pos = getScrollPos();
			blockeador.style.display = "block";
			if(Nav.esIE)blockeador.style.height = (scrn.h)+"px";
			else blockeador.style.height = (scrn.h+100)+"px";
			blockeador.style.width = dim.w+"px";
		}
		else blockeador.style.display = "none";
	}	
	this.mostrarCuadro = function(idDescarga,e){
		this.idDescarga = idDescarga;
		if(!ficha)ficha = $('descargasFicha');
		if(!blockeador){
			blockeador = $('blockeadorDescarga');
			if(Nav.esIE || Nav.esIE7) blockeador.style.filter = 'alpha(opacity=45)';
			else blockeador.style.MozOpacity = ".45";
		}
		if(!contenedorFichas[idDescarga]){
			accion = "html";
			req.pedir("requests/cargar.php",'file|=|../descargas/'+idDescarga+'Cuadro.html|&|');
			mostrarBlockeador(true);
		}
		else{
			ficha.innerHTML = contenedorFichas[idDescarga].html;
			oClase.centrarContenido(ficha);
			ficha.style.visibility = "visible";
			mostrarBlockeador(true);
		}
		if(e) StopEvent(e);		
	}	
	this.descargar = function(idContenido,e){
		if(!iframe)iframe = $('adjuntoIframe');
		if(Nav.esIE6)document.location = 'requests/descargaForzada.php'+'?idContenido='+idContenido;
		else iframe.src = 'requests/descargaForzada.php'+'?idContenido='+idContenido;
		if(e) StopEvent(e);
	}

	this.cerrarCuadro = function(){
		ficha.style.visibility = "hidden";
		ficha.style.top = "-1000px";
		mostrarBlockeador();
	}
		
}

