
function emptyFunction()
{
}

function sendCmd(cmd, opt)
{
  options = {
    callback: emptyFunction
  }

  var callback = opt.callback;

  postBody = "cmd="+cmd+"&blog="+_group_name;
  for( i=2; i<arguments.length;++i)
    postBody = postBody + "&p" + (i-1) + "=" + arguments[i];

  $.ajax({
    url:"/scripts/commands2.php", 
    data: postBody,
    type: 'POST',
    success: function(data, textStatus)
    {
      callback(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown)
    {
      alert("Failure: " + textStatus );
    }
  });
}

CommentHelper2 = function() { };
CommentHelper2.prototype =
{
  initialize: function()
  {
    this.inEdit = false;
  },
  deleteComment: function( commentId, postId )
  {
    sendCmd( "delete_comment",
           { callback: function(){
               $('#commentDeleteRestoreLink_'+commentId).attr('href', 'javascript:commentHelper.restoreComment('+commentId+','+postId+')');
               $('#commentDeleteRestoreLink_'+commentId).html('Restore');
             }},
           commentId, postId  );

  },
  restoreComment: function( commentId, postId )
  {
    sendCmd( "restore_comment",
           { callback: function(){
               //window.location = window.location;
               $('#commentDeleteRestoreLink_'+commentId).attr('href','javascript:commentHelper.deleteComment('+commentId+','+postId+')');
               $('#commentDeleteRestoreLink_'+commentId).html('Delete');
             }},
           commentId, postId  );

  },
  showEditor: function( elementId, commentId, postId )
  {
    if( this.inEdit )
    {
      if( $("#comment_input").val() != "" )
        if( !confirm("Lose your comment?") )
          return;
    }

    if( this.edit ) this.edit.remove();
    this.edit = null;
    this.inEdit = true;

    this.parentId = commentId;
    this.postId = postId;
    $('#'+elementId).prepend(comment_block);

    var _this = this;
    $("#add_comment_btn").click(function(){_this.onSave()});
    $("#cancel_comment_btn").click(function(){_this.stopEdit()});

    this.edit = $("#add_comment");
  },
  setDirty: function()
  {
    this.isDirty = true;
  },
  stopEdit: function()
  {
    if( this.inEdit )
    {
      if( $("#comment_input").val() != "" )
        if( !confirm("Lose your comment?") )
          return;
    }

    this.inEdit = false;
    this.edit.remove();
    this.edit = null;
  },
  onSave: function()
  {
    name = $("#comment_name").val();
    email = $("#comment_email").val();
    comment = $("#comment_input").val();

    var _this = this;
    sendCmd( "save_comment",
           { callback: function(data){_this.saveCallback(data)} },
           this.postId, this.parentId, name, email, comment );
  },
  saveCallback: function(t)
  {
    this.inEdit = false;
    this.stopEdit();
    $(this.elem).append(t);
  },
  startNewEdit: function(id, elementId, commentId, postId  )
  {
    //this.elem = $(id);
    this.elem = $('#'+elementId);
    var t = this;

    $('#'+id).show();
    this.showEditor( elementId, commentId, postId );
  },
  toggle: function(id)
  {
//    new Effect.toggle(id,'appear', {duration: 0.25});
    $('#'+id).toggle();
  }
};
