//Usage:
//new Accordion('usernavigation', { pane:'div',clickpane:'leftsidebar',bodypane:'panel_body',openpane:'panel1',duration:0.5,effect:'slide' });
Accordion = Class.create();
 
Accordion.prototype = {
      initialize:function(container, options) {
            this.container = $(container);
            this.options = options;
            this.pane = this.options.pane || 'div';
            this.clickpane = this.options.clickpane || '';
            this.bodypane = this.options.bodypane || '';
            this.openpane = this.options.openpane || '';
            this.duration = this.options.duration || 0.5;
            if(this.options.effect && this.options.effect == 'blind')
                  this.effect = 'blind';
            else
                  this.effect = 'slide';
            this.panels = $A(this.container.getElementsByTagName(this.pane));
            if(this.clickpane == '')
                  throw("You must specify the class of the click pane.");
            this.clickpanes = $$('.' + this.clickpane);
            if(this.bodypane == '')
                  throw("You must specify the class of the body pane.");
            this.bodypanes = $$('.' + this.bodypane);
            if(this.openpane == '')
                  this.openpane = $(this.panels[0]);
            else
                  this.openpane = $(this.openpane);
            var accordion = this;
            this.clickpanes.each(function(s) {
                  s.observe('click',accordion.slideAccordion.bindAsEventListener(accordion));
            });
            var effect = this.effect;
            this.bodypanes.each(function(s) {
                  s.id = s.up().id + '-body';
                  if(effect == 'slide') {
                        div = document.createElement('div');
                        div.innerHTML = s.innerHTML;
                        s.innerHTML = '';
                        s.appendChild(div);
                  }
            });
            this.openpane.immediateDescendants()[1].show();
      },
      slideAccordion:function(event) {
            var elem = Event.element(event);
            while(elem.className != this.clickpane) {
                  elem = elem.up();
            }
            elem = elem.next();
            var current;
            var bodypane = this.bodypane;
            this.panels.each(function(s) {
                  if (s.className == bodypane) {
                        if (s.style.display == 'block' || s.style.display == '')
                              current = s;
                  }
            });
            if (elem == current)
                        return;
            else if (elem) {
                        eldown = elem;
                        elup = current;
                        if(this.effect == 'blind')
                              new Effect.Parallel( [ new Effect.BlindUp(elup, { sync:true}), new Effect.BlindDown(eldown, { sync:true }) ], { duration: this.duration });
                        else
                              new Effect.Parallel( [ new Effect.SlideUp(elup, { sync:true}), new Effect.SlideDown(eldown, { sync:true }) ], { duration: this.duration });
            }
      }
};







