// Created and set free in July of 2007 by Jenna ‘Blueberry’ Fox <blueberry@creativepony.com>
// Released as public domain, with the request that credit is given for my contribution
// Also, I'd love to here where you've used or changed this code to do other interesting things!
// Requires MooTools 1.2

var AutoAccordion = new Class({
  Extends: Accordion,
  Implements: Events,
  
  initialize: function(handles, drawers, options) {
    this.addEvent('active', function(handle) { handle.addClass('selected'); });
    this.addEvent('background', function(handle) { handle.removeClass('selected'); });
    
    // run parent initializer
    Accordion.prototype.initialize.apply(this, arguments);
    
    // make a binding in this scope we can get at later
    var binding = this;
    
    // this next part adds the automatic opening magic to each “Handle”
    this.togglers.each(function(handle, index, array) {
      // and the magic hover opening dealie!
      var timer;
      handle.addEvents({
        'mouseenter': function() {
          $clear(timer);
          timer = binding.display.delay(300, binding, index);
        },
        'mouseleave': function() {
          $clear(timer);
        },
        'focus': binding.display.pass(index, binding), // supports tab based keyboard navigation
        'click': function(event) {
          event.stop();
          binding.display(index);
        }
      });
    });
  }
});

window.addEvent('domready', function(){
var accordion = new AutoAccordion('h3.atStart', 'div.atStart', {
 show: 0,
  opacity: false,
  onActive: function(toggler, element){
    toggler.setStyle('color', '#ff3300');
  },
 
  onBackground: function(toggler, element){
    toggler.setStyle('color', '#222');
  }
}, $('accordion'));

});