$(document).ready(function(){
  $(".school h3 a").click(function() {
    // Display AJAX loader
    $(this).after("<img class='loading' src='http://bostonpic.org/sites/all/modules/custom/schoolcs/loading.gif'>");
    // check if a school contact form is already open
    if ( $(".school-contact").length > 0 ) {
      // remove existing school contact form
      $(".school-contact").remove();
    }

    // Set get school contact on click event
    var csform = function(data){
      var contact = Drupal.parseJson(data);
      $("#school-"+contact['nid']).append(contact['cs']);
      $("img.loading").remove();
      $("#school-"+contact['nid']+" .school-contact").slideDown("slow", function() {
        $("input#edit-youth-name").focus();
      });

      // attach close event to remove school contact
      $(".school-contact .close a").click(function() {

        // enclose remove in callback to run after fadeout
        $(this).parent().parent().fadeOut("slow", function() {
          $(this).remove();
        });
        return false;
      });
    }
    $.get(this.href, null, csform);
    return false;
  });
});
