// -------------------------------------------------------------------------------------------------
function list_SetSortOption(sortCode)
{
  $.post(
    "php/ajax_listChangeSortOption.php", 
    { sort: sortCode },
    function(data) 
    { 
      window.location.reload();
    }
  );
}

// -------------------------------------------------------------------------------------------------
function list_HandleVote(domAnch, voteUp, itemID)
{
  var anchImage = $(domAnch).find('img');
  var anchImageSrc = anchImage.attr('src');
  
  // start the process, dim the button, show spinner
  $(domAnch).fadeTo('fast', 0.5);
  anchImage.attr('src', 'images/ajax-loader-small.gif');
  
  $.post(
    "php/ajax_listItemVote.php", 
    { voteUp: voteUp, item: itemID },
    function(data) 
    {
      if (data['problem'])
      {
        // show the error icon briefly to show that the vote didn't take
        $(anchImage).attr('src', 'images/thumb-err.png');
        
        // ...but then reset back to whatever it was
        var restoreButton = function() {
            $(anchImage).attr('src', anchImageSrc);
        }
        setTimeout(restoreButton, 2000);
      }
      else
      {
        $('#vote-up-num-'+itemID).html(data['up']);
        $('#vote-down-num-'+itemID).html(data['down']);
        
        $(anchImage).attr('src', anchImageSrc);
      }
      $(domAnch).fadeTo('fast', 1.0);
    },
   "json");
}


// -------------------------------------------------------------------------------------------------
function list_HandleListVote(domAnch, voteUp, listID)
{
  var anchImage = $(domAnch).find('img');
  var anchImageSrc = anchImage.attr('src');
  
  // start the process, dim the button, show spinner
  $(domAnch).fadeTo('fast', 0.5);
  anchImage.attr('src', 'images/ajax-loader-small.gif');
  
  $.post(
    "php/ajax_listVote.php", 
    { voteUp: voteUp, list: listID },
    function(data) 
    {
      if (data['problem'])
      {
        // show the error icon briefly to show that the vote didn't take
        $(anchImage).attr('src', 'images/thumb-err.png');
        
        // ...but then reset back to whatever it was
        var restoreButton = function() {
            $(anchImage).attr('src', anchImageSrc);
        }
        setTimeout(restoreButton, 2000);
      }
      else
      {
        $('[id^=vote-up-the-list]').html(data['up']);
        $('[id^=vote-down-the-list]').html(data['down']);
        
        $(anchImage).attr('src', anchImageSrc);
      }
      $(domAnch).fadeTo('fast', 1.0);
    },
   "json");
}



// -------------------------------------------------------------------------------------------------
// smack on a new list entry
function list_AddPost(domButton, listID, ordering, authStamp)
{
  var inputState = UIInputButton_disableForAjax(domButton);
  
  if (inputState != undefined)
  {
    // initiate the message send
    $.post(
      "php/ajax_listNewPost.php", 
      { msg: B64.encode(inputState.MessageText), list: listID, auth: authStamp },
      function(data) 
      { 
        // we're done, re-enable the button
        $(domButton).fadeTo('fast', 1.0);
      
        // in the unlikely event of failure, put the error message in as the button text
        if (data['success'] == undefined)
        {
          $(domButton).html(data['result']);
          
          var restoreButton = function() {
              $(domButton).html(inputState.PrevHTMLContents);
          }
          setTimeout(restoreButton, 2000);
          
          // re-enable button + text area
          $(inputState.DomTextWindow).removeAttr('readonly');
          $(domButton).attr('onclick', inputState.PrevOnclick);
        }
        else
        {
          var domListlockToReload = $('#lst-list-replacable');
          $(domListlockToReload).fadeTo('fast', 0.5);

          var newItemAnchor = 'a#entry_' + data['result'];
          
          // regenerate the block of comments
          $.post(
            "php/ajax_genListBlock.php", 
            { list: listID, order: ordering },
            function(data) 
            { 
              // display the new comments
              domListlockToReload.html(data['content']);
              $(domListlockToReload).fadeTo('fast', 1.0);
              
              if (data['entryCount'] >= data['minimumEntries'])
              {
                $('#UIControlBox-is-open').show();
                $('#UIControlBox-not-open').hide();
              }
              else
              {
                $('#UIControlBox-not-open').show();
                $('#UIControlBox-is-open').hide();
              }
              
              // re-tag editable stuff
              list_TagEditableItems();

              // clear out the comment text window
              var e = jQuery.Event( 'keydown', { which: $.ui.keyCode.ENTER } );

              $(inputState.DomTextWindow).val('');
              $(inputState.DomTextWindow).trigger(e);
              $(inputState.DomTextWindow).removeAttr('readonly');
              
              // re-enable and reset the button
              $(domButton).attr('onclick', inputState.PrevOnclick);
              $(domButton).html(inputState.PrevHTMLContents);
              
              // scroll to the new item
              $('html, body').animate(
                {
                  scrollTop: $(newItemAnchor).offset().top
                }, 2000);
            },
            "json");
        }
      },
      "json");  
  }
}

