var imagenBloqueActual = 1;
var optionsRemoved = new Array();
var optionsPeriodoRemoved = new Array();
var optionsAnyoDesdeRemoved = new Array();
var orderOptionsAnyoDesdeRemoved = new Array();
var optionsAnyoHastaRemoved = new Array();
var orderOptionsAnyoHastaRemoved = new Array();

function cambiaBloqueImagen(capa, bloque){
	
	if ( (capa==1 || capa==8 || capa==15) && bloque){
		$('.paginadorGaleria'+(capa-7)).hide();
		$('.paginadorGaleria'+(capa+7)).hide();
		$('.paginadorGaleria'+capa).fadeIn();
	}
	
	
	if (capa!=imagenBloqueActual){
		$('#cuadroBloqueImagen' + imagenBloqueActual).toggle();
		$('#cuadroBloqueImagen' + capa).toggle();
		
		$('#nombreImagenBloque' + imagenBloqueActual).toggle();
		$('#nombreImagenBloque' + capa).toggle();
		
		$('#pieImagenBloque' + imagenBloqueActual).toggle();
		$('#pieImagenBloque' + capa).toggle();
		
		imagen1 = $('#imagenMiniGaleria' + capa).attr ('src');
		imagen = imagen1.substring(21,imagen1.indexOf('/tipo/'));
		extension = imagen1.substring(imagen1.indexOf('/tipo/')+6,imagen1.indexOf('/w/'));
		
		$('#botonImagenBloque' + imagenBloqueActual).attr ('src', url + '/img/galeria_' + imagenBloqueActual + '_off.gif');		
		$('#botonImagenBloque' + capa).attr ('src', url + '/img/galeria_' + capa + '_on.gif');					
			
		imagenBloqueActual = capa;	
		
		navImagenesBloque(imagen, extension);
	}
	
}

function actualizaCal (mes, anio, dia){		
	 	$.getJSON(url + "/actualidad/actualizacal/mes/" + mes + "/anio/" + anio + "/dia/" + dia, function(json){
			$("#cuadroBloqueCalendarioContenido").html(json.calendario);
			$("#listadoActualidadDestacado").html(json.listado);
			tooltip();
	});
}

tooltipClass=".tooltip"; 
tooltip2Class=".tooltip2"; 

function str_replace(search, replace, subject) { 
	return subject.split(search).join(replace); 
}

this.tooltip=function(){
	xOffset=50;
	yOffset=20;
	fadeInTime=400;
	jQuery(tooltipClass).hover(
			function(e){
				this.t=this.title;
				this.title="";
				this.t=str_replace("::", "<br />", this.t);
				this.t=str_replace("[!]", "<span class='tooltipTitle'>", this.t);
				this.t=str_replace("[/!]", "</span><br />", this.t);
				this.t=str_replace("[", "<", this.t);this.t=str_replace("]", ">", this.t);
				if(this.t != "") {
					jQuery("body").append("<div id='tooltip'>"+this.t+"</div>");
					jQuery("#tooltip").css("top",(e.pageY-xOffset)+"px").css("left",(e.pageX+yOffset)+"px").fadeIn(fadeInTime);
				}
			},
			function(){
				this.title=this.t;
				jQuery("#tooltip").remove();
			});
	jQuery(tooltipClass).mousemove(
			function(e){
				jQuery("#tooltip").css("top",(e.pageY-xOffset)+"px").css("left",(e.pageX+yOffset)+"px");
			});
	jQuery(tooltip2Class).hover(
			function(e){
				this.t=this.title;
				this.title="";
				this.t=str_replace("::", "<br />", this.t);
				this.t=str_replace("[!]", "<span class='tooltipTitle'>", this.t);
				this.t=str_replace("[/!]", "</span><br />", this.t);
				this.t=str_replace("[", "<", this.t);this.t=str_replace("]", ">", this.t);
				if(this.t != "") {
					jQuery("body").append("<div id='tooltip2'>"+this.t+"</div>");
					jQuery("#tooltip2").css("top",(e.pageY-xOffset)+"px").css("left",(e.pageX+yOffset)+"px").fadeIn(fadeInTime);
				}
			},
			function(){
				this.title=this.t;
				jQuery("#tooltip2").remove();
			});
	jQuery(tooltip2Class).mousemove(
			function(e){
				jQuery("#tooltip2").css("top",(e.pageY-xOffset)+"px").css("left",(e.pageX+yOffset)+"px");
			});
	};
	
