$(document).ready(function() {
  
  $(".post").hover(
  function() {
    $(this).addClass("post-hover");
  },
  function()
  {
    $(this).removeClass("post-hover");
  });
  
  var currently_expanded = null;
  function loadReply(id) {
    if ( $("#reply-container-" + id).length > 0) {
      if (currently_expanded != id) {
        $("#post-" + currently_expanded).removeClass('post-selected');       
        $("#tweet-replies").hide();
        $("#tweet-reply").hide().load("/tweet/reply/" + id, function() {}).appendTo("#reply-container-" + id).show();  
        currently_expanded = id;
        $("#post-" + currently_expanded).addClass('post-selected');
      } else {
        $("#tweet-reply").hide();
        $("#tweet-replies").hide();
        $("#post-" + currently_expanded).removeClass('post-selected');        
        currently_expanded = null;
      }
    }
  }
  
  function loadReplies(id) {
    if ( $("#replies-container-" + id).length > 0) {
      if (currently_expanded != id) {
        $("#post-" + currently_expanded).removeClass('post-selected');      
        $("#tweet-reply").hide(); 
        $("#tweet-replies").hide().load("/tweet/replies/" + id, function() {}).appendTo("#replies-container-" + id).show();  
        currently_expanded = id;
        $("#post-" + currently_expanded).addClass('post-selected'); 
      } else {
        $("#tweet-replies").hide();
        $("#tweet-reply").hide();
        $("#post-" + currently_expanded).removeClass('post-selected');        
        currently_expanded = null;
      }
    }
  }
  
 $(".reply").click(function() {
   var tweet_id = $(this).attr('id').replace(/([^\d])/g, "");
   loadReply(tweet_id);
   return false;
 });
 
 $(".vote-button").click(function() {
   var tweet_id = $(this).attr('id').replace(/([^\d])/g, "");
     $.post("/tweet/vote/" + tweet_id, {ajax_function : 'vote', tweet_id : tweet_id}, function(data) {
       $("#votes-count-num-" + tweet_id).html(data);
     });
   return false;   
 });
 
 
 $(".replies").click(function() {
    var tweet_id = $(this).attr('id').replace(/([^\d])/g, "");
    loadReplies(tweet_id);
    return false;
  });

 $("#form-inline-reply").live("submit", function() {
   var inputs = [];
   $(":input", this).each(function() {
     inputs.push({name: this.name, value: this.value});
   })
   var url = $(this).attr('action');
   $.post(url, inputs, function(response) {
     $("#reply-container-" + currently_expanded).html(response);
   });
   return false;
 });
 
 $("#suggest-button").click(function() {
   $("#suggest-box").slideToggle("fast");
   $("#suggest-box").load("/tweet/suggest");
 });
 
 $("#form-inline-suggest").click(function() {
   $("#suggest-box").slideToggle("fast");
 });
 
 $("#suggest-cancel").live("click", function() {
   $("#suggest-box").slideToggle("fast");
  });
  
 $("#form-inline-suggest").live("submit", function() {
    var inputs = [];
    $(":input", this).each(function() {
      inputs.push({name: this.name, value: this.value});
    })
    var url = $(this).attr('action');
    $.post(url, inputs, function(response) {
      $("#suggest-box").html(response);
    });
    return false;
  });
  
});