// JavaScript Document
var xmlDocFotos;
var arRutaImg;
var arRutaImgMin;
var arRutaAlt;
var iIndexFoto=0;
var iIndexFinal;
var sPathTransparente="transparente.gif";
function importaXMLVisorFotos(fichero,funcion) {
  var mozilla = (typeof document.implementation != 'undefined')
      && (typeof document.implementation.createDocument != 'undefined');
  var ie = (typeof window.ActiveXObject != 'undefined');

  if (mozilla) {
    xmlDocFotos = document.implementation.createDocument("", "", null)
    xmlDocFotos.load(fichero);
    xmlDocFotos.onload = function()
    {
      funcion();
    }
  } else if (ie) {
    xmlDocFotos = new ActiveXObject("Microsoft.XMLDOM");
    xmlDocFotos.async = false;
    xmlDocFotos.load(fichero);
    if (xmlDocFotos.readyState == 4) funcion();
  }
  else {
    alert('Tu navegador no puede manejar este script');
    return;
  }
}

function importaVisorFotosXML(fichero) {
  importaXMLVisorFotos(fichero,PaintDataMenuVisor);
}

//Con el parametro
function importaXMLVisorFotosParam(fichero,funcion,keyAlbum) {
  var mozilla = (typeof document.implementation != 'undefined')
      && (typeof document.implementation.createDocument != 'undefined');
  var ie = (typeof window.ActiveXObject != 'undefined');

  if (mozilla) {
    xmlDocFotos = document.implementation.createDocument("", "", null)
    xmlDocFotos.load(fichero);
    xmlDocFotos.onload = function()
    {
      funcion(keyAlbum);
    }
  } else if (ie) {
    xmlDocFotos = new ActiveXObject("Microsoft.XMLDOM");
    xmlDocFotos.async = false;
    xmlDocFotos.load(fichero);
    if (xmlDocFotos.readyState == 4) funcion(keyAlbum);
  }
  else {
    alert('Tu navegador no puede manejar este script');
    return;
  }
}

function importaVisorFotosXMLParam(fichero,keyAlbum) {
  importaXMLVisorFotosParam(fichero,PaintDataMenuVisorParam,keyAlbum);
}

function PaintDataMenuVisor(){
		try{
			FillComboAlbum(0);
			getFotos(0);
			iIndexFinal=9;
			PaintCatalogo(iIndexFoto,iIndexFinal);
			PaintImgBig(iIndexFoto);
		}catch(err){
			alert(err.message);
		}
	}
//Con el parámetro
function PaintDataMenuVisorParam(keyAlbum){
		try{
			var arSep = keyAlbum.split('%20');
			var sTit='';
			if(arSep.length>1){//Para el caso de Firefox que mete %20 en los espacios en blanco
				for(var i=0;i<arSep.length;i++){
					sTit = sTit + arSep[i] + ' ';
				}
				sTit = sTit.replace(/^\s*|\s*$/g,"");
			}else{
				sTit=keyAlbum;	
			}
			FillComboAlbum(sTit);
			getFotos(sTit);
			iIndexFoto=0;
			iIndexFinal=9;		
			PaintCatalogo(iIndexFoto,iIndexFinal);
			PaintImgBig(iIndexFoto);			
		}catch(err){
			alert(err.message);
		}
	}

//Función que rellena el combo de albumes
function FillComboAlbum(sKey){
		try{
			var arTituloAlbum;
			arTituloAlbum = getAlbums();
			var oCombo = document.getElementById("album");
			for(i=0;i<arTituloAlbum.length;i++){
				oOption = new Option(arTituloAlbum[i][1],arTituloAlbum[i][0]);
				oCombo.options[i]=oOption;
			}
			oCombo.value=sKey;
		}catch(err){
			alert(err.message);
		}
	}	
//Función que obtiene los albumes	
function getAlbums(){
		try{
			var arAlbum = new Array();
			var arDataCombo;
			var arDatos = new Array()
			var arAlbum = xmlDocFotos.getElementsByTagName("Albumes");
			for(i=0;i<arAlbum.length;i++){
				arDataCombo = new Array();
				arDataCombo.push(arAlbum[i].getElementsByTagName("keyAlbum")[0].firstChild.data);
				arDataCombo.push(arAlbum[i].getElementsByTagName("nombreAlbum")[0].firstChild.data);
				arDatos.push(arDataCombo);
			}
			return arDatos;
		}catch(err){
			alert(err.message);
		}
	}
//Función que obtiene las rutas de las imagenes grandes, las pequeñas y los alt	
function getFotos(sKey){
		try{
			arRutaImg = new Array();
			arRutaImgMin = new Array();
			arRutaAlt = new Array();
			var arFotos = new Array();
			arFotos = xmlDocFotos.getElementsByTagName("Albumes");
			var arDataFotosAlbum = new Array();
			arDataFotosAlbum = arFotos[sKey].getElementsByTagName("fotos");		
			for(i=0;i<arDataFotosAlbum.length;i++){
				arRutaImg.push(arDataFotosAlbum[i].getElementsByTagName("rutaFoto")[0].firstChild.data);
				arRutaImgMin.push(arDataFotosAlbum[i].getElementsByTagName("rutaFotoMini")[0].firstChild.data);
				arRutaAlt.push(arDataFotosAlbum[i].getElementsByTagName("tituloFoto")[0].firstChild.data);
			}		
		}catch(err){
			alert(err.message);
		}
	}	
	
