Fx.Accordion=new Class({Extends:Fx.Elements,options:{fixedHeight:false,fixedWidth:false,display:0,show:false,height:true,width:false,opacity:true,alwaysHide:false,trigger:"click",initialDisplayFx:true,resetHeight:true},initialize:function(){var defined=function(obj){return obj!=null};var params=Array.link(arguments,{container:Type.isElement,options:Type.isObject,togglers:defined,elements:defined});this.parent(params.elements,params.options);var options=this.options,togglers=this.togglers=$$(params.togglers);this.previous=-1;this.internalChain=new Chain();if(options.alwaysHide){this.options.link="chain"}if(options.show||this.options.show===0){options.display=false;this.previous=options.show}if(options.start){options.display=false;options.show=false}var effects=this.effects={};if(options.opacity){effects.opacity="fullOpacity"}if(options.width){effects.width=options.fixedWidth?"fullWidth":"offsetWidth"}if(options.height){effects.height=options.fixedHeight?"fullHeight":"scrollHeight"}for(var i=0,l=togglers.length;i<l;i++){this.addSection(togglers[i],this.elements[i])}this.elements.each(function(el,i){if(options.show===i){this.fireEvent("active",[togglers[i],el])}else{for(var fx in effects){el.setStyle(fx,0)}}},this);if(options.display||options.display===0||options.initialDisplayFx===false){this.display(options.display,options.initialDisplayFx)}if(options.fixedHeight!==false){options.resetHeight=false}this.addEvent("complete",this.internalChain.callChain.bind(this.internalChain))},addSection:function(toggler,element){toggler=document.id(toggler);element=document.id(element);this.togglers.include(toggler);this.elements.include(element);var togglers=this.togglers,options=this.options,test=togglers.contains(toggler),idx=togglers.indexOf(toggler),displayer=this.display.pass(idx,this);toggler.store("accordion:display",displayer).addEvent(options.trigger,displayer);if(options.height){element.setStyles({"padding-top":0,"border-top":"none","padding-bottom":0,"border-bottom":"none"})}if(options.width){element.setStyles({"padding-left":0,"border-left":"none","padding-right":0,"border-right":"none"})}element.fullOpacity=1;if(options.fixedWidth){element.fullWidth=options.fixedWidth}if(options.fixedHeight){element.fullHeight=options.fixedHeight}element.setStyle("overflow","hidden");if(!test){for(var fx in this.effects){element.setStyle(fx,0)}}return this},removeSection:function(toggler,displayIndex){var togglers=this.togglers,idx=togglers.indexOf(toggler),element=this.elements[idx];var remover=function(){togglers.erase(toggler);this.elements.erase(element);this.detach(toggler)}.bind(this);if(this.now==idx||displayIndex!=null){this.display(displayIndex!=null?displayIndex:(idx-1>=0?idx-1:0)).chain(remover)}else{remover()}return this},detach:function(toggler){var remove=function(toggler){toggler.removeEvent(this.options.trigger,toggler.retrieve("accordion:display"))}.bind(this);if(!toggler){this.togglers.each(remove)}else{remove(toggler)}return this},display:function(index,useFx){if(!this.check(index,useFx)){return this}var obj={},elements=this.elements,options=this.options,effects=this.effects;if(useFx==null){useFx=true}if(typeOf(index)=="element"){index=elements.indexOf(index)}if(index==this.previous&&!options.alwaysHide){return this}if(options.resetHeight){var prev=elements[this.previous];if(prev&&!this.selfHidden){for(var fx in effects){prev.setStyle(fx,prev[effects[fx]])}}}if((this.timer&&options.link=="chain")||(index===this.previous&&!options.alwaysHide)){return this}this.previous=index;this.selfHidden=false;elements.each(function(el,i){obj[i]={};var hide;if(i!=index){hide=true}else{if(options.alwaysHide&&((el.offsetHeight>0&&options.height)||el.offsetWidth>0&&options.width)){hide=true;this.selfHidden=true}}this.fireEvent(hide?"background":"active",[this.togglers[i],el]);for(var fx in effects){obj[i][fx]=hide?0:el[effects[fx]]}if(!useFx&&!hide&&options.resetHeight){obj[i].height="auto"}},this);this.internalChain.clearChain();this.internalChain.chain(function(){if(options.resetHeight&&!this.selfHidden){var el=elements[index];if(el){el.setStyle("height","auto")}}}.bind(this));return useFx?this.start(obj):this.set(obj).internalChain.callChain()}});var Accordion=new Class({Extends:Fx.Accordion,initialize:function(){this.parent.apply(this,arguments);var params=Array.link(arguments,{container:Type.isElement});this.container=params.container},addSection:function(toggler,element,pos){toggler=document.id(toggler);element=document.id(element);var test=this.togglers.contains(toggler);var len=this.togglers.length;if(len&&(!test||pos)){pos=pos!=null?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)}}return this.parent.apply(this,arguments)}});