﻿// JavaScript Document


var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

// La redireccion desde cbensanche es dentro de un frame. Esto lo evita
function sal_frames()
{
   if ( window.parent!= window.self ) { ; 
      top.location.href = window.location.href
   }
}

// -------------------------------------------------------------------------------
function max (x,y) { return (x>y?x:y); }
function min (x,y) { return (x<y?x:y); }

// Recibe un objeto imagen le cambia el ultimo caracter a la
// imagen, de forma que si tenia un 1 ahora tenga un 0 y viceversa
function cambia_img(obj)
{
	src= obj.src;
	nom= src.substring (0, src.length-5);
	ext= src.substring (src.length-4, src.length);
	if (src.charAt (src.length-5) == '0')
	{
		obj.src= nom + "1"+ ext;
	}
	else {
		obj.src= nom + "0" + ext;
	}
}

// Va a otra pagina. 
function ir(dir)
{
	// La abre en otra ventana si es un pdf y el navegador no es opera
	nueva= 1;
   pospdf= dir.lastIndexOf (".pdf");
   if (pospdf == -1) { pospdf= dir.lastIndexOf (".PDF"); }
   if (pospdf != -1) {
			nueva= 1;
	}
	else {
		nueva= 0;
	}
	if (nueva == 1)
	{
		opts= '\'menubar=0,width='+document.body.clientWidth+',height='+document.body.clientHeight+',resizable=yes\'';
		window.open(dir,'pdf',opts);
	}
	else {
		location.href= dir;
	}
}


// Pone en el objeto imagen un jpg aleatorio que no haya puesto antes

var imagenes_usadas=null;
function rand_img (id_objimg, id_obja)
{
	obj= document.getElementById (id_objimg);
	obja= document.getElementById (id_obja);
	if (imagenes_usadas == null)
	{
		imagenes_usadas= new Array(fotos_inicio.length/3);//NUM_IMGS_ALEATORIAS);
		for (var i=0; i<fotos_inicio.length/3; i++)
		{
			imagenes_usadas[i]= 0;
		}
	}

	// Busca una aleatoria que no se haya usado
	num= Math.floor(Math.random() * imagenes_usadas.length);
	num_img=num;
	while ((imagenes_usadas[num_img] == 1) && (num_img<imagenes_usadas.length)) {
		num_img++;
	}
	
	if (num_img>= imagenes_usadas.length) {
		num_img=0;
		while ((imagenes_usadas[num_img] == 1) && (num_img<num)) {
			num_img++;
		}
	}
	
	if (imagenes_usadas[num_img] == 1) {
		// Ya se han usado todas. Hay que inicializarlas de nuevo
		imagenes_usadas= null; 
		rand_img (id_objimg, id_obja);
		
	}
	else {
		obja.title= fotos_inicio[num_img*3+1];
		obja.href= fotos_inicio[num_img*3+2];
		obj.src= fotos_inicio[num_img*3+0];//"imgs/rand/rand_"+(num_img+1)+".jpg";
		imagenes_usadas[num_img]= 1;
	}

}

// Inicia un bucle de cambio de imagenes cada TMP_CAMBIO_IMAGENES segundos. 
var imagen_a_cambiar=3;
function bucle_cambio_imagenes()
{
	rand_img ("img"+imagen_a_cambiar, "a"+imagen_a_cambiar);
	
// Esto hace que vaya cambiando las dos   
//	if (imagen_a_cambiar == 1) { imagen_a_cambiar= 3; }
//	else { imagen_a_cambiar= 1; }
	
	setTimeout ("bucle_cambio_imagenes();", TMP_CAMBIO_IMAGENES );
}

// script que se inicia al cargar la pantalla
function inicializacion ()
{
	setTimeout ("bucle_cambio_imagenes();", 3000 );
	setTimeout ("play_flash_entrada ();", 5000);
	setTimeout ("display_calendario();", TMP_CAMBIO_MENSAJES);
	setTimeout ("rotar_texto();", 1000);
	
	// Precarga las imagenes 
	var i; 
	var imagenes = new Array(
		"imgs/p1_1.gif", 
		"imgs/p2_1.gif", 
		"imgs/p3_1.gif", 
		"imgs/p4_1.gif", 
		"imgs/p5_1.gif", 
		"imgs/p6_1.gif", 
		"imgs/p7_1.gif", 
		"imgs/texto1.png", 
		"imgs/texto2.png", 
		"imgs/texto3.png", 
		"imgs/texto4.png", 
		"imgs/texto5.png", 
		"imgs/texto6.png", 
		"imgs/boton_video_0.gif", 
		"imgs/boton_video_1.gif" 
		); 
	var lista_imagenes = new Array(); 
	for(i in imagenes){ 
    	lista_imagenes[i] = new Image(); 
     	lista_imagenes[i].src = imagenes[i]; 
	} 
}

