//ַٵЧ,ҪFx.Elements
//ʾ:http://demos.mootools.net/Accordion

var Accordion = new Class({


 //̳Fx.Elements

 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(){
  //Array.linkȡ

  var params = Array.link(arguments, {'container': Element.type, 'options': Object.type, 'togglers': $defined, 'elements': $defined});
  //øFx.Elementsͬ

  arguments.callee.parent(params.elements, params.options);
  //ȡtoggleElements(ʱĶ)

  this.togglers = $$(params.togglers);
  //UI

  this.container = $(params.container);
  //󼤻Ķ

  this.previous = -1;
  //

  if (this.options.alwaysHide) this.options.wait = true;
  //ʾֵ

  if ($chk(this.options.show)){
   //display

   this.options.display = false;
   //ָ

   this.previous = this.options.show;

  }
  //ִֹstart

  if (this.options.start){
   //displayֵָ

   this.options.display = false;

   //showֵָ

   this.options.show = false;

  }
  //ṩFx.Eleemntsб仯Լ

  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.addSection(this.togglers[i], this.elements[i]);

  //ҵʾָ
  this.elements.each(function(el, i){
   //ָʾǰ

   if (this.options.show === i){
    //onActive¼

    this.fireEvent('onActive', [this.togglers[i], el]);

   } else {
    //÷ʾCSSΪ0

    for (var fx in this.effects) el.setStyle(fx, 0);

   }

  }, this);
  //displayж0ֵ,

  if ($chk(this.options.display)) this.display(this.options.display);

 },


 //ӽڵ
 //toggerΪ,eleemntΪ,posΪӵλ

 addSection: function(toggler, element, pos){
  //ȡ

  toggler = $(toggler);

  element = $(element);
  //togglersǷѰtoggler

  var test = this.togglers.contains(toggler);
  //ȡϳ

  var len = this.togglers.length;
  //togglertogglers

  this.togglers.include(toggler);

  //elementelements

  this.elements.include(element);
  //ϲΪ,Ҳǰλô0

  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));
  //ָ߶,Ϊ˼,Ӱ߶ֵpaddingboderֵ0

  if (this.options.height) element.setStyles({'padding-top': 0, 'border-top': 'none', 'padding-bottom': 0, 'border-bottom': 'none'});

  //ָ,Ϊ˼,Ӱֵpaddingboderֵ0

  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){
   //CSSΪ0

   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 bj = {};
  //,ȡñ仯Լ,Ϊstartṩ

  this.elements.each(function(el, i){

   obj[i] = {};
   //Ҫص

   var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
   //ʾشͬ¼

   this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
   //ʱÿֵΪ0

   for (var fx in this.effects) obj[i][fx] = hide ? 0 : el[this.effects[fx]];

  }, this);
  //ʼִЧ

  return this.start(obj);

 }

 

});

