/*
    Shaun the Sheep JavaScript Collection
    v1.2
    Updated: 13th October 2009, Richard Davey (web.producer@aardman.com)
*/

var board_id = 0;
var thread_id = 0;
var message_id = 0;

function openRM1(bid, tid, mid)
{
    board_id = bid;
    thread_id = tid;
    message_id = mid;
    
    $('#dialog1').dialog('open');
}

function openRM2(bid, tid, mid)
{
    board_id = bid;
    thread_id = tid;
    message_id = mid;
    
    $('#dialog2').dialog('open');
}

function parseDialogs()
{
    /* Attach click events to all the message reveal spans */
    $('.msgwarning_reveal').click(
    
        function () {
            
            var mid = $(this).attr("id").substr(11);
            
            $('#msgbody_' + mid).slideDown();
            $('#msgwarning_' + mid).slideUp();
        }
    );
    
    $('ul.message_controls li').hover(
    
        function () {
            $(this).removeClass('ui-state-default');
            $(this).addClass('ui-state-hover');
        },
         
        function () {
            $(this).removeClass('ui-state-hover');
            $(this).addClass('ui-state-default');
        }
    );
    
    $('.tooltip').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
        });

    $("#dialog1").dialog({
        autoOpen: false,
        bgiframe: true,
        resizable: false,
        height:220,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            'Hide Message': function()
            {
                $.get("/forum/actions/hide_message/" + board_id + "/" + thread_id + "/" + message_id);
                $(this).dialog('close');
                $("#msgwrapper" + message_id).hide();
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
    });
    
    $("#dialog2").dialog({
        autoOpen: false,
        bgiframe: true,
        resizable: false,
        height:220,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            'Report Message': function()
            {
                $.get("/forum/actions/report_message/" + board_id + "/" + thread_id + "/" + message_id);
                $(this).dialog('close');
                $("#msgwrapper" + message_id).hide();
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
    });
}

function attachFavourites ()
{
    $("#forum_fav").bind("click", function(e)
    {
        var thread_id = $("#forum_fav").text();
    
        if ($("#forum_fav").hasClass("fav_on"))
        {
            // Turn the favourite off
            $.ajax({
                type: "GET",
                url: "/forum/actions/remove_favourite/" + thread_id,
                success: function(msg)
                {
                    if (msg == 0)
                    {
                        $("#forum_fav").removeClass("fav_on");
                        $("#forum_fav").addClass("fav_off");
                    }
                }
            });
        }
        else
        {
            // Turn the favourite on
            $.ajax({
                type: "GET",
                url: "/forum/actions/mark_favourite/" + thread_id,
                success: function(msg)
                {
                    if (msg == 0)
                    {
                        $("#forum_fav").removeClass("fav_off");
                        $("#forum_fav").addClass("fav_on");
                    }
                }
            });
        }
    });
}

addLoadEvent(attachFavourites);
addLoadEvent(parseDialogs);