$(function(){
  initCollapse($("h4.collapsible"), "#checklist", "slow");
});

// Initialise collapse function on all elements matching start
// Hides all following sibling elements up to the 1st element matching end.
// If end is not provided, matches all subsequent siblings.
// If speed is not provided, defaults to "normal"
function initCollapse(start, end, speed){
  if (!speed) {
      speed = "normal";
  }

  start.addClass("collapse");

  // Hide siblings
  start.each(function() { // Iterate elements matching start
      $(this).nextAll().each(function() { // Iterate siblings
          if ($(this).is(end)) {
              return false; // stop iterating siblings
          }
          $(this).hide();
      });
  });

  // Bind click event to toggle
  start.click(function() {
      $(this).nextAll().each(function() { // Iterate siblings
          if ($(this).is(end)) {
              return false; // stop iterating siblings
          }
          $(this).toggle(speed);
      });
  });
}
