(function(jQuery) {
  "use strict";

  var $ = jQuery;

  if (window.top !== window) {
    // Don't run in iframes
    return;
  }

  // When the Markdown has changed, generate
  // new HTML.
  var updateHtmlFromMd = function(event) {
    var mdComment = event.data.mdComment;
    var htmlComment = event.data.htmlComment;

    var mdText = mdComment.val();
    var htmlText = md4mefi.md2html(mdText);
    htmlComment.val(htmlText);

    // The keyup event is the listener for 
    // refreshing the live preview.
    htmlComment.trigger('keyup');    
  };

  // Turn on the task that updates the live preview
  var turnOnLivePreview = function(event) {
    // Turn on live preview, by running the init_lp function.
    // That function is assigned to the onfocus handler.
    //
    // However, if we run jQuery's .trigger('focus')
    // function, we'll actually switch focus to the
    // htmlComment textarea. Therefore, run
    // the onfocus handler that's set through plain ol'
    // DOM manipulation.

    var htmlCommentDomNode = event.data.htmlComment.get(0);
    htmlCommentDomNode.onfocus();    
  };

  var turnOffLivePreview = function(event) {
    // Turn off live preview, by running the kill_lp function,
    // which is the onBlur handler.

    var htmlCommentDomNode = event.data.htmlComment.get(0);
    htmlCommentDomNode.onblur();          
  };

  var showHideHtml = function(event) {
    var htmlComment = event.data.htmlComment;
    var showHtml = event.data.showHtml;

    var isShow = showHtml.prop('checked');

    var animationOptions = {
      duration: 200,
      //easing: 'easeOut',
    };

    if (isShow) {
      htmlComment.show(animationOptions);
    }
    else {
      htmlComment.hide(animationOptions);
    }
  };


  $(document).ready(function() {
    // Todo: Abort if the body doesn't have a class of .thread
    var commentTd = $('body.thread #commenttd');
    var htmlComment = commentTd.find('#comment');
    var mdComment = $('<textarea>')
      .attr('rows', 8)
      .attr('cols', 60)
      .css('background-color', '#f3f0f0');

    var showHtmlLabel = $('<label><input type=checkbox> Show HTML</label>')
      .prop('checked', false);
    var showHtml = showHtmlLabel.find('input');

    commentTd.prepend(showHtmlLabel);
    commentTd.prepend(mdComment);
    htmlComment.hide();


    // Hide the formatting toolbar (B, I, link)
    var toolbar = $('#toolbar');
    var toolbarParentTr = toolbar.closest('tr');
    toolbarParentTr.hide();

    var eventData = {
      htmlComment: htmlComment,
      mdComment: mdComment,
      showHtml: showHtml,
    };
    mdComment.on('input', eventData, updateHtmlFromMd);
    mdComment.on('focus', eventData, turnOnLivePreview);
    mdComment.on('blur',  eventData, turnOffLivePreview);

    showHtml.on('change', eventData, showHideHtml);
  });

// noConflict(true) will revert both $ and jQuery, making
// it possible to use this plugin in combination w/ other
// versions of jQuery. Maybe this isn't necessary b/c
// Safari protects plugins from conflicts w/ the page's
// Javascript, but it makes me feel safer.
//
// See: http://stackoverflow.com/a/7882550/939467
})(jQuery.noConflict(true));
@@ -125,16 +125,427 @@
 n;%0A  %7D%0A%0A
+%0A  // Replace the HTML Help link with a Markdown Help link.%0A  var replaceHelpLink = function() %7B%0A    var anchor = $('#commentform a%5Bhref$=%22//faq.metafilter.com/tags/html%22%5D');%0A    anchor.attr('href', 'http://daringfireball.net/projects/markdown/basics');%0A    anchor.html('(Markdown help)');%0A%0A    // Normal MeFi has target = self. I don't like that; open in a new tab.%0A    anchor.attr('target', '_blank');%0A  %7D;%0A%0A%0A
   // Whe
@@ -2159,17 +2159,16 @@
 %7D%0A  %7D;%0A%0A
-%0A
   $(docu
@@ -2735,16 +2735,40 @@
 de();%0A%0A%0A
+    replaceHelpLink();%0A%0A
     // H

Replace the HTML Help link with a Markdown Help link.