/*
   Funciones javascript para uso genérico 
   Javascript generic functions 

   Nova Comunicación. http://www.internorma.es 

   Puedes usar y modificar estas funciones libremente,
   pero notificanos tus mejoras a info@internorma.es

   You may freely use and alter these functions
   but, please, notify your changes to info@internorma.es
*/

/*--------------------------------------------------------------------*/
/* Variables de uso común. */
/* Common use variables. */

var IE = /*@cc_on!@*/false;	
/*--------------------------------------------------------------------*/
/* Abre el lugar indicado por loc en una ventana emergente de ancho w y alto h */
/* Opens a new popup window of w width and h height with the location set in loc. */

function abrir(loc, w, h)
{
	if(typeof(w)=='undefined' || typeof(h)=='undefined')
	{
		w = 0.8 * screen.width;
		h = 0.7 * screen.height;
	}
	var L= (screen.width - w)/2;
	var T= (screen.height - h)/2;
	var nueva=open(loc,"","width="+w+", height="+h+", left="+L+", top="+T+", toolbar=0, resize=0, scrollbars=0, status=0, navbar=0");
}
/*--------------------------------------------------------------------*/
/* Borra los elementos de una matriz (vector). */
/* Erases the elements of a matrix (vector). */

function BorrarArray(matriz)
{
  for(;matriz.length;) matriz.shift();
}


/*--------------------------------------------------------------------*/
/* Genera un número aleatorio entre x e y. */
/* Returns a random numbre between x and y */

function randNum(x, y)
{
  var range = y - x + 1;
  return Math.floor(Math.random() * range) + x;
}

/*--------------------------------------------------------------------*/
/* IMAGENES * IMAGES */

/*--------------------------------------------------------------------*/
/* SCROLLDIV */

var scrollid=1;
var timerUp,timerDown;
defaultStep=1
step=defaultStep

/*- - - - - - - - - - - - - - -*/
/* Desplaza contínuamente un div hacia abajo con incremento de "salto" */
/* Continuously moves a div upward with a step of "salto" */

function scrollDivDown(scrollid,salto)
{
  step = salto ? salto : step; 
  var T=document.getElementById(scrollid).scrollTop;
  T+=step;
  document.getElementById(scrollid).scrollTop=T;
  if((T+document.getElementById(scrollid).clientHeight)<document.getElementById(scrollid).scrollHeight) timerDown=setTimeout("scrollDivDown(scrollid,step)",20)
}


/*- - - - - - - - - - - - - - -*/
/* Desplaza contínuamente un div hacia arriba con incremento de "salto" */
/* Continuously moves a div downward with a step of "salto" */

function scrollDivUp(scrollid,salto)
{
  step = salto ? salto : step; 
  var T=document.getElementById(scrollid).scrollTop;
  T-=step;
  document.getElementById(scrollid).scrollTop=T;
  if(T>0) timerUp=setTimeout("scrollDivUp(scrollid,step)",20)
}


/*- - - - - - - - - - - - - - -*/
/* Detiene el scroll */
/* Scroll stopping */

function noScroll()
{
  step=defaultStep;
  clearTimeout(timerUp);
  clearTimeout(timerDown);
}


/*--------------------------------------------------------------------*/
/* FECHA / DATE */
 
/* Escribe la fecha en formato: www, dd de mmmm de yyyy */
/* Writes the date in the format: www, dd de mmmm de yyyy */
/* MostrarFecha --> Español / Spanish */
/* ShowDate --> Inglés / English */

function MostrarFecha()  
{  
  var nombres_dias = new Array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado")  
  var nombres_meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")  
  var fecha_actual = new Date()  
  dia_mes = fecha_actual.getDate()     //dia del mes / Day of month 
  dia_semana = fecha_actual.getDay()       //dia de la semana / Day of week 
  mes = fecha_actual.getMonth() + 1  
  anio = fecha_actual.getFullYear()  
  document.write(nombres_dias[dia_semana] + ", " + dia_mes + " de " + nombres_meses[mes - 1] + " de " + anio)  
}  

