
// -------------------------------------------------------------------------------------------------
function messages_ExpandFull(msgID, enabled)
{
  var msgFull = $('#msg-full_' + msgID);
  var msgTrunc = $('#msg-truncated_' + msgID);

  if (enabled == true)
  {
    $(msgTrunc).hide();
    $(msgFull).show();
  }
  else
  {
    $(msgTrunc).show();
    $(msgFull).hide();
  }
}

// -------------------------------------------------------------------------------------------------
function messages_ModifyItem(domItem, msgID, operation)
{
  var messageBlock = $(domItem).parentsUntil('.lst-message').parent();
  
  // fade the message to half, while the AJAX post processes asynchronously
  messageBlock.fadeTo('fast', 0.5);
  
  $.post(
    "php/ajax_msgModify.php", 
    { id: msgID, op: operation },
    function(data) 
    { 
      if (data['success'] != undefined)
      {
        // we're done, remove the block from the screen
        messageBlock.slideUp();
        messages_UpdateBoxCounters();
      }
      else
      {
        window.alert(data['result']);
      }
    },
    "json");
}

// -------------------------------------------------------------------------------------------------
function messages_ExpandAndFetchImageContent(msgID)
{
  var msgFull = $('#msg-full_' + msgID);
  var msgTrunc = $('#msg-truncated_' + msgID);
  $(msgTrunc).hide();
  $(msgFull).show();
    
  var replBlock = $("#UIImageMessageRep_" + msgID);
  
  // load in the message if the load icon is still present
  if (replBlock.find('.tmp-load').get(0) != undefined)
  {
    $.post(
      "php/ajax_msgFetch.php", 
      { id: msgID },
      function(data) 
      { 
        replBlock.html("<img class='ui-corner-all lst-avatar-img' src='" + data + "'/>");
      },
      "html");
  }
}

// -------------------------------------------------------------------------------------------------
function messages_UpdateBoxCounters()
{
  // get the latest stats and update the UI components with the new counts
  $.get(
    "php/ajax_msgGetCounters.php", 
    null,
    function(data) 
    { 
      $('span#UIMessagesUnread').html('(' + data['unread'] + ')');
      $('span#UIMessagesArchived').html('(' + data['archived'] + ')');
      $('span#UIMessagesSent').html('(' + data['sent'] + ')');      
    },
    "json");
}

// -------------------------------------------------------------------------------------------------
// user wants to send a message to a chosen user
function messages_SendFromForm(domButton, recipientUserID, authStamp)
{
  var inputState = UIInputButton_disableForAjax(domButton);
  
  if (inputState != undefined)
  {
    // initiate the message send
    $.post(
      "php/ajax_msgSend.php", 
      { msg: B64.encode(inputState.MessageText), id: recipientUserID, auth: authStamp },
      function(data) 
      { 
        // we're done, re-enable the button
        $(domButton).fadeTo('fast', 1.0);
        $(domButton).attr('onclick', inputState.PrevOnclick);
        $(inputState.DomTextWindow).removeAttr('readonly');
      
        // in the unlikely event of failure, put the error message in as the button text
        if (data['success'] == undefined)
        {
          // show the error
          $(domButton).html(data['result']);
        }
        else
        {
          var e = jQuery.Event( 'keydown', { which: $.ui.keyCode.ENTER } );

          $(inputState.DomTextWindow).val('');
          $(inputState.DomTextWindow).trigger(e);

          // initially we show that the message was dispatched
          $(domButton).html("<strong>Message Sent!</strong>");
          
          messages_UpdateBoxCounters();
        }
        
        // ... then reset back to "Reply" or whatever it was N seconds later
        var restoreButton = function() {
            $(domButton).html(inputState.PrevHTMLContents);
        }
        setTimeout(restoreButton, 3000);        
      },
      "json");  
  }
}

// -------------------------------------------------------------------------------------------------
function messages_SetImageMessageAsAvatar(domButton, messageID, authStamp)
{
  var inputState = UIInputButton_disableForAjax(domButton);
  
  if (inputState != undefined)
  {
    // initiate the message send
    $.post(
      "php/ajax_msgImageMessageAsAvatar.php", 
      {id: messageID, auth: authStamp },
      function(data) 
      { 
        // we're done, re-enable the button
        $(domButton).fadeTo('fast', 1.0);
        $(domButton).attr('onclick', inputState.PrevOnclick);
        $(inputState.DomTextWindow).removeAttr('readonly');
      
        // in the unlikely event of failure, put the error message in as the button text
        if (data['success'] == undefined)
        {
          // show the error
          $(domButton).html(data['result']);
        }
        else
        {
          var e = jQuery.Event( 'keydown', { which: $.ui.keyCode.ENTER } );

          $(inputState.DomTextWindow).val('');
          $(inputState.DomTextWindow).trigger(e);

          // initially we show that the message was dispatched
          $(domButton).html("<strong>That Probably Worked!</strong>");
          
          messages_UpdateBoxCounters();
        }
        
        // ... then reset back to "Reply" or whatever it was N seconds later
        var restoreButton = function() {
            $(domButton).html(inputState.PrevHTMLContents);
        }
        setTimeout(restoreButton, 3000);        
      },
      "json");  
  }
}