// Begin processing jQuery commands after the page loads
$(function() {
  showAccordion();  // Show the accordion sections
  simpleFill();     // Fill in any default values in forms
  flashHide();      // Hide the flash message when appropriate
  
  $("input.focus:last").focus();  // Set the focus on the last input tag with a class of "focus"
})

function showAccordion(){
  $("#accordion").accordion({
    clearStyle: false,
    active: false,
    collapsible: true
  });
}

function simpleFill() {
  // Add the default text when the field is blank
  $(".defaultText").focus(function(srcc) {
    if ($(this).val() == $(this)[0].title || $(this).val() == "") {
      $(this).removeClass("defaultTextActive");
      $(this).val("");
    }
  });

  // Remove the default text when the user enters the field
  $(".defaultText").blur(function() {
    if ($(this).val() == "") {
      $(this).addClass("defaultTextActive");
      $(this).val($(this)[0].title);
    }
  });

  // Remove the default text when the form is submitted
  $("form").submit(function() {
    $(this).find(".defaultText").each(function() {
      if($(this).val() == $(this)[0].title) {
        $(this).val("");
      }
    });
  });

  $(".defaultText").blur();
}

function flashHide() {
  // Only hide the ui-state-highlight flash messages.  Error messages should
  // stay on the screen indefinately.
  setTimeout( function() {
    $("#flash.ui-state-highlight").hide("slide", {
      direction: "up"
    }, 2000)
  }, 5000)
}
