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();
	
	//clearInterval(this.SuperOfertaInterval);
	this.SuperOfertaInterval = setTimeout("Banner.refrescarCuentaAtrasBanner('" + this.Id + "');", 1050);
}

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;

Banner.prototype.SuperOfertaElementoEnlace = null;
Banner.prototype.SuperOfertaElementoPrecios = null;
Banner.prototype.SuperOfertaElementoPrecioNormal = null;
Banner.prototype.SuperOfertaElementoPrecioOferta = null;
Banner.prototype.SuperOfertaElementoNombreProducto = null;
Banner.prototype.SuperOfertaElementoContenedorImagenProducto = null;
Banner.prototype.SuperOfertaElementoImagenProducto = null;
Banner.prototype.SuperOfertaElementoH1 = null; //dígito 1 de las horas (decenas)
Banner.prototype.SuperOfertaElementoH2 = null; //dígito 2 de las horas (unidades)
Banner.prototype.SuperOfertaElementoM1 = null; //dígito 1 de las minutos (decenas)
Banner.prototype.SuperOfertaElementoM2 = null; //dígito 2 de las minutos (unidades)
Banner.prototype.SuperOfertaElementoS1 = null; //dígito 1 de las segundos (decenas)
Banner.prototype.SuperOfertaElementoS2 = null; //dígito 2 de las segundos (unidades)
Banner.prototype.SuperOfertaInterval = null;
	
/**
  * Añade un nuevo contenido a la lista de contenidos del banner
  **/
