/**
 *	Obtiene el ancho de un elemento
 */
function bannersAncho(elemento)
{
	ancho = parseFloat(elemento.clientWidth);
	/**
	 *	Borde y margen
	 *		Firefox y otros
	 */
	if (document.defaultView && document.defaultView.getComputedStyle)
	{
		var defaultView = elemento.ownerDocument.defaultView;
		if (defaultView)
		{
			var computedStyle = defaultView.getComputedStyle(elemento, null);
			if (computedStyle)
			{
				ancho += (parseFloat(computedStyle.getPropertyValue('border-right-width')) || 0);
				ancho += (parseFloat(computedStyle.getPropertyValue('border-left-width')) || 0);
				ancho += (parseFloat(computedStyle.getPropertyValue('margin-right')) || 0);
				ancho += (parseFloat(computedStyle.getPropertyValue('margin-left')) || 0);
			}
		}
	}
	/**
	 *		IE
	 */
	else if (elemento.currentStyle)
	{
		ancho += (parseFloat((elemento.currentStyle['border-right-width'] || elemento.currentStyle['borderRightWidth'])) || 0);
		ancho += (parseFloat((elemento.currentStyle['border-left-width'] || elemento.currentStyle['borderLeftWidth'])) || 0);
		ancho += (parseFloat((elemento.currentStyle['margin-right'] || elemento.currentStyle['marginRight'])) || 0);
		ancho += (parseFloat((elemento.currentStyle['margin-left'] || elemento.currentStyle['marginLeft'])) || 0);
	}

	return(ancho);
}

/**
 *	Obtiene el alto de un elemento
 */
function bannersAlto(elemento)
{
	alto = parseFloat(elemento.clientHeight);
	/**
	 *	Borde y margen
	 *		Firefox y otros
	 */
	if (document.defaultView && document.defaultView.getComputedStyle)
	{
		var defaultView = elemento.ownerDocument.defaultView;
		if (defaultView)
		{
			var computedStyle = defaultView.getComputedStyle(elemento, null);
			if (computedStyle)
			{
				alto += (parseFloat(computedStyle.getPropertyValue('border-top-width')) || 0);
				alto += (parseFloat(computedStyle.getPropertyValue('border-bottom-width')) || 0);
				alto += (parseFloat(computedStyle.getPropertyValue('margin-top')) || 0);
				alto += (parseFloat(computedStyle.getPropertyValue('margin-bottom')) || 0);
			}
		}
	}
	/**
	 *		IE
	 */
	else if (elemento.currentStyle)
	{
		alto += (parseFloat((elemento.currentStyle['border-top-width'] || elemento.currentStyle['borderTopWidth'])) || 0);
		alto += (parseFloat((elemento.currentStyle['border-bottom-width'] || elemento.currentStyle['borderBottomWidth'])) || 0);
		alto += (parseFloat((elemento.currentStyle['margin-top'] || elemento.currentStyle['marginTop'])) || 0);
		alto += (parseFloat((elemento.currentStyle['margin-bottom'] || elemento.currentStyle['marginBottom'])) || 0);
	}

	return(alto);
}

/**
 *	Objeto para la marquesina
 */
