GALERIAFOTOS = {
	xmldoc : null, /* Xml con los datos de la galería */
	oGaleria : null, /* Raiz del html de la galería */
	oBotonera : null, /* Raiz del html de la botonera */
	oVisor : null, /* Raiz del html donde va la foto */
	oTitular : null, /* Raiz del html donde va el titular */
	oDesarrollo : null, /* Raiz del html donde va la el texto desarrollo */
	iPosicionActual : null, /* Foto en la que nos encontramos */
	inicio : function() {
		GALERIAFOTOS.oGaleria = document.getElementById("alojaGaleria");
		if (GALERIAFOTOS.oGaleria) {
			GALERIAFOTOS.oVisor = document.getElementById("alojaVisor");
			GALERIAFOTOS.oTitular = document.getElementById("tituloVisor");
			GALERIAFOTOS.oDesarrollo = document.getElementById("textoVisor");
			GALERIAFOTOS.oBotonera = document.getElementById("alojaBotonera");

			var cod = GALERIAFOTOS.oGaleria.className.substr(16);
			//alert("Hola: "+cod);
			//alert(document.getElementById("datosGaleria").href);


			// Recuperar los datos y cuando se hayan cargado asignar los eventos a los botones
			//GALERIAFOTOS.makeRequest ('/codigo/especiales/galeriaXml.asp?id='+cod);
			GALERIAFOTOS.makeRequest (document.getElementById("datosGaleria").href);
		}
		else
			alert ("no hay galería");
	},
	makeRequest : function(url)  {

        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() { GALERIAFOTOS.alertContents(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);

    },
    alertContents : function (http_request) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	GALERIAFOTOS.xmldoc = http_request.responseXML;
            	var parseError = (!GALERIAFOTOS.xmldoc.documentElement || GALERIAFOTOS.xmldoc.documentElement.tagName == "parsererror")
            	if (!parseError)
            		//Asignar eventos a botones
								GALERIAFOTOS.ponEventoBotones(GALERIAFOTOS.oBotonera);

            	//alert(GALERIAFOTOS.xmldoc.documentElement.textContent);
              //alert(http_request.responseText);

            } else {
                alert('Problemas con la llamada para recuperar los datos.');
            }
        }

    },
    ponEventoBotones : function(oBotonera) {

    	var oEnlaces = oBotonera.getElementsByTagName("A");
    	var pos = 0;
    	for (n=0; n < oEnlaces.length; n++) {
    		/* Ver que no se trata del enlace anterior o siguiente */

    		switch (oEnlaces.item(n).className) {
    			case 'adelante':
    				//printfire ('encontrado anterior');
    				oEnlaces.item(n).onclick = GALERIAFOTOS.cambiaFotoSiguiente;
    				break;
    			case 'atras':
    			  //printfire ('encontrado siguiente');
    				oEnlaces.item(n).onclick = GALERIAFOTOS.cambiaFotoAnterior;
    				break;
    			default:
		    		oEnlaces.item(n).fotoPosicion = pos;
		    		oEnlaces.item(n).onclick = GALERIAFOTOS.cambiaFoto;
		    		pos++;
	    	}
    	}
    	//alert ("eventos en "+GALERIAFOTOS.oBotonera.tagName + oEnlaces.length);

    },
    cambiaFoto : function () {
    	// Recupera la posición de la foto
    	return GALERIAFOTOS.cambiaFotoDePosicion(this.fotoPosicion);
    },
    cambiaFotoAnterior : function () {
    	return GALERIAFOTOS.cambiaFotoDePosicion(GALERIAFOTOS.posicionActualFoto() - 1);
    },
    cambiaFotoSiguiente : function () {
    	return GALERIAFOTOS.cambiaFotoDePosicion(GALERIAFOTOS.posicionActualFoto() + 1);
    },
    cambiaFotoDePosicion : function (iPosicion) {
    	oDatosFoto = GALERIAFOTOS.getFoto (iPosicion);
			if (oDatosFoto != null) { //Si no nos hemos salido de la colección de fotos
	    	GALERIAFOTOS.iPosicionActual = iPosicion; // actualizamos la nueva posición

				/* Imagen */
	    	oImg = oDatosFoto.getElementsByTagName("img").item(0).cloneNode(true);
	    	oImgDest = document.createElement("img");

				for (var i = 0; i < oImg.attributes.length; i++) {
					// Copiar los atributos
				  //printfire(oImg.attributes[i].nodeName + ": "+ oImg.attributes[i].nodeValue);
					if (oImg.attributes[i])
				  	oImgDest.setAttribute(oImg.attributes[i].nodeName, oImg.attributes[i].nodeValue);
				 }

	    	oImgOrigen = GALERIAFOTOS.oVisor.getElementsByTagName("img").item(0);
	    	GALERIAFOTOS.oVisor.replaceChild(oImgDest, oImgOrigen);

	    	/* Títular */
    		if (oDatosFoto.getElementsByTagName("titular").item(0).firstChild) {
    			/* Si tiene titular */
		    	oTitularDest = oDatosFoto.getElementsByTagName("titular").item(0).firstChild.cloneNode(1);
		    	oTitularOrigen = GALERIAFOTOS.oTitular.firstChild;

					// Vaciamos el texto original y le añadimos el nuevo
					for (var i = 0; i < GALERIAFOTOS.oTitular.childNodes.length; i++)
						GALERIAFOTOS.oTitular.removeChild(GALERIAFOTOS.oTitular.childNodes.item(i));

					//GALERIAFOTOS.oTitular.appendChild(document.createTextNode(oTitularDest.nodeValue));
					GALERIAFOTOS.oTitular.innerHTML = oTitularDest.nodeValue;
				}

				/* Desarrollo */
    		if (oDatosFoto.getElementsByTagName("desarrollo").item(0).firstChild) {
    			/* Si tiene desarrollo */

		    	oDesarrolloDest = oDatosFoto.getElementsByTagName("desarrollo").item(0).firstChild.cloneNode(1);
		    	oDesarrolloOrigen = GALERIAFOTOS.oDesarrollo.firstChild;

					// Vaciamos el texto original y le añadimos el nuevo
					for (var i = 0; i < GALERIAFOTOS.oDesarrollo.childNodes.length; i++)
						GALERIAFOTOS.oDesarrollo.removeChild(GALERIAFOTOS.oDesarrollo.childNodes.item(i));

					GALERIAFOTOS.oDesarrollo.innerHTML = oDesarrolloDest.nodeValue;

					/* Estilos botonera */
					GALERIAFOTOS.ponEstilosBotonera ();
				}
	 		}

   		return false;
    },
    getFoto : function(iPosicion) {
    	return (GALERIAFOTOS.xmldoc.getElementsByTagName("foto").item(iPosicion));
    },
    posicionActualFoto : function() {
    	// Si es la primera vez calculamos en que posición cae la foto que se está viendo
    	if (GALERIAFOTOS.iPosicionActual == null) {
				var urlFoto = ''; // Url de la foto actual, a partir de esta sabremos a donde apuntan los botones anterior y siguiente
	    	urlFoto = GALERIAFOTOS.oVisor.getElementsByTagName("img").item(0).getAttribute("src"); // Sacamos la url de la foto actual

	    	var imagenes = GALERIAFOTOS.xmldoc.getElementsByTagName("img");
	    	for (n=0; n < imagenes.length && imagenes.item(n).getAttribute("src") != urlFoto; n++) {
  	  		//printfire (imagenes.item(n).getAttribute("src") == urlFoto);
    		}
				GALERIAFOTOS.iPosicionActual = n;
	    }

	    return (GALERIAFOTOS.iPosicionActual);

    },
    ponEstilosBotonera : function() {
    	// Pone los estilos de la botoner acordes a la imagen seleccionada */
    	var oEnlaces = GALERIAFOTOS.oBotonera.getElementsByTagName("A");
    	for (n=1; n < oEnlaces.length-1; n++) {
    		if (oEnlaces.item(n).fotoPosicion && oEnlaces.item(n).fotoPosicion == GALERIAFOTOS.iPosicionActual)
    			oEnlaces.item(n).className = "numeroFotoActual";
    		else
    			oEnlaces.item(n).className = "numeroFoto";

    	}

    }
}


EXTRAS.addEvent(window, 'load', GALERIAFOTOS.inicio, false);
