    /**
    * Builds an accordion-style widget.
    * @author Adam J. McIntyre
    */

YAHOO.namespace('widget.Accordion');YAHOO.widget.Accordion=function(el,ajaxUrl,templateFunction,animationSettings,silent){this.el=YAHOO.util.Dom.get(el);this.ajaxUrl=ajaxUrl||null;this.conn=null;this.animationSettings=animationSettings||{speed:.850,frames:true};this.silent=silent||false;this.currentBladeIndex=0;this.bladeEls=[];this.onOpenCompleteEvent=new YAHOO.util.CustomEvent('onOpenComplete',this);this.onCloseCompleteEvent=new YAHOO.util.CustomEvent('onCloseComplete',this);this.onBeforeDataLoadEvent=new YAHOO.util.CustomEvent('onBeforeDataLoad',this);this.onDataLoadedEvent=new YAHOO.util.CustomEvent('onDataLoaded',this);this.onAfterDataLoadEvent=new YAHOO.util.CustomEvent('onAfterDataLoad',this);this.onTemplateCompleteEvent=new YAHOO.util.CustomEvent('onTemplateComplete',this);this.onAfterShowAllEvent=new YAHOO.util.CustomEvent('onShowAll',this);this.onAfterHideAllEvent=new YAHOO.util.CustomEvent('onHideAll',this);if(templateFunction){this.onDataLoaded(function(e,args,o){templateFunction.f(e,args,o);this.onTemplateCompleteEvent.fire(args,this);},templateFunction.o);}
this._bindClick();};var proto=YAHOO.widget.Accordion.prototype;proto.CLASSNAME='accordion';proto._bindClick=function(){var that=this;this.bladeEls=YAHOO.util.Selector.query('#'+this.el.id+' li.yui-accordion > div.hd');if(!this.silent){YAHOO.util.Event.addListener(this.bladeEls,'click',function(){var el=this;if(YAHOO.util.Dom.hasClass(el.parentNode,'open')){that.closeBlade(el);}
else{that.openBlade(el);}});}}
proto.openBlade=function(el){var that=this;var nextEl=YAHOO.util.Dom.getNextSibling(el);this.onBeforeDataLoadEvent.fire(this,el,nextEl);if(YAHOO.util.Dom.hasClass(el.parentNode,'remote')&&!YAHOO.util.Dom.hasClass(el.parentNode,'loaded')){var sUrl=this.ajaxUrl||el.parentNode.getAttribute('data-url');var callback={cache:false,success:function(o){var cb=function(e,args){var targetEl=YAHOO.util.Dom.getFirstChild(args[0][0]);var bdEl=args[0][0];that._openBladeAnim(bdEl,function(){YAHOO.util.Dom.replaceClass(el.parentNode,'closed','open');YAHOO.util.Dom.addClass(el.parentNode,'loaded');YAHOO.util.Dom.setStyle(bdEl,'filter','');that.onAfterDataLoadEvent.fire(this,el,args[0][0]);},el.parentNode.getAttribute('data-speed'));that.removeOnTemplateComplete(cb);}
var f=function(e,args){cb();}
var arg={el:nextEl};that.onTemplateComplete(cb);that.onDataLoadedEvent.fire(nextEl,o);},failure:function(o){alert("We encountered an error requesting this data. Please try again.");},error:function(o){alert("We encountered an error requesting this data. Please try again.");}};this.conn=YAHOO.util.Connect.asyncRequest('GET',sUrl+'?'+
el.parentNode.getAttribute('data-params'),callback);}
else{that._openBladeAnim(nextEl,function(){YAHOO.util.Dom.replaceClass(el.parentNode,'closed','open');YAHOO.util.Dom.addClass(el.parentNode,'loaded');YAHOO.util.Dom.setStyle(nextEl,'filter','');that.onAfterDataLoadEvent.fire(this,el,nextEl);},el.parentNode.getAttribute('data-speed'));}}
proto._openBladeAnim=function(el,callback,sp){if(sp){var speed=sp;}
else{var speed=this.animationSettings.speed=='adaptive'?(YAHOO.util.Dom.getFirstChild(el).offsetHeight/1000)*.75:this.animationSettings.speed;}
var anim=new YAHOO.util.Anim(el,{height:{to:YAHOO.util.Dom.getFirstChild(el).offsetHeight},opacity:{from:0,to:0.99}},speed,YAHOO.util.Easing.easeIn);anim.setAttribute('useSeconds',this.animationSettings.frames);anim.onComplete.subscribe(callback);anim.animate();this.onOpenCompleteEvent.fire(el);}
proto.closeBlade=function(el){var nextEl=YAHOO.util.Dom.getNextSibling(el);YAHOO.util.Dom.replaceClass(el.parentNode,'open','closed');var anim=new YAHOO.util.Anim(nextEl,{height:{to:0},opacity:{to:0}},0.25,YAHOO.util.Easing.easeOut);anim.animate();this.onCloseCompleteEvent.fire(this,el);}
proto.showAll=function(){if(this.currentBladeIndex<this.bladeEls.length){var el=this.bladeEls[this.currentBladeIndex];var that=this;if(this.currentBladeIndex==0){this.onAfterDataLoad(this._showAllCallback);}
this.openBlade(el);}
else{this.currentBladeIndex=0;this.removeOnAfterDataLoad(this._showAllCallback);this.onAfterShowAllEvent.fire(this);return false;}}
proto._showAllCallback=function(){this.currentBladeIndex++;this.showAll();}
proto.hideAll=function(){var els=YAHOO.util.Selector.query('#'+this.el.id+' li > div.hd');for(var i=0;i<els.length;i++){this.closeBlade(els[i]);}
this.onAfterHideAllEvent.fire(this);}
proto.onOpenComplete=function(callback,obj){this.onOpenCompleteEvent.subscribe(callback,obj);}
proto.onCloseComplete=function(callback,obj){this.onCloseCompleteEvent.subscribe(callback,obj);}
proto.onBeforeDataLoad=function(callback,obj){this.onBeforeDataLoadEvent.subscribe(callback,obj);}
proto.onDataLoaded=function(callback,obj){this.onDataLoadedEvent.subscribe(callback,obj);}
proto.removeOnDataLoaded=function(callback,obj){this.onDataLoadedEvent.unsubscribe(callback,obj);};proto.onAfterDataLoad=function(callback,obj){this.onAfterDataLoadEvent.subscribe(callback,obj);}
proto.removeOnAfterDataLoad=function(callback,obj){this.onAfterDataLoadEvent.unsubscribe(callback,obj);}
proto.onTemplateComplete=function(callback,obj){this.onTemplateCompleteEvent.subscribe(callback,obj);}
proto.removeOnTemplateComplete=function(callback,obj){this.onTemplateCompleteEvent.unsubscribe(callback,obj);}
proto.onAfterShowAll=function(callback,obj){this.onAfterShowAllEvent.subscribe(callback,obj);}
proto.onAfterHideAll=function(callback,obj){this.onAfterHideAllEvent.subscribe(callback,obj);}