// Global constants
var replacementChar = /%s/
var timeoutID
var playing

// Global variables
var outputTitle
var outputSubTitle
var outputURL
var imagesOnAPage
var index
var NomImage
var StateVisor
var Hoja
var Programa
var Pagina
var DirFotos
var DirFotos2
var resolucion = screen.width
var HojaProg = [1,1,1,1,1]
var HojaAbierta = [0,0,0,0,0]
var SWCALENDAR
var Idioma = 1
var LinDesc = new Array("","","")
var tmp
var tmp2
var numImagenes = 0
var ImgLoad = 0

var LinContacto = new Array(
    "<font size='7'>Página de Contacto<br></font>",
    "<font size='5'><br>Teléfono --> (+34) 654 45 80 88<br> ",
    "<br>E-Mail : <a href='javascript: mailpage()'><font color='#333399'>npastorm@yahoo.es</a></font></font>"
)

//==========================================================================================

function Zona_Estadist(Pag,Zon){
//	WEBO_ZONE=Pag;
//	WEBO_PAGE=Zon;
//    <!--INICIO WEBOSCOPE CASA NACHO --> <!-- SOLO MODIFICAR WEBO_ZONE Y WEBO_PAGE--> 
//    <a href="http://www.weboscope.com/?LANGUAGE=ES"><!-- FIN WEBOSCOPE COPYRIGHT WEBORAMA--></a> 
//    <script language="javascript">
//WEBO_ZONE=1;
//WEBO_PAGE=1;
//weboscope_ok=0;
//</script>
//    <script language="javascript" src="http://es.weborama.fr/scripts/weboscope_free_es.js"></script>
//    <script>
//if(weboscope_ok==1){weboscope_free(WEBO_ZONE,WEBO_PAGE,124656);}
//</script>
//    <a href="http://www.weboscope.com/?LANGUAGE=ES">Weboscope medición de audiencia 
//    y de eficacia</a> <a href="http://www.weboscope.com/free/?LANGUAGE=ES">Estadísticas 
//    gratuitas</a>
// </div>

}

function AbrirPagina(modo) {
   HIDE("HOJA1_div")
   HIDE("HOJA3_div")
	DisplayTable()
//        DisplayNavegador(1)
        DisplayVisor()
}

function InicializaPage(NumHoja) {

   DirFotos="VillaNacho"
   DirFotos2="."
   DirFotos2="VillaNacho"
   SWCALENDAR = 0

   tmp = location.href.split("?")

   if (tmp[1] != null) {
           Idioma=1; 
           if (tmp[2] != null) {PonIdioma()}
           DirFotos = (tmp[1]);
           DirFotos2 = DirFotos 
   }
}

function PonIdioma() {
      Idioma=2
      tmp2 = tmp[2].split('lg')
      Idioma = parseInt(tmp2[1]) 
//    if (tmp[2] == 'lg2') {Idioma=2)};
//    if (tmp[2] = 'lg3') {Idioma=3)};
}

   
function PrintFrame() {
  document.write('<iframe frameborder=no height="100%" width="100%" marginheight=0 marginwidth=0 name=menu scrolling=no src="menu.htm?' + DirFotos2 + '"> </iframe>')
}

function DisplayCabecera(NumHoja) {
   Hoja = NumHoja
   Programa = HojaProg[Hoja-1]
//   printBodyStartTag()
   init()
//   DisplayBarra(0)
//   DisplayTitulo(0)
//   DisplaySubTitulo(0)

   if (resolucion == 800) { 
//     desp==0
//     if (document.all) {PAGINA.style.pixelLeft=desp}; 
//     else {document.PAGINA.left=desp}
   } 

   document.images["Foto00"].src= DirFotos2 + "/grande/Foto_0000.jpg"
   document.images["Foto99"].src= DirFotos2 + "/grande/Foto_0099.jpg"
   AbrirPagina(0)
   if (SWCALENDAR == 1) {
     DisplayCalendar()
     HIDE("HOJA4_div")
   }
   Hoja=0
//   ABRE_HOJA(6)
   ABRE_HOJA(1)
}

function CambiaProg(NewProg) {
   if (NewProg == 1) { Pagina = "index.htm?"+Hoja}
   if (NewProg == 2) { Pagina = "GaleriaFotos.htm"}
   if (NewProg == 3) { Pagina = "Calendar.htm"}
   window.location.href = Pagina
}

function Activa_HOJA(modo) {
   NO('Boton1')
   NO('Boton2')
   NO('Boton3')
   if (SWCALENDAR == 0) {
      HIDE('Boton4')
      HIDE('Boton5')
      HIDE('Boton6')
   }
//   NO('Boton7')

   if (modo == 1) { SI('Boton1')}
   if (modo == 2) { SI('Boton2')}
   if (modo == 3) { SI('Boton3')}
   if (modo == 4) { SI('Boton4')}
   if (modo == 5) { SI('Boton5')}   
   if (modo == 6) { SI('Boton6')}   
//   if (modo == 7) { SI('Boton7')}    
}

