  function RefreshComments(id, from)
  {
    from = from || 0;
    var $id="Comments"+id;
    $('#'+$id+' #ShowCommentProgress').show();
    jQuery.ajax({
          type: "POST",
          url:"/en/comments/get",
          data: {group:id, from:0},
          success: function(res)
          {
            //alert(res.count);
            $('#'+$id+' #ShowCommentProgress').hide();
            $('#'+$id+" #CommentItems").html(res.text).show();
          },
          error: function()
          {
            $('#'+$id+' #ShowCommentProgress').hide();
            $('#'+$id+' #CommentItems').html("There is an error").show();
          },
          dataType: 'json'
    });
    return false;
  }

  function InitCommentsBlock(id)
  {
    var $id = 'Comments'+id;
    $('#'+$id+' #ShowCommentsLink').click(function(){
      RefreshComments(id);
      $('#'+$id+' #ShowCommentsLink').hide();
    });

    $('#'+$id+' #AddCommentForm  #comment').val("");

    $('#'+$id+' #AddCommentForm').form({
        url: '/en/comments/add',
        errorClass: 'fail',
        success:function()
        {
            RefreshComments(id);
            $('#'+$id+' #AddCommentForm  #comment').val("");
        }
    });

    $('#'+$id+' #AddCommentButton').click(function(){
      $('#'+$id+' #AddCommentForm').form('submit');
    });
  }

  function DeleteComment(group, id)
  {
      var $gr="Comments"+group;
      $('#'+$gr+' #ShowCommentProgress').show();

      jQuery.ajax({
          type: "POST",
          url:"/en/comments/delete",
          data: {id:id},
          success: function(res)
          {
            RefreshComments(group);
          },
          error: function()
          {
            $('#'+$gr+' #ShowCommentProgress').hide();
            $('#'+$gr+' #CommentItems').html("There is an error").show();
          },
          dataType: 'html'
    });
  }

  function CommentVisible(group, id, vis)
  {
      var $gr="Comments"+group;
      $('#'+$gr+' #ShowCommentProgress').show();

      jQuery.ajax({
          type: "POST",
          url:"/en/comments/visible",
          data: {id:id, visible: vis},
          success: function(res)
          {
            RefreshComments(group);
          },
          error: function()
          {
            $('#'+$gr+' #ShowCommentProgress').hide();
            $('#'+$gr+' #CommentItems').html("There is an error").show();
          },
          dataType: 'html'
    });
  }

var WaitDialog={
    dlg:null,
    wait_depth:0,
    init:function(sel)
    {
      var _this = this;
      sel = sel || "#WaitDialog";
      this.dlg = $(sel);
      this.dlg.dialog({
        modal:true, autoOpen:false, height:"70",
        beforeClose:function()
        {
          return _this.wait_depth==0;
        }
      });
    },
    show:function()
    {
      if (this.dlg==null) this.init();
      if (this.wait_depth==0)
        this.dlg.dialog('open');
      this.wait_depth+=1;
    },
    hide:function()
    {
      if (this.dlg==null) this.init();
      this.wait_depth-=1;
      if (this.wait_depth==0)
        this.dlg.dialog('close');
    }
  }

