/*
Script: Accordion.js
  An Fx.Elements extension which allows you to easily create accordion type controls.
 
License:
  MIT-style license.
*/
 
var Accordion = new Class({
 
  Extends: Fx.Elements,
 
  options: {/*
    onActive: $empty,
    onBackground: $empty,*/
    display: 0,
    show: false,
    height: true,
    width: false,
    opacity: true,
    fixedHeight: false,
    fixedWidth: false,
    wait: false,
    alwaysHide: false
  },
 
  initialize: function(){
    var params = Array.link(arguments, {'container': Element.type, 'options': Object.type, 'togglers': $defined, 'elements': $defined});
    this.parent(params.elements, params.options);
    this.togglers = $$(params.togglers);
    this.container = $(params.container);
    this.previous = -1;
    if (this.options.alwaysHide) this.options.wait = true;
    if ($chk(this.options.show)){
      this.options.display = false;
      this.previous = this.options.show;
    }
    if (this.options.start){
      this.options.display = false;
      this.options.show = false;
    }
    this.effects = {};
    if (this.options.opacity) this.effects.opacity = 'fullOpacity';
    if (this.options.width) this.effects.width = this.options.fixedWidth ? 'fullWidth' : 'offsetWidth';
    if (this.options.height) this.effects.height = this.options.fixedHeight ? 'fullHeight' : 'scrollHeight';
    for (var i = 0, l = this.togglers.length; i < l; i++) {
	this.togglers[i].href = "javascript:;";
	this.addSection(this.togglers[i], this.elements[i]);
	}
    this.elements.each(function(el, i){
      if (this.options.show === i){
        this.fireEvent('active', [this.togglers[i], el]);
      } else {
        for (var fx in this.effects) el.setStyle(fx, 0);
      }
    }, this);
    if ($chk(this.options.display)) this.display(this.options.display);
  },
 
  addSection: function(toggler, element, pos){
    toggler = $(toggler);
    element = $(element);
    var test = this.togglers.contains(toggler);
    var len = this.togglers.length;
    this.togglers.include(toggler);
    this.elements.include(element);
    if (len && (!test || pos)){
      pos = $pick(pos, len - 1);
      toggler.inject(this.togglers[pos], 'before');
      element.inject(toggler, 'after');
    } else if (this.container && !test){
      toggler.inject(this.container);
      element.inject(this.container);
    }
    var idx = this.togglers.indexOf(toggler);
    toggler.addEvent('click', this.display.bind(this, idx));
    if (this.options.height) element.setStyles({'padding-top': 0, 'border-top': 'none', 'padding-bottom': 0, 'border-bottom': 'none'});
    if (this.options.width) element.setStyles({'padding-left': 0, 'border-left': 'none', 'padding-right': 0, 'border-right': 'none'});
    element.fullOpacity = 1;
    if (this.options.fixedWidth) element.fullWidth = this.options.fixedWidth;
    if (this.options.fixedHeight) element.fullHeight = this.options.fixedHeight;
    element.setStyle('overflow', 'hidden');
    if (!test){
      for (var fx in this.effects) element.setStyle(fx, 0);
    }
    return this;
  },
 
  display: function(index){
    index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
    if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
    this.previous = index;
    var obj = {};
    this.elements.each(function(el, i){
      obj[i] = {};
      var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
      this.fireEvent(hide ? 'background' : 'active', [this.togglers[i], el]);
      for (var fx in this.effects) obj[i][fx] = hide ? 0 : el[this.effects[fx]];
    }, this);
    return this.start(obj);
  }
 
});

