google.load ('jquery', '1');

google.setOnLoadCallback(function() {
	
	$.ajaxSetup({ cache: false });
	
	$('.formulario form').live('submit', function (){
		
		//	tratamos de evitar el reenvío del formulario (p.e. con f5)
		
		if ($(this).hasClass('sendingForm'))
		{
			return (false);
		}
		
		//	validamos el formulario
		var esValido = true;
		
		//	1.	Campos requeridos
		var camposRequeridos = $(this).find('.form_field').children('.requerido');
		
		camposRequeridos.each(function(index){
			var tipo = $(this).attr('type');
			if ($(this).val() == '' || (tipo == 'checkbox' && !$(this).is(':checked')))
			{
				esValido = false;
				return (mensaje_error_campo ($(this), 'es requerido'));
			}
		});
		
		if (!esValido) return (esValido);
		
		//	2.	Campos numéricos
		var camposNumericos = $(this).find('.form_field').children('.form_number');
		
		camposNumericos.each(function(index){
			
			if (($(this).val() != '') && (($(this).val() - 0) != $(this).val()))
			{
				esValido = false;
				return (mensaje_error_campo ($(this), 'no es un número v\u00E1lido'));
			}
		});
		
		if (!esValido) return (esValido);
		
		//	3.	e-mail

		var camposEmail = $(this).find('.form_field').children('.form_email');
		
		camposEmail.each(function(index){
			var patron = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
			if (($(this).val() != '') && (!$(this).val().match(patron)))
			{
				esValido = false;
				return (mensaje_error_campo ($(this), 'no es un e-mail v\u00E1lido'));
			}
		});
		
		//	4.	ficheros
		
		var camposFile = $(this).find('.form_field').children('.form_file');
		camposFile.each(function(index){
			var filename = $(this).val();
			var extension = filename.substring(filename.lastIndexOf('.') + 1);
			if (filename != '' && extension != 'doc' && extension != 'docx' && extension != 'odt' && extension != 'pdf')
			{
				esValido = false;
				return (mensaje_error_campo ($(this), 'no tiene un formato correcto'));
			}
		});
		
		if (esValido)
		{
			return true;
		}
		
		return false;
	});
		
	function mensaje_error_campo (campo, descripcion_error)
	{
		alert ('El campo ' + campo.attr('name') + ' ' + descripcion_error);
		campo.focus();

		return (false);
	}
	
	$('a#ampliar_contenido').live('click', function(){
		var enlace = $(this);
		var nota_lista = enlace.closest('div.nota_lista');
		var texto_ampliado = nota_lista.children('#texto_ampliado:first');
		var id_nota = nota_lista.attr('id');
		
		id_nota = id_nota.substring(4, id_nota.indexOf('-', 4));
		
		if (texto_ampliado.length == 0)
		{
			$.get ('', {modulo: 'lista_notas', accion: 'get_campo_notas', id_nota: id_nota, campo: 'texto_ampliado'}, function(data){
				var linea = nota_lista.children('.descripcion_nota:first');
				
				data = '<div id = \'texto_ampliado\' class = \'activo\'>' + data + '</div>';
				linea.after(data);
				
				enlace.text('Ver menos');
			});
		}
		else
		{
			if (texto_ampliado.hasClass ('activo'))
			{
				texto_ampliado.hide();
				enlace.text('Ver m' + String.fromCharCode(225) + 's');
			}
			else
			{
				texto_ampliado.show();
				enlace.text('Ver menos');
			}
			
			texto_ampliado.toggleClass('activo');
			texto_ampliado.toggleClass('oculto');
		}
		
		return false;
	});
	
	$('a#mapa').live('click', function(){
		var direccion = $(this).closest('.zona_contacto');
		var contenedor = direccion.closest('.direcciones');
		var id = direccion.attr('id');
		
		id = id.substring(9, id.indexOf('-', 9));
		
		$.get('', {modulo: 'contacto', accion: 'mapa', id: id}, function(data){
			contenedor.html(data);
		});
		
		return false;
	});
	
	$('a#cerrarmapa').live('click', function(){
		var contenedor = $(this).closest('.direcciones');
		var id_div = contenedor.attr('id');
		
		$.get('', {modulo: 'contacto', accion: 'direcciones', id_div: id_div}, function(data){
			contenedor.html(data);
		});
		
		return false;
	});
	
	$('div.ruta_faq a, div.linea_faq a').live('click', function(){
		var enlace = $(this);
		var contenedor = enlace.closest('div.faq');
		var id = enlace.attr('id');
		var id_div = contenedor.attr('id');
		
		//	vemos si tenemos ya el contenido
		
		var contenido = contenedor.find('div.contenido_faq');
		var info = false;
		var ruta = contenedor.children('div.ruta_faq:visible');
		
		contenido.each(function(){
			if ($(this).attr('id') == id)
			{
				info = $(this);
				return false;
			}
		});
		
		id = id.substring(3, id.indexOf('-', 3));
		
		if (!info)
		{
			var accion;
			if (enlace.hasClass('hoja'))
				accion = 'ampliar_faq';
			else
				accion = 'subarbol_faq';
			
			$.get('', {modulo: 'faq', accion: accion, id_div: id_div, id: id}, function(data){
				contenido.hide();
				ruta.hide();
				contenedor.append(data);
			});
		}
		else
		{
			if (contenido.is(':hidden'))
			{
				contenido.hide();
				ruta.hide();
				info.prev().show();
				info.show();
			}
		}
		
		return false;
	});
	
	$('div.anyos_hemeroteca a.ruta, div.paginas_hemeroteca a.ruta').live('click', function(){
		var enlace = $(this);
		var contenedor = enlace.closest('div.hemeroteca');
		
		//	obtenemos todas las listas y ocultamos el contenido
		var lista = contenedor.find('div.lista_ampliada');
		var cabecera = lista.prev('div.cabecera_hemeroteca');
		lista.hide();
		cabecera.hide();
		
		var destino = enlace.attr('href');
		//	el destino del enlace tiene la forma ?anyo=AAAA&pagina=X; sabemos que las posiciones son fijas
		var anyo = destino.substring (6, 10);
		var pagina = destino.substring (18);
		
		var id_modulo = contenedor.attr('id');
		var id_solicitada = 'noticias' + anyo + '-' + pagina + '-' + id_modulo.substring(0, id_modulo.indexOf('-'));
		
		var encontrado = lista.is('#' + id_solicitada);
		
		if (encontrado)
		{
			//	existe: lo mostramos
			var lista_mostrar = contenedor.find('div.lista_ampliada#' + id_solicitada);
			var cabecera_mostrar = lista_mostrar.prev('div.cabecera_hemeroteca');
			lista_mostrar.show();
			cabecera_mostrar.show();
		}
		else
		{
			//	no lo tenemos: lo obtenemos
			$.get('', {modulo: 'hemeroteca', accion: 'mostrar', id_div: id_modulo, anyo: anyo, pagina: pagina},function(data){
				contenedor.append(data);
			});
		}
		
		return false;
	})
	
});
