$(function () {
    $("#activity-stream a.comment").click(function () {
        var comments = $(this).parent().parent().parent().find(".stream-comments");
        comments.toggle();
        var count = $(this).find("span.fb_comments_count");
        count.toggle();
        $(this).find(".brackets").toggle();
        if (count.is(":visible")) {
            FB.XFBML.parse(this);
        }
        return false;
    });
    $("#activity-filter").change(function () {
        window.location.href = $(this).val();
    });
    $(".stream-more").live("click", function () {
        var link = $(this);
        var loadDiv = $("<div>");
        loadDiv.load($(this).attr("href") + " div.stream-item, div.stream-navigation", function (response, status, xhr) {
            $("#activity-stream").append(loadDiv.html());
            link.parent().remove();
            $("#activity-stream").find("a.comment").each(function () {
                if ($(this).find("span.fb_comments_count").length === 0) {
                    $(this).click(function () {
                        $(this).parent().parent().parent().find(".stream-comments").toggle();
                        return false;
                    });
                }
            });

            twttr.widgets.load();

            $("#activity-stream").find('.comment, .stream-comments').each(function () {
                if ($(this).find("span.fb_comments_count").length === 0) {
                    FB.XFBML.parse(this);
                }
            });
        });

        return false;
    });
});

