/*
    Shaun the Sheep JavaScript Collection
    v1.3
    Updated: 29th April 2010, Richard Davey (web.producer@aardman.com)
*/

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

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

function openRM2(bid, tid, mid, uid)
{
    board_id = bid;
    thread_id = tid;
    message_id = mid;
    user_reported = uid;
    
    $('#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');
        }
    );
    
    $('.ftooltip').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
        });

    $("#dialog1").dialog({
        autoOpen: false,
        zIndex: 9999,
        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,
        zIndex: 9999,
        bgiframe: true,
        resizable: false,
        height:220,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            'Report Message': function()
            {
                // when trying to report a mesage, perform a check to see if the current logged in user has reported the the user being reported more than once
                // if they have, it will alert them to the fact that they can't report them twice
                $.ajax({
                    url: "/forum/actions/check_reports/" + user_reported,
                    success: function(data)
                    {
                        if (data >= 1)
                        {
                            alert("You have already reported this user. A message has been sent to the moderation team for them to review.");
                        }
                        else
                        {
                            $.get("/forum/actions/report_message/" + board_id + "/" + thread_id + "/" + message_id + "/" + user_reported);
                            $("#msgwrapper" + message_id).hide();
                        }
                    }
                });
                $(this).dialog('close');
            },
            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 pollVote ()
{
    $("#pollVote").submit(
        function(event)
        {
            event.preventDefault();
            
            $('input[name="ajax"]',this).val('true');
            
            var callback = function(data)
            {
                if (data == "error")
                {
                    $("#pollVoteSubmit").attr('value', 'Try again later');
                }
                else
                {
                    $("#pollVoteSubmit").hide();
                    $("#pollVoteSubmit").attr('disabled', 'disabled');
                    $("#pollVoteResult").attr('style', 'display: block');
                }
            }
            
            $.post( $(this).attr('action'), $(this).serialize(), callback );
        }
    );
    
    
}

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

function externalImage ()
{
    $('.entry img').filter(
        function()
        {
            if ($(this).attr("src").substr(0, 27) == "http://images.shaunthesheep")
            {
                return false;
            }
            else
            {
                $(this).click(
                    function()
                    {
                        window.open($(this).attr("src"), "_blank");
                        //window.open("/forum/external/" + $(this).attr("src"), "_blank");
                    }
                );
                $(this).css('cursor', 'pointer');
                $(this).attr('title', 'This is an external image. Aardman are not responsible for the content displayed.');
                return true;
            }
        }
    );
}

addLoadEvent(pollVote);
addLoadEvent(attachFavourites);
addLoadEvent(parseDialogs);
addLoadEvent(externalImage);

