// Definicion de variables globales

var contenedor = 'galeria_cont';
var url_media = 'img/';

// variables slideshow
var slideVel = 3000;// milisegundos
var crossFadeDuration = 5;// segundos
var int_entrefundido_slide;
var imagenes = new Array('img01.jpg', 'img02.jpg', 'img03.jpg', 'img04.jpg', 'img05.jpg', 'img06.jpg', 'img07.jpg', 'img08.jpg', 'img09.jpg', 'img10.jpg' );
var id_actual = 0;// es cero pq es la unica galeria
//var imagen_actual = -1;
var totalFotos = 10;


/////////////////////////////////////
//////  CONTROL DE SLIDESHOW ///////

function iniciaSlide(){
	galeria.setFotoAct(0);// resetea el numero de foto actual del objeto galeria
	muestraSlide();
}

function muestraSlide(){
// alert('muestraSlide.');
	var url = url_media;
	var ant = galeria.fotoAnt();
	var act = galeria.fotoAct();
	var sig = galeria.fotoSig();
	var total = galeria.getNumFotos();
	
	if(act >= total){
		galeria.setFotoAct(0);
		muestraSlide();
		return;
	}
	Element.hide ($(contenedor));
	$(contenedor).innerHTML = '<img src="'+ url + galeria.getFotoUrl(act) + '" alt="foto"  />';
	new Effect.Appear(contenedor ,{
							duration: 2.0,
							transition: Effect.Transitions.linear,
							from: 0,
							to: 1.0,
							afterFinish:entrefundido_slide
							});
	galeria.setFotoAct(Number(galeria.fotoAct())+1);
}


function entrefundido_slide(){
		int_entrefundido_slide = setInterval(oculta_slide, slideVel);
}

function oculta_slide(){
	clearInterval(int_entrefundido_slide);
	new Effect.Fade(contenedor ,{
							duration: 2.0,
							transition: Effect.Transitions.linear,
							from: 1.0,
							to: 0,
							afterFinish: muestraSlide,
//							afterUpdate: chequeoSlideActivo,
							sync: false
							});
}




/*********************************/
/*****  Utilidades y clases  *****/	

// Crea el objeto galeria, que contiene el array de objetos: fotos
function parser(datos){
//	alert('parser: '+ datos.responseText);
//	$(contenedor).innerHTML = "datos.responseText";// salida de datos
	galeria = new Galeria(id_actual);
	galeria.setNumFotos(totalFotos);
	for(var i=0; i<galeria.getNumFotos(); i++){
		var foto = new Foto(imagenes[i]);
		galeria.addFoto(foto);
	}
	
//	Element.show($(contenedor));
	iniciaSlide();

//	alert('getFotoUrl: '+ galeria.getFotoUrl(2));
//	alert('galeria.getNumFotos: '+ galeria.getNumFotos());
/*
	if(galeria.getNumFotos() > 0){
		muestraThumbs();
	}else{alert('no hay fotos');}
*/
	
}


///////////////////////////////////
//////  @@@@@ CLASES @@@@@ ////////


/******************************************/
// Clase Foto, crea cada imagen con sus datos
function Foto (url) {
	this.url = url;
}

Foto.prototype.getUrl = function() { return this.url;}



/******************************************/
// Clase Galeria, almacena todas la fotos de la seccion actual
function Galeria(id){
//alert('Galeria: ' + id);
	this.id = id;
	this.fotos = new Array;//cada objeto foto{id,url,desc,fecha}
	this.total_fotos;//new Number;
	this.foto_actual = new Number;
	
}
//aņade objetos foto
Galeria.prototype.addFoto = function(foto){
//alert("addFoto: lenght: " + this.fotos.length);
	this.fotos.push(foto);
	return this.fotos;
}
// devuelve el total de fotos de esta galleria
Galeria.prototype.getNumFotos = function() {
	if(this.total_fotos != null){
		return this.total_fotos;
	}else{
		return this.fotos.length;
	}
}

Galeria.prototype.setNumFotos = function(val) { this.total_fotos = val; }
// devuelve el fichreo de la foto numFoto
Galeria.prototype.getFotoUrl = function(numFoto) {
	this.foto_actual = numFoto;
	return this.fotos[numFoto].url;
}

Galeria.prototype.fotoAnt = function() {
	if(this.foto_actual > 0){
		return Number(this.foto_actual) -1;
	}else{
		return -1;
	}
}
Galeria.prototype.fotoAct = function() { return this.foto_actual;}
Galeria.prototype.setFotoAct = function(val) { this.foto_actual = val;}
Galeria.prototype.fotoSig = function() {
	if(this.foto_actual < this.total_fotos -1){
		return Number(this.foto_actual) + 1;
	}else{
		return -1;
	}
}


// INICIO //
// en el onload del body


