var MailingList = {
    showError: function(message) {
        $("#mailinglist-form").fadeOut("fast", function() {
			$("#mailinglist-form-error").text(message).fadeIn("fast", function() {
                window.setTimeout(function() {
                    $("#mailinglist-form-error").fadeOut("fast", function() {
						$("#mailinglist-form").fadeIn("fast");
					});
                }, 2000);
			});
		});
    },
    showThanks: function(message) {
        $("#mailinglist-form").fadeOut("fast", function() {
			$("#mailinglist-form-thanks").text(message).fadeIn("fast");
		});
    }
};
    
$(function() {
    
    /*
     * Add behavior to mailing list signup form.
     */

    $("#mailinglist-form").submit(function() {
        var data = { response: "json", email: $("#email").val(), zipcode: $("#zipcode").val() };
        $.ajax({
            type: "POST",
            url: "/mailinglist/subscribe/",
            data: data,
            dataType: "json",
            success: function(data) {
                if (data.status) {
                    if (data.status == 100) {
                        MailingList.showThanks("Thank you for joining our mailing list. Look for an email from Sunlight Foundation in the near future.");
                    } else if (data.status == 101) {
                        MailingList.showError("Looks like something wasn't quite right. Try again?");
                    } else if (data.status == 102) {
                        MailingList.showError("Are you sure that was a valid email address?");
                    } else {
                        MailingList.showError(data.message);
                    }
                }
            },
            error: function(xhr, textStatus, errorThrown) {
                var message = xhr.responseText;
                MailingList.showError(message);
            }
        });
        return false;
    });


});