
function togglehelp() {
  if( $('#markdownhelp').is(':visible') ) {
    $("#markdownhelp").slideUp('slow');
    $('#togglehelp').text($("#textshowhelp").attr('value'));
  }
  else {
    $("#markdownhelp").slideDown('slow');
    $('#togglehelp').text($("#texthidehelp").attr('value'));
  }
}

$(document).ready(function() {

  var texts = new Array();
  $('label').each(function() {
    var corresponding = $(this).attr('for');
    texts[corresponding] = $(this).text().slice(0, -1);
    $(this).remove();
  });
  texts['openid'] = 'OpenID (optional)';

  $(':input').each(function() {
    if($(this).val() == "") {
      $(this).val(texts[$(this).attr('id')]);
    }
    if($(this).val() == texts[$(this).attr('id')]) {
      $(this).css('color', '#888');
      $(this).css('font-style', 'italic');
    }
  });

  $(':input').focus(function() {
    if($(this).val() == texts[$(this).attr('id')])
    {
      $(this).val('');
      $(this).css('color', '#000');
      $(this).css('font-style', 'normal');
    }
  });
  $(':input').blur(function() {
    if($(this).val() == '') {
      $(this).val(texts[$(this).attr('id')]);
      $(this).css('color', '#888');
      $(this).css('font-style', 'italic');
    }
    if($(this).val() != texts[$(this).attr('id')]) {
      $(this).removeClass("mistake");
    }
  });
  $('#openid').keyup(function() {
    var val = $(this).val();
    var rel = $('#openidsubmit').attr('rel');
    if(val != '' && val != texts['openid'] && rel == 'hidden') {
      $('#openidsubmit').attr('rel', '').fadeIn(300);
    } else if((val == '' || val == texts['openid']) && (rel == '' || rel == undefined)) {
      $('#openidsubmit').attr('rel', 'hidden').fadeOut(300);
    }
  });
  var val = $('#openid').val();
  if(val == '' || val == texts['openid']) {
    $('#openidsubmit').attr('rel', 'hidden').fadeOut(0);
  }

  var inputheight = $('#openid').height();
  $('input#openidsubmit').val("\u21b5")
    .css('border', 'none')
    .css('-webkit-border-radius', '4px')
    .css('-moz-border-radius', '4px')
    .css('border-radius', '4px')
    .css('background-color', '#ccc')
    .css('text-align', 'center')
    .css('vertical-align', 'middle')
    .css('height', (inputheight-3)+'px')
    .css('width', (inputheight-3)+'px')
    .css('position', 'relative')
    .css('margin-top', '-5px')
    .css('margin-left', '-'+(inputheight+6)+'px')
    .css('margin-right', '6px');

  $('#newcomment').submit(function(event) {
    var nickname = $(this).find('#abcdef').val();
    var text = $(this).find('#ghijkl').val();
    if((text == '') || (text == texts['ghijkl'])) {
      $(this).find('#ghijkl').addClass("mistake").focus();
      event.preventDefault();
    }
    if(($(this).find('#abcdef').size() > 0) && ((nickname == '') || (nickname == texts['abcdef']))) {
      $(this).find('#abcdef').addClass("mistake").focus();
      event.preventDefault();
    }
  });

  $('#markdownhelp').hide();
  $('#commentformatting p:first').html('<a id="togglehelp">'+$("#textshowhelp").attr('value')+'</a>');
  $('#togglehelp').click(function(event) {
      togglehelp();
      event.preventDefault();
    });

});


