function Banner(id, pausa, ancho, alto)
{
	if (typeof(pausa)=="undefined")
		pausa = 0;

	this.Id = id;
	this.Pausa = pausa;
	
	if ((typeof(ancho)=="undefined") || (ancho<=0))
		this.Ancho = "100%";
	else	
		this.Ancho = parseInt(ancho) + "px";
		
	if ((typeof(alto)=="undefined") || (alto<=0))
		this.Alto = "100%";
	else
		this.Alto = parseInt(alto) + "px";
		
	this.Contenidos = new Array();
}

Banner.prototype.Id = null;

Banner.prototype.Ancho = null;

Banner.prototype.Alto = null;

//Pusa en milisegundos que se mantiene cada contenido
Banner.prototype.Pausa = null;

Banner.prototype.IndiceContenido = -1;

Banner.prototype.Contenidos = null;

Banner.prototype.Interval = null;

Banner.prototype.PermitirRepetidos = false;

//imagen principal donde se irán cargando las diferentes imágenes
Banner.prototype.ElementoImagen = null;
	
/**
  * Añade un nuevo contenido a la lista de contenidos del banner
  **/
Banner.prototype.addContenido = 
function(idDB, src, enlace, enlaceTarget, tooltip)
{
	// comprobamos que el contenido no exista ya en el banner
	if (!this.PermitirRepetidos)
	for(var i=0; i<this.Contenidos.length; i++)
	{
		if (this.Contenidos[i].idDB == idDB)
			return false;
	}
	
	if (!document.body)
	{
		alert('No existe el body del documento.');
		return false;
	}
	
	var e = document.getElementById('bancon_'+idDB);
	if (!e)
	{
		e = document.createElement("img");
		e.id = 'bancon_'+idDB;
		e.src = src;
		e.alt = "";
		e.title = tooltip;
		e.style.display='none';
		//e.className = "png"; /*para poder aplicar trasnaparencias en IE6 si hiciese falta */ /*OJO:puede ocasionar comportamientos extaños en IE6 como que deje de funcionar el evento onclick a veces*/
	
		document.body.insertBefore(e, document.body.firstChild);
	}
	
	//añadimos el contenido a la lista
	this.Contenidos.push({idDB: idDB, id: e.id, src:  src, enlace: enlace, enlaceTarget: enlaceTarget, tooltip: tooltip});
	
	return true;
}

//indica si se ha completado la primeraCarga o inicialización del banner
Banner.prototype.inicializado = false;

/**
 * Para sobrescribir
 */
Banner.prototype.onContentChanged = null;

Banner.prototype.onContentChanging = null;

Banner.prototype.play = 
function(pausa)
{
	//Inicializamos de nuevo el intervalo
	clearInterval(this.Interval);
	this.Interval = setInterval("Banner.showBanner('" + this.Id + "');", pausa);
}

Banner.prototype.debug = 
function(mensaje)
{
	if (this.Id == "xbanner7")
		alert(mensaje);
}


Banner.prototype.show = 
function(indice)
{
	var eBanner = document.getElementById(this.Id);

	if (!eBanner)
	{
		return false;
	}

	if (!eBanner._banner)
	{
		eBanner._banner = this;
		eBanner.IndiceContenido = 0;
	}

	var pausa = 500;
	if (this.inicializado)
	{
		pausa = this.Pausa;	
	}
	else
	{
		this.play(pausa);
	}
	

//this.debug(this.Id + " " +(!this.primeraCarga)+" "+pausa+" "+this.Pausa);

	//calculamos el nuevo índice de los contenidos que debe mostrar el banner en caso de no especificar ninguno en concreto
	if (typeof(indice)=="undefined")
	{
		indice = (this.IndiceContenido + 1) % this.Contenidos.length;

	}
	else
	{
		this.play(pausa);
	}

	if (indice >= this.Contenidos.length || indice < 0)
		return false;

	var contenido = this.Contenidos[indice];
	var eImg = document.getElementById(contenido.id);
	//si el elemento no es una imagen probamos con su primer hijo
	//if (eImg && new String(eImg.tagName).toLowerCase()!='img')
	//{
	//	this.debug('El elemento del contenido del banner debe ser una imagen, no '+eImg.tagName+'.');
	//	return false;
	//}
//this.debug(indice + " "+contenido.id+" "+ eImg.tagName);
	// si la imagen que toca mostrar a continuación ya se ha cargado
	if (eImg && eImg.complete)
	{
		if (!this.ElementoImagen)
		{
			this.ElementoImagen = document.getElementById(this.Id + "Imagen");
			if (!this.ElementoImagen)
			{
				this.ElementoImagen = document.createElement('img');
				this.ElementoImagen.style.width = this.Ancho;
				this.ElementoImagen.style.height = this.Alto;
				this.ElementoImagen.style.zIndex = 0;
				eBanner.appendChild(this.ElementoImagen);
			}
		}
		this.ElementoImagen.src = eImg.src;
		this.ElementoImagen.title = eImg.title;
		
		if (contenido.enlace && (contenido.enlace!="") && (contenido.enlace!="#"))
		{

			this.ElementoImagen.style.cursor = "pointer";
			var reJS = new RegExp("^[ ]*javascript:");
			if (reJS.test(contenido.enlace))
			{
				this.ElementoImagen.onclick = new Function(contenido.enlace.replace(reJS, ''));
//alert(this.ElementoImagen.onclick);
			}
			else
			if (contenido.enlaceTarget == "_blank")
			{
				this.ElementoImagen.onclick = function(){
					var w = window.open(contenido.enlace, contenido.id);
					w.focus();
				}
			}
			else
			{
				this.ElementoImagen.onclick = function()
				{
					location.href=contenido.enlace;
				}
			}
		}
		else
		{
			this.ElementoImagen.style.cursor = "default";
			this.ElementoImagen.onclick = function(){
				return false;
			}
		}
		var indiceAnterior = this.IndiceContenido;
		
		if (this.onContentChanging && (indice!=this.IndiceContenido))
		{
			this.onContentChanging.call(this);
		}
		
		this.IndiceContenido = indice;
		
		if (this.onContentChanged && (indiceAnterior!=indice))
		{
			this.onContentChanged.call(this);
		}
		
		if (!this.inicializado)
		{
			this.inicializado =  true;
			this.play(this.Pausa);
		}
	}
	
	return true;
}



Banner.showBanner = 
function(idBanner){
	var e = document.getElementById(idBanner);
	if (e)
	{
		//alert(e);
		e._banner.show();
	}
}