$(window).load(function() {
  if (window.location.hash == '#confirmation') {
    $('#confirmation').fadeIn('fast');

    $('#confirmation a').each(function() {
      $(this).click(function(event) {
        $('#confirmation').fadeOut('slow');
      });
    });

  }

  $('ul.navigation li a').each(function() {
    $(this).click(function(event) {
      var target = this.hash;
      var offset = $(target).offset().top;
      event.preventDefault();
      $('html, body').animate({scrollTop: offset}, 300, function() {
          location.hash = target;
        });
    });
  });
});

function submit_registration(event) {
  $(this).find("input[type=submit]").attr("disabled", "disabled").addClass("disabled").blur();

  $.ajax({
    type: "POST",
    url: "/registrations.js",
    data: $(this).serializeArray(),
    dataType: "json",
    success: function(xhr) {
      // ghetto manual error handler because i can't get jquery to extract the responseText
      // when sending some status that's not :ok
      if (xhr.saved) {
        $("#registration_container").append(xhr.body);
        $("#paypal_container form").submit();
      } else {
        $("#registration_container").html(xhr.body);
        $("#registration_container #new_registration").submit(submit_registration);
        $("#registration_container").find("input[type=text]")[0].focus();
      }
    }
  });

  return false;
}

$(document).ready(function() {
  $("#new_registration").submit(submit_registration);
});
