$(function () {

    jQuery.preLoadImages("/Content/Img/Site/subscribe_hover.png");

    $("a#subscribe_button").click(function () {
        if ($(this).hasClass("selected")) {

            // Close
            $("#newsletter").animate({ 'margin-top': '-201px' }, {
                duration: 500, easing: 'easeInOutCubic'
            });

            $(this).removeClass("selected").animate({ 'top': '0' }, {
                duration: 500, easing: 'easeInOutCubic'
            });

        } else {

            // Open
            $("#newsletter").animate({ 'margin-top': '0' }, {
                duration: 500, easing: 'easeInOutCubic'
            });

            $(this).addClass("selected").animate({ 'top': '-201px' }, {
                duration: 500, easing: 'easeInOutCubic'
            });

        }
        return false;
    });

    $("form#newsletter_subscribe input[name=name], form#newsletter_subscribe input[name=email]").focus(function () {
        var defaultText = $(this).val();
        if (defaultText === "Your name" || defaultText === "Your email address") {
            $(this).select();
        }
    });

    $("#newsletter_subscribe").submit(function (event) {

        event.preventDefault();

        // Validate

        var form = $(this);

        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var name = form.find("input[name=name]");
        var email = form.find("input[name=email]");

        isValid = true;

        if (!emailReg.test(email.val())) {
            email.css("color", "Red");
            isValid = false;
        } else {
            email.css("color", "#947C62");
        }

        if (name.val() == "Your name") {
            name.css("color", "Red");
            isValid = false;
        } else {
            name.css("color", "#947C62");
        }

        if (isValid) {

            form.parent().find("h2").html("One moment...");

            form.hide();
            $("#newsletter #working").fadeIn(function () {

                var values = form.serialize() + "&ajax=true";

                $.ajax({
                    url: '/newsletter-subscribe',
                    dataType: 'json',
                    data: values,
                    type: 'post',
                    success: function (data, status, request) {
                        console.log(data);
                        $("#newsletter #working").hide();

                        form.parent().find("h2").html((data.Success) ? "Success!" : "Whoops!");

                        $("#newsletter #results").html("<p><img src=\"/Content/Img/Site/" + ((data.Success) ? "success" : "error") + ".png\"> " + ((data.Success) ? "You have been successfully added to our mailing list!" : "There was a problem subscribing you to our mailing list. Please <a href=\"/contact\">contact</a> us to resolve the issue, or <a id=\"try_again\" href=\"#\">try again</a>.") + "</p>").fadeIn(); ;

                        $("#newsletter #results #try_again").unbind().click(function (event) {
                            event.preventDefault();
                            $(this).parent().hide();
                            form.parent().find("h2").html("Your Details");
                            form.fadeIn();
                        });

                    },
                    error: function (request, status, error) { }
                });

            });

        }

    });

    //$("a#subscribe_button").click();

});