Banner.prototype.addContenido = 
function(idDB, tipo, src, enlace, enlaceTarget, tooltip, cuentaAtras, prodNombre, prodImagen, precioOferta, precioNormal)
{
	if (tipo == Banner.BANCONTIP_SUPEROFERTA)
		enlaceTarget = "_self";

	// 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, tipo: tipo, id: e.id, src:  src, 
		enlace: enlace, enlaceTarget: enlaceTarget, tooltip: tooltip, 
		cuentaAtras: cuentaAtras, prodNombre: prodNombre, 
		prodImagen: prodImagen, precioOferta: precioOferta, 
		precioNormal: precioNormal});
	
	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)
{

	if (this.Contenidos==null || this.Contenidos.length == 0)
		return false;
	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;
		this.ElementoImagen.alt = 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);
		}
	}
	
	if (contenido.tipo == Banner.BANCONTIP_SUPEROFERTA)
	{
	
		if (this.SuperOfertaElementoPrecioNormal == null)
		{

			this.SuperOfertaElementoEnlace = document.createElement('div');
			this.SuperOfertaElementoEnlace.style.position = "absolute";
			this.SuperOfertaElementoEnlace.style.left = "0px";
			this.SuperOfertaElementoEnlace.style.top = "0px";
			this.SuperOfertaElementoEnlace.style.width = this.Ancho;
			this.SuperOfertaElementoEnlace.style.height = this.Alto;
			this.SuperOfertaElementoEnlace.style.zIndex = 100;
			
			eBanner.appendChild(this.SuperOfertaElementoEnlace);
		
			this.SuperOfertaElementoPrecios = this._crearElementoFlotante('div', -9, 40);
			
			this.SuperOfertaElementoPrecioNormal = document.createElement('span');
			this.SuperOfertaElementoPrecioNormal.className = "superOfertaPrecioNormal";
			this.SuperOfertaElementoPrecios.appendChild(this.SuperOfertaElementoPrecioNormal);

			this.SuperOfertaElementoPrecioOferta = document.createElement('span');
			this.SuperOfertaElementoPrecioOferta.className = "superOfertaPrecioOferta";
			this.SuperOfertaElementoPrecios.appendChild(this.SuperOfertaElementoPrecioOferta);			
			
			if (contenido.prodNombre.length > 22)
			{
				this.SuperOfertaElementoNombreProducto = this._crearElementoFlotante('div', -9, 15);
				this.SuperOfertaElementoNombreProducto.className = "superOfertaProdNombreLargo";
				this.SuperOfertaElementoNombreProducto.style.width = "160px";
			}
			else
			{
				this.SuperOfertaElementoNombreProducto = this._crearElementoFlotante('div', -9, 21);
				this.SuperOfertaElementoNombreProducto.className = "superOfertaProdNombre";
			}
			this.SuperOfertaElementoNombreProducto.style.zIndex = 10;
			//this.SuperOfertaElementoNombreProducto.style.backgroundColor = "red";
			
			this.SuperOfertaElementoImagenProducto = contenido.prodImagen;
			//this.SuperOfertaElementoImagenProducto.style.position = "absolute";
			//this.SuperOfertaElementoImagenProducto.style.left = "3px";
			//this.SuperOfertaElementoImagenProducto.style.top = "3px";
			//eBanner.appendChild(this.SuperOfertaElementoImagenProducto);
			
			var tblImagen = document.createElement('table');
			tblImagen.border = "0";
			tblImagen.cellpadding = "0";
			tblImagen.cellspacing = "0";
			tblImagen.style.position = "absolute";
			tblImagen.style.left = "3px";
			tblImagen.style.top = "3px";
			tblImagen.style.width = "100px";
			tblImagen.style.height = "100px";
			tblImagen.insertRow(0) ;
			tblImagen.rows[0].insertCell(0);
			tblImagen.rows[0].cells[0].align = "center";
			tblImagen.rows[0].cells[0].valign = "middle";
			tblImagen.rows[0].cells[0].appendChild(this.SuperOfertaElementoImagenProducto);
			eBanner.appendChild(tblImagen);
			this.SuperOfertaElementoContenedorImagenProducto = tblImagen;
			
			this.SuperOfertaElementoH1 = this._crearElementoFlotante('img', 128, 70);
			this.SuperOfertaElementoH1.src = "images/superoferta/0.jpg";
			this.SuperOfertaElementoH2 = this._crearElementoFlotante('img', 148, 70);
			this.SuperOfertaElementoH2.src = "images/superoferta/0.jpg";
			this.SuperOfertaElementoM1 = this._crearElementoFlotante('img', 174, 70);
			this.SuperOfertaElementoM1.src = "images/superoferta/0.jpg";
			this.SuperOfertaElementoM2 = this._crearElementoFlotante('img', 194, 70);
			this.SuperOfertaElementoM2.src = "images/superoferta/0.jpg";
			this.SuperOfertaElementoS1 = this._crearElementoFlotante('img', 220, 70);
			this.SuperOfertaElementoS1.src = "images/superoferta/0.jpg";
			this.SuperOfertaElementoS2 = this._crearElementoFlotante('img', 240, 70);
			this.SuperOfertaElementoS2.src = "images/superoferta/0.jpg";
			
		}
		this.SuperOfertaElementoPrecioNormal.innerHTML = contenido.precioNormal+" &euro;";
		this.SuperOfertaElementoPrecioOferta.innerHTML = " "+contenido.precioOferta+" &euro;";
		this.SuperOfertaElementoNombreProducto.innerHTML = contenido.prodNombre;
		
		this._mostrarSuperOferta(true);
		
		//alert(contenido.cuentaAtras);
	}
	else
	{
		if (this.SuperOfertaElementoPrecioNormal != null)
		{
			this._mostrarSuperOferta(false);
		}
	}
	//alert(contenido);
	this.refrescarCuentaAtras(false);
	return true;
}

Banner.prototype.refrescarCuentaAtras = 
function(restar)
{
	if (this.Contenidos == null)
		return false;
	//clearInterval(this.SuperOfertaInterval);
	
	for(var i=0; i<this.Contenidos.length; i++)
	{
		var contenido = this.Contenidos[i];
		if (contenido.tipo != Banner.BANCONTIP_SUPEROFERTA)
			continue;
		if (restar)
			contenido.cuentaAtras--;
		if (contenido.cuentaAtras <= 0)
			refrescarFrame(top);
		var dt = new Date(contenido.cuentaAtras*1000);
		var secs = dt.getSeconds();
		var s1 = parseInt(secs/10);
		var s2 = secs%10;
		//alert(contenido.cuentaAtras+";"+s1+";"+s2);
		this.SuperOfertaElementoS1.src = "images/superoferta/"+s1+".jpg";
		this.SuperOfertaElementoS2.src = "images/superoferta/"+s2+".jpg";
		
		var mins = dt.getMinutes();
		var m1 = parseInt(mins/10);
		var m2 = mins%10;
		this.SuperOfertaElementoM1.src = "images/superoferta/"+m1+".jpg";
		this.SuperOfertaElementoM2.src = "images/superoferta/"+m2+".jpg";
		
		var hrs = (dt.getHours()-1) + (dt.getDate()-1)*24;
		var h1 = parseInt(hrs/10);
		var h2 = hrs%10;
		this.SuperOfertaElementoH1.src = "images/superoferta/"+h1+".jpg";
		this.SuperOfertaElementoH2.src = "images/superoferta/"+h2+".jpg";
	}
	
	if (restar)
		this.SuperOfertaInterval = setTimeout("Banner.refrescarCuentaAtrasBanner('" + this.Id + "');", 1050);
}

