﻿/// <reference path="jquery-1.2.6.js" />
/// <reference path="core.js" />

Forums.Announcements =
{
    decorateAnnouncement: function()
    {
        var announcement = $(this);
        $("h4", this).prepend(Forums.Constants.ExpandCollapseLink);
        $("h4", this).click(function(evt)
        {
            $(announcement).children("div.head, div.body").slideToggle("fast");
            $(announcement).toggleClass("expanded");
        });

        $("h4", this).mouseover(function()
        {
            $(announcement).addClass("hover");
        });

        $("h4", this).mouseout(function()
        {
            $(announcement).removeClass("hover");
        });
    }
};

Forums.Forum =
{
    markAllRead: function()
    {
        var link = $(this);
        $(this).click(
            function()
            {
                if (confirm(ForumsRes.prompt_markAllRead))
                {
                    $("img.icon", link).addClass("submitting");
                    $.post($(link).attr("href"), {}, function(results)
                    {
                        if (results.success)
                        {
                            window.location = window.location;
                            $("img.icon", link).removeClass("submitting");
                        }
                    }, "json");
                }

                return false;
            });
    },

    forumAction: function(link, data)
    {
        return function()
        {
            $.post($(link).attr("href"), data, function(results)
            {
                if (results.success)
                {
                    $(link).hide();
                }
            }, "json");
            return false;
        };
    },

    favorite: function()
    {
        var link = $(this);
        $(this).click(Forums.Forum.forumAction(link, { isFavorited: true }));
    },

    unfavorite: function()
    {
        var link = $(this);
        $(this).click(Forums.Forum.forumAction(link, { isFavorited: false }));
    },

    loaded: function()
    {
        $("div.leaderboards", this).each(Forums.Leaderboards.init);
    },

    decorateForum: function()
    {
        $("a.btn[name='markAllRead']").each(Forums.Forum.markAllRead);
        $("a[name='favorite']").each(Forums.Forum.favorite);
        $("a[name='unfavorite']").each(Forums.Forum.unfavorite);
    }
};

$(function()
{
    $.addToRail(railItems, Forums.Forum.loaded)
    $("li.announcement").each(Forums.Announcements.decorateAnnouncement);
    $("li.thread").each(Forums.Thread.decorateThread);
    $("a.popup").popup();

    $("div.pager").clone().insertAfter($("ul.threads"));

    Forums.Threads.initUpdater();
    Forums.Forum.decorateForum();
});