//Función que pinta desde el indice inicial 10 fotos o hasta que se se acabe el album	
function PaintCatalogo(iIndex,iIndexFinal){
		try{
			var oImg;
			var sIdImg;
			var numFotos=0;
			var iImage = 0;
			for(i=iIndex;i<=iIndexFinal;i++){
				iImage = parseInt(i)-parseInt(iIndex);
				sIdImg = "img"+iImage;
				oImg =document.getElementById(sIdImg);
				if(arRutaImgMin[i]=='undefined' || arRutaImgMin[i]==null){
					oImg.src = "Fotos/"+sPathTransparente;
					oImg.alt = sPathTransparente;					
				}else{
					oImg.src = "Fotos/"+arRutaImgMin[i];
					oImg.alt = arRutaAlt[i];										
				}
				numFotos ++;
			}
		}catch(err){
			alert(err.message);
		}
	}
	
//Función que pinta en el div principal la foto grande
function PaintImgBig(iIndex){
		try{
			var oImgMain =document.getElementById("imgMain");
			var oTextFoto = document.getElementById("textoFoto");
			oImgMain.src = "Fotos/"+arRutaImg[iIndex];
			var iTitIndex = parseInt(iIndex)+ parseInt(1);
			var iTotal;
			iTotal= parseInt(arRutaAlt.length);					
			oTextFoto.innerHTML=arRutaAlt[iIndex]+" (Foto "+iTitIndex+" de "+iTotal+")";
		}catch(err){
			alert(err.message+"PaintImgBig");
		}
	}
//Función que cambia la foto
function ChangeFoto(idImg){
		try{
			var oImgSelected = document.getElementById(idImg);
			var oImgPrincipal = document.getElementById("imgMain");
			var oTextPrincipal = document.getElementById("textoFoto");
			var arPathCompleto = oImgSelected.src.split("/");
			var sPathFotoMini="";
			//Compruebo que no está vacia la celda
			if(oImgSelected.src.indexOf("transparente.gif")==-1){
				for(i=arPathCompleto.length-2;i<arPathCompleto.length;i++){
					sPathFotoMini = sPathFotoMini+arPathCompleto[i]+"/";
				}
				var iIndFin = parseInt(sPathFotoMini.length) - parseInt(1);
				sPathFotoMini=sPathFotoMini.substr(0,iIndFin);
				var iIndexFotoSelect = CalculateNamePrincipalFoto(sPathFotoMini);
				oImgPrincipal.src="Fotos/"+arRutaImg[iIndexFotoSelect];
				var iTitIndex = parseInt(iIndexFotoSelect)+ parseInt(1);
				var iTotal;
				iTotal= parseInt(arRutaAlt.length);					
				oTextPrincipal.innerHTML=arRutaAlt[iIndexFotoSelect]+" (Foto "+iTitIndex+" de "+iTotal+")";
			}
		}catch(err){
			alert(err.message+"ChangeFoto");
		}
	}
	
//Función que calcula el indice de la foto seleccionada en el Array
function CalculateNamePrincipalFoto(sPathFoto){
		try{
			var iRes = 0;
			for(i=0;i<arRutaImgMin.length;i++){
				if(arRutaImgMin[i]==sPathFoto){
					iRes = i;
					return iRes;
					break;
				}
			}	
		}catch(err){
			alert(err.mesage+"CalculateNamePrincipalFoto");
		}
	}
//Función que va al primero
function GoFirst(){
		try{
			iIndexFoto=0;
			iIndexFinal=9;
			PaintCatalogo(iIndexFoto,iIndexFinal);
			PaintImgBig(iIndexFoto);		
		}catch(err){
			alert(err.message);
		}
	}
//Función que va al Anterior	
function GoBack(){
		try{
			if(parseInt(arRutaImg.length)>parseInt(10)){
				if(parseInt(iIndexFoto)-parseInt(10)>=parseInt(0)){
					iIndexFoto=parseInt(iIndexFoto)-parseInt(10);
					iIndexFinal=parseInt(iIndexFinal)-parseInt(10);
					PaintCatalogo(iIndexFoto,iIndexFinal);
					PaintImgBig(iIndexFoto);							
				}
			}
		}catch(err){
			alert(err.message);
		}
	}		
	

//Función que muestra los diez siguientes
function GoNext(){
		try{
			if(parseInt(arRutaImg.length)>parseInt(10)){
				if(parseInt(iIndexFinal)<parseInt(arRutaImg.length)){
					iIndexFoto=parseInt(iIndexFoto)+parseInt(10);
					iIndexFinal=parseInt(iIndexFinal)+parseInt(10);
					PaintCatalogo(iIndexFoto,iIndexFinal);
					PaintImgBig(iIndexFoto);					
				}
			}			
		}catch(err){
			alert(err.message);
		}
	}	
	
//Función que va al primero
function GoLast(){
		try{
			if(parseInt(arRutaImg.length)>parseInt(10)){
				if(parseInt(iIndexFinal)<parseInt(arRutaImg.length)){
					iIndexFoto=parseInt(iIndexFoto)+parseInt(10);
					iIndexFinal=parseInt(iIndexFinal)+parseInt(10);
					while(parseInt(iIndexFinal)<parseInt(arRutaImg.length)){
						iIndexFoto=parseInt(iIndexFoto)+parseInt(10);
						iIndexFinal=parseInt(iIndexFinal)+parseInt(10);				
					}
					PaintCatalogo(iIndexFoto,iIndexFinal);
					PaintImgBig(iIndexFoto);					
				}
			}						
		}catch(err){
			alert(err.message);
		}
	}
//Función que cambia de Album	
function changeAlbum(){
		try{
			var sAlbum = document.getElementById("album").value;
			FillComboAlbum(sAlbum);
			getFotos(sAlbum);
			iIndexFoto=0;
			iIndexFinal=9;		
			PaintCatalogo(iIndexFoto,iIndexFinal);
			PaintImgBig(iIndexFoto);			
		}catch(err){
			alert(err.message);
		}
		var oSelectAlbum
	}	
