
//<!--CÓDIGO QUE PERMITIRÁ MOSTRAR LAS DISTINTAS LÍNEAS EN EL SCROLLER-->

/******************************************************************************************
ATENCIÓN: HAY QUE ACORDARSE DE LLAMAR A LA FUNCION IniciarScroller() EN EL EVENTO
ONLOAD DEL BODY (SI NO SE HACE, NO FUNCIONARÁ).
******************************************************************************************/


// Array con las noticias a mostrar. Se puede incluir código HTML. Cada elemento del array
// corresponde con una noticia. 
// Si todo está bien definido, esto es lo único que hay que modificar.

/*****************************************************************************/
/////---------------- Esto es lo que hay que modificar ------------------//////

// Variables que contienen cada cuantos milisegundos se hace scroll y cuantos milisegundos
// se parará al llegar al top.
var lngScrollDelay = 50;
var lngScrollPause = 3000;
// Variable que indica cuantos pixeles se hace scroll cada vez.
var lngScrollAmount = 2;
// Tipo de fuente con que se mostrará por defecto el texto del scroller.
var strFuente = 'Verdana';
// Tamaño de la letra que se mostrará por defecto en el scroller.
var strTamano = '10pt';
// Indica la dirección en la que se moverá el scroll.
var strDir = 'up';
/////--------------------------------------------------------------------//////
/*****************************************************************************/

// Sirve para que no se active el scroller cuando esta en el intervalo de sg que se para y el usuario pasa el ratón por encima.
var mintActivo = 1;

// Objeto contenedor. Hace referencia al div que hay que incluir en la página en la que se 
// quieren mostrar las noticias.
var objContenedor;

// Variables usadas para el funcionamiento del scroller.
var lngIdTime = 0, lngIdTime2 = 0;
var objDivAux = null, objDivAux2 = null;

function IniciarScroller(){
var obj;
	
	objContenedor = document.getElementById('divScroller');
		
	for (intIndex = 0; intIndex < arr.length; intIndex++){

		// Creo el elemento
		obj = document.createElement("div")
		// añado las características que va a tener en la página.	
		with (obj){
			id = intIndex;
			style.display ='none';
			style.position ='absolute';// 'relative';
			style.backgroundColor = '#FFFFFF';

			switch (strDir){
			case 'left':
			// El scroll se hará hacia la izquierda de la pantalla.

				style.height = '100%';
				style.width = '95%';
				style.top = 0;
				style.left = objContenedor.offsetWidth;
				break;
			
			case 'right':
			// El scroll se hará hacia la direcha de la pantalla.

				style.height = '100%';
				style.width = '95%';
				style.top = 0;
				style.right = -objContenedor.offsetWidth;
				break;
				
			case 'up':
			// El scroll se hará hacia la parte de arriba de la pantalla.
			
				style.width = '100%';
				style.height = '95%';
				style.top = objContenedor.offsetHeight;
				style.left = 0;
				break;
				
			case 'down':
			// El scroll se hará hacia la parte de abajo de la pantalla.
			
				style.width = '100%';
				style.height = '95%';
				style.top = - objContenedor.offsetHeight;
				style.left = 0;
				break;

			}	// switch
			
			style.fontFamily = strFuente;
			style.fontSize = strTamano;
			style.textAlign = 'justify';
			innerHTML = arr[intIndex];
		}	//	with


		// Añado el hijo al control contenedor.
		objContenedor.appendChild(obj);
		obj = null;
		
	}	// for ...
	

		
	// llamo a la función que comienza el movimiento.
	lngIdTime = setInterval('MoverDiv(0)',lngScrollDelay,'javascript');

}	//	IniciarScroller

