$(document).ready(function() {
  $("#btnCommentsNew").click(function() {
    $("#name").val('');
    $("#subject").val('');
    $("#text").val('');
  
    $("#parent_id").val('0'); 
    $("#commentsAction").val('add'); 
    $("#frmModuleCommentsAdd").submit(); 
    return false;
  });
  $("#btnCommentsAllNodes").click(function() {
    $("#parent_id").val('0'); 
    $("#wholenode").val(''); 
    $("#commentsAction").val(''); 
    action = $("#frmModuleCommentsAdd").attr('action').split('#');
    $("#frmModuleCommentsAdd").attr('action', action[0] + '#comments');
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsAll").click(function() {
    $("#commentsAction").val('all'); 
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsNone").click(function() {
    $("#commentsAction").val('none'); 
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsSel").click(function() {
    $("#commentsAction").val('sel'); 
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsSearch").click(function() {
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsSave").click(function() {
    $("#commentsAction").val('save'); 
    $("#frmModuleCommentsAdd").attr('method', 'post');
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsEditSave").click(function() {
    $("#commentsAction").val('editsave'); 
    $("#frmModuleCommentsAdd").attr('method', 'post');
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $(".btnCommentsPage").click(function() {
    $("#commentsPage").val($(this).attr('id').substr(3)); 
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsCancel").click(function() {
    $("#commentsAction").val('sel'); 
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $(".btnCommentsExp").click(function() {
    btnCommentsExp(this);
  });
  $(".btnCommentsCol").click(function() {
    btnCommentsCol(this);
  });
  $(".commentsAddEdit").click(function() {
    return commentsAddEdit(this);
  });
  $(".wholenode").click(function() {
    return wholeNode(this);
  });
  $(".react").click(function() {
    return react(this);
  });
  $("#btnCommentsRows").click(function() {
    $.cookie("commentsTree", "false");
    $("#commentsPage").val(1); 
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  $("#btnCommentsTree").click(function() {
    $.cookie("commentsTree", "true");
    $("#commentsPage").val(1); 
    $("#frmModuleCommentsAdd").submit();
    return false;
  });
  
  if ($.cookie('scrollTop') != '') {
    $(window).scrollTop($.cookie('scrollTop'));
    $.cookie('scrollTop', '');
  }
});

function commentsAddEdit(obj) {
  $("#commentsAction").val('edit');
  $("#commentid").val($(obj).attr('id').substr(3));
  action = $("#frmModuleCommentsAdd").attr('action').split('#');
  $("#frmModuleCommentsAdd").attr('action', action[0] + '#commentsid' + $(obj).attr('id').substr(3));
  $("#frmModuleCommentsAdd").submit();
  return false;
}

function wholeNode(obj) {
  $("#wholenode").val($(obj).attr('id').substr(3)); 
  action = $("#frmModuleCommentsAdd").attr('action').split('#');
  $("#frmModuleCommentsAdd").attr('action', action[0] + '#comments');
  $("#frmModuleCommentsAdd").submit();
  return false;
}

function react(obj) {
  $("#name").val('');
  $("#subject").val('');
  $("#text").val('');
  
  $("#parent_id").val($(obj).attr('id').substr(3)); 
  $("#commentsAction").val('add'); 
  action = $("#frmModuleCommentsAdd").attr('action').split('#');
  $("#frmModuleCommentsAdd").attr('action', action[0] + '#commentsid' + $(obj).attr('id').substr(3));
  $("#frmModuleCommentsAdd").submit();
  return false;
}

function btnCommentsExp(obj) {
  $.ajax({
    url: cfg['wwwroot'] + "/plugins/comments/ajax.php",
    cache: false,
    id: $(obj).parent().parent().children(".checkbox").attr('id').substr(4),
    data: {action: "view",
           view: "full",
           commentsQuery: $("#commentsQuery").val(),
           commentsTree: $.cookie('commentsTree'),
           template: 'planetopia',
           ajax: 'true',
           id: $(obj).parent().parent().children(".checkbox").attr('id').substr(4),
           datlang: lang['lang'],
           envlang: lang['lang']},
    beforeSend: function(){
    },
    success: function(html){
      $("#cdi" + this.id).html(html);
      $("#cdi" + this.id).find(".btnCommentsCol").click(function() {
        btnCommentsCol(this);
      });
      $("#cdi" + this.id).find(".wholenode").click(function() {
        wholeNode(this);
      });
      $("#cdi" + this.id).find(".react").click(function() {
        react(this);
      });
      $("#cdi" + this.id).find(".commentsAddEdit").click(function() {
        commentsAddEdit(this);
      });
    }
  });
  return false;
}

function btnCommentsCol(obj) {
  $.ajax({
    url: cfg['wwwroot'] + "/plugins/comments/ajax.php",
    cache: false,
    id: $(obj).parent().parent().children(".checkbox").attr('id').substr(4),
    data: {action: "view",
           view: "short",
           commentsQuery: $("#commentsQuery").val(),
           commentsTree: $.cookie('commentsTree'),
           template: 'planetopia',
           ajax: 'true',
           id: $(obj).parent().parent().children(".checkbox").attr('id').substr(4),
           datlang: lang['lang'],
           envlang: lang['lang']},
    beforeSend: function(){
    },
    success: function(html){
      $("#cdi" + this.id).html(html);
      $("#cdi" + this.id).find(".btnCommentsExp").click(function() {
        btnCommentsExp(this);
      });
      $("#cdi" + this.id).find(".wholenode").click(function() {
        wholeNode(this);
      });
      $("#cdi" + this.id).find(".react").click(function() {
        react(this);
      });
      $("#cdi" + this.id).find(".commentsAddEdit").click(function() {
        commentsAddEdit(this);
      });
    }
  });
  return false;
}