// JavaScript Document
var arrSe = new Array();
arrSe['productosFicha'] = "productos";
arrSe['home'] = "productos";
arrSe[''] = "productos";
var Galeria = function(contenedorGaleria,se,idElemento){
	var oCl = this;
	var contGaleria = document.getElementById(contenedorGaleria);
	if(contGaleria)contGaleria.style.display = "none";
	this.req = new Request(oCl);	
	ERROR.alerta = false;
	this.accion = "obtenerCuadro";
	this.directorio = arrSe[se];
	this.idElemento = idElemento;
	var posicion = 1;
	var contPrincipal = document.getElementById("contenedorPrincipal");
	if(this.directorio != "programas")posicion = 0;
	var archivo = this.directorio+"/"+this.idElemento+"Galeria"+posicion+".xml";
	this.arreglo = new Array();
	this.arregloMultiple = new Array();
	this.arregloActual = new Array();
	this.fotoActual = "";
	
	this.foto = "";

	var cerrarVista = function(){
		oCl.foto.style.visibility = "hidden";
		oCl.blockeador.style.display = "none";
		oCl.foto.style.top = "-1000px";

	}
	this.iniciar = function(){
		this.foto = document.getElementById("popupGal");
		if(this.foto){
			this.foto.style.visibility = "visible";
			this.imagen = document.getElementById("imagenGrande");
			this.loading = document.getElementById("leyendoImagen");
			this.anterior = document.getElementById("anteriorGaleria");
			this.siguiente = document.getElementById("siguienteGaleria");
			var cerrar = document.getElementById("cerrarGaleria");
			AddEvent(cerrar,'click',cerrarVista);
			this.foto.style.width = "400px";
			this.contImag = document.getElementById("contImag");
			this.popupFotoGal = document.getElementById("popupFotoGal");
			this.contFlecha = document.getElementById("contFlecha");
			this.epigrafe = document.getElementById("epigrafe");
			this.blockeador = document.createElement("div");
			this.blockeador.className = "blockeadorGaleria";
			this.anteriorGris = document.getElementById("anteriorGrisGal");
			this.siguienteGris = document.getElementById("siguienteGrisGal");

			if(Nav.esIE || Nav.esIE7) this.blockeador.style.filter = 'alpha(opacity=45)';
			else this.blockeador.style.MozOpacity = ".45";

			AddEvent(oCl.anterior,'click',function(){cambiarFoto(-1)});
			AddEvent(oCl.siguiente,'click',function(){cambiarFoto(1)});					
			AddEvent(oCl.imagen,'load',function(){cargaFinalizada()});
			this.centrarContenido();
			cerrarVista();
			this.crearGaleria(1);		
			
		}
	}
	var crearObje
	this.onRequestLoad = function(){
		switch(this.accion){
			case "armarGaleria":
				var nodo = this.req.respuestaXML;
				if(nodo && nodo.childNodes.length >0){
					if(contGaleria){
						contGaleria.innerHTML ="";
						var prev = obtenerHijo(nodo,0);
						var imag = obtenerHijo(prev,1);	
						for(i=0;i<imag.childNodes.length;i++){
							if(imag.childNodes[i].tagName)crearCuadro(imag.childNodes[i]);
						}
						var sep = document.createElement("div");
						sep.className = "floatFix";
						sep.style.border = "0";
						contGaleria.appendChild(sep);
						contGaleria.style.display = "block";
					}
				}
				controlAltosDisparado("derecha,izquierda",",");
			break;
			case "armarGaleriaMultiple":
				var nodo = this.req.respuestaXML;
				if(nodo && nodo.childNodes.length >0){
					var prev = obtenerHijo(nodo,0);
					var imag = obtenerHijo(prev,1);	
					oCl.arregloMultiple[this.idRegistro] = new Array();
					for(i=0;i<imag.childNodes.length;i++){
						if(imag.childNodes[i].tagName){
							var  ob = new Object();		
							ob.w = imag.childNodes[i].getAttribute("wF");
							if(parseInt(ob.w)<240)ob.w = 240;
							ob.h = imag.childNodes[i].getAttribute("hF");
							ob.grande = imag.childNodes[i].getAttribute("f");
							ob.epigrafe = obtenerHijo(imag.childNodes[i],0).data//objeto.firstChild.data;
							oCl.arregloMultiple[this.idRegistro][oCl.arregloMultiple[this.idRegistro].length] = ob;
							oCl.arregloMultiple[this.idRegistro][oCl.arregloMultiple[this.idRegistro].length-1].indice = oCl.arregloMultiple[this.idRegistro].length-1;
						}
					}
				}
	
				mostrarFoto(oCl.arregloMultiple[this.idRegistro][this.indice],this.idRegistro);
			break;
		}
	}
	var obtenerHijo = function(objeto,indice){
		var hijo = null;
		var descontar = indice;
		for(k=0;k<objeto.childNodes.length;k++){
			if((objeto.childNodes[k].tagName) || (objeto.childNodes[k].nodeName == "#cdata-section"))descontar--;
			if(descontar < 0){
				hijo = new Object();
				hijo = objeto.childNodes[k];
				return hijo;
			}
		}
		return hijo;
	}

	var cambiarFoto = function(valor){
		var ind = parseInt(oCl.fotoActual)+(valor);
		if(oCl.idRegistro,oCl.idRegistro>0)mostrarFoto(oCl.arregloActual[ind],oCl.idRegistro);
		else mostrarFoto(oCl.arregloActual[ind]);
	}

	this.galeriaMultiple = function(idRegistro,indice){
		this.idRegistro = idRegistro;
		this.indice = indice;
		if(!this.arregloMultiple[idRegistro]){
			this.accion = "armarGaleriaMultiple";
			var arch = this.directorio+"/"+this.idRegistro+"Galeria0.xml";
			this.req.pedir("xmlHttpRequest/cargador_xml.php?file=../"+arch, null, 'GET');
		}
		else{
			mostrarFoto(this.arregloMultiple[idRegistro][indice],idRegistro);	
		}
	}

	var mostrarFoto = function(objeto,idRegistro){
		if(idRegistro){
			var arreg = oCl.arregloMultiple[idRegistro];
			oCl.arregloActual = oCl.arregloMultiple[idRegistro];
		}
		else{
			this.idRegistro = 0;
			this.indice = 0;
			arreg = oCl.arreglo;
			oCl.arregloActual = oCl.arreglo; 
		}
		var scrn = getBodyDims();
		document.body.appendChild(oCl.blockeador);
		oCl.blockeador.style.display = "block";
		oCl.anterior.style.display = "none";
		oCl.siguiente.style.display = "none";
		oCl.contFlecha.style.display = "none";
		oCl.contImag.style.display = "none";
		oCl.loading.style.display = "block";
		
		oCl.blockeador.style.height = (scrn.h)+"px";
		oCl.blockeador.style.width = scrn.w+"px";
		oCl.popupFotoGal.style.height = (parseInt(objeto.h)+80)+"px";
		oCl.popupFotoGal.style.width = (parseInt(objeto.w)+10)+"px";
		oCl.foto.style.height = (parseInt(objeto.h)+80)+"px";
		oCl.foto.style.width = (parseInt(objeto.w)+10)+"px";
		oCl.imagen.src = objeto.grande;
		oCl.imagen.alt = objeto.epigrafe;
		oCl.loading.style.top = (Math.round((oCl.foto.offsetHeight - oCl.loading.offsetHeight)/2))+"px";
		oCl.loading.style.left = (Math.round((oCl.foto.offsetWidth - oCl.loading.offsetWidth)/2))+"px";
		oCl.epigrafe.innerHTML = objeto.epigrafe;
		

		
		oCl.centrarContenido();
		if(objeto.indice == arreg.length-1){
			oCl.siguiente.style.display = "none";
			oCl.siguienteGris.style.display = "";
		}
		else{
			oCl.siguiente.style.display = "";
			oCl.siguienteGris.style.display = "none";
		}
		if(objeto.indice == 0){
			oCl.anterior.style.display = "none";
			oCl.anteriorGris.style.display = "";
		}
		else{
			oCl.anterior.style.display = "";
			oCl.anteriorGris.style.display = "none";
		}
		oCl.epigrafe.style.display = "none";
		oCl.foto.style.visibility = "visible";
		if((objeto.indice == oCl.fotoActual) && ((typeof oCl.fotoActual) == "number"))cargaFinalizada(arreg);
		oCl.fotoActual = objeto.indice;
	}
	var cargaFinalizada = function(){
		oCl.loading.style.display = "none";
		if(oCl.epigrafe.innerHTML != "")oCl.epigrafe.style.display = "block";
		oCl.contImag.style.display = "block";	
		oCl.popupFotoGal.style.height = "auto";
		if(oCl.arregloActual.length > 1)oCl.contFlecha.style.display = "block";
		oCl.foto.style.height = oCl.popupFotoGal.offsetHeight+"px";
	}
	var crearCuadro = function(objeto){
		var div = document.createElement("img");		
		div.className = "fotoMini";
		div.onmouseover = function(){div.className="fotoMiniOn";};
		div.onmouseout = function(){div.className="fotoMini";};		
		div.src = objeto.getAttribute("m");
		div.w = objeto.getAttribute("wF");
		if(parseInt(div.w)<236)div.w = 236;

		div.h = objeto.getAttribute("hF");		
		div.grande = objeto.getAttribute("f");
		div.onclick = function(){mostrarFoto(div)};
		div.epigrafe = obtenerHijo(objeto,0).data//objeto.firstChild.data;
		oCl.arreglo[oCl.arreglo.length] = div;
		oCl.arreglo[oCl.arreglo.length-1].indice = oCl.arreglo.length-1;
		contGaleria.appendChild(div);
	}
	this.centrarContenido = function(){
		var pos = getScrollPos();
		var scrn = getBodyWHAvaible();
		var dim = getBodyDims();
		this.foto.style.top = (Math.round((scrn.h - this.foto.offsetHeight)/2)+pos.y)+"px";
		this.foto.style.left = (Math.round((scrn.w - this.foto.offsetWidth)/2)+pos.x)+"px";
	}
	this.crearGaleria = function(posicion){
		this.accion = "armarGaleria";
		this.req.pedir("xmlHttpRequest/cargador_xml.php?file=../"+archivo, null, 'GET');
		
	}

	this.iniciar();
	
}