var esIE = false;
//Detectar el navegador explorer 5.01
if (navigator.appVersion.indexOf("MSIE") != -1) { esIE = true; }
swIE501 = (navigator.appVersion.indexOf("5.01") == (-1))?1:0;
//Detectar el navegador explorer 5.5
swIE55 = (navigator.appVersion.indexOf("5.5") != -1)?1:0;
//Se detecta si es opera
esOpera = (navigator.userAgent.indexOf("Opera") !=-1)?1:0;

//Se sobre escribe el estilo para los casos que tenga Javascript
var newStyle = "<style>"; var endStyle = "</style>";
//todos los navegadores menos intenet explorer 5.01
//if (swIE501) { newStyle += ".bloqueDestacadosInterior { float:left;border:0;width:100%;}";} Cambio por prueba en el Buscador Mapa Judicial
if (swIE501) { newStyle += ".bloqueDestacadosInterior { float:left;width:100%;}";}
else { newStyle += ".ListCentro.class03 #contenidosBox{width:20.1em;}";}
document.write(newStyle+endStyle);

var rounded = {
	corners:function(obj)
	{
		innerContenedora = obj.innerHTML;
		//Declaraccion de las capas de de cierre superior
		objUp = '<div class="b"><div class="l"><div class="r"><div class="t"><div id="bl" class="bl"><div id="br" class="br"><div class="tl"><div class="tr">';
		//Cierres de los div de las capas de border
		objDown = '</div></div></div></div></div></div></div></div>';
		newContenedora = objUp+innerContenedora+objDown;
		obj.innerHTML = newContenedora;
	}
}

var vWindow = {
	/*
		url:direccion de la pagina
		target:donde queremos abrir la pagina
		w: ancho
		h: alto
		l: posiccion respecto a la izquierda de la ventana
		t: posiccion respecto al top de la ventana
		s: scroll
		tb: toolbar
		mb: menu bar
	*/
	abrir:function(url,target,w,h,l,t,s,tb,mb)
	{
		ventana = window.open (url,target,"width="+w+",height="+h+",left="+l+",top="+t+",scrollbars="+s+",toolbar="+tb+",menubar="+mb);
		ventana.focus();
	}
}

