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() {
    
    /*
     * Create tabbed interface on index.
     */
    
    $('#container-1 > ul').tabs();
    $('#container-1 > ul').tabs({ fx: { opacity: 'toggle', fxSpeed: '1000'} });
    
    
    
    /*
     * Create carousel if included on page.
     */
     
    $(".the_carousel").jCarouselLite({
        visible: 1,
        auto: 10000,
        speed: 1000,
        btnNext: ".next",
        btnPrev: ".prev"   
    });
    
    /*
     * 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("Thanks for signing up!");
                    } else if (data.status == 101) {
                        MailingList.showThanks("Looks like you've already signed up. Thanks!");
                    } 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;
    });


});