/***************************************************************************************
* Según el parámetro recibido (left, right, up, down) llama a la función que realmente
* realiza el scroll.
****************************************************************************************/
function MoverDiv(strId){
	

	switch (strDir){
	case 'left':
	// El scroll se hará hacia la izquierda de la pantalla.

		// Aprovecho para establecer el ancho del contenedor, de manera que aunque me cambien el tamaño de la pantalla, lo mantenga correcto
		objContenedor.style.width = document.body.offsetWidth - objContenedor.offsetLeft;
		
		MoverDiv_Left(strId);
		break;
			
	case 'right':
	// El scroll se hará hacia la direcha de la pantalla.
				
		MoverDiv_Right(strId);
		break;

	case 'up':
	// El scroll se hará hacia la parte de arriba de la pantalla.
				
		MoverDiv_Up(strId);
		break;
		
	case 'down':
	// El scroll se hará hacia la parte de abajo de la pantalla.

		MoverDiv_Down(strId);
		break;
			
	}	// switch

}


/******************************************************************************************
* Esta función es la que se encarga de que se haga el scroll hacia la Izda.
* Recibe el id del div que hay que mover.
******************************************************************************************/
function MoverDiv_Left(strId){
var objDiv, objDiv2;
	
	// referencio el objeto para trabajar más cómodamente.
	objDiv = document.getElementById(strId);
	objDiv.style.width = '95%';
	if (objDivAux == null) objDivAux = objDiv;

	with (objDiv){

		if (style.left == '') style.left =  objContenedor.offsetWidth;
		style.display = 'block';

		// Añadido aquí para que las imágenes no sean más altas que el div.
		if (document.getElementsByName('imgDestacados').item(strId).height > objContenedor.offsetHeight) document.getElementsByName('imgDestacados').item(strId).height = objContenedor.offsetHeight;

		if (offsetLeft + offsetWidth <= 0){
		// El div ha sobrepasado los límites del cuadro que muestra las noticias.
		
			// Oculto el div anterior			
			style.display = 'none';
			// Elimino el intervalo para que no se ejecute más
			window.clearInterval(lngIdTime);
			
			lngIdTime = lngIdTime2;
			lngIdTime2 = 0;
			
			// Cambio el div activo por si el usuario hace un onMouseOver.
			objDivAux = objDivAux2;
			objDivAux2 = null;
			return false;
			
		}else{
			style.left = offsetLeft - lngScrollAmount;
		}	// (offsetTop + offsetHeight <= 0)
		
		if (offsetLeft > 0 && offsetLeft <= lngScrollAmount){

			mintActivo = 0;

			// Finalizo el intervalo para que el usuario pueda leer la noticia.
			window.clearInterval(lngIdTime);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv.id + ', 1)',lngScrollPause,'javascript');
			
			// Obtengo el siguiente div a mover.
			objDiv2 = CambiarDiv(objDiv.id);
			objDivAux2 = objDiv2;
			// Posiciono el nuevo div
			objDiv2.style.left = objContenedor.style.width;
			
			// me aseguro que no hay otro intervalo ejecutándose. 
			window.clearInterval(lngIdTime2);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv2.id + ', 2)',lngScrollPause,'javascript');

		} // (offsetTop > 0 && offsetTop < 3)
			
	}	// with objDiv

	
}

