$(document).ready(function() {

  // If we're on a login page, focus and select the username box
  $username = $('input#username');
  if($username.length)
    $username[0].focus().select();

  // If we have a fragment identifier, do the yellow fade effect
  if(window.location.hash.length>1)
    $(window.location.hash).vkfade('#FFFFC7');

  // Hide the go button and make the select element behave like a menu
  $('#ideas-nav input').hide();
  $('#ideas-nav select').change(function() { this.form.submit(); });

  // Hide the label and expand the comment box for each song when it gets focus
  $('#ideas-songs ol form').addClass('ideas-self-describing-labels');
  $('#ideas-songs ol form textarea')
    .height('1.8em')
    .focus(function(){
      if(!this.value.length) {
        $(this.form).children('h4').hide();
        $(this).height('5em')[0].focus();
      }
    })
    .blur(function(){
      if(!this.value.length) {
        $(this).height('1.8em');
        $(this.form).children('h4').show();
      }
    });
  // Send focus to the comment box when we click the label
  // (for the benefit of old versions of Safari) and when
  // the form is submitted with no comment
  $('#ideas-songs ol form label').click(function(){
    $textarea = $('#'+$(this).attr('for'));
    if($textarea.length && !$textarea.val().length)
      $($textarea[0]).focus();
  });
  $('#ideas-songs ol form').submit(function(){
    $textarea = $(this).find('textarea');
    if($textarea.length && !$textarea.val().length) {
      $($textarea[0]).focus();
      return false;
    }
  });

  // Send focus to the name box when the 'add song'
  // form is submitted with no name
  $('#ideas-songs>li>form').submit(function(){
    $name = $(this).find('#name');
    if($name.length && !$name.val().length) {
      $name[0].focus();
      return false;
    }
  });
});
