$(document).ready(function() {

	$("#fadehoverHead").hover(function(){
		$(this).children("img.a").stop().animate({"opacity": "0"}, 250);
	},function(){
		$(this).children("img.a").stop().animate({"opacity": "1"}, 450);
	});
	
	$('#navigation a').stop().animate({'marginLeft':'-45px'},1000);
	$('#navigation > li').hover(
		function () {
			$('a',$(this)).stop().animate({'marginLeft':'-2px'},200);
		},
		function () {
			$('a',$(this)).stop().animate({'marginLeft':'-45px'},200);
		}
	);
	$("label").inFieldLabels();
	$('form').highlight();

	$(window).load(function() {
		$('#slider').nivoSlider();
	});

	$(".workBig").hover(function(){
		$(this).find(".a").stop().animate({"opacity": "0"}, 300);
	},function(){
		$(this).find(".a").stop().animate({"opacity": "1"}, 300);
	});

	$('.sendAgain').click(function (){
		$('.done').fadeOut('slow');
		$('.failed').fadeOut('slow');
		$('#formulario').fadeIn('slow');
		$('.loading').hide();
		$('.textInput').removeAttr('disabled');
	});

	$("a.galeria").fancybox({
		'titlePosition'  : 'over'
	});
	
	$('#submit').click(function () {		

		var name = $('input[name=name]');
		var email = $('input[name=email]');
		var comment = $('textarea[name=comment]');
		
		if (name.val()=='') {
			name.addClass('inputerror');
			name.focus();
			return false;
		} else name.removeClass('inputerror');

		if (email.val()=='' || !isValidEmailAddress(email.val())) {
			email.addClass('inputerror');
			email.focus();
			return false;
		} else email.removeClass('inputerror');

		if (comment.val()=='') {
			comment.addClass('inputerror');
			comment.focus();
			return false;
		} else comment.removeClass('inputerror');

		var data = 'name=' + name.val() + '&email=' + email.val() + '&comment='  + encodeURIComponent(comment.val());
		$('.textInput').attr('disabled','disabled');
		$('.loading').show();

		$.ajax({
			url: "process.php",	
			type: "GET",
			data: data,	
			cache: false,
			success: function (html) {
				if (html==1) {
					$('#formulario').fadeOut('slow');
					$('.done').fadeIn('slow');
				} else {
					$('#formulario').fadeOut('slow');
					$('.failed').fadeIn('slow');
				}
			}
		});
		return false;
	});
});

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

