if(typeof Effect=="undefined")
{
	throw ("You must have the script.aculo.us library to use this accordion");
}

var Accordion=Class.create(
	{
		initialize:function(c,a)
		{
			if(!$(c))
			{
				throw ("Attempted to initalize accordion with id: "+c+" which was not found.");
			}
			this.accordion=$(c);
			this.options={
				toggleClass:"accordion-toggle",
				toggleActive:"accordion-toggle-active",
				contentClass:"accordion-content"
			};
			
			this.contents=this.accordion.select("div."+this.options.contentClass);
			this.isAnimating=false;
			this.maxHeight=0;
			this.current=a?this.contents[a-1]:this.contents[0];
			this.current_a = a?a:0;
			this.toExpand=null;

			this.checkMaxHeight();
			this.initialHide();
			this.attachInitialMaxHeight();

			var b=this.clickHandler.bindAsEventListener(this);
			this.accordion.observe("click",b);


		},
		
		expand:function(a)
		{
			this.toExpand=a.next("div."+this.options.contentClass);
			if(this.current!=this.toExpand)
			{	this.toExpand.show();
				this.animate();
			}
		},
		
		checkMaxHeight:function()
		{
			for(var a=0;a<this.contents.length;a++)
			{
				if(this.contents[a].getHeight()>this.maxHeight)
				{
					this.maxHeight=this.contents[a].getHeight();
				}
			}
		},
		
		attachInitialMaxHeight:function()
		{
			this.current.previous("div."+this.options.toggleClass).addClassName(this.options.toggleActive);
			if(this.current.getHeight()!=this.maxHeight)
			{
				this.current.setStyle({height:this.maxHeight+"px"});
			}
		},
		
		clickHandler:function(b)
		{
			var a=b.element();
			if(a.hasClassName(this.options.toggleClass)&&!this.isAnimating)
			{
				this.isAnimating = true;
				this.expand(a);
			}
		},
		
		initialHide:function()
		{
			for(var a=0;a<this.contents.length;a++)
			{
				if( a!=this.current_a )
				{
					this.contents[a].hide();
					this.contents[a].setStyle({height:0});
				}
			}
		},
		
		animate:function()
		{
			var c=new Array();
			var a={
				sync:true,
				scaleFrom:0,
				scaleContent:false,
				transition:Effect.Transitions.sinoidal,
				scaleMode:{originalHeight:this.maxHeight,originalWidth:this.accordion.getWidth()},
				scaleX:false,
				scaleY:true
			};

			c.push(new Effect.Scale(this.toExpand,100,a));
			a={
				sync:true,
				scaleContent:false,
				transition:Effect.Transitions.sinoidal,scaleX:false,scaleY:true
			};

			c.push(new Effect.Scale(this.current,0,a));
			
			var b=0.75;
			
			new Effect.Parallel(c,
				{
					duration:b,
					fps:35,
					queue:{position:"end",scope:"accordion"},
					beforeStart:function()
						{
							this.isAnimating=true;
							
							if(this.current.previous("div."+this.options.toggleClass))
							{
								this.current.previous("div."+this.options.toggleClass).removeClassName(this.options.toggleActive);
							}
							
							if(this.current.previous("div."+this.options.toggleClass))
							{	this.toExpand.previous("div."+this.options.toggleClass).addClassName(this.options.toggleActive);
							}
						}.bind(this),
					afterFinish:function()
						{
							this.current.hide();
							this.toExpand.setStyle({height:this.maxHeight+"px"});
							this.current=this.toExpand;
							this.isAnimating=false;
						}.bind(this)
				}
			);
		}
	}
);