jQuery(document).ready(function(){
	tooltip();
	jQuery('.imagenPrincipalLinkBloque').lightBox({
		overlayBgColor: '#FFF',
		overlayOpacity: 0.6,
		imageLoading: url + '/imagenes/loading.gif',
		imageBtnClose: url + '/imagenes/close.gif',
		imageBtnPrev: url + '/imagenes/prev.gif',
		imageBtnNext: url + '/imagenes/next.gif',
		containerResizeSpeed: 350
	});		
		
});


function navImagenesBloque(imagen, tipo){		
	
	jQuery('.imagenPrincipalLinkBloque').attr('href', url + '/galeria/imagenes/'+imagen+'.'+tipo);	
	
	jQuery('.imagenPrincipalLinkBloque').lightBox({
		overlayBgColor: '#FFF',
		overlayOpacity: 0.6,
		imageLoading: url + '/imagenes/loading.gif',
		imageBtnClose: url + '/imagenes/close.gif',
		imageBtnPrev: url + '/imagenes/prev.gif',
		imageBtnNext: url + '/imagenes/next.gif',
		containerResizeSpeed: 350
	});	
	
}

function restriccionCategoria(categoria) {
	var nombre_cat = categoria.value;
	var tipos_select = document.getElementById('Tipo');
	var periodo_select = document.getElementById('Periodo');
	var anyo_desde = document.getElementById('anyoDesde');
	var anyo_hasta = document.getElementById('anyoHasta');
	
	for (i=0;i<optionsRemoved.length;i++) {
		tipos_select.options.add(optionsRemoved[i], tipos_select.options.length);
	}
	optionsRemoved = new Array();
	for (i=0;i<optionsPeriodoRemoved.length - 1;i++) {
		periodo_select.options.add(optionsPeriodoRemoved[i], i);
	}
	if (optionsPeriodoRemoved.length>0) {
		/*var temp_cbo_opt = periodo_select.options[periodo_select.options.length]
		periodo_select.options[periodo_select.options.length] = null;*/
		periodo_select.options.add(optionsPeriodoRemoved[optionsPeriodoRemoved.length-1], periodo_select.options.length);
		//periodo_select.options.add(temp_cbo_opt, periodo_select.options.length);
	}
	
	optionsPeriodoRemoved = new Array();
	for (i=0;i<optionsAnyoDesdeRemoved.length;i++) {
		anyo_desde.options.add(optionsAnyoDesdeRemoved[i], orderOptionsAnyoDesdeRemoved[i]);
	}
	optionsAnyoDesdeRemoved = new Array();
	orderOptionsAnyoDesdeRemoved = new Array();
	for (i=0;i<optionsAnyoHastaRemoved.length;i++) {
		anyo_hasta.options.add(optionsAnyoHastaRemoved[i], orderOptionsAnyoHastaRemoved[i]);
	}
	optionsAnyoHastaRemoved = new Array();
	orderOptionsAnyoHastaRemoved = new Array();
	
	if (nombre_cat == "Intervenciones de partido") {
		var options_only = new Array(".............", "Mitines y Actos", "Intervenci\u00F3n", "Discursos");
		var existe = false;
		for (i=0;i<tipos_select.options.length;i++) {
			existe = false;
			for (j=0;j<options_only.length;j++) {
				if (tipos_select.options[i].label == options_only[j]) {
					existe = true;	
				}
			}
			if (!existe) {
				optionsRemoved[optionsRemoved.length] = tipos_select.options[i];
				tipos_select.options[i] = null;
				i--;		
			}
		}
	}
	if (nombre_cat == "Intervenciones Institucionales") {
		var options_only = new Array(".............", "Intervenci\u00F3n", "Discursos", "Conferencias de Prensa", "Palabras / Discursos / Conferencias", "Declaraciones / Mensajes");
		var existe = false;
		for (i=0;i<tipos_select.options.length;i++) {
			existe = false;
			for (j=0;j<options_only.length;j++) {
				if (tipos_select.options[i].label == options_only[j]) {
					existe = true;	
				}
			}
			if (!existe) {
				optionsRemoved[optionsRemoved.length] = tipos_select.options[i];
				tipos_select.options[i] = null;
				i--;		
			}
		}
		var options_only = new Array("3");
		for (i=0;i<periodo_select.options.length;i++) {
			existe = false;
			for (j=0;j<options_only.length;j++) {
				if (periodo_select.options[i].value == options_only[j]) {
					existe = true;	
				}
			}
			if (!existe) {
				optionsPeriodoRemoved[optionsPeriodoRemoved.length] = periodo_select.options[i];
				periodo_select.options[i] = null;
				i--;		
			}
		}
		var optionsRemovedIndex = 0;
		for (i=0;i<anyo_desde.options.length;i++) {
			if ((anyo_desde.options[i].value < "1996") || (anyo_desde.options[i].value > "2004")) {
				optionsAnyoDesdeRemoved[optionsAnyoDesdeRemoved.length] = anyo_desde.options[i];
				orderOptionsAnyoDesdeRemoved[orderOptionsAnyoDesdeRemoved.length] = i + optionsRemovedIndex;
				anyo_desde.options[i] = null;
				optionsRemovedIndex++;
				i--;		
			}
		}
		for (i=0;i<anyo_desde.options.length;i++) {
			if (anyo_desde.options[i].value == "1996") {
				anyo_desde.options[i].selected = true;	
			}
		}
		var optionsRemovedIndex = 0;
		for (i=0;i<anyo_hasta.options.length;i++) {
			if ((anyo_hasta.options[i].value < "1996") || (anyo_hasta.options[i].value > "2004")) {
				optionsAnyoHastaRemoved[optionsAnyoHastaRemoved.length] = anyo_hasta.options[i];
				orderOptionsAnyoHastaRemoved[orderOptionsAnyoHastaRemoved.length] = i + optionsRemovedIndex;
				anyo_hasta.options[i] = null;
				optionsRemovedIndex++;
				i--;		
			}
		}		
	}
	if (nombre_cat == "Intervenciones Parlamentarias") {
		var options_only = new Array(".............", "Intervenci\u00F3n", "Discursos", "Declaraciones / Mensajes", "Discurso Investidura", "Debates monogr\u00E1ficos", "Estado Naci\u00F3n", "Preguntas / Respuestas");
		var existe = false;
		for (i=0;i<tipos_select.options.length;i++) {
			existe = false;
			for (j=0;j<options_only.length;j++) {
				if (tipos_select.options[i].label == options_only[j]) {
					existe = true;	
				}
			}
			if (!existe) {
				optionsRemoved[optionsRemoved.length] = tipos_select.options[i];
				tipos_select.options[i] = null;
				i--;		
			}
		}
	}
	if (nombre_cat == "Medios de comunicaci\u00F3n") {
		var options_only = new Array(".............", "Art\u00EDculos", "Entrevistas", "Declaraciones / Mensajes");
		var existe = false;
		for (i=0;i<tipos_select.options.length;i++) {
			existe = false;
			for (j=0;j<options_only.length;j++) {
				if (tipos_select.options[i].label == options_only[j]) {
					existe = true;	
				}
			}
			if (!existe) {
				optionsRemoved[optionsRemoved.length] = tipos_select.options[i];
				tipos_select.options[i] = null;
				i--;		
			}
		}
		var optionsRemovedIndex = 0;
		for (i=0;i<anyo_desde.options.length;i++) {
			if ((anyo_desde.options[i].value < "1996")) {
				optionsAnyoDesdeRemoved[optionsAnyoDesdeRemoved.length] = anyo_desde.options[i];
				orderOptionsAnyoDesdeRemoved[orderOptionsAnyoDesdeRemoved.length] = i + optionsRemovedIndex;
				anyo_desde.options[i] = null;
				optionsRemovedIndex++;
				i--;		
			}
		}
		for (i=0;i<anyo_desde.options.length;i++) {
			if (anyo_desde.options[i].value == "1996") {
				anyo_desde.options[i].selected = true;	
			}
		}
		var optionsRemovedIndex = 0;
		for (i=0;i<anyo_hasta.options.length;i++) {
			if ((anyo_hasta.options[i].value < "1996")) {
				optionsAnyoHastaRemoved[optionsAnyoHastaRemoved.length] = anyo_hasta.options[i];
				orderOptionsAnyoHastaRemoved[orderOptionsAnyoHastaRemoved.length] = i + optionsRemovedIndex;
				anyo_hasta.options[i] = null;
				optionsRemovedIndex++;
				i--;		
			}
		}
	}
}