/******************************************************************************************
* Esta función es la que se encarga de que se haga el scroll hacia la Dcha.
* Recibe el id del div que hay que mover.
******************************************************************************************/
function MoverDiv_Right(strId){
var objDiv, objDiv2;
	
	// referencio el objeto para trabajar más cómodamente.
	objDiv = document.getElementById(strId);
	if (objDivAux == null) objDivAux = objDiv;

	with (objDiv){

		if (style.left == '') style.left = -objContenedor.offsetWidth;
		style.display = 'block';

		// Añadido aquí para que las imágenes no sean más altas que el div.
		if (document.getElementsByName('imgDestacados').item(strId).height > objContenedor.offsetHeight) document.getElementsByName('imgDestacados').item(strId).height = objContenedor.offsetHeight;

		if (offsetLeft >= objContenedor.offsetWidth){
		// El div ha sobrepasado los límites del cuadro que muestra las noticias.
		
			// Oculto el div anterior			
			style.display = 'none';
			// Elimino el intervalo para que no se ejecute más
			window.clearInterval(lngIdTime);
			
			lngIdTime = lngIdTime2;
			lngIdTime2 = 0;
			
			// Cambio el div activo por si el usuario hace un onMouseOver.
			objDivAux = objDivAux2;
			objDivAux2 = null;
			return false;
			
		}else{
			style.left = offsetLeft + lngScrollAmount;
		}	// (offsetTop + offsetHeight <= 0)
		
		if (offsetLeft > 0 && offsetLeft <= lngScrollAmount){

			mintActivo = 0;

			// Finalizo el intervalo para que el usuario pueda leer la noticia.
			window.clearInterval(lngIdTime);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv.id + ', 1)',lngScrollPause,'javascript');
			
			// Obtengo el siguiente div a mover.
			objDiv2 = CambiarDiv(objDiv.id);
			objDivAux2 = objDiv2;
			// Posiciono el nuevo div
			objDiv2.style.left = - objContenedor.offsetWidth;
			
			// me aseguro que no hay otro intervalo ejecutándose. 
			window.clearInterval(lngIdTime2);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv2.id + ', 2)',lngScrollPause,'javascript');

		} // (offsetTop > 0 && offsetTop < 3)
			
	}	// with objDiv

	
}

/******************************************************************************************
* Esta función es la que se encarga de que se haga el scroll hacia arriba.
* Recibe el id del div que hay que mover.
******************************************************************************************/
function MoverDiv_Up(strId){
var objDiv, objDiv2;
	
	// referencio el objeto para trabajar más cómodamente.
	objDiv = document.getElementById(strId);
	if (objDivAux == null) objDivAux = objDiv;

	with (objDiv){

		if (style.top == '') style.top =  objContenedor.offsetHeight;
		style.display = 'block';

		// Añadido aquí para que las imágenes no sean más altas que el div.
		if (document.getElementsByName('imgDestacados').item(strId).height > objContenedor.offsetHeight) document.getElementsByName('imgDestacados').item(strId).height = objContenedor.offsetHeight;

		if (offsetTop + offsetHeight <= 0){
		// El div ha sobrepasado los límites del cuadro que muestra las noticias.
		
			// Oculto el div anterior			
			style.display = 'none';
			// Elimino el intervalo para que no se ejecute más
			window.clearInterval(lngIdTime);
			
			lngIdTime = lngIdTime2;
			lngIdTime2 = 0;
			
			// Cambio el div activo por si el usuario hace un onMouseOver.
			objDivAux = objDivAux2;
			objDivAux2 = null;
			return false;
			
		}else{
			style.top = offsetTop - lngScrollAmount;
		}	// (offsetTop + offsetHeight <= 0)
		
		if (offsetTop > 0 && offsetTop <= lngScrollAmount){

			mintActivo = 0;

			// Finalizo el intervalo para que el usuario pueda leer la noticia.
			window.clearInterval(lngIdTime);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv.id + ', 1)',lngScrollPause,'javascript');
			
			// Obtengo el siguiente div a mover.
			objDiv2 = CambiarDiv(objDiv.id);
			objDivAux2 = objDiv2;
			// Posiciono el nuevo div
			objDiv2.style.top = objContenedor.style.height;
			
			// me aseguro que no hay otro intervalo ejecutándose. 
			window.clearInterval(lngIdTime2);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv2.id + ', 2)',lngScrollPause,'javascript');

		} // (offsetTop > 0 && offsetTop < 3)
			
	}	// with objDiv

	
}

