/**
 * @class   Expandables
 *    Automatically create accessible, expandable headings that fail
 *    gracefully if JavaScript is disabled.
 */
var Expandables = {
  headClassName: 'expandHead',  // The class name given to all expandable links
  
  
  /**
   * @class     Expandables
   * @function  getIdFromHREF
   *    Returns any text after the '#' in a URL.
   * 
   * @param     string  required  href
   *    The URL to extract the Id from.
   * @return    string
   *    The extracted Id. Empty string otherwise.
   */
  getIdFromHREF: function(href) {
    return href.substring(href.indexOf('#')+1, href.length);
  },
  
  
  /**
   * @class     Expandables
   * @function  inArray
   *    Tests to see if the given value is in the given array.
   * 
   * @param     variable  required  needle
   *    The value to test for.
   * @param     variable  required  haystack
   *    The arrays of values to test.
   * @return    boolean
   *    True if the needle is found, false otherwise.
   */
  inArray: function(needle, haystack) {
    for (var i=0, end=haystack.length; i<end; i++) {
      if (haystack[i] == needle) {
        return true;
      }
    }
    return false;
  },
  
  
  /**
   * @class     Expandables
   * @function  initialize
   *    Attaches the proper DOM events and prepares the DOM for the
   *    expandable links.
   * 
   * @param     string  required  exceptions
   *    An array of Ids for tags that should not be hidden by default.
   * @return    void
   */
  initialize: function(exceptions) {
    if (document.getElementsByTagName && document.getElementById) {
      var els = document.getElementsByTagName('a');
      var id = '';
      if (!exceptions) {
        exceptions = [];
      }
      for (var i=0, end=els.length; i<end; i++) {
        if (els[i].className == this.headClassName) {
          els[i].onclick = function() {
            return Expandables.toggle(this);
          };
          id = this.getIdFromHREF(els[i].href);
          if (!this.inArray(id, exceptions)) {
            document.getElementById(id).style.display = 'none';
          }
        }
      }
    }
  },
  
  
  
  /**
   * @class     Expandables
   * @function  toggle
   *    Toggles the expandable.
   * 
   * @param     string  required  el
   *    A node reference to the link element that was clicked.
   * @return    boolean
   *    False is returned to nullify the link click.
   */
  toggle: function(el) {
    var toggleStyle = document.getElementById(this.getIdFromHREF(el.href)).style;
    toggleStyle.display = toggleStyle.display === 'none' ? '' : 'none';
    toggleStyle = null;
    return false;
  }
};