function ABRE_HOJA(modo) {
   Zona_Estadist(1,modo)
   Activa_HOJA(modo);

   if (modo != 6) { 
      if (Hoja == 1) { HIDE("HOJA1_div")}
      if (Hoja == 2) { HIDE("HOJA2_div")}
      if (Hoja == 3) { HIDE("HOJA3_div")}
      if (Hoja == 4) { HIDE("HOJA4_div")}
      Hoja=modo   
   }
 
//   if (HojaProg[Hoja-1] != Programa) { CambiaProg(HojaProg[Hoja-1])}

   if (Hoja == 2)
                {if (HojaAbierta[Hoja-1]!=1) {CargarImagenes()}}
   if (Hoja != 2) { if (HojaAbierta[1]==1) { HIDE_VISOR()}}

   if (Hoja == 1) { SHOW("HOJA1_div")}
   if (Hoja == 2) { SHOW("HOJA2_div")}
   if (Hoja == 3) { SHOW("HOJA3_div")}
   if (Hoja == 4) { SHOW("HOJA4_div")}
 
//   if (modo == 6) { if (SwOferta == 1) { openWin(0)}}
   if (modo == 6) { if (SwOferta == 1) {MM_OpenWindow(DirFotos2+'/ofertas.htm','Ventanuco','alwaysRaised=1,resizable=1,scrollbars=0')}}

   if (modo != 6) { HojaAbierta[Hoja-1]=1}
}

function SHOW(divName){
      if (document.layers) {document.layers[divName].visibility='visible'}
       else{document.getElementById(divName).style.visibility='visible'}
}

function HIDE(divName) {
      if (document.layers) {document.layers[divName].visibility='hidden'}
       else{document.getElementById(divName).style.visibility='hidden'}
}

function changeBg(layername) { 
   if (document.all) document.all(layername).style.backgroundColor="#ff0000"; 
   else if (document.layers) document.layers[layername].bgColor='red' 
} 

function SI(layername) { 
   if (document.all) document.all(layername).style.background='url(./graficos/nav_activa.gif)'; 
   else if (document.layers) document.layers[layername].background.src='./graficos/nav_activa.gif'; 
} 

function NO(layername) { 
   if (document.all) document.all(layername).style.background='url(./graficos/nav_bg.gif)'; 
   else if (document.layers) document.layers[layername].background.src='./graficos/nav_bg.gif'; 
} 

function CambiarImagen(In) {
        document.images["VISOR_im"].src= DirFotos2 + "/grande/" + imageDB[In]
//        if (NomImage2.width >> 600)
               { VISOR_im.style.width=650}
}

function ABRE_VISOR(In) {
     if (numImagenes==ImgLoad)
     { 
        index=In
        try{       
          CambiarImagen(In)}
        finally{
          SHOW("VISOR")
          StateVisor=1
        }
        HIDE('HOJA2_div')
      }               
}

function CERRAR_VISOR() {
        SHOW('HOJA2_div')    
        HIDE("VISOR")
        clearTimeout(timeoutID)
        StateVisor=0    
}

function HIDE_VISOR() {
        HIDE("VISOR")
        clearTimeout(timeoutID)
        StateVisor=0    
}

function Play_VISOR() {
	if (playing == 1) {
	   playing = 0
	   clearTimeout(timeoutID)
           CERRAR_VISOR()
        }        
        else {
           playing = 1
           index = 0
           ABRE_VISOR(index)
           timeoutID = setInterval("Sig_Imagen(1)", 3000)
        }
}

function Sig_Imagen(Inc) {
	if (index >= imageDB.length) {
              index = 0
	}

	index = index + Inc
        CambiarImagen(index)
}


function init() {
	// Make output strings from theme.js and data.js
        outputTitle = themeTitle.replace(replacementChar, title);
	outputSubTitle = themeSubTitle.replace(replacementChar, subTitle);
	outputURL = themeURL.replace(replacementChar, URL);
        outputURL = outputURL.replace(replacementChar, URL);

	// Perform some initial calculations
	imagesOnAPage = rows * columns
	
	index = 0
}

function DisplayBarra(writePageTitle) {
//        document.write("<DIV CLASS=\"Barra1\" style=\"position:absolute; width:100%; height:15px; z-index:84;background: #CCCCCC; left: 0px; top: 0px\">")
	document.write("</DIV>")
        document.write("<DIV CLASS=\"Barra2\" style=\"position:absolute; width:100%; height:15px; z-index:94;background: #CCCCCC; left: 0px; top: 70px\">")
	document.write("</DIV>")
}

