$("#commenttext").keyup(function() {
  $(".commentchars").text(this.value.length);
});

function karma(cid, delta, clearpermalink) {
  $.post("/customcode/owen_callcfc.cfm?component=o_comment&func=modifykarma&type=form&rand=" + o_ajaxrand, { cid: cid, delta: delta, clearpermalink: clearpermalink }, function(data) {
    if (data.code == 1) {
      if (delta == 1) { rated = " (You +1)"; } else { rated = " (You -1)"; }
      $(".comment_" + cid + "_rating").append(rated);
      $(".comment_" + cid + "_karmabuttons").hide();
    } else { 
      alert(data.message);
    }
  }, "json");
}

function showcomment(cid) {
  $(".comment_id_" + cid + " .userimg").toggle();
  $(".comment_id_" + cid + " .usertext").toggle();
  $(".comment_id_" + cid + " .toprightoptions").toggle();
  $(".comment_id_" + cid + " .toxic").toggle();
}

function reportcomment(cid, permalink) {
  $.get("/customcode/owen_callcfc.cfm?component=o_comment&func=reportcomment&type=url&rand=" + o_ajaxrand, { cid: cid, permalink: permalink }, function(data) {
    alert(data.message);
  }, "json");
}

function unapprovecomment(cid, clearpermalink) {
  $.post("/customcode/owen_callcfc.cfm?component=o_comment&func=unapprovecomment&type=form&rand=" + o_ajaxrand, { cid: cid, clearpermalink: clearpermalink }, function(data) {
    if (data.code == 1) {
      alert(data.message);
      window.location.reload(true);
    } else {
      alert(data.message);
    }
  }, "json");
}

function banuser(userid) {
  $.get("/customcode/owen_callcfc.cfm?component=o_comment&func=banuser&type=url&rand=" + o_ajaxrand, { userid: userid }, function(data) {
    alert(data.message);
  }, "json");
}

function performLoadFunctions(list) {
  $.get("/customcode/owen_callcfc.cfm?component=o_comment&func=performloadfunctions&type=url&rand=" + o_getajaxrand(), { list: list }, function(data) {
    if (data.karmacode == 1) {
      for ( var i in data.cids ) {
        if (data.cids[i]["delta"] == 1) { rated = " (You +1)"; } else { rated = " (You -1)"; }
        $(".comment_" + data.cids[i]["cid"] + "_rating").append(rated);
        $(".comment_" + data.cids[i]["cid"] + "_karmabuttons").hide();
      }
    }
    if (data.usercode == 1) {
      $(".notloggedin").toggle();
      $(".loggedin .username").html(data.user.name);
      $(".loggedin").toggle();
      if (data.user.accesslevel == 1) {
        $(".toprightoptions .moderator").show();
      }
    }
  }, "json");
}

$(document).ready(function() {
  $("#commentloginform").submit(function() {
    var myform = this;
    if (o_login(myform.email.value, myform.password.value)) {
      performLoadFunctions(o_commentlistids);
      loadsitewideheader();
    }
    return false;
  });

  $("#comment_form").submit(function() {
    var myForm = this;
    $('input[type=submit]', this).attr('disabled', 'disabled').attr('value', 'Submitting...');
    $.post('/customcode/owen_callcfc.cfm?component=o_comment&func=ajaxcreatecomment&type=form&rand=' + o_ajaxrand, $(this).serialize(), function(data) {
      if (data.code == 1) {
        alert(data.message);
        window.location.reload();
      } else { 
        alert(data.message);
        $("input[type=submit]", myForm).removeAttr('disabled').attr('value', 'Submit Comment');
      }
    }, "json");
    return false;
  });
});