/*
    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 addLoadEvent (func)
{
    var oldonload = window.onload;

    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

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");
                    }
                }
            });
        }
    });
}

/* inspired by http://aktuell.de.selfhtml.org/artikel/javascript/bbcode/  */
function ins(txtSmilie)
{
  aTag = ''; 
  eTag = '';

  var input = document.getElementById("message");
  input.focus();

  /* IE */
  if(typeof document.selection != 'undefined') {
    /* insert text */
    var range = document.selection.createRange();
    var insText = txtSmilie;
    range.text = aTag + insText + eTag;
    /* correct cursor position */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* gecko-based */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* insert text */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = txtSmilie;
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* correct cursor position */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* all others */
  else
  {
    var pos;
    pos = input.value.length;
    var insText = txtSmilie;
    input.value = input.value.substr(0, pos) + aTag + insText + eTag;
  }
}

addLoadEvent(attachFavourites);
addLoadEvent(parseDialogs);