function Galeria(sTitulo, aImagenesStr, aCaptionsStr, sDate , eventoInit, eventoReload )
{
   this.actual = 0;
   this.length = aImagenesStr.length ;
   this.sTitulo = sTitulo;
   this.sDate  = sDate ;
   this.aImagenes = new Array;

   //registra funciones de callback de inicio y recarga
   this.eventoReload = eventoReload;   
   this.eventoInit   = eventoInit;
   
   //precarga imagenes en Image
   for ( var i = 0; i < aImagenesStr.length; i++ ) {   	
	    var oImage = new Image;
    	oImage.src = aImagenesStr[i];
    	this.aImagenes.push(oImage);   
   }
   
   this.aCaptions = aCaptionsStr;
   this.initGallery();  
}



Galeria.prototype.initGallery = function()
{
	 // TODO: obtener numImg de la URL y mostrarla.

	var id = 0;
	var HeaderStuff = window.location.search;
	var mgNumStart = HeaderStuff.indexOf("id=");
	if (mgNumStart != -1) {
		id = HeaderStuff.substring(mgNumStart + 3) - 1;
	}
	this.actual = id;
	this.reload();

	 //lanzar funcion callback con inicializaciones del documento
	 this.eventoInit(this);	
	 
	 // cargar artículo relacionado, si lo hay.
	var relArt = document.getElementById("_relatedArticle");
	if (relArt && sRelatedArticle != "" && sRelatedArticle != "#")
		relArt.innerHTML = '<a href="' + sRelatedArticle + '">Leer la noticia</a>'; 

}


Galeria.prototype.reload = function()
{
	idMainImagen = "_main_imagen";
	idMainCaption = "_main_caption";

	//actualizar imagen
	c = document.getElementById(idMainImagen);	
	c.src = this.aImagenes[this.actual].src; 
	c.alt = this.aCaptions[this.actual];
	
	//actualizar caption
	document.getElementById(idMainCaption).innerHTML = this.aCaptions[this.actual]; 
	
	// actualizar título
	if (this.aCaptions[this.actual].length > 0)
		document.title = this.aCaptions[this.actual];
	else
		document.title = this.sTitulo;

	
	// actualizar URL
	//location.search = "id=" + this.actual;
	
	//funcion callback para actualizacion de HTML
	this.eventoReload(this);	
}	


	
Galeria.prototype.gotoInicio = function()
{	
	this.actual =0;
	this.reload();
}


Galeria.prototype.gotoFin = function()
{	
	this.actual = this.length-1;
	this.reload();
}


Galeria.prototype.gotoSig = function()
{
	if (this.actual<(this.length-1 ) ){
		this.actual++ ;	
	}	else {
		this.actual=0;
	}
	this.reload();
}


Galeria.prototype.gotoAnt = function()
{
	if (this.actual>0 ){
		this.actual-- ;	
	}	else {
		this.actual=this.length-1;
	}
	this.reload();
}


Galeria.prototype.gotoNumber = function(numero)
{
	this.actual=numero;
	this.reload();
}



// paginacion
Galeria.prototype.pintaPaginacion2 = function() {
	// maximo del indice de paginacion 
	cfgmaxpaginacion = 20;	
	texto ="";	
	var minPaginacion =  Math.floor( this.actual / cfgmaxpaginacion ) *cfgmaxpaginacion ;
	var maxPaginacion = (minPaginacion+cfgmaxpaginacion)  ;	
	if (maxPaginacion > this.length) {		
		var maxPaginacion = this.length ;	
	}	 
	for (i=(minPaginacion); i< maxPaginacion ; i++) {
			if (i==this.actual ) {
				texto = texto +"<span class='nav-galeria-activo'>"+ (i+1)+ "</span>&nbsp;";
			}else {
				url = window.location.pathname + "?id=" + (i + 1);
				texto = texto +"<a   href='" + url + "'>" + (i+1)+ "</a>&nbsp;";	
			}// fin if			
	}//for	
	return texto
}

Galeria.prototype.pintaPaginacion1 = function() {
	// maximo del indice de paginacion 
	cfgmaxpaginacion = 20;	
	texto ="";	
	var minPaginacion =  Math.floor( this.actual / cfgmaxpaginacion ) *cfgmaxpaginacion ;
	var maxPaginacion = (minPaginacion+cfgmaxpaginacion)  ;	
	if (maxPaginacion > this.length) {		
		var maxPaginacion = this.length ;	
	}	 
	for (i=(minPaginacion); i< maxPaginacion ; i++) {
			if (i==this.actual ) {
				texto = texto + "<span class='nav-galeria-activo'>"+ (i+1)+ "</span>&nbsp;";
			}else {
				texto = texto + "<a href='javascript:mygallery.gotoNumber(" + i + ");'>" + (i+1)+ "</a>&nbsp;";	
			}// fin if			
	}//for	
	return texto
}