// -------------------------------------------------------------------------------------------------
// handle the request to add a new comment to an item (listID = 0) or list (listItemID = -1)
function list_AddComment(domButton, listItemID, listID, replaceID, authStamp)
{
  var inputState = UIInputButton_disableForAjax(domButton);
  
  if (inputState != undefined)
  {
    // initiate the message send
    $.post(
      "php/ajax_newComment.php", 
      { msg: B64.encode(inputState.MessageText), id: listItemID, list: listID, auth: authStamp },
      function(data) 
      { 
        // we're done, re-enable the button
        $(domButton).fadeTo('fast', 1.0);
      
        // in the unlikely event of failure, put the error message in as the button text
        if (data['success'] == undefined)
        {
          $(domButton).html(data['result']);
          
          var restoreButton = function() {
              $(domButton).html(inputState.PrevHTMLContents);
          }
          setTimeout(restoreButton, 2000);
          
          // re-enable button + text area
          $(inputState.DomTextWindow).removeAttr('readonly');
          $(domButton).attr('onclick', inputState.PrevOnclick);
        }
        else
        {
          var domCommentBlockToReload = $('#lst-comment-replacable-'+replaceID);
          $(domCommentBlockToReload).fadeTo('fast', 0.5);
          
          // regenerate the block of comments
          $.post(
            "php/ajax_genCommentBlock.php", 
            { list: listID, item: listItemID },
            function(data) 
            { 
              // display the new comments
              domCommentBlockToReload.html(data);
              $(domCommentBlockToReload).fadeTo('fast', 1.0);
              
              // clear out the comment text window
              var e = jQuery.Event( 'keydown', { which: $.ui.keyCode.ENTER } );

              $(inputState.DomTextWindow).val('');
              $(inputState.DomTextWindow).trigger(e);
              $(inputState.DomTextWindow).removeAttr('readonly');
              
              // re-enable and reset the button
              $(domButton).attr('onclick', inputState.PrevOnclick);
              $(domButton).html(inputState.PrevHTMLContents);
            },
            "html");
        }
      },
      "json");  
  }
}

// -------------------------------------------------------------------------------------------------
function list_TagEditableItems()
{
  $('span.edit_list_entry').each(function(index) 
  {
    var entryID = parseInt( $(this).attr('id').substring(2) );

    $(this).editable('php/ajax_listPostEdit.php?id=' + entryID,
    { 
      loadurl     : 'php/ajax_listPostFetch.php',
      indicator   : '<img src="images/ajax-loader-small.gif">',
      tooltip     : 'Click to edit post...',
      style       : 'display: inline;',
      type        : "textarea",
      submit      : "Save",
      cancel      : "Cancel",
    });    
  });

}

// -------------------------------------------------------------------------------------------------
function list_UNRejectItem(domButton, entryID)
{
  $.post(
    "php/ajax_listPostReject.php", 
    { msg: "", id: entryID },
    function(data) 
    { 
      location.reload(); 
    });
}

// -------------------------------------------------------------------------------------------------
function list_RejectItem(domButton, entryID)
{
  $('#UIRejectionDialog').dialog({
      modal: true,
      resizable: false,
      buttons: {
        "Kill It": function(event) 
        {
          var rejectReason = $("#UIRejectionDialog #Reason");
          
          $.post(
            "php/ajax_listPostReject.php", 
            { msg: rejectReason.val(), id: entryID },
            function(data) 
            { 
              location.reload(); 
            });
        },
        "No Don't" : function(event)
        {
          $( this ).dialog( "close" );
        }
      }
    });
}