function ver_propiedades(obj)
{
	win= window.open("", "",'resizable,width=600,height=600');
	win.document.open();
	win.document.write("<html><head></head><body>");
	result= obj.name +"\n";
	for (var i in obj)
	{
		result += i +": "+ obj[i] +"<br>";
	}
	
	win.document.write (result);
	win.document.write("</body></html>");
	win.document.close();
}


// reproduce el video que se le indica
var reproduciendo="";
function play_flash (video)
{
	play_flash_2 (video, ANCHO_VIDEO_FLASH, ALTO_VIDEO_FLASH, 2, getScrollXY()[1] + 100);
}
function play_flash_center(video)
{
 	play_flash_2 (video, ANCHO_VIDEO_FLASH, ALTO_VIDEO_FLASH, (document.body.clientWidth-ANCHO_VIDEO_FLASH)/2, getScrollXY()[1] + 100);
}
function play_flash_sinmover(video)
{
	c= document.getElementById ("flashcontainer");
 	play_flash_2 (video, ANCHO_VIDEO_FLASH, ALTO_VIDEO_FLASH, c.style.left, c.style.top);
}
function play_flash_nisu(video)
{
 	play_flash_2 (video, ANCHO_VIDEO_FLASH, ALTO_VIDEO_FLASH, (document.body.clientWidth-ANCHO_VIDEO_FLASH-50), getScrollXY()[1] + 150);
}
function play_flash_2 (video, ancho, alto, posx, posy)
{	
	c= document.getElementById ("flashcontainer");
	c.style.display="none";
	c.style.left= posx;
	c.style.top= posy;

	var so = new SWFObject("FLVPlayer_Progressive.swf", "video", ancho, alto, "7", "#336699");
	so.addVariable ("MM_ComponentVersion", "1");
	so.addVariable ("skinName", "Clear_Skin_2");
	so.addVariable ("streamName", video);
	so.addVariable ("autoPlay", "true");
	so.addVariable ("autoRewind", "true");
	so.write("flashcontainer");

	if (reproduciendo != video) {
		reproduciendo= video
		c.style.display="block";
	}
	else {
		reproduciendo="";
	}
}

// Reproduce el video en la pantalla principal
function play_flash_entrada()
{	
	document.getElementById ("img2").style.display="none";
	c= document.getElementById ("flashcontainer");
	c.style.display="block"; 
//	var so = new SWFObject("videos/video_entrada.swf", "video", ANCHO_VIDEO_FLASH, ALTO_VIDEO_FLASH, "7", "#336699");
//	var so = new SWFObject("videos/mates.swf", "video", 250, 200, "7", "#336699");
//	var so = new SWFObject("videos/NuestroJuego.swf", "video", 250, 200, "7", "#336699");
	var so = new SWFObject("videos/VideoDiciembreEnsanchebaja.swf", "video", 250, 190, "7", "#336699");
//	var so = new SWFObject("videos/Presentacion_Escuela.swf", "video", 250, 190, "7", "#336699");   
//	var so = new SWFObject("FLVPlayer_Progressive.swf", "video", 250, 200, "7", "#336699");
	so.addVariable ("MM_ComponentVersion", "1");
	so.addVariable ("skinName", "Clear_Skin_2");
//	so.addVariable ("streamName", "videos/NuestroJuego.flv");
	so.addVariable ("autoPlay", "true");
	so.addVariable ("autoRewind", "true");

	so.addParam ("wmode","opaque");
   so.write("flashcontainer");
}


// Cambia el gif del esque del proyecto
var temp_para_esquema= 0;
var temp_rayo_esquema= 0;
function para_motor ()
{
	document.getElementById ('esquema').src= "imgs/esquema_fijo.gif";
	temp_para_esquema= 0;
}
function cambia_rayo ()
{
	document.getElementById ('esquema').src= "imgs/esquema_movil.gif";
	temp_rayo_esquema= 0;
}
function activa_motor (rayo)
{
	if (temp_para_esquema != 0)
	{
		clearTimeout (temp_para_esquema);
		temp_para_esquema=0;
	}
	if (temp_rayo_esquema != 0)
	{
		clearTimeout (temp_rayo_esquema);
		temp_rayo_esquema= 0;
	}

    if (rayo == 1)
	{
		document.getElementById ("esquema").src= "imgs/esquema_rayo.gif";
		temp_rayo_esquema= setTimeout ("cambia_rayo();", TMP_RAYO_ESQUEMA);
	}
	else {
		document.getElementById ("esquema").src= "imgs/esquema_movil.gif";
	}
	temp_para_esquema= setTimeout ("para_motor();", TMP_PARA_ESQUEMA);
}