// Documento
var documento = {
	getElementsByClass:function(searchClass,node,tag) 
	{
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) 
		{
			if ( pattern.test(els[i].className) ) 
			{
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
}

//Ventana emergente FLASH
var vFlash = {
	abrir:function(url,w,h) { vWindow.abrir(url,'newWin',w+10,h+10,100,100,0,0);}
}

window.onload = function()
{
	
	//definicion de pestanias
	tamTotal = 752; //Anchura total dela plantillas en pixeles
	numItems = 1;
	while (document.getElementById("pest"+numItems) != null) { numItems++; }
	numItems -= 1;
	wWidthPest = parseInt((tamTotal/numItems),10);
	//en el caso que es opera ya que se detecta igual que explorer, pero es diferente a la hora de componer los tamanios
	if (esOpera) { resto = (5*2);}
	else
		{ resto = (esIE)?(5.3*2):(5*2);}
	//si es explorer 5.5
	if (swIE55) { resto = 0;}
	wWidthPest -= resto;
	wWidthPest = ((wWidthPest*5)/80);
		
	for (var i=1;i<=numItems;i++)
	{	objPest = "pest"+i;
		aux_pest = wWidthPest;
		//En los casos de un item seleccionado
		switch (document.getElementById(objPest).className)
		{
			case "simple Sel":
			case "simple ultSel":
			case "Sel":			diferencia = 0.125; break;
			default:			diferencia = 0.04; break;
		}
		aux_pest = wWidthPest - diferencia;
		document.getElementById(objPest).style.width=aux_pest+"em";
	}

	if (document.getElementById('pest1') != null){
		if (swIE55) { document.getElementById('pest1').style.width="11.8em";}
		else {document.getElementById('pest1').style.width = (esIE)?"11.05em":"11.085em";}
	}

	//Declaracion de la capa contenedora a la cual se le quiere aplicar los bordes redondeados
	document.getElementsByClassName=function(className)
	{
		var data = [];
		var node=(document.getElementById("CuerpoCentro"))? document.getElementById("CuerpoCentro"):document;
		var tags = (!document.all) ? node.getElementsByTagName("*") : document.all
		for(var i=0;i<tags.length;i++) { if(tags[i].className == className) data[data.length]=tags[i];}
		return data;
	}
	var myObject = document.getElementsByClassName("bloqueDestacadosInterior");
	for (var i=0;i<myObject.length;i++)
	{
		var objContenedora = myObject[i];
		if (objContenedora != null) { if (swIE501) { rounded.corners(objContenedora); } }
	}	
	
	//Declaracion de los iconos de impresion
	var objIconos = document.getElementById("iconos");
	if (objIconos != null) { objIconos.style.display = "block"; }

	//Objeto de impresion
	var objPrint = document.getElementById("print");
	if (objPrint != null)
	{
		objPrint.onclick = function() { window.print(); return false;}
		//objPrint.onkeypress = objPrint.onclick;
	}
	
	var objVerImpr = document.getElementById("versionImprimible");
	if (objVerImpr != null)
	{
		objVerImpr.onclick = function() { vWindow.abrir(this.href,'winInfo',610,425,100,100,'yes','no','no');return false;}
		//objPrint.onkeypress = objPrint.onclick;
	}
	
	//definicion volver
	if(document.getElementById("linkVolver")!=null) {
		document.getElementById("linkVolver").innerHTML = '<a class="volver" id="goTo" href="#">&gt; volver</a>';
	}
	
	var contGoTo = 1; //Contador de click en la pagina
	var objGoTo = document.getElementById("goTo");
	if (objGoTo != null)
	{
		objGoTo.style.display = "inline";
		objGoTo.onclick = function() { history.go(-(contGoTo)); return false; }
		//objGoTo.onkeypress = objGoTo.onclick;
		classMayor = document.getElementsByClassName("mayor");
		for (var i=0;i<classMayor.length;i++)
		{
			var objContenedora = classMayor[i];
			if (objContenedora != null) { objContenedora.style.display = "inline"; }
		}
		classRaya = document.getElementsByClassName("raya");
		for (var i=0;i<classRaya.length;i++)
		{
			var objContenedora = classRaya[i];
			if (objContenedora != null) { objContenedora.style.display = "inline"; }
		}
	}
	//definicion del boton subir
	var objGoUp = document.getElementById("goUp");
	if (objGoUp != null)
	{
		objGoUp.onclick = function() { contGoTo=2;}
		//objGoUp.onkeypress = objGoUp.onclick;
	}
	
	//Galeria de imagenes
	contIDGal = 0; IDGal = "AmpGal";
	//numero de elementos que hay en la plantilla
	while (document.getElementById(IDGal+contIDGal) != null) { contIDGal++; }
	//quiere decirse que si hay galeria de imagenes
	if (contIDGal > 0)
	{
		for (var i=0;i<contIDGal;i++)
		{
			objGal = document.getElementById(IDGal+i);
			objGal.onclick = function() { 
				vWindow.abrir(this.href,'winInfo',610,425,100,100,'yes','no','no');return false;
			}
		//	objGal.onkeypress = objGal.onclick;
		}
	}//galeria de imagenes
	
	var objAmplia = document.getElementById("AmpGuia");
	if (objAmplia != null)
	{
		objAmplia.onclick = function() { vWindow.abrir(this.href,'winInfo',610,425,100,100,'yes','no','no');return false; }
	//	objAmplia.onkeypress = objAmplia.onclick;
	}
		
	//Links pie pagina
/*
	var contItemsPie = 1;
	while (document.getElementById("LinkPie"+contItemsPie) != null)
	{
		objPie = document.getElementById("LinkPie"+contItemsPie);
		objPie.onclick = function() { vWindow.abrir(this.href,'',596,480,100,100,'yes','no','no'); return false; }
		contItemsPie++;
	}
*/
	//Combo Informacion practica
	var irA = {
		redirect:function(opSel)
		{
			if (opSel != (-1)) { document.location.href = opSel; }
			else
			{ alert("Seleccione una opcion"); }
		}
	}
	var objCmbInfPrac = document.getElementById("imgOpcion");
	if (objCmbInfPrac != null)
	{
		objCmbInfPrac.onclick = function()
		{
			vSelect = document.getElementById("cmbOpcion").value;
			irA.redirect(vSelect);return false;
		}
	//	objCmbInfPrac.onkeypress = objCmbInfPrac.onclick;
	}
	
	//ficha detalle Modulo Atencion al ciudadano
	var objModAten = document.getElementById("modAten");
	if (objModAten != null)
	{
		objModAten.onclick = function() { vWindow.abrir(this.href,'winInfo',610,425,100,100,'yes','no','no'); return false;}
		//objModAten.onkeypress = objModAten.onclick;
	}
	
	//listado de Modelos de Impresisos
	var objModelosImpresos = documento.getElementsByClass("modelo");
	if (objModelosImpresos != null)
	{
		for (k=0;k<objModelosImpresos.length;k++) {
			var herramientas = documento.getElementsByClass("herramientasModelo",objModelosImpresos[k]);
			var enlaces = null;
			for (i=0;i<herramientas.length;i++) 
			{
				enlaces=herramientas[i].getElementsByTagName('a');
				for (j=0;j<enlaces.length;j++)
				{
					enlaces[j].onclick = function () 
					{
						vWindow.abrir(this.href,'winInfo',610,425,100,100,'yes','no','no'); 
						return false;
					}
				}
			}
		}
	}
	if(document.getElementById("contenido_botones2")) {
		document.getElementById("contenido_botones2").style.visibility = "visible";
	}

}

///// Rotar Banners - Requiere JQuery (ESOTOARI) \\\\\

// Llamada a la rotación cuando carga la página (necesario para JQuery)
$(document).ready(function() {
	rotarBanners();
});

//Definimos variables
var imagenActual = 0;
var slideTimeout =  0;
var slideTimeoutInterno =  0;
var slides = [];
var intervaloSlide = 1500;

function rotarBanners() {

      // Si sólo hay una imagen salimos
      if (window.num_caja_roja == undefined) {
            return false;
      } else if (num_caja_roja == 0) {
		return false;
	}

	//Javascript esta activado, mostramos el div de los números
	$(".contadorImagen").css("display", "block");
	// Recorremos los divs de imágenes 
	for (i=0; i<=num_caja_roja;i++){
		// Creamos el array
		slides[i] = document.getElementById('slide'+i);
		// Para todos menso el primero: quitamos las clase oculto (javascript esta activado), y les quitamos opacidad.
		if (i>0) {
			$(".slide").removeClass("oculto");
			slides[i].style.opacity = "0";
			slides[i].style.filter ="alpha(opacity=0)";
		}
		// Nos aseguramos que ningun número este seleccionado
		$(".contadorImagen li:eq(0)").addClass("sel");	
	}
	// Esperamos el intervalo mostradno la 1ª imagen, y llamamos a la función recursiva
	slideTimeoutInicial = setTimeout("cambio_caja_roja()", intervalo);
}

function onClickASlide (numImgPulsada) {
	// Cuando pinchan en un número de imagen paramos el bucle
	clearTimeout(slideTimeout);
	// Por si el fade in anterior no se ejecutó todavía, se borra
	clearTimeout(slideTimeoutInterno);
	// Por si todavia no se ha iniciado la función recursiva
	clearTimeout(slideTimeoutInicial);
	// Llamamos a la función recursiva
	cambio_caja_roja(numImgPulsada);
}

function cambio_caja_roja(toImagen){
	// Ocultamos la imagen actual
	$("#slide"+imagenActual).fadeTo(intervaloSlide, 0);

	// Comprobamos si se ha pinchado en un número, si no, pasamos a la siguiente imagen.
	if (toImagen != undefined) {
		imagenActual = toImagen;
	} else if (imagenActual == num_caja_roja) {
		imagenActual = 0;
	} else {
		imagenActual++;
	}
	
	// Esperamos 1/2tiempo para que la imagen anterior desaparezca y mostramos la siguiente.
	slideTimeoutInterno = setTimeout("$('#slide'+imagenActual).fadeTo(intervaloSlide, 1)", intervaloSlide/2);
	
	// Marcamos como seleccionado el número de imagen que corresponda
	$(".contadorImagen li").removeClass("sel");
	$(".contadorImagen li:eq("+imagenActual+")").addClass("sel");
	
	// Iteramos
	slideTimeout = setTimeout("cambio_caja_roja()", intervalo+intervaloSlide);
}