var voteUp = function(story_comment_id) {
  $("vote_up_" + story_comment_id).hide();
  $("vote_down_" + story_comment_id).hide();
  var score = $("score_" + story_comment_id).innerHTML;
  score = (score / 1) + 1;
  
  if(score > 0)
    $("score_" + story_comment_id).update("+" + score);
  else
    $("score_" + story_comment_id).update(score);

  new Ajax.Request(URLS.vote_up + story_comment_id);
}

var voteDown = function(story_comment_id) {
  $("vote_up_" + story_comment_id).hide();
  $("vote_down_" + story_comment_id).hide();
  var score = $("score_" + story_comment_id).innerHTML;
  score = (score / 1) - 1;
  
  if(score > 0)
    $("score_" + story_comment_id).update("+" + score);
  else
    $("score_" + story_comment_id).update(score);

  new Ajax.Request(URLS.vote_down + story_comment_id);
}

var yes = function(story_id) {
  $("story_" + story_id + "_yes_link").addClassName("loading");
  new Ajax.Request(URLS.yes + story_id, {
    onComplete: function(t) {
      $("story_" + story_id + "_yes_link").removeClassName("loading");
      $("story_" + story_id + "_yes").update("It's OK, you'll still go to heaven.");
      $("story_" + story_id + "_yesed_count").update(parseInt($("story_" + story_id + "_yesed_count").innerHTML) + 1);
    }.bind(this, story_id)
  });
}

var no = function(story_id) {
  $("story_" + story_id + "_no_link").addClassName("loading");
  new Ajax.Request(URLS.no + story_id, {
    onComplete: function(t) {
      $("story_" + story_id + "_no_link").removeClassName("loading");
      $("story_" + story_id + "_no").update("OMG, you'll go to hell !");
      $("story_" + story_id + "_noed_count").update(parseInt($("story_" + story_id + "_noed_count").innerHTML) + 1);
    }.bind(this, story_id)
  });
}

var add_favourite = function(story_id) {
  if(!GLOBALS.is_user) {
    if(confirm("You have to be logged in. Do you want to login now ?")) {
      window.location = URLS.signin;
    }
  } else {
    $("story_" + story_id + "_favourite_link_add").addClassName("loading");
    new Ajax.Request(URLS.add_favourite + story_id, {
      onComplete: function(t) {
        $("story_" + story_id + "_favourite_link_add").removeClassName("loading");
        $("story_" + story_id + "_favourite_link_add").hide();
        $("story_" + story_id + "_favourite_link_remove").show();
        $("story_" + story_id + "_favourite_count").update(parseInt($("story_" + story_id + "_favourite_count").innerHTML) + 1);
      }.bind(this, story_id)
    });
  }
}

var remove_favourite = function(story_id) {
  if(!GLOBALS.is_user) {
    if(confirm("You have to be logged in. Do you want to login now ?")) {
      window.location = URLS.signin;
    }
  } else {
    $("story_" + story_id + "_favourite_link_remove").addClassName("loading");
    new Ajax.Request(URLS.remove_favourite + story_id, {
      onComplete: function(t) {
        $("story_" + story_id + "_favourite_link_remove").removeClassName("loading");
        $("story_" + story_id + "_favourite_link_remove").hide();
        $("story_" + story_id + "_favourite_link_add").show();
        $("story_" + story_id + "_favourite_count").update(parseInt($("story_" + story_id + "_favourite_count").innerHTML) - 1);
      }.bind(this, story_id)
    });
  }
}