$(function() {
    $("body").addClass("js");

    /* RSS feed/Email subscription tabs */

    $("#news-subscriptions li").click(function() {
        new_tab = $(this).attr("class");
        new_tab_content = $("#news-subscriptions div." + new_tab);

        $("#news-subscriptions div").not(new_tab_content).hide();
        new_tab_content.show();

        // Update tab state
        $(this).children("a").addClass("active").end().parent().children("li").not($(this)).children("a").removeClass("active");

        return false;
    });


    /* Section nav tabs */

    $("#content-main .section-nav dd").click(function() {

        // Update tab state
        $(this).children("a").addClass("active").end().parent().children("dd").not($(this)).children("a").removeClass("active");

        return false;
    });


    /* Category box tabs */

    $("#content-main .category-nav dd").click(function() {

        category = $(this).parent().parent().attr("id");
        new_tab_content = $("#" + category + "-" + $(this).attr("class"));

        $(this).parent().children("dd").not($(this)).removeClass("active");

        // Show new tab content
        $("#" + category + " .article-group").not(new_tab_content).fadeOut(100, function() {
            new_tab_content.fadeIn(100);
        });

        // Update tab state
        $(this).children("a").addClass("active").end().parent().children("dd").not($(this)).children("a").removeClass("active");

        return false;
    });

    /* Search boxes */
    search_box_msg = "search term";
    $(".col-left fieldset .search-term").attr("value", search_box_msg);

    $(".col-left fieldset .search-term").focus(function() {

        if ($(this).attr("value") == search_box_msg) {
            $(this).attr("value", "");
        }
    });

    $(".col-left fieldset .search-term").blur(function() {

        if ($(this).attr("value") == "") {
            $(this).attr("value", search_box_msg);
        }
    });

    /* Email boxes */
    email_box_msg = "yourname@email.com";
    $("#email-address").attr("value", email_box_msg);

    $("#email-address").focus(function() {

        if ($(this).attr("value") == email_box_msg) {
            $(this).attr("value", "");
        }
    });

    $("#email-address").blur(function() {

        if ($(this).attr("value") == "") {
            $(this).attr("value", email_box_msg);
        }
    });

    /* Send to a friend */

    $("#send-to-friend-content input").val("");

    $("#send-to-friend").click(function() {
        $(this).fadeOut(150, function() {
            $("#send-to-friend-content").fadeIn(150).find("label").show();
        });
        return false;
    })
    $("#cancel-send-friend").click(function() {
        $(this).parent().parent().fadeOut(150, function() {
            $("#share-links .sent-message").remove();
            $("#send-to-friend").fadeIn(150);
        });
    })



    /* Handle labels (shown over inputs for JS users) */
    $("#send-to-friend-content label").click(function() {
        $(this).hide();
        $("#share-links .sent-message").fadeOut(150);
    });
    $("#send-to-friend-content input").focus(function() {
        if (!$(this).val()) {
            $(this).parent().children("label").hide();
        }
        $("#share-links .sent-message").fadeOut(150);
    });
    $("#send-to-friend-content li input").blur(function() {
        if (!$(this).val()) {
            $(this).parent().children("label").show();
        }
    })

    $("form#send-to-friend-content").submit(function() {
        var form_contents = $(this).serialize();
        var ajax_uri = "article_2-col.html"; // temporary value to show send to a friend working

        $.ajax({
            type: "POST",
            url: ajax_uri,
            data: form_contents,
            success: function(result) {
                $("#send-to-friend-content p.sent-message").remove();
                $("#cancel-send-friend").parent().children("legend").after("<p class=\"sent-message\">Sent! Send another?</p>");
            }
        });
        return false;
    })

});