

function current_article_id() {
  return $('div.post').find('.add_topcomment_icon').attr("id")
}

function article_vote(value) {
  var post = current_article_id();
  $.post('/async/forum/vote', { article_id: post, value: value }, function(r) { });
}

function comment(comment_id) {
  return $("div#" + comment_id + ".comment");
}

function comment_vote(comment_id, value) {
  //var post = current_article_id();
  unbind_comment(comment_id);
  $.post('/async/forum/vote', { comment_id: comment_id, value: value }, 
      function(r) { 
      
        //console.log("state: " + r["state"]);
        //console.log("karma: " + r["karma"]);
        //console.log("comment_id: " + r["comment_id"]);

        var c = comment(r["comment_id"]);

        c.find(".points:first").text(r["karma"] + " poäng.");

        var plus_icon = r["state"] == 1 ? "add_bg" : "add";
        var minus_icon = r["state"] == -1 ? "delete_bg" : "delete";

        $(c.find(".icon.plus:first")).attr({style : "background: url('/icons/" + plus_icon + ".png')"});
        $(c.find(".icon.minus:first")).attr({style : "background: url('/icons/" + minus_icon + ".png')"});

        bind_comment(comment_id);
      }, "json");
}

function bind_comments() {
  $('.icon.plus').click(vote_up_comment);
  $('.icon.minus').click(vote_down_comment);
  //$('div.comment_area').find('.icon.plus').click(vote_up_comment);
  //$('div.comment_area').find('.icon.minus').click(vote_down_comment);
}

function bind_comment(comment_id) {
  var c= comment(comment_id);
  c.find('.icon.plus:first').click(vote_up_comment);
  c.find('.icon.minus:first').click(vote_down_comment);
}

function vote_down_comment(e) {
  comment_vote(this.id, -1);
  e.preventDefault();
}

function vote_up_comment(e) {
  comment_vote(this.id, 1);
  e.preventDefault();
}

function unbind_comment(comment_id) {
  //console.log("unbinding " + comment_id);
  var c = comment(comment_id);
  c.find('.icon.plus:first').unbind('click');
  c.find('.icon.minus:first').unbind('click');
}

function bind_add_comment() {
  $(".add_comment_icon").click(function(e){ $(".add_comment." + this.id).toggle(); parent = this.id; e.preventDefault(); });
  $(".add_topcomment_icon").click(function(e){ $(".add_topcomment." + this.id).toggle(); parent = this.id; e.preventDefault(); });
}

$(document).ready(function(){
  
  //bind_add_comment();

  var parent = "";

  // Creation of comments to articles
  $(".add_topcomment_icon").click(function(e){ $(".add_topcomment").toggle(); e.preventDefault(); });
  $('.add_topcomment').submit(function(e) {
    $.post('/async/add_comment', $(this).serialize(),
      function(response) {
        $(".add_topcomment").toggle();
        $(".add_topcomment" + " textarea").attr({value : ""});
        $(".comments").append(response);
        $(".comment_area").attr({style: "display: inline"})
        bind_comments();
      });
    e.preventDefault();
  });

  // Creation of comments to comments
  $(".add_comment_icon").click(function(e){ $(".add_comment." + this.id).toggle(); parent = this.id; e.preventDefault(); });
  $('.add_comment').submit(function(e) {
    $.post('/async/add_comment', $(this).serialize(),
      function(response) {
        $(".add_comment." + parent).toggle();
        $(".add_comment." + parent + " textarea").attr({value : ""});
        $(".comment#" + parent).append(response);
        bind_comments();
      });
    e.preventDefault();
  });

  var icon = ""


  bind_comments();


  $('div.post').find('.chunk_add').click(function(e){
    article_vote(1);
    e.preventDefault();
  });

  $('div.post').find('.chunk_delete').click(function(e){
    article_vote(-1);
    e.preventDefault();
  });

  // Delete comment (admin only)
  $(".delete_comment_icon").click(function(e){

  	var answer = confirm("Ta bort kommentaren?")
  	if (answer){
      $.get('/async/delete_comment/' + this.id,
        function(r) {
          window.location.reload();
        }
      );                           
  	}

    e.preventDefault();
  });
});   