/******************************************************************************************
* Esta función es la que se encarga de que se haga el scroll hacia abajo.
* Recibe el id del div que hay que mover.
******************************************************************************************/
function MoverDiv_Down(strId){
var objDiv, objDiv2;
	
	// referencio el objeto para trabajar más cómodamente.
	objDiv = document.getElementById(strId);
	if (objDivAux == null) objDivAux = objDiv;

	with (objDiv){

		if (style.top == '') style.top = -objContenedor.offsetHeight;
		style.display = 'block';

		// Añadido aquí para que las imágenes no sean más altas que el div.
		if (document.getElementsByName('imgDestacados').item(strId).height > objContenedor.offsetHeight) document.getElementsByName('imgDestacados').item(strId).height = objContenedor.offsetHeight;

		if (offsetTop >= objContenedor.offsetHeight){
		// El div ha sobrepasado los límites del cuadro que muestra las noticias.
		
			// Oculto el div anterior			
			style.display = 'none';
			// Elimino el intervalo para que no se ejecute más
			window.clearInterval(lngIdTime);
			
			lngIdTime = lngIdTime2;
			lngIdTime2 = 0;
			
			// Cambio el div activo por si el usuario hace un onMouseOver.
			objDivAux = objDivAux2;
			objDivAux2 = null;
			return false;
			
		}else{
			style.top = offsetTop + lngScrollAmount;
		}	// (offsetTop + offsetHeight <= 0)
		
		if (offsetTop > 0 && offsetTop <= lngScrollAmount){

			mintActivo = 0;

			// Finalizo el intervalo para que el usuario pueda leer la noticia.
			window.clearInterval(lngIdTime);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv.id + ', 1)',lngScrollPause,'javascript');
			
			// Obtengo el siguiente div a mover.
			objDiv2 = CambiarDiv(objDiv.id);
			objDivAux2 = objDiv2;
			// Posiciono el nuevo div
			objDiv2.style.top = - objContenedor.offsetHeight;
			
			// me aseguro que no hay otro intervalo ejecutándose. 
			window.clearInterval(lngIdTime2);
			// Llamo a la función que ejecutará este div pasados xxxx milisegundos.
			setTimeout('ReiniciarDiv(' + objDiv2.id + ', 2)',lngScrollPause,'javascript');

		} // (offsetTop > 0 && offsetTop < 3)
			
	}	// with objDiv

	
}


/******************************************************************************************
* Esta función es la que reinicia el intervalo tras la pausa que hacen las noticias al llegar
*	al top del control contenedor.
* Se pasan el id del div a parar y un 1 o un 2 para limpiar el intervalo correspondiente
*	a lngIdTime o lngIdTime2.
******************************************************************************************/
function ReiniciarDiv(intDiv, intId){
	
	mintActivo = 1;

	if (intId == 1){
		lngIdTime = setInterval('MoverDiv(' + intDiv + ')',lngScrollDelay,'javascript');
	}else{
		lngIdTime2 = setInterval('MoverDiv(' + intDiv + ')',lngScrollDelay,'javascript');
	}

}

/******************************************************************************************
* Esta función es la que cambia la noticia (div) que hace el scroll.
* Se pasa el id del div actual.
******************************************************************************************/
function CambiarDiv(intDiv){
var objDiv = null;

	if (parseInt(intDiv) < arr.length-1) {
	// Todavía no hemos llegado al último div
		objDiv = document.getElementById(parseInt(intDiv) + 1);
	}else{
	// Ya hemos llegado al último div.
		objDiv = document.getElementById('0');
	}
	
	return objDiv;
	
}

/******************************************************************************************
* Esta función es la que para el intervalo cuando se hace el MouseOver.
******************************************************************************************/
function StopScroller(){

	if (lngIdTime != 0) window.clearInterval(lngIdTime);
	if (lngIdTime2 != 0) window.clearInterval(lngIdTime2);

}

/******************************************************************************************
* Esta función es la que reinicia el intervalo cuando se hace el MouseOut.
******************************************************************************************/
function PlayScroller(){
	
	if (mintActivo == 0) return false;
	
	// Me aseguro de que he parado los intervalos anteriores.
	StopScroller();
	if (objDivAux != null) lngIdTime = setInterval('MoverDiv(' + objDivAux.id + ')',lngScrollDelay,'javascript');
	if (objDivAux2 != null) lngIdTime2 = setInterval('MoverDiv(' + objDivAux2.id + ')',lngScrollDelay,'javascript');

}
//<!--FIN DE CÓDIGO QUE PERMITIRÁ MOSTRAR LAS DISTINTAS LÍNEAS EN EL SCROLLER-->