// Se le llama cuando el cursor entre en la imagen obj. Activa la 
// imagen alternativa y muestra el bocadillo posicionandolo sobre 
// el objeto
function entra (obj, event)
{

	boc= document.getElementById ("bocadillo");
	num= obj.id.charAt(1);
	boc.src= "imgs/texto"+num+".png";

	boc.style.left= document.body.scrollLeft+event.x -event.offsetX + desplazamientos[2*(num-1)];
	boc.style.top= document.body.scrollTop+event.y  -event.offsetY + desplazamientos[2*(num-1)+1];
	
	boc.style.display="block";
	cambia_img (obj);
}

// Se le llama cuando el cursor sale an la imagen obj. Activa la 
// imagen alternativa y oculta el bocadillo
// el objeto
function sal (obj, event)
{
	boc= document.getElementById ("bocadillo");
	boc.style.display="none";
	cambia_img (obj);
}


function entra_txt (obj, event)
{

	boc= document.getElementById ("bocadillo");
	num= obj.id.charAt(1);
	boc.src= "imgs/texto"+num+".png";
	boc.style.left= document.body.scrollLeft+event.x -event.offsetX + desplazamientos[2*(num-1)];
	boc.style.top= document.body.scrollTop+event.y  -event.offsetY + desplazamientos[2*(num-1)+1];
	
	boc.style.display="block";
}
function sal_txt (obj, event)
{
	boc= document.getElementById ("bocadillo");
	boc.style.display="none";
}

// --- Actualizacion de los mensajes de texto (calendario)
var num_calendario = 0;

function display_text(id, str) {
   rep= str;
  if (NS4) {
    with (document[id].document) {
      open();
      write(rep);
      close();
    }
  } else {
    document.all[id].innerHTML = rep;
  }
}
function display_calendario ()
{
   if (texto_rot.length > num_calendario)
   {
      display_text("banner", texto_rot[num_calendario]);
      num_calendario++;
      if (num_calendario >= texto_rot.length) { num_calendario= 0; }
      
      setTimeout ("display_calendario();", TMP_CAMBIO_MENSAJES);
   }
}

//--- Acualizacion del texto rotatorio
var char_rot=0;
var num_visibles=0;
function rotar_texto()
{
	if (num_visibles < NUM_LETRAS_ROTAR_TEXTO) { num_visibles+=2; }
	else if (texto_rotativo.length-char_rot < num_visibles) { 
		num_visibles-=2;
		if (num_visibles < 0) { num_visibles= 0; }
	}
	char_fin= char_rot+num_visibles;
	if (char_fin >= texto_rotativo.length) { char_fin= texto_rotativo.length-1;}
	if (char_fin < NUM_LETRAS_ROTAR_TEXTO) { 
		esp_ini= cadena_de_espacios (2*(NUM_LETRAS_ROTAR_TEXTO - num_visibles));
		esp_fin="";
		inicio=1;
	}
	else {
		esp_ini="";
		esp_fin= cadena_de_espacios (2*(NUM_LETRAS_ROTAR_TEXTO - num_visibles))+"";
		inicio=0;
	}
	texto= esp_ini+texto_rotativo.substring (char_rot, char_fin)+esp_fin;
	display_text("msj_rot", texto);

	if (char_fin >= texto_rotativo.length-1) 
   { 
      char_rot=0; 
		num_visibles=0;
      setTimeout ("rotar_texto();",TMP_ROTAR_TEXTO*10);
	} else {
      if (inicio==0) {
            char_rot+=2;
      }
      setTimeout ("rotar_texto();",TMP_ROTAR_TEXTO);
   }
}

// Devuelve una cadena de <nbsp>
function cadena_de_espacios(num)
{
	ret="";
	for (i=0; i< num; i++) {
//		ret += "&nbsp;";
	}
	return ret;
}

// Convierte una cadena que puede tener acentos o ñ a una en html con las entidades correspondientes
// NO HACE FALTA USARLO
function cadena_a_html(str)
{
	var i, alt, cad;
	cad="";
	for (i=0; i < str.length; i++)
	{
		switch (str.charCodeAt(i))
		{
			case 192: alt="&Aacute;"; break;
			case 201: alt="&Eacute;"; break;
			case 205: alt="&Iacute;"; break;
			case 209: alt="&Ntilde;"; break;
			case 211: alt="&Oacute;"; break;
			case 218: alt="&Uacute;"; break;
			case 225: alt="&aacute;"; break;
			case 233: alt="&eacute;"; break;
			case 237: alt="&iacute;"; break;
			case 241: alt="&ntilde;"; break;
			case 243: alt="&oacute;"; break;
			case 250: alt="&uacute;"; break;
			default: alt= str.charAt(i); break;
		}
		
		cad += alt;
	}
	
	return cad;
}