var bannersMarquesina = function(idMarquesina, velocidadX, velocidadY)
{
	/**
	 *	Multiplicador de la velocidad de desplazamiento
	 */
	this.multiplicadorVelocidad = 1;

	/**
	 *	Establece la dirección del desplazamiento
	 */
	this.velocidad = function(x, y)
	{
		x = parseInt(x);
		y = parseInt(y);
		if (!isNaN(x))
		{
			this.velocidadX = x;
		}
		if (!isNaN(y))
		{
			this.velocidadY = y;
		}
	};

	/**
	 *	Establece el desplazamiento hacia la izquierda
	 */
	this.izquierda = function()
	{
		this.velocidad(-1, 0);
	};

	/**
	 *	Establece el desplazamiento hacia la derecha
	 */
	this.derecha = function()
	{
		this.velocidad(1, 0);
	};

	/**
	 *	Establece el desplazamiento hacia arriba
	 */
	this.arriba = function()
	{
		this.velocidad(0, -1);
	};

	/**
	 *	Establece el desplazamiento hacia abajo
	 */
	this.abajo = function()
	{
		this.velocidad(0, 1);
	};

	/**
	 *	Función para detener la marquesina
	 */
	this.detener = function()
	{
		this.velocidad(0, 0);
	};

	/**
	 *	Informa si la marquesina se encuentra detenida
	 */
	this.detenida = function()
	{
		detenida = false;
		if ((this.velocidadX == 0) && (this.velocidadY == 0))
		{
			detenida = true;
		}
		return(detenida);
	};

	/**
	 *	Informa si la marquesina se desplaza horizontalmente
	 */
	this.horizontal = false;

	/**
	 *	Informa si la marquesina se desplaza verticalmente
	 */
	this.vertical = false;

	/**
	 *	Función para desplazar la marquesina
	 */
	this.mover = function()
	{
		if ((!this.detenida()) && (this.marquesina != null) && (this.marquesina != undefined))
		{
			this.posicionX1 += this.velocidadX;
			this.posicionY1 += this.velocidadY;
			this.posicionX2 += this.velocidadX;
			this.posicionY2 += this.velocidadY;

			if (this.velocidadX < 0)
			{
				if (this.posicionX1 < (0 - this.ancho))
				{
					this.posicionX1 = this.posicionX2 + this.ancho;
				}
				if (this.posicionX2 < (0 - this.ancho))
				{
					this.posicionX2 = this.posicionX1 + this.ancho;
				}
			}
			else
			{
				if (this.posicionX1 > this.ancho)
				{
					this.posicionX1 = this.posicionX2 - this.ancho;
				}
				if (this.posicionX2 > this.ancho)
				{
					this.posicionX2 = this.posicionX1 - this.ancho;
				}
			}
			if (this.velocidadY < 0)
			{
				if (this.posicionY1 < (0 - this.alto))
				{
					this.posicionY1 = this.posicionY2 + this.alto;
				}
				if (this.posicionY2 < (0 - this.alto))
				{
					this.posicionY2 = this.posicionY1 + this.alto;
				}
			}
			else
			{
				if (this.posicionY1 > this.alto)
				{
					this.posicionY1 = this.posicionY2 - this.alto;
				}
				if (this.posicionY2 > this.alto)
				{
					this.posicionY2 = this.posicionY1 - this.alto;
				}
			}
	
			this.banners1.style.left = this.posicionX1 + 'px';
			this.banners1.style.top = this.posicionY1 + 'px';
			this.banners2.style.left = this.posicionX2 + 'px';
			this.banners2.style.top = this.posicionY2 + 'px';
		}
	};

	/**
	 *	Constructor
	 */
	if (this.marquesina = document.getElementById('banners-' + idMarquesina))
	{
		/**
		 *	Estilos para la marquesina
		 */
		this.marquesina.style.position = 'relative';
		this.marquesina.style.overflow = 'hidden';

		/**
		 *	Proceso los banners para generar 2 tiras de imágenes
		 */
		velocidadX = parseInt(velocidadX);
		velocidadY = parseInt(velocidadY);
		if ((!isNaN(velocidadX)) && (velocidadX != 0))
		{
			velocidadY = 0;
			this.horizontal = true;
		}
		else if ((!isNaN(velocidadY)) && (velocidadY != 0))
		{
			velocidadX = 0;
			this.vertical = true;
		}
		else
		{
			velocidadX = velocidadY = 0;
		}
		var hijosMarquesina = this.marquesina.childNodes;
		var cantidadHijosMarquesina = hijosMarquesina.length;
		var arrayHijosMarquesina = new Array();
		this.ancho = 0;
		this.alto = 0;

		var banners1 = document.createElement('div');
		banners1.setAttribute('id', 'banners-' + idMarquesina + '-1');
		for (cont = (cantidadHijosMarquesina - 1); cont >= 0; cont--)
		{
			if ((hijosMarquesina[cont].tagName == 'A') || (hijosMarquesina[cont].tagName == 'IMG'))
			{
				if (hijosMarquesina[cont].tagName == 'A')
				{
					anchoElemento = altoElemento = 0;
					if (imagen = hijosMarquesina[cont].getElementsByTagName('img'))
					{
						anchoElemento = bannersAncho(imagen[0]);
						altoElemento = bannersAlto(imagen[0]);
					}
				}
				else
				{
					anchoElemento = bannersAncho(hijosMarquesina[cont]);
					altoElemento = bannersAlto(hijosMarquesina[cont]);
				}
				if (velocidadX != 0)
				{
					this.ancho += anchoElemento;
					if (altoElemento > this.alto)
					{
						this.alto = altoElemento;
					}
				}
				else if (velocidadY != 0)
				{
					if (anchoElemento > this.ancho)
					{
						this.ancho = anchoElemento;
					}
					this.alto += altoElemento;
				}
				var nodo = hijosMarquesina[cont];
				var clonNodo = nodo.cloneNode(true);
				arrayHijosMarquesina[arrayHijosMarquesina.length] = clonNodo;
			}
			this.marquesina.removeChild(hijosMarquesina[cont]);
		}
		for (cont = arrayHijosMarquesina.length - 1; cont >= 0; cont--)
		{
			banners1.appendChild(arrayHijosMarquesina[cont]);
		}
		banners1.style.width = this.ancho + 'px';
		banners1.style.height = this.alto + 'px';
		banners1.style.left = '0px';
		banners1.style.top = '0px';
		banners1.style.position = 'absolute';
		this.posicionX1 = 0;
		this.posicionY1 = 0;

		this.marquesina.appendChild(banners1);
		this.banners1 = document.getElementById(banners1.getAttribute('id'));

		banners2 = this.banners1.cloneNode(true);
		banners2.setAttribute('id', 'banners-' + idMarquesina + '-2');
		if (velocidadX != 0)
		{
			banners2.style.left = this.ancho + 'px';
			banners2.style.top = '0px';
			this.posicionX2 = this.ancho;
			this.posicionY2 = 0;

			if (velocidadX < 0)
			{
				this.multiplicadorVelocidad = (-1) * velocidadX;
				this.izquierda();
			}
			else
			{
				this.multiplicadorVelocidad = velocidadX;
				this.derecha();
			}
		}
		else if (velocidadY != 0)
		{
			banners2.style.left = '0px';
			banners2.style.top = this.alto + 'px';
			this.posicionX2 = 0;
			this.posicionY2 = this.alto;

			if (velocidadY < 0)
			{
				this.multiplicadorVelocidad = (-1) * velocidadY;
				this.arriba();
			}
			else
			{
				this.multiplicadorVelocidad = velocidadY;
				this.abajo();
			}
		}
		this.marquesina.appendChild(banners2);
		this.banners2 = document.getElementById(banners2.getAttribute('id'));

		this.intervalo = setInterval(new Function('bannersMarquesina' + idMarquesina + '.mover();'), 1);
	}
};

