$(document).ready(function(){
  // comment stuff
  var
  $comments, $link, $text, $preview,
  showResponse = function(){},
  $commentform = $('#profile-respond');
  if ( $commentform.length != 0 )
  {
    // show/hide comment form
    $comments = $('#profile-comments');
    $link = $( '<a class="add" href="#profile-respond">' + $commentform.find('label:first').text() +
               '</a>' ).appendTo($comments)
                       .click(showResponse);
    if ( window.location.hash.indexOf('profile-respond') == -1 )
    {
      showResponse = function(e){
        showResponse = function(){};
        e.preventDefault();
        $commentform.slideDown('slow',function(){
          // Safari seems to need a refresh
          if ( $('#fm-submit').height() === 0 )
          {$commentform.html($commentform.html()+'');}
        });
      };
      $commentform.hide('fast',function(){$(this).slideUp('fast');});
      $comments.find('a[href="#profile-respond"]').click(showResponse);
    }
    // preview
    $text = $commentform.find('#post-response');
    $('<fieldset id="add_comment_preview" style="display:none; margin-top: .5em;">' +
      '<h4>Preview</h4>' +
      '<div id="add_comment_preview_text"></div></fieldset>').appendTo($commentform);
    $preview = $('#add_comment_preview_text');
    function updatePreview()
    {
      $preview.html($.textile($text.val()))
              .parents('fieldset:hidden')
              .animate({height:'show',opacity:'show'},'fast');
    }
    if( $text.val().length > 0 )
    {updatePreview();}
    $text.keyup(updatePreview);
  }
});
