/**
 *	Obtiene el ancho de un elemento
 */
function momentosAncho(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 momentosAlto(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 momentosMarquesina = 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.momentos1.style.left = this.posicionX1 + 'px';
			this.momentos1.style.top = this.posicionY1 + 'px';
			this.momentos2.style.left = this.posicionX2 + 'px';
			this.momentos2.style.top = this.posicionY2 + 'px';
		}
	};

	/**
	 *	Constructor
	 */
	if (this.marquesina = document.getElementById('momentos-' + idMarquesina))
	{
		/**
		 *	Estilos para la marquesina
		 */
		this.marquesina.style.position = 'relative';
		this.marquesina.style.overflow = 'hidden';

		/**
		 *	Proceso los momentos 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 momentos1 = document.createElement('div');
		momentos1.setAttribute('id', 'momentos-' + idMarquesina + '-1');
		for (cont = (cantidadHijosMarquesina - 1); cont >= 0; cont--)
		{
			if (hijosMarquesina[cont].tagName == 'DIV')
			{
				anchoElemento = momentosAncho(hijosMarquesina[cont]);
				altoElemento = momentosAlto(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--)
		{
			momentos1.appendChild(arrayHijosMarquesina[cont]);
		}
		momentos1.style.width = this.ancho + 'px';
		momentos1.style.height = this.alto + 'px';
		momentos1.style.left = '0px';
		momentos1.style.top = '0px';
		momentos1.style.position = 'absolute';
		this.posicionX1 = 0;
		this.posicionY1 = 0;

		this.marquesina.appendChild(momentos1);
		this.momentos1 = document.getElementById(momentos1.getAttribute('id'));

		momentos2 = this.momentos1.cloneNode(true);
		momentos2.setAttribute('id', 'momentos-' + idMarquesina + '-2');
		if (velocidadX != 0)
		{
			momentos2.style.left = this.ancho + 'px';
			momentos2.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)
		{
			momentos2.style.left = '0px';
			momentos2.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(momentos2);
		this.momentos2 = document.getElementById(momentos2.getAttribute('id'));

		this.intervalo = setInterval(new Function('momentosMarquesina' + idMarquesina + '.mover();'), 60);
	}
};

/**
 *	Verifica la ubicación del puntero del mouse y modifica el sentido de la marquesina en caso de ser necesario
 */
function momentosVerificarSentido(marquesina, evento, forzar)
{
	if ((!marquesina.detenida()) || (forzar))
	{
		cursorX = cursorY = 0;
		ancho = momentosAncho(marquesina.marquesina);
		alto = momentosAlto(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 momentosIniciarMarquesina(idMarquesina, velocidadX, velocidadY)
{
	return(new momentosMarquesina(idMarquesina, velocidadX, velocidadY));
}

