/**
 * Collapse forms version 1
 * Written by Rohan Mitchell, May 2008
 * Licence: LGPL
 */


// Hide the collapsed section and show the form
function show_collapse_form(form) {
  // Hide collapsed section
  form.find(".collapsed").hide();

  // Show form
  if(form.hasClass('slow')) {
    form.find("form").show('slow');
  } else {
    form.find("form").show();
  }

  // If present, clear the value of the specified element(s)
  var clearTo = '';
  if(form.find(".clear-to").length &gt; 0) {
    clearTo = form.find(".clear-to").html();
  }
  form.find("form .clear").val(clearTo);

  // If present, focus on the specified element
  form.find("form .focus").focus();
}


// Hide the form and show the collapsed section
function hide_collapse_form(form) {
  // Hide form
  if(form.hasClass('slow')) {
    form.find("form").hide('slow');
  } else {
    form.find("form").hide();
  }

  // Show collapsed section
  form.find(".collapsed").show();
}


// Wire collapse form events for the specified container
function init_collapse_forms(container) {
  if(container.hasClass("collapse-form")) {
    containers = container;
  } else {
    containers = container.find(".collapse-form");
  }

  containers.find("a.expand").click(function() {
      show_collapse_form($(this).parents(".collapse-form"));
      return false;
    });

  containers.find(".cancel a").click(function() {
      hide_collapse_form($(this).parents(".collapse-form"));
      return false;
    });
}


/* Example usage
 *
$(document).ready(function() {
    // Wire up collapse forms
    init_collapse_forms($(document));
});
*/