/*- - - - - - - - - - - - - - -*/
function ShowDate()  
{  
  var day_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")  
  var month_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")  
  var today = new Date()  
  month_day = today.getDate()     //dia del mes /Day of month 
  week_day = today.getDay()       //dia de la semana / Day of week 
  month = today.getMonth() + 1  
  year = today.getFullYear()  
  document.write(day_names[week_day] + ", " + month_names[month - 1] + " " + month_day + " " + year)  
}  

/*--------------------------------------------------------------------*/
/* Muestra un elemento oculto. */
/* Shows a hidden element. */

function showid(id)
{
  document.getElementById(id).style.display="block";
}

/*--------------------------------------------------------------------*/
/* Oculta un elemento visible. */
/* Hides a visible element. */

function hideid(id)
{
  document.getElementById(id).style.display="none";
}

/*--------------------------------------------------------------------*/
function hideidwait(id)
{
  setTimeout('hideid("'+id+'")', 500);
}

/*--------------------------------------------------------------------*/
/* Muestra un elemento y oculta el resto.
   id: nº de elemento a mostrar.
   maxid: nº más alto para elemento.
   Los elementos se identifican por un número (id="n").
   El menor valor para id debe ser 1.
*/
/* Shows as element and hides the others.
   id: nº of element to show.
   maxid: higher element number.
   Elements are identified by a number (id="n").
   The lowest value for id must be 1.
*/


function SwitchShownId(id)
{
	if(typeof(visibleid)=='undefined') visibleid=1;
	document.getElementById(visibleid).style.display="none";
	visibleid=id;
    document.getElementById(visibleid).style.display="block";
	scrollid=id;
}

function switchVisibility(id)
{
	if(document.getElementById(id).style.visibility == "hidden") document.getElementById(id).style.visibility = "visible";
    else document.getElementById(id).style.visibility = "hidden";
}

function opacity(id,pct)
{
	if (navigator.appName.indexOf("Microsoft")!= -1)
		document.getElementById(id).style.filter="alpha(opacity="+pct.toString()+")";
	else
		document.getElementById(id).style.opacity=pct/100;
}


/*--------------------------------------------------------------------*/
/* Muestra un elemento y oculta el resto.
   id: nº de elemento a mostrar.
   maxid: nº más alto para elemento.
   Los elementos se identifican por un número (id="n").
   El menor valor para id debe ser 1.
*/
/* Shows as element and hides the others.
   id: nº of element to show.
   maxid: higher element number.
   Elements are identified by a number (id="n").
   The lowest value for id must be 1.
*/

var interval = 10000;
var imageNum = 0;
imageArray = new Array();
var totalImages = 0;
var imgCurr = "img0";
var imgNext = "img1";
var imgPrev = "img2";
transp=100;
transp2=0;
var stopped =0;
var recur_call;
var timerID;
var timerID_T;
var direction = 1;

function prevImage(img) {
	if(!imageNum--) imageNum= totalImages-1;
	document.getElementById(img).src = imageArray[imageNum];
}

function nextImage(img) {
	if(++imageNum == totalImages) imageNum= 0;
	document.getElementById(img).src = imageArray[imageNum];
}