// paginacion
Galeria.prototype.pintaThumbs = function() {
	// maximo del indice de paginacion 
	cfgmaxpaginacion = 5;	
	texto ="";	
	var minPaginacion =  Math.floor( this.actual / cfgmaxpaginacion ) *cfgmaxpaginacion ;
	var maxPaginacion = (minPaginacion+cfgmaxpaginacion)  ;	
	if (maxPaginacion > this.length) {		
		var maxPaginacion = this.length ;	
	}	 
	for (i=(minPaginacion); i< maxPaginacion ; i++) {
				texto = texto +"<div class='_galerias-relacionadas_fotos_galeria'>";
				texto = texto +"<a href='#'><img src='"+this.aImagenes[i].src+"' ";	
				texto = texto +" width='79' height='51' border='0' alt='" + aCaptions[i] + "'/></a> "; 			
				texto = texto +"<h4><a href='#'>"+aCaptions[i]+"</a> </h4></div>";
	}//for	
	return texto
}



Galeria.prototype.pintaInfoPaginacion = function() {
	return "Foto: " +(this.actual+1) + " de "+ this.length;
}


// -------------- util


function loadContent(file){
  var scriptTag = document.getElementById('loadScript');
  var head = document.getElementsByTagName('head').item(0)
  if(scriptTag) head.removeChild(scriptTag);
  script = document.createElement('script');
  script.src = file;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script)
}

// ---------------
var mygallery ;
var mygalleries ;
var mygalleriesGroup;


function init() {	
	mygallery = new Galeria (sTitulo, aImagenes, aCaptions, sDate, evInit, evReload );
	
	mygalleries = new Galerias (aRelThumb, aRelTit , aRelLink, evGsInit, evGsReload);

	if (typeof aCatThumb != "undefined") 
		mygalleries = new Galerias (aCatThumb, aCatTit , aCatLink, evGsInit, evGsReload);
}



function evGsInit (galerias) {
	document.getElementById("galerias-relacionadas_fotos").innerHTML = galerias.pintaThumbs();
	document.getElementById("galerias-relacionadas_nav").innerHTML = galerias.pintaBotones();
}

function evGsReload (galerias) {
	document.getElementById("galerias-relacionadas_fotos").innerHTML = galerias.pintaThumbs();
	document.getElementById("galerias-relacionadas_nav").innerHTML = galerias.pintaBotones();
}



function evInit (galeria) {
	idGaleriaTitulo   = "_galeriaTitulo";
	idGaleriaFechaPub = "_fechaPublicacion";

	 //--- definir titulo
	 document.getElementById(idGaleriaTitulo).innerHTML = galeria.sTitulo;
	
	 //--- title de la pagina
	 document.title =galeria.sTitulo;	 
	 
	 //--- definir fecha 
	 document.getElementById(idGaleriaFechaPub).innerHTML = galeria.sDate;	
}

function evReload(galeria) {
	//-- elemento de paginacion
	idPaginacion1 =	"_indicePaginacion1";
	idPaginacion2 = "_indicePaginacion2";	
	
	document.getElementById(idPaginacion1).innerHTML = galeria.pintaPaginacion1() ;
	document.getElementById(idPaginacion2).innerHTML = galeria.pintaPaginacion2() ;

	//-- info de paginacion
	idInfoPaginacion ="_infoPaginacion";
	document.getElementById(idInfoPaginacion).innerHTML = galeria.pintaInfoPaginacion();

	//-- botonera visibilidad
	idBotonAnterior1 ="_botonera_ant1";
	idBotonAnterior2 ="_botonera_ant2";
	idBotonSiguiente1 ="_botonera_sig1";
	idBotonSiguiente2 ="_botonera_sig2";
	
	if (galeria.actual==0) {	
	// primero
		document.getElementById(idBotonAnterior1).style.visibility ="hidden";	
		document.getElementById(idBotonAnterior2).style.visibility ="hidden";			
	} else {
	//no primero
		document.getElementById(idBotonAnterior1).style.visibility ="visible";		
		document.getElementById(idBotonAnterior2).style.visibility ="visible";		
	}
	
	if (galeria.actual==galeria.length-1) {	
	//ultimo
		document.getElementById(idBotonSiguiente1).style.visibility ="hidden";	
		document.getElementById(idBotonSiguiente2).style.visibility ="hidden";	
		
	} else {
	//no ultimo
		document.getElementById(idBotonSiguiente1).style.visibility ="visible";	
		document.getElementById(idBotonSiguiente2).style.visibility ="visible";	
	}
	
	//evento del usuario (publicidad, OJD)
	if (typeof eventUsr != "undefined") eventUsr();
}

function include(src, dest){
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = src;
	dest.appendChild(script);
} 

function launchJavascript(responseText) {
  // RegExp from prototype.sonio.net
  var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
			   
  var match    = new RegExp(ScriptFragment, 'img');
  var scripts  = responseText.match(match);

	if(scripts) {
		var js = '';
		for(var s = 0; s < scripts.length; s++) {
			var match = new RegExp(ScriptFragment, 'im');
			js += scripts[s].match(match)[1];
		}
		eval(js);
	}
}	

