$(document).ready(function() {
	$("#contactSubmit").click( function(e) {
		e.preventDefault();

		if ( $("#contactNotification").is(":hidden") ) {
			doContact();
		} else {
			$("#contactNotification").hide(
				500,
				function() {
					$("#contactNotification").removeClass("success");
					$("#contactNotification").removeClass("fail");
					$("#contactMsg").removeClass("fail");
					doContact();
				}
			);
		}
	});
});

function doContact() {
	//alert("submitting with email: " + $("#contactEmail").val() + " and mesg: " + $("#contactMsg").val());
	$.post (
		"/data/contact",
		{
			email: $("#contactEmail").val(),
			msg: $("#contactMsg").val(),
			accion: "true"
		},
		function(xml) {

			if ( $("status", xml).text() == "success" ) {

				$("#contactNotification").addClass("success");
				$("#contactNotification").html("Thanks for the message. I'll get back to you soon.");

				$("#contactMsg").slideToggle(
					1000,
					function() {
						$("#contactMsg").val("");
						$("#contactEmail").val("");
						$("#contactMsg").slideToggle(500);
					}
				);

			} else {

				var errorMsg = "";

				if ( $("robotError", xml).text() == "" ) {

					if ( $("fieldError[name=msg]", xml).text() == "" ) {
						errorMsg = "Unknown error";
					} else {
						errorMsg = $("fieldError[name=msg]", xml).text();
						$("#contactMsg").addClass("fail");
					}

				} else {

					errorMsg = $("robotError", xml).text();

				}

				$("#contactNotification").addClass("fail");
				$("#contactNotification").html(errorMsg);
			}
			$("#contactNotification").show(500);

		}

	);
}

