PEGNEWS.load_queue.push(function () {
  
  // A weak attempt to keep people from pressing preview on an empty comment.
  $("input.submit-preview").attr("disabled", true);
  
  function prevent_empty_comments() {
    if ($("#id_comment").attr("value") !== "") {
      $("input.submit-preview").attr("disabled", false);
    } else {
      $("input.submit-preview").attr("disabled", true);
    }
  }
  
  $("#id_comment").keyup(prevent_empty_comments);
  prevent_empty_comments();

  function create_modal_dialog(title) {
    if ($("#modal_dialog").length === 0) {
      $("body").append('<div id="modal_dialog"></div>');
    }
    //This function creates the dialog, but does not pop it up
    $("#modal_dialog").dialog({
      autoOpen: false,
      bgiframe: true,
      draggable: false,
      modal: true,
      position: 'top',
      resizable: false,
      title: title,
      width: 550,
      close: function(event, ui) {
        new_comment = $('#comment-preview-form textarea').val();
        $('#comment-form textarea').val(new_comment);
        
        // Check and see if the Facebook button is checked.
        if ($('#id_facebook').attr('checked')) {
          $('#id_facebook2').attr('checked', true );
        } else {
           $('#id_facebook2').attr('checked', false );
        }
        
        $('#modal_dialog').dialog('destroy');     
        $('#modal_dialog').html('');
      },
      open: function(event, ui) {
        $('#modal_dialog').append('<img src="' + PEGNEWS.media_url +
          'img/site/wait26trans.gif">');
      }
    });
  }

  function popup_dialog() {
    // Safe to call more than once
    if (!$("#modal_dialog").dialog('isOpen')) {
      $("#modal_dialog").dialog('open');
    }
  }
  
  if (user_can_moderate_comments) {
    // Delete comments
    $('a.delete-comment').click(function(e){
      e.preventDefault();
      loadAddress = $(this).attr('href');
      create_modal_dialog('Delete Comment');
      $("#modal_dialog").load(loadAddress);
      popup_dialog();
    });
  } else {
    // Flag comments
    $('a.flag-comment').click(function(e){
      e.preventDefault();
      loadAddress = $(this).attr('href');
      create_modal_dialog('Delete Comment');
      $("#modal_dialog").load(loadAddress);
      popup_dialog();
    });
  }
  
  function set_popup_timeout() {
    setTimeout(popup_dialog, 250);
  }
  
  function handle_request_errors(text_status) {
    if (text_status !== "success") {
      //Handle error
      $("#modal_dialog").html("Sorry, an error occurred. We've been notified.");
      $("#modal_dialog").dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
    }
    popup_dialog();
  }

  function preview_comment() {
    create_modal_dialog('Complete your comment');
    var form_data = $("#comment-form").serializeArray();
    form_data.push({'name': 'preview', 'value': true});
    $("#modal_dialog").load(comments_post_comment_ajax, form_data, comment_previewed);
    set_popup_timeout();
    return false;
  }
  $("input.submit-preview").click(preview_comment);
  
  function comment_previewed(response_text, text_status, XMLHttpRequest) {
    handle_request_errors(text_status);
    
    $("#comment-preview-form").submit(post_comment);
    $("#revise").click(replace_preview_with_revision);
    $("#preview").click(post_comment);
    
    if ($('#id_facebook2').attr('checked')) {
      $('#id_facebook').attr('checked', true );
    } else {
       $('#id_facebook').attr('checked', false );
    }
  }
  
  function request_confirmation() {
    create_modal_dialog('Email verification');
    var form_data = [{'name': 'dummy_data', 'value': 'for_post'}];
    $("#modal_dialog").load(reg_send_challenge_email, form_data,
                            confirmation_requested);
    set_popup_timeout();
    return false;
  }
  $("input.verify").click(request_confirmation);
  
  function confirmation_requested(response_text, text_status, XMLHttpRequest) {
    handle_request_errors(text_status);
  }
  
  function disable_buttons() {
    $('#preview').attr('disabled', true);
    $('#revise').attr('disabled', true);
    $('#submit').attr('disabled', true);
  }

  function enable_buttons() {
    $('#preview').attr('disabled', false);
    $('#revise').attr('disabled', false);
    $('#submit').attr('disabled', false);
  }

  function replace_preview_with_revision() {
    $("#comment-preview").hide();
    $("#submit_comment").hide();
    $("#comment-revision").show();
    return false;
  }

  function replace_revision_with_preview() {
    new_comment = $('#comment-preview-form textarea').val();
    $("#comment-preview div.comment-inner").html(new_comment);
    $("#comment-preview").show();
    $("#submit_comment").show();
    $("#comment-revision").hide();
    return false;
  }

  function post_comment() {
    var form_data = $("#comment-preview-form").serializeArray();
    if ($(this).attr('id') === 'preview') {
      form_data.push({'name': 'preview', 'value': true});
    }
    $("#ajax-comment-widget").load(comments_post_comment_ajax, form_data,
                                   comment_posted);
    disable_buttons();
    return false;
  }
  
  function comment_posted(response_text, text_status, XMLHttpRequest) {
    handle_request_errors(text_status);
    
    $("#comment-preview-form").submit(post_comment);
    $("#revise").click(replace_preview_with_revision);
    $("#preview").click(post_comment);
  }
  
  var hash = self.document.location.hash;
  if (hash){
      var comment = $(hash).parent();
      if (comment.hasClass("comment")){
          comment.css("background-color", "#fffee0");
      }
  }

});
