var misFotos = new Array ();
var intval="";

function Foto( grande, peque, ruta) {
	this.grande = grande ;
	this.peque = peque ;
	this.ruta = ruta ;
}


function Galeria()
{
    timeOut = 7000;
    this.URL = "/es/procesador_ajax.php";
	this.idHotel = 0;
    this.fotos;
	this.indices;
	var foto_actual;
	var lista_imagenes = new Array();
	

	this.init = function (idhotel, fotos){
		
			
			//Logica de inciio de la galeria
			var param= "funcion=xml_fotos&idhotel=" + idhotel + "&fotos=" + fotos ;
			var ajaxRequest = new Ajax.Request(
						this.URL,
						{
							method: 'get',
							parameters: param,
							asynchronous: true,
							onComplete: this.showResponse  
	
						});
			
			// Logica de cambio automatico
			
		}
		
	this.showResponse= function (xmlHttpRequest, responseHeader)
    {
		
		//document.write = alert(this.inspect());
		// Función que recibe el resultado de la petición
		var vari=xmlHttpRequest.responseXML;
		//alert(this);
		//this.writeList(vari);
				
		//dump (this,0);
		var imagenes = vari.getElementsByTagName('imagen');	
				
		//alert(imagenes);
		for (var i = 0; i < imagenes.length; i++) 
		{
			// Accedemos al objeto XML cliente 
            var objetin = imagenes[i]; 
			var grande = objetin.getElementsByTagName('grande')[0].firstChild.data;
			var peque = objetin.getElementsByTagName('thumb')[0].firstChild.data;
			var ruta = objetin.getElementsByTagName('ruta')[0].firstChild.data;
			//alert (grande ); 
			//mi = new Foto ( grande , peque );
			insertar(grande,peque,ruta);
			
		}
		
		//Inicializas el bucle de cambios de fotos
		//cambiar_fotos_minis();
		$('pag_total').update ( misFotos.length );
		foto_actual = -1;
		update_galeria(foto_actual++);
		cargarimagenes();
		intval=setInterval(
			function()
			{
				foto_actual++;
				if (foto_actual > misFotos.length - 1)
					foto_actual = 0;
				update_galeria(foto_actual); 
			}, timeOut);
		
	}
	
	function insertar( grande, peque, ruta)
	{
		misFotos.push ( new Foto (grande,peque, ruta) );
	}
	
	
	function cargarimagenes(){ 
	
		for(i in misFotos){ 
		lista_imagenes[i] = new Image(); 
		lista_imagenes[i].src = misFotos[i].ruta; 
		
		} 
	}
		
    // Funciones de Escritura de DOM
	function crear_mini_galeria ( fotos, indices ) {
		var mini ="<ul>";
		for ( var i =0 ; i < fotos.length; i++){
			mini += "<li><a href=\"javascript: myGaleria.stop_Int(); myGaleria.set_foto_actual('" + parseInt( indices[i] - 1 ) + "'); myGaleria.start_Int(); \"  rel=\"transition[appear]\">" + fotos[i] + "</a></li>";
			}
		mini += " </ul>";
		return mini;
	}		
	
	this.set_foto_actual = function (actual){
		foto_actual=parseInt( actual );
	}
	
	// Funciones de animacion del DOM
	this.start_Int = function (){
    //function start_Int (){
        if(intval==""){
		  update_galeria(foto_actual++);
          intval=setInterval(
			function()
			{
				foto_actual++;
				if (foto_actual > misFotos.length - 1)
					foto_actual = 0;
				update_galeria(foto_actual); 
			}, timeOut);
      }else{
          stop_Int()
      }
    }

	this.stop_Int = function (){
        if(intval!=""){
          window.clearInterval(intval)
          intval=""
         // myTimer.innerHTML="Interval Stopped"
      }
    }
	
	function cambiar_foto_principal (actual) {
		
		
		if( actual >= misFotos.length - 1 )
			var mas = - 1;
		else
			var mas= parseInt(actual);
		
		
		if( actual > 0 )
			var menos = parseInt( actual ) - 2;
		else
			var menos = misFotos.length - 2;
		
		$('galeriaimagen').hide();
		$('galeriaimagen').update ( misFotos[actual].grande );
		
		Effect.Appear('galeriaimagen');
		
		$('botonmas').update ( "<a href=\"javascript: myGaleria.stop_Int(); myGaleria.set_foto_actual('" + mas  + "'); myGaleria.start_Int();\">+</a>");
		$('botonmenos').update ( "<a href=\"javascript: myGaleria.stop_Int(); myGaleria.set_foto_actual('" + menos  + "'); myGaleria.start_Int();\">-</a>" );
		
		actual++;
		
		$('pag_actual').update ( actual );
		
	}
	
	function update_galeria( actual ){
		if ( actual > misFotos.length - 1){
			actual = 0;
			}
		cambiar_foto_principal (foto_actual);
		cambiar_fotos_minis(foto_actual)
		}
	
	
	function cambiar_fotos_minis(actual){
		// para todos los thumbnails
		
		fotos = new Array ();
		indices = new Array ();
		
		var indice;
		/*for ( i=0; (i < 8 ) && ( i < misFotos.length ) ; i++ )
		{
			fotos.push ( misFotos[ i ].peque ); 
		}*/
		
		// 3 PRIMEROS THUMBS
		for( i = 3; i > 0 ; i--)
		{
			if( actual - i < 0 )
				indice = parseInt( (misFotos.length - 1 ) + ( actual - i) );
			else 
				indice=parseInt(actual - i); 
			fotos.push ( misFotos[ indice ].peque ); 
			indices.push( indice );
		}
		
		//CUARTO THUMB
		fotos.push ( misFotos[ actual ].peque );
		indices.push( actual );
		
		//4 THUMBS POSTERIORES
		for( i = 1; i < 5 ; i++)
		{
			if( ((misFotos.length - i ) - actual) <= 0 )
				indice=parseInt(((misFotos.length - i ) - actual) * (-1)); 
			else 
				indice=parseInt(actual + i); 
		fotos.push ( misFotos[ indice ].peque );
		indices.push( indice );
		}
		
		//$('fichagaleriaminiaturas').hide();
		$('fichagaleriaminiaturas').update( crear_mini_galeria ( fotos, indices ) ); 
		//Effect.Appear('fichagaleriaminiaturas');
		
		
	}
}


