function collapse_comment(comment_id)
{
  var comment = String("#" + comment_id);
  $(comment).addClass("collapsed");
  $(comment + " .replies").slideUp();
}

function expand_comment(comment_id)
{
  var comment = String("#" + comment_id);
  
  activate_comments($(comment + " .replies .comment"));
  
  $(comment).removeClass("collapsed");
  $(comment + " .replies").slideDown();
}

function toggle_comment_expansion(comment_id)
{
  var comment = String("#" + comment_id);
  if($(comment).hasClass("collapsed")) { expand_comment(comment_id); }
  else { collapse_comment(comment_id); }
}

function load_replies(post_id)
{
  var comment = String("#comment_" + post_id);
  var controller = $(comment + " .collapse_controller");
  var replies = String(comment + " .replies");
  
  collapse_comment("comment_" + post_id);
  
  $.ajax({
    type: "GET",
    url: "/posts/" + post_id + "/replies",
    success: function(markup) {
      $(replies).empty().append(markup);
      var child_comments = String(replies + " .comment");
      var records_length = $(child_comments).length;
      
      if(records_length < 1) { $(controller).hide(); }
      else { $(controller).text( String("Show (" + records_length + ") comments") ); }
      
      $(child_comments).each( function(i) {
        if($(comment).hasClass("comment_level2")){ $(this).addClass("comment_level3"); }
        else if($(comment).hasClass("comment_level3")){ $(this).addClass("comment_level4"); }
        else { $(this).addClass("comment_level2"); }
        $(this).append("<img src='/images/buttons/reply_to_arrow.png' class='decoration_left' />");
      });
    }
  });
}

function activate_reply_ui(comment_id)
{
  var reply_ui = String("#" + comment_id + " .reply_ui");
  var post_id = comment_id.split("_")[1];
  
  $(reply_ui).html( "<img src='/images/buttons/post_a_reply.png' height='20px' width='68px' title='Reply to this post' />" );
  
  $(reply_ui).click( function(){
    // Disable button
    $(reply_ui).html("<img src='/images/buttons/posting_now.png' height='20px' width='68px' title='post reply form is open' />");
    $(reply_ui).unbind();
    
    // Pull results
    $.ajax({
      type: "GET",
      url: "/posts/" + post_id + "/replying_to",
      success: function(markup){
        reply_to_comment(comment_id, markup);
      }
    });
  });
}

function reset_form(comment_id)
{
  var comment = String("#" + comment_id);
  var reply_form = String(comment + " .reply_to_form");
  
  $(reply_form).slideUp("normal", function(){
    $(reply_form).html( "&nbsp;" );
    activate_reply_ui(comment_id);
  });
}

function activate_new_reply(comment_id, markup)
{
  var comment = String("#" + comment_id);
  var replies = String(comment + " .replies");
  
  reset_form(comment_id);
  $(replies).slideUp("normal", function(){
    $(replies).append(markup);
    $(replies).slideDown();
  });
  
  
}

function reply_to_comment(comment_id, markup)
{
  var comment = String("#" + comment_id);
  var reply_form = String(comment + " .reply_to_form:first");
  var replies = $(comment + " .replies");
  var controller = $(comment + " .collapse_controller");
  
  collapse_comment(comment_id);
  
  // Load Form and display it
  $(reply_form).html( markup );
  $(reply_form).slideDown("normal", function(){
    $(reply_form + " textarea:first").focus();
  });
  
  // Activate on submit stuff
  $(reply_form + " .post_form_button").click( function (){
    // Grab form data
    var post = String(reply_form + " .post_form");
    
    // Update replies
    $.ajax({
      type: "POST",
      url: $(reply_form + " .post_form:first").attr("action"),
      data: $(post).serialize(),
      success: function(markup) {
        $(replies).empty().append(markup);
        var child_comments = String(replies + " .comment");
        var records_length = $(child_comments).length;

        if(records_length < 1) { $(controller).hide(); }
        else { $(controller).text( String("Show (" + records_length + ") comments") ); }

        $(child_comments).each( function(i) {
          if($(comment).hasClass("comment_level2")){ $(this).addClass("comment_level3"); }
          else if($(comment).hasClass("comment_level3")){ $(this).addClass("comment_level4"); }
          else { $(this).addClass("comment_level2"); }
          $(this).append("<img src='/images/buttons/reply_to_arrow.png' class='decoration_left' />");
        });
        
        expand_comment(comment_id);
        reset_form(comment_id);
      }
    });
  });
  
  // Activate cancel stuff
  $(reply_form + " .cancel").click( function(){
    reset_form(comment_id);
  });
}

function activate_reply(comment_id)
{
  var comment = String("#" + comment_id);
  
  reset_form(comment_id);
  collapse_comment(comment_id);
  if(!$(comment).hasClass("preloaded"))
  {
    load_replies(comment_id.split("_")[1]);
    $(comment).addClass("preloaded");
  }
}

function activate_report(comment_id)
{
  $("#" + comment_id + " .report_ui").remove();
}

function activate_collapse_controller(comment_id)
{
  var controller = String("#" + comment_id + " .collapse_controller");
  
  if($(controller).text() != null)
  {
    $(controller).click(function(){ toggle_comment_expansion(comment_id); });
  }
}

function activate_comments(comments)
{ 
  $(comments).each( function(i)
    {
      var comment_id = $(this).attr("id");
      
      activate_collapse_controller(comment_id);
      activate_reply(comment_id);
      activate_report(comment_id);
    }
  );
}

activate_comments($(".comment"));