function reset_form_dis_avanzado() {
	var categorias_select = document.getElementById('Categoria');
	var tipos_select = document.getElementById('Tipo');
	var periodo_select = document.getElementById('Periodo');
	var anyo_desde = document.getElementById('anyoDesde');
	var anyo_hasta = document.getElementById('anyoHasta');
	var mes_desde = document.getElementById('mesDesde');
	var mes_hasta = document.getElementById('mesHasta');
	var texto = document.getElementById('Texto');
	var idioma = document.getElementById('Idioma');
	categorias_select.selectedIndex = 0;
	tipos_select.selectedIndex = 0;
	periodo_select.selectedIndex = 0;
	anyo_desde.selectedIndex = 0;
	anyo_hasta.selectedIndex = 0;
	mes_desde.selectedIndex = 0;
	mes_hasta.selectedIndex = 0;
	texto.value = "";
	idioma.selectedIndex = 0;
}

function permite(elEvento, permitidos) {
  // Variables que definen los caracteres permitidos
  var numeros = "0123456789";
  var caracteres = " abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
  var numeros_caracteres = numeros + caracteres;
  var teclas_especiales = [8, 37, 39, 46, 9, 250, 218, 225, 193, 233, 201, 237, 205, 243, 211, 241, 209];
  // 8 = BackSpace, 46 = Supr, 37 = flecha izquierda, 39 = flecha derecha 9=suprimir, 250=ú, 218=Ú, 225=á, 193=Á, 233=é, 201=É, 237=í, 205=Í, 243=ó, 211=Ó, 241=ñ, 209=Ñ 

  // Seleccionar los caracteres a partir del parámetro de la función
  switch(permitidos) {
    case 'num':
      permitidos = numeros;
      break;
    case 'car':
      permitidos = caracteres;
      break;
    case 'num_car':
      permitidos = numeros_caracteres;
      break;
  }

  // Obtener la tecla pulsada
  var evento = elEvento || window.event;
  var codigoCaracter = evento.charCode || evento.keyCode;
  var caracter = String.fromCharCode(codigoCaracter);

  // Comprobar si la tecla pulsada es alguna de las teclas especiales
  // (teclas de borrado y flechas horizontales)
  var tecla_especial = false;
  for(var i in teclas_especiales) {
    if(codigoCaracter == teclas_especiales[i]) {
      tecla_especial = true;
      break;
    }
  }

  // Comprobar si la tecla pulsada se encuentra en los caracteres permitidos
  // o si es una tecla especial
  return permitidos.indexOf(caracter) != -1 || tecla_especial;
}