/**
 *	Verifica la ubicación del puntero del mouse y modifica el sentido de la marquesina en caso de ser necesario
 */
function bannersVerificarSentido(marquesina, evento, forzar)
{
	if ((!marquesina.detenida()) || (forzar))
	{
		cursorX = cursorY = 0;
		ancho = bannersAncho(marquesina.marquesina);
		alto = bannersAlto(marquesina.marquesina);
		elemento = marquesina.marquesina;
		while (elemento != null)
		{
			cursorX -= elemento.offsetLeft;
			cursorY -= elemento.offsetTop;
			elemento = elemento.offsetParent;
		}

		if (evento.clientX)
		{
			cursorX += evento.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			cursorY += evento.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		else if (evento.pageX)
		{
			cursorX += evento.pageX;
			cursorY += evento.pageY;
		}

		if (marquesina.horizontal)
		{
			if (cursorX > (ancho / 2))
			{
				marquesina.derecha();
			}
			else
			{
				marquesina.izquierda();
			}
		}
		else if (marquesina.vertical)
		{
			if (cursorY > (alto / 2))
			{
				marquesina.abajo();
			}
			else
			{
				marquesina.arriba();
			}
		}
	}
}

/**
 *	Inicia la marquesina
 */
function bannersIniciarMarquesina(idMarquesina, velocidadX, velocidadY)
{
	return(new bannersMarquesina(idMarquesina, velocidadX, velocidadY));
}

function bannersOnloadEvent(func) 
{
	if (window.onload)
	{
		bannersAddEvent(window, 'load' , window.onload);
	}
	bannersAddEvent(window, 'load' , func);
}

function bannersAddEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, true);
		return(true);
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on" + evType, fn);
		return(r);
	}
	else
	{
		return(false);
	}
}