function DisplayTitulo(writePageTitle) {
	document.write("<DIV CLASS=\"title_div\" style=\"position:absolute; background: #999999; width:100%; height:60px; z-index:1; left: 0px; top: 0px\">")
	document.write(outputTitle)
	document.write("</DIV>")
}

function DisplaySubTitulo() {
        document.write('<div align="center"><font size="6" color="#CC9933" face="Georgia, Times New Roman, Times, serif"><font color="#003399">' + subTitle + '</font></font></div>')
}

function PrintDescrip() {
      if (Idioma==1) {LinDesc=DescEsp};
      if (Idioma==2) {LinDesc=DescIng};

      for (var i = 0; i < LinDesc.length; i++) { 
           document.write(LinDesc[i])
         }        
}

function PrintContacto() {
	for (var i = 0; i < LinContacto.length; i++) { 
           document.write(LinContacto[i])
      }        
}

function DisplayVisor() {
	document.write("<DIV id=\"VISOR\" style=\"position:absolute; width:640px; height:500px; z-index:3; left: 0px; top: 0px; background: #CCCCCC; visibility: hidden\">")
        DisplayNavegador(1);
//        document.write("<DIV style=\"position:absolute; width:605px; height:500px; z-index:4; left: 80px; top: 50px; background: #CCCCCC;>")
	document.write("<SPAN CLASS=\"pageFont\">")
        document.write(themeVisor)
	document.write("</SPAN>")
	document.write("</DIV>")
//	document.write("</DIV>")
}

function PrintHOJA1() {
  document.write("<div id='HOJA1_div' style='position:absolute; width:640px; height:570px; z-index:22; left: 5px; top: 5px; background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px none #000000'>") 
  document.write("<h1><font size='3' color='#FFCCCC'><font color='#333399'><i>"); 
  PrintDescrip()
  document.write("</i></font></font></h1>");
  document.write("<img name='Foto00' src='grande/Foto_0000.jpg' border=0 width='640' height='394'>");
  document.write("</div>");
}

function PrintHOJA3() {
  document.write("<div id='HOJA3_div' style='position:absolute; width:640px; height:570px; z-index:22; left: 5px; top: 5px; background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px none #000000'>") 
  document.write("<p align='center'>");
  document.write("<font size='6' color='#FFCCCC'><font color='#333399'><b>"); 
  PrintContacto()
  document.write("</b></font></font></p>");
  document.write("<img name='Foto99' src='grande/Foto_0099.jpg' border=0 width='640' height='394'>");
  document.write("</div>");
}

function printImage() {
	document.write("<DIV CLASS=\"image_div\">")
	document.write("<TABLE>")
	document.write("<TD>")
	document.write("<TR valign=middle>")
	document.write("<TD align=center>")
	outputImageLink = themeImageLink.replace(replacementChar, index);
	outputImageLink = outputImageLink.replace(replacementChar, playing);
	document.write(outputImageLink)
	document.write("</TD>")
	document.write("</TR>")
   for (var i = index*6; i < index*6+6; i++) {
	   document.write("<TR valign=middle>")
	   document.write("<TD CLASS=\"imageFont\" align=center>" + imageMetadataDB[i] + "</TD>")
	   document.write("</TR>")
   }
	document.write("</TD>")
	document.write("</TABLE>");
	document.write("</DIV>")
}

