var Accordion = Class.create( {
	initialize : function(container, options)
	{
		this.container = container;
		this.accordions = new Array();
		this.heights = {};
		
		this.animating = false;
		
		this.options = {
			duration: 0.5,
			padding: 10,
			rel: 'accordion'
		};

		if (options)
		{
			for (var key in this.options)
			{
				if (options[key])
				{
					this.options[key] = options[key];
				}
			}
		}
		
		this.start();
	},
	 
	start : function()
	{
		this.container.select('a').each(function(element){
			if (element.rel == this.options.rel)
			{
				element.observe('click', this.activate.bind(this));
				this.accordions.push(element);
			}
		}.bind(this));
		
		this.build();
	},
	
	build: function()
	{
		var i = 0;
		this.accordions.each(function(elm){
			name = elm.name;
			elm = $(name);
			this.heights[name] = elm.getStyle('height').replace('px', '');
			
			elm.setStyle({overflow: 'hidden'});
			
			if(i)
			{
				new Effect.Morph(elm, {
					duration: 0,
					style: 'height: 0px;'
				});
			}
			i++;
		}.bind(this));
	},
	
	activate: function(event)
	{
		event.stop();
		
		if (this.animating)
		{
			return false;
		}
		this.animating = true;
		
		accordion = event.element();
		container = $(accordion.name);
		
		this.accordions.without(accordion).each(function(elm){
			elm = $(elm.name);
			new Effect.Morph(elm, {
				duration: this.options.duration,
				style: 'height: 0px;',
				afterFinish: function() {	
					this.animating = false;
				}.bind(this)
			});
		}.bind(this));
		
		height = parseInt(this.heights[accordion.name]) + this.options.padding;
		
		new Effect.Morph(container, {
			duration: this.options.duration,
			style: 'height: ' + height + 'px;',
			afterFinish: function() {	
				this.animating = false;
			}.bind(this)
		});
	}
});