Banner.prototype._mostrarSuperOferta = 
function(mostrar)
{
	if (mostrar)
		mostrar = "";
	else
		mostrar = "none";
	this.SuperOfertaElementoPrecios.style.display = mostrar;
	this._aplicarEnlace(this.SuperOfertaElementoPrecios);

	this.SuperOfertaElementoPrecioNormal.style.display = mostrar;
	this._aplicarEnlace(this.SuperOfertaElementoPrecioNormal);

	this.SuperOfertaElementoPrecioOferta.style.display = mostrar;
	this._aplicarEnlace(this.SuperOfertaElementoPrecioOferta);
	
	this.SuperOfertaElementoNombreProducto.style.display = mostrar;
	this._aplicarEnlace(this.SuperOfertaElementoNombreProducto);
	
	this.SuperOfertaElementoContenedorImagenProducto.style.display = mostrar;
	this._aplicarEnlace(this.SuperOfertaElementoContenedorImagenProducto);
	
	this.SuperOfertaElementoImagenProducto.style.display = mostrar;
	this._aplicarEnlace(this.SuperOfertaElementoImagenProducto);
	
	this.SuperOfertaElementoEnlace.style.display = mostrar;
	this._aplicarEnlace(this.SuperOfertaElementoEnlace);
	
	this.SuperOfertaElementoH1.style.display = mostrar; //dígito 1 de las horas (decenas)
	this._aplicarEnlace(this.SuperOfertaElementoH1);
	
	this.SuperOfertaElementoH2.style.display = mostrar; //dígito 2 de las horas (unidades)
	this._aplicarEnlace(this.SuperOfertaElementoH2);
	
	this.SuperOfertaElementoM1.style.display = mostrar; //dígito 1 de las minutos (decenas)
	this._aplicarEnlace(this.SuperOfertaElementoM1);
	
	this.SuperOfertaElementoM2.style.display = mostrar; //dígito 2 de las minutos (unidades)
	this._aplicarEnlace(this.SuperOfertaElementoM2);
	
	this.SuperOfertaElementoS1.style.display = mostrar; //dígito 1 de las segundos (decenas)
	this._aplicarEnlace(this.SuperOfertaElementoS1);
	
	this.SuperOfertaElementoS2.style.display = mostrar; //dígito 2 de las segundos (unidades)
	this._aplicarEnlace(this.SuperOfertaElementoS2);
	
}

Banner.prototype._aplicarEnlace = 
function(e)
{
	if (this.Contenidos == null || this.IndiceContenido<0)
		return false;
	var contenido = this.Contenidos[this.IndiceContenido];
	e.style.cursor = "pointer";
	e.title = contenido.tooltip;
	e.onclick = function(){ location.href=contenido.enlace; }
}

Banner.prototype._crearElementoFlotante = 
function(tagName, x, y)
{
	var eBanner = document.getElementById(this.Id);
	var e = document.createElement(tagName);
	e.style.position = "absolute";
	if (x<0)
	{
		e.style.right = (-x)+"px";
		e.align = "right";
	}
	else
	{
		e.style.left = x+"px";
	}
	
	if (y<0)
	{
		e.style.bottom = (-y)+"px";
	}
	else
	{
		e.style.top = y+"px";
	}
	e.style.zIndex = 0;
	eBanner.appendChild(e);
	return e;
}

Banner.BANCONTIP_IMAGEN = "IMAGEN";
Banner.BANCONTIP_SUPEROFERTA = "SUPEROFERT";

Banner.showBanner = 
function(idBanner){
	var e = document.getElementById(idBanner);
	if (e)
	{
		//alert(e);
		e._banner.show();
	}
}

Banner.refrescarCuentaAtrasBanner = 
function(idBanner){

	var e = document.getElementById(idBanner);
	if (e)
	{
		if (e._banner)
			e._banner.refrescarCuentaAtras(true);
	}
}
