/**
 * Enables toggle of content
 * 
 * @author Mathias Brodala <mathias.brodala@dtele.de>
 */
(function($) {

  $(function() {
  
    $(".toggle").each(function() {
    
      var $element = $(this);
      var $header = $("> :header:first", $element);
      var $button = $("<button/>", {type: "button", "class": "toggle off", text: "+"});
      
      $button.click(function() {
      
        $button.text($button.text() == "+" ? "-" : "+");
        $button.toggleClass("off on");
        $header.siblings().slideToggle();
      });
      
      // If there is no header, insert fake
      // container to maintain hierarchy
      if ($header.length < 1) {
      
        $header = $("<span/>").prependTo($element);
      }
      
      // Append toggle button to header
      $header.prepend($button);
      
      // Hide content by default
      $header.siblings().hide();
    });
  });
})(jQuery);
