$(document).ready(function(){
	
	
	function imagenesDelDia()
	{
		
		for(var i = 1; i <= 4; i++)
		{
			var urlImg = $("#link" + i).attr("title");		
			
			$("#imagen" + i).css("background", 'transparent url("' + urlImg + '") no-repeat 0px 0px');
		}
	}
	
	//imagenesDelDia();
	
	$("#ampliar").click(function(){
		$("#cut-mapa").css("width", "580px");
		$("#cut-mapa").css("height", "400px");
		$("#cut-mapa").css("border", "3px solid #CCC");
		$("#cerrar-mapa").css("display", "inline");
		$("#ampliar").css("display", "none");
			return false;
	});
	
	$("#cerrar-mapa").click(function(){
		$("#cut-mapa").css("width", "0px");
		$("#cut-mapa").css("height", "0px");
		$("#cut-mapa").css("border", "none");
		$("#cerrar-mapa").css("display", "none");
		$("#ampliar").css("display", "inline");
		return false;
	});
	
	$(".img-full-news").click(function(event){
		var src = event.target.src;
		var alt = event.target.alt;
		
		var src_old = $("#img-full").attr("src");
		var alt_old = $("#pie").html();		
		
		$("#img-full").attr("src", src);
		$("#pie").html(alt);
		
		event.target.src = src_old;
		event.target.alt = alt_old;
	});
	
	$("#enviando").click(function(){
		var nombre = $.trim($("#nombre").val()).length;
		var apellidos = $.trim($("#apellidos").val()).length;
		var correo = $.trim($("#correo").val()).length;
		var mensaje = $.trim($("#mensaje").val()).length;
		var terminos = $("#terminos").is(":checked");		
		
		if(terminos) {
			if(nombre === 0 || apellidos === 0 || correo === 0 || mensaje === 0) {
				alert("Debe rellenar los campos marcados con *");
				return false;
			} else {
				return true;
			}			
		} else {
			alert("Debe aceptar los terminos");
			return false;
		}
	});
	
	var indice_inicio = 14;
	$("#leer-mas-seccion").click(function(event){
		
		$.ajax({
			  url: "/ajax/public/seccion.php?permalink=" + event.target.rel + "&inicio=" + indice_inicio,
			  context: document.body,
			  cache: false,
			  dataType: 'json',
			  success: function(data){	
				  
				  indice_inicio = indice_inicio + 14;
				  
				  jQuery.parseJSON(data);
				  var total = data.noticia.length;
				  
				  //Noticias portada 
				  var html = '';				  
				  
				  for(var i = 0; i <= (total/2); i++)
				  {
					  html = html + '<div class="item-news">';
					  html = html + '<p class="seccion">'+ data.noticia[i][0].cintillo +'</p>';
					  html = html + '<h2>';
					  html = html + '<a href="'+ data.noticia[i][3].permalink +'">'+ data.noticia[i][1].titulo +'</a>';
					  html = html + '</h2>';
					  html = html + '<p class="texto">'+ data.noticia[i][2].texto +'</p>';					  
					  html = html + '</div>';
				  }
				  
				  $("#noticias-portada-seccion").empty();
				  $(html).appendTo("#noticias-portada-seccion");
				  
				  //Otras noticias seccion
				  html = '';				  
				  var inicio = ((total/2) + 1);
				  
				  for(var e = inicio; e < total; e++)
				  {
					  html = html + '<div class="item-news">';
					  html = html + '<p class="seccion">'+ data.noticia[e][0].cintillo +'</p>';
					  html = html + '<h2>';
					  html = html + '<a href="'+ data.noticia[e][3].permalink +'">'+ data.noticia[e][1].titulo +'</a>';
					  html = html + '</h2>';
					  html = html + '<p class="texto">'+ data.noticia[e][2].texto +'</p>';					  
					  html = html + '</div>';
				  }

				  $("#otras-noticias-seccion").empty();
				  $(html).appendTo("#otras-noticias-seccion");				  
				  
				  //Mostrar boton de inicio
				  var rel = event.target.rel;
				  var permalink = rel.replace("-", "/");
				  $("#inicio").remove();
				  $('<a href="/'+ permalink +'" id="inicio">Inicio</a>').appendTo("#archivo");
			  }
		});
		
		return false;
		
	});
	
});
