$(document).ready(function(){
  //hide the "preview" button
  $('#comment-preview').hide();
  //insert the live comment preview before the form
  $('#comment_form').before('<div id="comment-live-preview"><h3>Comment Preview:</h3><p><span id="liveNamePreview">You</span> said:</p><p id="liveCommentPreview"></p></div>');
  
  //comment author
  $('#comment-author').one('blur',function() {
    $('#liveNamePreview').replaceWith('<span id="live-name-preview"></span>');
  });
  
  var $myName = '';
  
  $('#comment-author').blur(function() {
    $myName = $(this).val();
    $('#live-name-preview').html( $myName );
  });
  
  //URL
  var $url = '';
  
  $('#comment-url').one('blur',function() {
    $('#live-name-preview').replaceWith('<a id="live-url-preview" href=""></a>')
    $url = $(this).val();
    $('#live-url-preview').attr({href : $url});
    $('#live-url-preview').html( $myName );
  });
  
  $('#comment-text').one('focus',function() {
    $('#liveCommentPreview').replaceWith('<p id="live-comment-preview"></p>');
  });
  
  //comments
  var $comment = '';
  
  $('#comment-text').keyup(function() {
    $comment = $(this).val();
    $comment = $comment.replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />').replace(/(<\/?)script/g,"$1noscript");
    $comment = $comment.replace("<i>", "").replace("</i>","").replace("<b>", "").replace("</b>","").replace("<u>", "").replace("</u>","").replace("<img", "img");
    $('#live-comment-preview').html( $comment );
  });
});