if (window.disqus === undefined) {
    var disqus = {};
}

(function () {
    // initialization

    disqus.initialize = function () {
        // do all initialization for the moderate page
        // including calling comment or thread specific initialization

        // set up show more button.
        $('#show-more button').click(function (event) {

            // Hide button and display loading image
            $('#show-more button').hide();
            $('#show-more img.loading').show();

            disqus.getList();

            return false;
        });

        disqus.getList();

        $("#info").click(function() {

            //hide all tabs
            $("#info-tab").hide();
            $("#activity-tab").hide();

            //show tab

            $("#info-tab").show();
            $("#nav li").removeClass("selected");
            $("#info").addClass("selected");

            return false;
        });

        $("#activity").click(function() {

            //hide all tabs
            $("#info-tab").hide();
            $("#activity-tab").hide();

            //show tab

            $("#activity-tab").show();
            $("#nav li").removeClass("selected");
            $("#activity").addClass("selected");

            return false;
        });

        // show the first tab
        $('#nav a:first').click();

        // Follow/unfollow
        $('#follow-button').click(function() {
            var button = $(this);
            var action = button.hasClass('following') ?
                'unfollow' : 'follow';

            DISQUS.sexyapi.users[action]({
                data: {
                    target: $(this).attr('data-user-id')
                },
                success: function() {
                    button.toggleClass('following');
                }
            });
            return false;
        });
    };
})();

(function () {
    // json call for populating comments page.
    var since = null;

    disqus.startLoading = function () {
        $('#loading-message').show();
        $('#loading-overlay').show();
    };

    disqus.stopLoading = function () {
        $('#loading-message').hide();
        $('#loading-overlay').hide();
    };

    disqus.getList = function (filters) {
        // load a list of comments from a json call to the url variable.

        disqus.startLoading();

        // build parameters.
        var data = {
            include: 'user',
            limit: 10
        };
        if (context.username) {
            data.user = 'username:' + context.username;
        } else {
            data.anon_user = context.userhash;
        }

        // since loaded for show more.
        if (since) {
            data.since = since;
        }

        // make json call
        DISQUS.sexyapi.users.listActivity({
            data: data,
            success: function(data) {
                if (data.length) {
                    since = disqus.date.parseISO8601(data[data.length-1].createdAt).getTime() / 1000;
                } else if (!since) {
                    // First request; no activity
                    $('#waiting-for-data').show();
                    $('#show-more').hide();
                }

                $.each(data, function(i, entry) {
                    var tmpl;
                    switch (entry.type) {
                        case 'post':        tmpl = $('#comment-tmpl');     break;
                        case 'thread_like': tmpl = $('#thread-like-tmpl'); break;
                        case 'like':        tmpl = $('#like-tmpl');        break;
                    }
                    tmpl.tmpl($.extend({}, entry.object, {'context': context})).appendTo('#activity-entries');
                });

                if (data.length == 10) {
                     $('#show-more img.loading').hide();
                     $('#show-more button').show();
                     $('#show-more').show();
                } else {
                     $('#show-more').hide();
                }

            }
        });
    };
})();

// on document ready.
$(document).ready(function () {
    disqus.initialize();
});

