Drupal.ltvCommentAutoAttach = function() {
  // wiring up the comment control form
  $('a.comment-order-link').click(function() {
    if($('#edit-order').val() == 2) {
      $('#edit-order').val(1);
    } else {// $('#edit-order').val() == 1
      $('#edit-order').val(2);
    }
    $(this).parents('form').submit();
    return false;
  });
  $('a.comment-mode-link').click(function() {
    if($('#edit-children-expanded').val() > 0) {
      $('#edit-children-expanded').val(0); // collapse

      $('.comment-wrapper').not('.comment-reply-stub').each(function(){
		    Drupal.ltvCommentHideChildren(this.id.match(/[0-9]+$/));
		  });

      $(this).html('Expand all');
    } else {// $('#edit-children-expanded').val() == 0
      $('#edit-children-expanded').val(1); // expand

      $('.comment-wrapper').not('.comment-reply-stub').each(function(){
        Drupal.ltvCommentShowChildren(this.id.match(/[0-9]+$/));
      });

      $(this).html('Collapse all');
    }
  });
  $('#edit-comments-per-page').change(function(){$(this).parents('form').submit();});

  // wiring up the comment
  $('.comment-wrapper').not('.comment-reply-stub').each(function(){
    // this is a comment wrapper and the in is the comment id
    var cid = this.id.match(/[0-9]+$/);

    // check to see if it this comment can be replied to
    if($('.comment-reply a', this).length > 0) {
      $(this).get(0).allow_children = true;
    } else {
      $(this).get(0).allow_children = false;
    }


    // wire up reply button
    if($(this).get(0).allow_children) {
	    $('.comment-reply a', this).click(function() {
	      Drupal.ltvCommentShowReply(cid);
	      return false;
	    });
    }

    // get child count
    $(this).get(0).num_children = 0;

    $(this).get(0).num_children = parseInt($(this).next('.indented').children('.comment-wrapper').not('.comment-reply-stub').length);
    $('.comment-reply-count-container', this).html(
      '<div class="comment-reply-count">' + $(this).get(0).num_children + ' replies </div>' // this only works on the assumption that children cannot have children
    );


    // if has children add controls to show/hide thime
    if($(this).next('.indented').children('.comment-wrapper').not('.comment-reply-stub').length > 0) {
      $('.comment-reply-count-container .comment-reply-count', this).hide();

      var showButton;
      var hideButton;
      showButton = $('<span></span>')
                        .attr('class', 'comment-show-children')
                        .html($(this).get(0).num_children + ' replies (+)')
                        .click(function() {
                          Drupal.ltvCommentShowChildren(cid);
                        });
      hideButton = $('<span></span>')
                        .attr('class', 'comment-hide-children')
                        .html($(this).get(0).num_children + ' replies (-)')
                        .click(function() {
                          Drupal.ltvCommentHideChildren(cid);
                        });
      $('.comment-reply-count-container', this)
	      .append(showButton)
	      .append(hideButton);

      // create reply stub below the children
      if($(this).get(0).allow_children) {
        Drupal.ltvCommentReplyStub(cid).insertAfter($(this).next('.indented'));
      }
    } else if($(this).get(0).allow_children) { // no children, but can have children
      Drupal.ltvCommentReplyStub(cid).insertAfter($(this));
    }

    // hide children
    // if points are less than 0 hide it
    if(parseInt($('#vote_points_' + cid).text().match(/[\-0-9]+/)) < -7) {
      Drupal.ltvCommentHide(cid);
    }
  });

  // auto format according to comment control form
  if($('#edit-children-expanded').val() > 0) {
    $('.comment-wrapper .comment-show-children').each(function(){$(this).click();});
  } else {
    $('.comment-wrapper .comment-hide-children').each(function(){$(this).click();});
  }
}