function es_correcto_car(caracter, permitidos) {
  // Variables que definen los caracteres permitidos
  var numeros = "0123456789";
  var caracteres = " abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
  var numeros_caracteres = numeros + caracteres;
  var teclas_especiales = [8, 37, 39, 46, 9, 250, 218, 225, 193, 233, 201, 237, 205, 243, 211, 241, 209];
  // 8 = BackSpace, 46 = Supr, 37 = flecha izquierda, 39 = flecha derecha 9=suprimir, 250=ú, 218=Ú, 225=á, 193=Á, 233=é, 201=É, 237=í, 205=Í, 243=ó, 211=Ó, 241=ñ, 209=Ñ 

  // Seleccionar los caracteres a partir del parámetro de la función
  switch(permitidos) {
    case 'num':
      permitidos = numeros;
      break;
    case 'car':
      permitidos = caracteres;
      break;
    case 'num_car':
      permitidos = numeros_caracteres;
      break;
  }

  var codigoCaracter = caracter.charCodeAt(0);

  // Comprobar si la tecla pulsada es alguna de las teclas especiales
  // (teclas de borrado y flechas horizontales)
  var tecla_especial = false;
  for(var i in teclas_especiales) {
    if(codigoCaracter == teclas_especiales[i]) {
      tecla_especial = true;
      break;
    }
  }

  // Comprobar si la tecla pulsada se encuentra en los caracteres permitidos
  // o si es una tecla especial
  return permitidos.indexOf(caracter) != -1 || tecla_especial;
}

function es_correcta_busqueda(texto, idioma) {
	var valido = true;
	for (var i=0;i<texto.length;i++) {
		if (!es_correcto_car(texto[i], 'num_car')) {
			valido = false;
			break;
		}		
	}
	if (!valido) {
		if (idioma == 'esp') {
			alert('La b\u00FAsqueda que ha realizado contiene caracteres no permitidos. S\u00F3lo puede introducir letras, n\u00FAmeros y espacios');	
		} else {
			alert('The search contains non valid characters. You can only write letters, numbers and spaces');	
		}	
		return false;
	} else {
		return true;	
	}
}

