var validEmail = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

jQuery(document).ready(function() {
	
	// FadeIn FadeOut al ver una nueva imagen
	jQuery(".outer-photo").toggle();
    jQuery(".outer-photo").fadeIn(600);
    
	jQuery (".next a, .prev a").click(function(event){
		event.preventDefault();
		linkLocation = this.href;
		$(".outer-photo").fadeOut(300, redirectPage);		
	});
		
	function redirectPage() {
		window.location = linkLocation;
	}
	
	// Muestra / Oculta elemento
	jQuery('.show-hide-comments').click(function() {
	    $('#comments').slideToggle();
	    return false;
	});
	
	jQuery('.show-hide-filter').click(function() {
		$(this).toggleClass("active");
	    $('#categories').toggle(150);
	    return false;
	});
	
	// Fancybox
	jQuery("a#onlyimage").fancybox({
		'hideOnContentClick': true,
		'overlayOpacity': 1,
		'overlayColor': '#000',
		'titleShow': false,
		'transitionOut': 'elastic',
		'speedIn': 600
	});
	
	// Fancybox generico
	jQuery("#inline").fancybox({
		'overlayColor': '#000'
	});
	
	//Fancybox GMap
	jQuery(".gmap, .creativecommons").fancybox({
			'width'				: '75%',
			'height'			: '75%',
	        'autoScale'     	: false,
	        'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
	});
	
	// Tooltip
	jQuery("img[title], .photo a[title]").tooltip({
		effect: 'fade',
		opacity: 0.75
	});

	// Form comentarios 
	jQuery('#mycommentform').submit( function( e ) {
		
		// Evita que haga submit el formulario
		e.preventDefault();
		
		var form = this;
		var required = {
			'name' : 'noempty', 
			'email' : 'validemail',
			'message' : 'noempty' 
		};
		
		// Recogida de valores y comprobación
		var values = {}, valid = true;
		$("input, textarea", form).each( function() {
		
			var value = this.value;
			var name = this.name;
			
			// Si no tiene parámetro name, pasamos de el
			if (!this.name) return;
			
			if ( this.type == "checkbox" || this.type == "radio" ) {
				values[ name ] = this.checked ? value : "";
			} else {
				values[ name ] = value;
			}
			
			// Comprobacion
			if ( required[ name ] ) {
			
				var check = required[ name ].split(",");
				for (var i in check) {
				
					switch ( check[i] ) {
					
						case "noempty":
							if ( value.replace( /^\s+|\s+$/g, '' ) == "" ) valid = false;
							break;
							
						case "validemail":
							if ( value && validEmail.test( value ) == false ) valid = false;
							break;
					
					}
				}
			}
		} );
		
		if ( valid ) {
		
			// Bloquea todos los campos, para evitar multiples llamadas
			$("input", form).attr( "disabled", "disabled" );
		
			// Envio de formulario
			$.post( form.action, values, function( data ) {
				
				// Genera el comentario
				var newComment =  "<li class=\"new\">";
					newComment += "<div class=\"autor\"><span>"+values.name+"</span></div>";
					newComment += "<div class=\"message\">"+values.message+"</div>";
					newComment += "</li>";
					newComment = $( newComment );
					
				// Añade el comentario
				$("#comments .comments-list ul").append( newComment );
				newComment.hide().slideDown();
				
				$("#thankscomment").html("Gracias por tu comentario").show();
				
				// Vacía el textarea, dejarndolo preparado para un nuevo comentario
				$("textarea", form).val( "" );
				
				// Desbloquea los campos
				$("input", form).removeAttr( "disabled" );
				
			} );
			
		} else {
		
			// Faltan campos
			$("#thankscomment").html("Por favor, rellena los campos obligatorios correctamente").show();
			
		}
		
	});

		
});
