/*	Script: multiple.open.accordion.js
		Creates a Mootools <Fx.Accordion> that allows the user to open more than one element.
		
		Dependancies:
			 mootools - 	<Moo.js>, <Function.js>, <Array.js>, <String.js>, <Element.js>, <Fx.Base.js>, <Fx.Elements.js>, <Fx.Styles.js>, <Fx.Style.js>
			
		Author:
			Aaron Newton, <aaron [dot] newton [at] cnet [dot] com>
*/
var MultipleOpenAccordion=Fx.Elements.extend({options:{openAll:true,allowMultipleOpen:true,firstElementsOpen:[0],start:'open-first',fixedHeight:false,fixedWidth:false,alwaysHide:true,wait:false,onActive:Class.empty,onBackground:Class.empty,height:true,opacity:true,width:false},initialize:function(togglers,elements,options){this.parent(elements,options);this.setOptions(options);this.previousClick=null;this.elementsVisible=[];togglers.each(function(tog,i){$(tog).addEvent('click',function(){this.toggleSection(i)}.bind(this))},this);this.togglers=togglers;this.h={};this.w={};this.o={};this.now=[];this.elements.each(function(el,i){el=$(el);this.now[i]={};el.setStyle('overflow','hidden');if(!(this.options.openAll&&this.options.allowMultipleOpen))el.setStyle('height',0)},this);if(!this.options.openAll||!this.options.allowMultipleOpen){switch(this.options.start){case'first-open':this.showSection(this.options.firstElementsOpen[0]);break;case'open-first':this.toggleSection(this.options.firstElementsOpen[0]);break}}if(this.options.openAll&&this.options.allowMultipleOpen)this.showAll();else if(this.options.allowMultipleOpen)this.openSections(this.options.firstElementsOpen)},hideThis:function(i){this.elementsVisible[i]=false;if(this.options.height)this.h={'height':[this.elements[i].offsetHeight,0]};if(this.options.width)this.w={'width':[this.elements[i].offsetWidth,0]};if(this.options.opacity)this.o={'opacity':[this.now[i]['opacity']||1,0]};this.fireEvent("onBackground",[this.togglers[i],this.elements[i]])},showThis:function(i){this.elementsVisible[i]=true;if(this.options.height)this.h={'height':[this.elements[i].offsetHeight,this.options.fixedHeight||this.elements[i].scrollHeight]};if(this.options.width)this.w={'width':[this.elements[i].offsetWidth,this.options.fixedWidth||this.elements[i].scrollWidth]};if(this.options.opacity)this.o={'opacity':[this.now[i]['opacity']||0,1]};this.fireEvent("onActive",[this.togglers[i],this.elements[i]])},toggleSection:function(iToToggle){if(iToToggle!=this.previousClick||this.options.alwaysHide||this.options.allowMultipleOpen){this.previousClick=iToToggle;var objObjs={};var err=false;this.elements.each(function(el,i){var update=false;this.now[i]=this.now[i]||{};if(i==iToToggle){if(this.elementsVisible[i]&&(this.options.allowMultipleOpen||this.options.alwaysHide)){if(!(this.options.wait&&this.timer)){update=true;this.hideThis(i)}else{this.previousClick=null;err=true}}else if(!this.elementsVisible[i]){if(!(this.options.wait&&this.timer)){update=true;this.showThis(i)}else{this.previousClick=null;err=true}}}else if(this.elementsVisible[i]&&!this.options.allowMultipleOpen){if(!(this.options.wait&&this.timer)){update=true;this.hideThis(i)}else{this.previousClick=null;err=true}}if(update)objObjs[i]=$merge(this.h,$merge(this.o,this.w))},this);if(err)return false;return this.custom(objObjs)}return false},showSection:function(i,useFx){if($pick(useFx,false)){if(!this.elementsVisible[i])this.toggleSection(i)}else{this.setSectionStyle(i,$(this.elements[i]).scrollWidth,$(this.elements[i]).scrollHeight,1);this.elementsVisible[i]=true;this.fireEvent("onActive",[this.togglers[i],this.elements[i]])}},hideSection:function(i,useFx){if($pick(useFx,false)){if(this.elementsVisible[i])this.toggleSection(i)}else{this.setSectionStyle(i,0,0,0);this.elementsVisible[i]=false;this.fireEvent("onBackground",[this.togglers[i],this.elements[i]])}},setSectionStyle:function(i,w,h,o){if(this.options.opacity)$(this.elements[i]).setOpacity(o);if(this.options.height)$(this.elements[i]).setStyle('height',h+'px');if(this.options.width)$(this.elements[i]).setStyle('width',w+'px')},showAll:function(){if(this.options.allowMultipleOpen){this.elements.each(function(el,idx){this.showSection(idx,false)},this)}},hideAll:function(){if(this.options.allowMultipleOpen){this.elements.each(function(el,idx){this.hideSection(idx,false)},this)}},openSections:function(sections){if(this.options.allowMultipleOpen){this.elements.each(function(el,idx){if(sections.test(idx))this.showSection(idx,false);else this.hideSection(idx,false)},this)}}});MultipleOpenAccordion.implement(new Options);MultipleOpenAccordion.implement(new Events);