Drupal.ltvCommentHide = function(cid) {
  $('#comment-wrapper-' + cid).get(0).commentHidden = true;

  // create our placeholder
  var stub = $('<div></div>')
            .attr('id', 'comment-wrapper-' + cid + '-stub')
            .attr('class', 'comment-wrapper-stub')
            // append .comment-reply-count-container
            .append(
              $('<div></div>')
              .attr('class', 'comment-stub-reply-count-container')
              .html($('#comment-wrapper-' + cid + ' .comment-reply-count').text())
            )
            .append(
              $('<div></div>')
              .attr('class', 'comment-stub-body')
	            .append('Hidden due to low community rating. ')
	            .append(
	              $('<a></a>')
	              .attr('href', 'javascript:void(0)')
	              .click(function(e) {
	                Drupal.ltvCommentShow($(this).parent().parent().attr('id').match(/[0-9]+/));
	              })
	              .html('See comment')
	            )
	            // append .timestamp
	            .append(
                $('<div></div>')
                .attr('class', 'comment-stub-timestamp')
                .html($('#comment-wrapper-' + cid + ' .timestamp .comment-byline').text())
              )
	          )
            // append #vote_points_ + cid
            .prepend(
              $('<div></div>')
              .attr('class', 'comment-stub-vote-container')
	            .append(
	              $('<div></div>')
	              .attr('class', 'comment-stub-vote_points')
	              .html($('#comment-wrapper-' + cid + ' #vote_points_' + cid).text())
	            )
	            // append #vote_count_ + cid
	            .append(
	              $('<div></div>')
	              .attr('class', 'comment-stub-vote_count')
	              .html($('#comment-wrapper-' + cid + ' #vote_count_' + cid).text())
	            )
            )
            .append('<br class="clear" />')
	          ;

  // hide the children
  Drupal.ltvCommentHideChildren(cid)
  // hide comment and create placeholder
  $('#comment-wrapper-' + cid).hide('slow').before(stub);
}
Drupal.ltvCommentShow = function(cid) {
  $('#comment-wrapper-' + cid).get(0).commentHidden = false;

  $('#comment-wrapper-' + cid + '-stub').hide('slow',
    function() {
      $('#comment-wrapper-' + cid).show('slow');
    }
  );
}

Drupal.ltvCommentHideChildren = function(cid) {
  if($('#comment-wrapper-' + cid).next('.indented').length == 0) return;

  $('#comment-wrapper-' + cid).next('.indented').hide('slow');

  $('#comment-wrapper-' + cid + ' .comment-show-children').show();
  $('#comment-wrapper-' + cid + ' .comment-hide-children').hide();

  if($('#comment-wrapper-' + cid).get(0).allow_children) {
    Drupal.ltvCommentReplyStub(cid).hide('slow');
  }
}
Drupal.ltvCommentShowChildren = function(cid) {
  if($('#comment-wrapper-' + cid).next('.indented').length == 0) return; // no children
  if($('#comment-wrapper-' + cid).get(0).commentHidden) return; // don't show children of hidden comments

  $('#comment-wrapper-' + cid).next('.indented').show('slow');

  $('#comment-wrapper-' + cid + ' .comment-show-children').hide();
  $('#comment-wrapper-' + cid + ' .comment-hide-children').show();

  if($('#comment-wrapper-' + cid).get(0).allow_children) {
    Drupal.ltvCommentReplyStub(cid).show('slow');
  }
}