function DisplayNavegador(writePageLinks) {

	var themePreviousHolder = themeImageLinkPrevious
	var themeNextHolder = themeImageLinkNext
	var themePlayHolder = themeImageLinkPlay
	var themeStopHolder = themeImageLinkStop
	if (writePageLinks == 1)
	{
		themePreviousHolder = themeThumbLinkPrevious
		themeNextHolder = themeThumbLinkNext
		themePlayHolder = themeThumbLinkPlay
		themeStopHolder = themeThumbLinkStop
	}

	document.write("<DIV CLASS=\"links_div\" style=\"position:absolute; width:140px; height:60px; z-index:5; left: 500px; top: 5px\">")

	document.write("<TABLE>")
	document.write("<TR>")

	// Write URL
//	document.write("<TD CLASS=\"pageFont\">")
//	document.write("<DIV CLASS=\"url_div\">")
//	document.write(outputURL)
//	document.write("</DIV>")
//	document.write("</TD>")

	// Write previous link
	document.write("<TD CLASS=\"pageFont\">")
	document.write("<DIV CLASS=\"previous_div\">")
        writePageLinks=1
        index=1
	if (index != 0) {
		var previousIndex
		if (writePageLinks == 0) {
			previousIndex = index - 1;
			outputPreviousLink = themePreviousHolder.replace(replacementChar, previousIndex);
			outputPreviousLink = outputPreviousLink.replace(replacementChar, playing);
			document.write(outputPreviousLink)
		}
		else {
			if (index != 0) {
				previousIndex = index-imagesOnAPage;
				if (previousIndex < 0) {
					previousIndex = 0	  		
				}
				outputPreviousLink = themePreviousHolder.replace(replacementChar, previousIndex);
				outputPreviousLink = outputPreviousLink.replace(replacementChar, playing);
				document.write(outputPreviousLink)
			}
		}
	}
	document.write("</DIV>")
	document.write("</TD>")
	
	// Write next link
	document.write("<TD CLASS=\"pageFont\">")
	document.write("<DIV CLASS=\"next_div\">")
	var nextIndex
        writePageLinks=0
//        index=0
	if (writePageLinks == 0)
	{
		nextIndex = index + 1;
		if (nextIndex < imageDB.length) {
			outputNextLink = themeNextHolder.replace(replacementChar, nextIndex);
			outputNextLink = outputNextLink.replace(replacementChar, playing);
			document.write(outputNextLink)
		}
	}
	else {
	    nextIndex = index+imagesOnAPage;
		if (nextIndex < thumbDB.length) {
			outputNextLink = themeNextHolder.replace(replacementChar, nextIndex);
			outputNextLink = outputNextLink.replace(replacementChar, playing);
			document.write(outputNextLink)
		}
	}
	document.write("</DIV>")
	document.write("</TD>")


	// Write play/stop
	document.write("<TD CLASS=\"pageFont\">")
	document.write("<DIV CLASS=\"slideShowControl_div\">")
//	if (playing == 1) {
//		document.write(themeStopHolder)
//	}
//	else {
		document.write(themePlayHolder)
//	}
	document.write("</DIV>")
	document.write("</TD>")

	document.write("</TR>")
	document.write("</TABLE>")

	document.write("</DIV>")
}

function DisplayTable() {
        document.write("<DIV id=\"HOJA2_div\" style=\"position:absolute; width:640px; height:580px; z-index:4; left: 0px; top: 0px;  background: #CCCCCC; visibility: hidden\">")
//        document.write("<DIV id=\"FOTOS_div\" style=\"position:absolute; width:785px; height:620px; z-index:4; left: 10px; top: 10px>")
	document.write("<TABLE border :1px; ; Valign \"middle\">")
	counter = index
	for (var i = 0; i < rows; i++) {
		document.write("<TR>")
		for (var j = 0; j < columns; j++) {
			if (counter < thumbDB.length) {
				document.write("<TD>")
                                outputThumbLink = themeThumbLink.replace(replacementChar, i*columns+j)
                                outputThumbLink = outputThumbLink.replace(replacementChar, i*columns+j)
				document.write(outputThumbLink)
  				counter++

//				document.write("</TD></TR>")
				
				//Write out the metadata
//				//for (var k = counter*6; k < counter*6+6; k++) {
//				//   document.write("<TR valign=bottom>")
//				//   document.write("<TD CLASS=\"outer\" align=center onclick=\"ABRE_VISOR(0)\">Ver</TD>")
//				//   document.write("<TD CLASS=\"thumbFont\" align=center>" + thumbMetadataDB[k] + "</TD>")
//				//   document.write("</TR>")
//			
				
//				document.write("</TABLE>");
				document.write("</TD>")
			}
		}
		document.write("</TR>")
	}
	document.write("</TABLE>")
//	document.write("</DIV>")
        document.write("</DIV>")
}



function LoadImages() {
	for (var i = 0; i < imagesOnAPage; i++) {
		if ((i+index) < thumbDB.length) {
			var j = 0
			while (document.images[j] != null && document.images[j].name == "IGNORE") {
				j++
			}
			if (document.images[i+j] != null) {
				document.images[i+j].src = DirFotos2 + "/" + thumbDB[i+index]
			}
		}
	}
}

function CargarImagenes() {
//        SHOW('precarga')
        index=0
	for (var i = 0; i < imagesOnAPage; i++) {
		if ((i) < thumbDB.length) {
			var j = 0
			while (document.images[j] != null && document.images[j].name != "GaleriaFotos") {
			j++
			}
			if (document.images[i+j] != null) {
				document.images[i+j].src = DirFotos2 + "/" + thumbDB[i];
                                document.images[i+j].onLoad=ImgonLoad();
                                ImgLoad++;
			}
		}
	}      
//        HIDE('precarga')
        HojaAbierta[1]=1
}

function ImgonLoad( )
{
   numImagenes++
}

function CargarImagen() {
   var i = 0
   while (document.images[i].name == "IGNORE") {
      i++
   }
	document.images[i].src = DirFotos2 + "/grande/" + imageDB[index]
}
