

var submitVote = function(id, direction, action, callback) {
  $.ajax({
    url: action,
    type: "POST",
    data: { 'id' : id, 'vote' : direction },
    dataType: "json",    
    success: function(response){      
      callback(response);
    }
  });
}

//Todo - success callback
var submitComment = function(comment, parent_comment_id) {  
  $.ajax({
    url: "",
    type: "POST",
    data: { 'comment[comment_text]' : comment, 'comment[parent_id]' : parent_comment_id },
    dataType: "json",    
    success: function(response){ 
      if (response.status == 0) { 
        //apend the new comment to the parent and destroy the reply box
        if (parent_comment_id) {
          $("#c_" + parent_comment_id).closest(".comment").after(response.html);  
        } else {
          $("#comments").prepend(response.html);
        }
        $("#dynamic-comment-form textarea").val("").parent().parent().remove();
        $("#main-comment-form textarea").val("");
      }      
    }
  });
}


var processVote = function(buttonPressed, postProcess) {
  var f = $(buttonPressed).parent();
  var id = $(f).find("input[name=id]").val();
  var direction = $(buttonPressed).val();
  var action = $(f).attr("action");
  var response = submitVote(id, direction, action, postProcess);  
  return response;
}

//TODO: Recode very verbose
var processEntityVote = function(response) {  
  if (response["status"] == 0) { 
    var buttonPressed = "button[value=" + response["direction"] + "]";
    
    var prefix = "";
    if (response["type"] == "item_vote" ) {
      prefix = "i_";
    }
    else if (response["type"] == "comment_vote") {
      prefix = "c_";
    }
    else {
      return;
    }
    
    var form = $("form input[id=" + prefix + response["id"] + "]").parent();
    
    var pressedClass = "";
    var updateWhichScore = "";
    var otherButton = "";
    if (response["direction"] == "up") {
      pressedClass = "upvote";
      otherButton = "button[value=down]";
      otherButtonClass = "down-notvoted";
      updateWhichScore = "em.vote-up";
    }
    else if (response["direction"] == "down") {
      pressedClass = "downvote";
      otherButton = "button[value=up]";
      otherButtonClass = "up-notvoted";
      updateWhichScore = "em.vote-down";
    }
    
    $(buttonPressed, form).addClass(pressedClass);      
    $(otherButton, form).addClass(otherButtonClass);          
    
    //Update main score
    var currentScore = $("strong", form).html();
    currentScore = parseInt(currentScore, 10);    
    $("strong", form).html(currentScore + response["magnitude"]);
    
    //Update detailed scores
    var individualScore = $(updateWhichScore, form).html();
    individualScore = parseInt(individualScore, 10);    
    $(updateWhichScore, form).html(individualScore + response["magnitude"]);    
  }
}

$(document).ready(function(){

  /*** 
  /* Comment submission 
  ***/
  $(".comment-form .submit_button").live("click", function(){
    var form = $(this).parent();
    var comment = $(form).find("textarea").val();
    var parent_comment_id = $(form).find("input[name=parent_id]").val();
    var response = submitComment(comment, parent_comment_id);
    return false;
  });

  /*** 
  /* Comment vote 
  ***/
  $(".comment button.up, .comment button.down").live("click", function(){
    var response = processVote(this, processEntityVote) || {};
    return false;
  }); 
  
  /***
  /* Comment reply
  ***/
  var g_commentBox = $("#main-comment-form").clone().attr("id", "dynamic-comment-form");
  
  $(".comment .reply-link").live("click", function(){
    var thisComment = $(this).closest(".item-details"); 
    var parentCommentId = $(thisComment).parent().find("form").find("input[name=id]").val();
    console.log("parentCommentId:" + parentCommentId);
    $("input[name=parent_id]", g_commentBox).val(parentCommentId);
    $(thisComment).append(g_commentBox);
    return false;
  });   
   

  /*** 
  /* Item vote 
  ***/
  $(".item button").live("click", function(){
    var response = processVote(this, processEntityVote) || {};
    return false;
  }); 
  

  
});


