function fader(grupo) {
    var $vista = $(grupo).find("img.vista");
    if ( $vista.length == 0 ) $vista = $(grupo).find("img:last-child"); 
    var $next =  $vista.next().length ? $vista.next() : $(grupo).find("img:first-child");
    $vista.addClass('ultima');
    $next.css({opacity: 0.0}, 3000).addClass('vista').animate({opacity: 1}, 3000, function() {
    	$vista.removeClass('vista ultima');
    });
}

function prensa() {
    $("#listaprensa dl dt a").click( function(){
		var enlace = $(this).attr("href");
		var foto = enlace.replace(".pdf", ".jpg");
		$("#listaprensa dl").removeClass("activo");
		$(this).parents("dl").addClass("activo");
		$("#prensa_foto").fadeOut(500, function(){
			$("#prensa_foto a").attr("href", "media/prensa/"+enlace);
			$("#prensa_foto img").attr("src", "media/prensa/"+foto)
				.load( function(){ $("#prensa_foto").fadeIn(500); });			
		});
		return false;
	});
}

function externallinks(){
	$("a[rel='external']").click(function(){ this.target = "_blank"; }); 
}

function validacontacto(){
	$("#contactoform .submit input").fadeTo(0, 0.2).attr("disabled", "disabled");

	$("#contactoform input#telefono").change(function(){
		if ($("#contactoform input#telefono").val().length < "9"){
			$("#contactoform input#telefono").css("border-color", "#b20309");
		}
		else{
			telefono = $(this).attr("value");
			telefono = telefono.replace(/ /g, '').replace('+', '00');
			$(this).attr("value", telefono);
			$("#contactoform input#telefono").css("border-color", "#3f5400");
		}
	});
	
	$("#contactoform input#correo").change(function(){
		var mail = /^[a-zA-Z0-9._-]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (mail.test($("#contactoform input#correo").val()) != true){
			$("#contactoform input#correo").css("border-color", "#b20309");
		}
		else{
			$("#contactoform input#correo").css("border-color", "#3f5400");
		}
	});

	$('#contactoform input').change(function() {
		var mail = /^[a-zA-Z0-9._-]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		var telefono = /^[0-9]+$/;
		if($("#contactoform input#nombre").val() != "" &&
			 $("#contactoform input#apellidos").val() != ""&&
			 mail.test($("#contactoform input#correo").val()) &&
			 telefono.test($("#contactoform input#telefono").val()) &&
			 $("#contactoform input#telefono").val().length >= "9"
			 )
		{
			$("#contactoform .submit input").fadeTo(500, 1).removeAttr("disabled");			
		}
	});
}

$(document).ready(function(e) {
	$("#galeria.multiple img:first-child").addClass("vista");
	setInterval( "fader('#galeria.multiple')", 5000 );
	validacontacto();
	externallinks();
	prensa();
	
	$("#newsletter a").click(function(){
		$("#legales").show("slow");
		return false;
	});
	$("#legales span").click(function(){
		$("#legales").hide("slow");
	});
});