function switchImage(cont, step, interval)
{
	var auxImg, newImg;
	
	if(typeof(interval)=='undefined') interval = 3000;
	else if(interval<3000) interval = 3000;

	if(typeof(cont)=='undefined') cont = 1;
	if(typeof(step)=='undefined') step = 2;

	if(!totalImages)
	{
		window.history.back();
		return;
	}
	clearTimeout(timerID);
	clearTimeout(timerID_T);

	recur_call = "switchImage("+cont+","+step+","+interval+")";

	switch (step) {
		case 0:
			return;
		case -1:
			newImg = imgPrev;
			break;
		case 1:
		case 2:
			newImg = imgNext;
			break;
	}

	if (transp)
	{
		transp-=4;
		transp2+=4;
		if (navigator.appName.indexOf("Microsoft") != -1) {
			document.getElementById(imgCurr).style.filter = "alpha(opacity=" + transp.toString() + ")";
			document.getElementById(newImg).style.filter = "alpha(opacity=" + transp2.toString() + ")";
		}
		else {
			document.getElementById(imgCurr).style.opacity = transp / 100;
			document.getElementById(newImg).style.opacity = transp2 / 100;
		}
	}
	if (transp) timerID_T = setTimeout(recur_call, 20);
	else
	{
//		document.getElementById("kk").innerHTML = imageNum + ' ' + newImg + ' ' + step;
		if (step == 2) imageNum = randNum(0, totalImages - 1);
		else {
			imageNum += step;
			if(direction != step)
			{
				imageNum += 2*step;
				direction = step;
			}
			if (imageNum < 0) imageNum = totalImages - 1;
			if (imageNum >= totalImages) imageNum = 0;
		}
		switch (step) {
			case -1:
				auxImg = imgNext;
				imgNext = imgCurr;
				imgCurr = newImg;
				document.getElementById(auxImg).src = imageArray[imageNum];
				imgPrev = auxImg;
				break;
			case 1:
			case 2:
				auxImg = imgPrev;
				imgPrev = imgCurr;
				imgCurr = newImg;
				document.getElementById(auxImg).src = imageArray[imageNum];
				imgNext = auxImg;
				break;
		//	document.getElementById("kk").innerHTML = imageNum  + imgCurr ; 
		}	
		transp = 100;
		transp2 = 0;
		if (cont) timerID = setTimeout(recur_call, interval);
	}
} 

var timerSH;
function showHide(id, interval, transp)
{
	var recur_call = "showHide('"+id+"', "+interval+", "+(++transp)+")";
    var transp2;
	
	transp2 =  transp<0 ? 100 + transp : 100-transp;
	if (navigator.appName.indexOf("Microsoft") != -1) {
		document.getElementById(id).style.filter = "alpha(opacity=" + transp2.toString() + ")";
	}
	else {
		document.getElementById(id).style.opacity = transp2 / 100;
	}

	if (transp) timerSH = setTimeout(recur_call, 20);
	else 
	{
		if (transp2) timerSH = setTimeout(recur_call, interval);
		else clearTimeout(timerSH);
	}
} 

/*
 
 function switchImage(step)
{
	var recur_call = "switchImage("+step+")";
	if (transp == 100) {
		img = imgA;
		if (!step) 
			imageNum = randNum(0, totalImages - 1);
		else {
			imageNum += step;
			if (imageNum < 0) 
				imageNum = totalImages - 1;
			if (imageNum == totalImages) 
				imageNum = 0;
		}
		document.getElementById(img).src = imageArray[imageNum];
		imgA = imgB;
		imgB = img;
	}
	transp--;
	transp2++;	
	if (navigator.appName.indexOf("Microsoft")!= -1)
	{
		document.getElementById(imgA).style.filter="alpha(opacity="+transp.toString()+")";
		document.getElementById(imgB).style.filter="alpha(opacity="+transp2.toString()+")";
	}
	else
	{
		document.getElementById(imgA).style.opacity=transp/100;
		document.getElementById(imgB).style.opacity=transp2/100;
	}
	if (transp) timerID = setTimeout(recur_call, 20);
	else 
	{
		transp = 100;
		transp2=0;
		timerID = setTimeout(recur_call, interval);
	}
} 
*/

function gotoImage(num)
{
	imageNum=num;
	switchImage(0);
}

function centerV(id, container)
{
  var H = document.getElementById(container).height;
  var h = document.getElementById(id).height;
  var margen= (H - h) / 2;
  document.getElementById(id).style.Top=margen.toString();
}


/*--------------------------------------------------------------------*/



function wait()
{}

