$(function(){
  $(".favorite-quote .add").click(function(){
    var q_id = $(this).attr("href").substring(1)
    $.post("/my_favorite_quotes", {favorite_quote_id:q_id})
    $(this).replaceWith("Done! <a href='/my_favorite_quotes'>View Your Favorite Quotes</a>")
    return(false);
  })
  
  $(".favorite-quote .remove").click(function(){
    var q_id = $(this).attr("href").substring(1)
    $.post("/my_favorite_quotes/" + q_id, {_method:"delete"})
    $(this).replaceWith("This quote has been removed!")
    return(false);
  })
  
  //Bookmarked Topics
  //TODO refactor
  function topicListBookmarkAdd() {
    var q_id = $(this).attr("href").substring(1)
    $.post("/bookmarked_topics", {"bookmarked_topic[topic_id]":q_id})
    
    var row_class = ".t" + q_id    
    $(row_class + " .favorite a").removeClass("add").addClass("remove").unbind("click").click(topicListBookmarkRemove)
    
    if($("#bookmarked .no-bookmarked-topics").size() > 0) {
      $("#bookmarked .no-bookmarked-topics p").hide();
      $("#bookmarked .no-bookmarked-topics .topics").addClass("show")
    }
    
    if($("#bookmarked " + row_class).size() == 0) {
      $(this).parents("tr").clone(true).appendTo("#bookmarked table tbody")
    }
    return(false);
  }
  
  function topicListBookmarkRemove() {
    var q_id = $(this).attr("href").substring(1)
    $.post("/bookmarked_topics/" + q_id, {_method:"delete"})
    
    var row_class = ".t" + q_id    
    $(row_class + " .favorite a").removeClass("remove").addClass("add").unbind("click").click(topicListBookmarkAdd)
    return(false);
  }
  
  $(".topics .favorite .add").click(topicListBookmarkAdd)
  $(".topics .favorite .remove").click(topicListBookmarkRemove)
  
  
  
  function topicBookmarkAdd() {
    var q_id = $(this).attr("href").substring(1)
    $.post("/bookmarked_topics", {"bookmarked_topic[topic_id]":q_id})
    
    $(this).removeClass("add").addClass("remove").unbind("click").click(topicBookmarkRemove)
    $(this).text("Un-Bookmark")
    return(false);
  }
  
  function topicBookmarkRemove() {
    var q_id = $(this).attr("href").substring(1)
    $.post("/bookmarked_topics/" + q_id, {_method:"delete"})
    
    $(this).removeClass("remove").addClass("add").unbind("click").click(topicBookmarkAdd)
    $(this).text("Bookmark")
    return(false);
  }
  
  $(".topics.show .favorite .add").click(topicBookmarkAdd)
  $(".topics.show .favorite .remove").click(topicBookmarkRemove)
  
  
  //Forum Questions
  function voteUp(){
    var q_id = $(this).attr("rel");
    $.post("/post_votes", {"post_vote[post_id]":q_id, "post_vote[value]":"1"});
    count = parseInt($(this).parent().find(".upvotes").text());
    count++;
    $(this).parent().find(".upvotes").text(count)
    $(this).find("img").attr("src", "/images/common/thumb-upactive.png");
    $(this).unbind("click")
    $(this).parent().find(".vote-down").unbind("click")
    $(this).parent().find("span.vote").removeClass("vote")
    return false;
  }
  
  function voteDown(){
    var q_id = $(this).attr("rel");
    $.post("/post_votes", {"post_vote[post_id]":q_id, "post_vote[value]":"-1"});
    count = parseInt($(this).parent().find(".downvotes").text());
    count++;
    $(this).parent().find(".downvotes").text(count)
    $(this).find("img").attr("src", "/images/common/thumb-downactive.png");
    $(this).unbind("click")
    $(this).parent().find(".vote-up").unbind("click")
    $(this).parent().find("span.vote").removeClass("vote")
    return false;
  }
  
  $(".vote-up").click(voteUp)
  $(".vote-down").click(voteDown)
})
