
$(document).ready(function() {

  $('#commentsubmit').val('preview / submit');

  $('#newcomment').submit(function(event) {
    if($('#comment_preview').length == 0) {
      if($('#ghijkl').val() != '') {
        var req = window.location.href;
        var base_url = req.substr(0, req.lastIndexOf('/'));
        var authenticated = !$('#openid[type=text]').length;
        $.post(base_url+'/ajax/comment', { comment: $('#ghijkl').val(), authenticated: authenticated }, function(data) {
          $('#commentsubmit').val('submit!');
          $('#comment_preview').remove();
          var width = $('#ghijkl').width();
          var height = $('#ghijkl').height();
          $('#ghijkl').css('display', 'none');
          $('#ghijkl').after('<div id="comment_preview"></div>');
          if(!authenticated && data.indexOf('<!--') != -1) {
            $('#commentformatting').after('<div id="comment_alert">Note: Since you\'re not authenticated via OpenID, all links have been removed from your comment.</div>');
          }
          $('#comment_preview').width(width).height(height);
          $('#comment_preview').html(data);
          $('#commentsubmit').after('<input type="button" id="comment_edit" value="edit..." />');
          $('#comment_edit').click(function() {
            $('#commentsubmit').val('preview / submit');
            $('#comment_preview').remove();
            $('#comment_alert').remove();
            $('#ghijkl').css('display', '');
            $(this).remove();
          });
        });
      }
      event.preventDefault();
    }
  });

});