Drupal.ltvCommentShowReply = function(cid) {
  var stub = Drupal.ltvCommentReplyStub(cid).show('slow');

  // I would prefer this not be an absolute path; however, I don't feel like expending the brain cells to figure out how to do that.
  stub.css('background', '#FAF2DD url(/sites/all/modules/ltv_comment/comment-children-arrow-up.png) no-repeat scroll 5px 0');

  Drupal.ltvCommentShowChildren(cid);

  if($('#comment-form', stub).length == 0) { // we don't already have the form, so let's get it
	  var url = $('#comment-wrapper-' + cid + ' .comment-reply a').attr('href').split('/');
	  url[1] = 'ltv_comment';
	  url.push('ajax');

	  $.ajax({
	    url: url.join('/'),
	    type: 'GET',
	    dataType: 'html',
	    success: function(data, textStatus) {
	      var form = $(data);

			  $('.reply-preview', form).hide();
			  $('.comment-byline', form).hide();

        // CANCEL button
        $('#edit-cancel', form)
          .click(function() {
            Drupal.ltvCommentCancelReply(cid);
            return false;
          })
          .insertBefore($('#edit-preview', form));
        // PREVIEW button
        $('#edit-preview', form).click(function() {
          Drupal.ltvCommentPreviewReply(cid);
          return false;
        })
        .removeClass('form-preview-button');
        // EDIT button
	      $('#edit-continue-edit', form)
	        .hide()
          .click(function() {
            Drupal.ltvCommentEditReply(cid);
            return false;
          })
          .insertBefore($('#edit-preview', form));
        // SUBMIT button
        //$('#edit-submit', form).attr('type', 'hidden'); //IE doesn't like this
        $('#edit-submit', form).remove();
        $(form).append($('<input id="edit-submit" type="hidden" name="op" value="Post comment" />'));
        $('.comment_buttons', form).append($('#edit-comment-submit', form));

	      form.hide();

        $('a.comment-reply-link', stub).after(form);

	      form.show('slow');
	    }
	  });
  } else {// form already exists
    // make sure the form is visible
    $('#comment-form', stub).show('slow');
  }
}

Drupal.ltvCommentPreviewReply = function(cid) {
  var stub = Drupal.ltvCommentReplyStub(cid);

  $('.reply-preview', stub)
    .text($('textarea', stub).val())
    .show('slow');
  $('.comment-byline', stub).show('slow');
	$('textarea', stub).hide('slow');
	$('#edit-preview', stub).hide();
	$('#edit-continue-edit', stub).show();
}

Drupal.ltvCommentEditReply = function(cid) {
  var stub = Drupal.ltvCommentReplyStub(cid);

  $('.reply-preview', stub).hide('slow');
  $('.comment-byline', stub).hide('slow');
  $('textarea', stub).show('slow');
  $('#edit-preview', stub).show();
  $('#edit-continue-edit', stub).hide();
}

Drupal.ltvCommentCancelReply = function(cid) {
  var stub = Drupal.ltvCommentReplyStub(cid);

  // I would prefer this not be an absolute path; however, I don't feel like expending the brain cells to figure out how to do that.
  stub.css('background', '#FAF2DD url(/sites/all/modules/ltv_comment/comment-children-arrow.png) no-repeat scroll 5px 0');

  var block_to_hide = stub.prev('.indented').length > 0? $('#comment-form', stub): stub;

  block_to_hide.hide('slow');
}

Drupal.ltvCommentReplyStub = function(cid) {
  if($('#comment-wrapper-' + cid + ' .comment-reply a').length == 0) return $(); // cannot have reply
  if($('#comment-wrapper-' + cid + '-reply').length > 0) return $('#comment-wrapper-' + cid + '-reply');
  
  return $('<div></div>')
        .attr('id', 'comment-wrapper-' + cid + '-reply')
        .attr('class', 'comment-wrapper comment-reply-stub')
        .append(
          $('<a></a>')
            .attr('href', $('#comment-wrapper-' + cid + ' .comment-reply a').attr('href'))
            .attr('class', 'comment-reply-link')
            .click(function() {
              Drupal.ltvCommentShowReply(cid);
              return false;
            })
            .text('REPLY TO THIS THREAD')
        )
        .append('<div class="clear"></div>')
        .hide();
}

// Global killswitch
if (Drupal.jsEnabled) {
	$(document).ready(function(){
	  $(".comments-wrapper .pagination a").each(function() {
	    var thisURL = $(this).attr("href");
	    $(this).attr("href",thisURL+"#comments");
	  });
	});
  $(document).ready(Drupal.ltvCommentAutoAttach);
}