/**
* Javascript/Prototype simple accordion
* (c) 2008 Dan Ladds <dan@danladds.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/

var Simple_Accordion = Class.create
({

	toggles : new Array (),
	
	contents : new Array (),
	
	lastId : 0,
	
	maxHeight : 0,

	// Constructor
    initialize : function (name)
    {
    	// Form names
    	toggleName  = name + '_toggle';
    	contentName = name + '_content';
    	
    	// Get elements
    	this.toggles  = $$('.' + toggleName);
    	this.contents = $$('.' + contentName);
    	
    	var s = this;
    	
    	this.contents.each
    	(
    		function (content, i)
			{
				toggle = s.toggles[i];
			
				// Set click function
				var a = function ()
				{
					s.changeTo (i);
				}
				
				toggle.observe ('click', a);
				toggle.observe ('toggle:changeto', a);
			}
		);
		
		// Set highest content area
		this.setHighest ();

		// Fold all except first
		for (j = 1; j < this.contents.length; ++j)
		{
			this.contents[j].hide ();
			this.contents[j].setStyle ({height: 0});
		}
		
		
		this.toggles[0].addClassName ('accordion_toggle_active');
    },
    
    setHighest : function ()
    {
    	for (j = 1; j < this.contents.length; ++j)
    	{
    		var height = this.contents[j].getHeight ();
    		
    		if (height > this.maxHeight)
    		{
    			this.maxHeight = height;
    		}
    	}
    },
    
    changeTo : function (index)
    {
    	// Ignore click on current
    	if (index == this.lastId) return false;
    
    	// Move active class
    	this.toggles[this.lastId].removeClassName ('accordion_toggle_active');
    	this.toggles[index].addClassName ('accordion_toggle_active');
    	
    	var transOps = 
    	{
    		sync : true,
    	};
    	
    	this.contents[index].show ();
    	
    	this.animate (this.contents[index], this.contents[this.lastId]);
    	
    	// Set active ID
    	this.lastId = index;
    	
    	// For ease of use inline
    	return false;
    },

	animate : function (open, close)
	{
		var effects = new Array(); 

		var options = 
		{  
			sync: true,  
			scaleFrom: 0,  
			scaleContent: false,  
			transition: Effect.Transitions.sinoidal,  
			scaleMode:
			{  
				originalHeight: this.maxHeight,  
				originalWidth: open.getWidth()  
			},
			scaleX: false,  
			scaleY: true  
		};  
		
		effects.push (new Effect.Scale (open, 100, options));
		
		options = 
		{
			sync : true,
			scaleContent : false,
			transition : Effect.Transitions.sinoidal,
			scaleX : false,
			scaleY : true
		}
		
		effects.push (new Effect.Scale (close, 0, options));
		
		new Effect.Parallel
		(
			effects,
			{
				duration: 0.6,
				queue :
				{
					position : 'end',
					scope : 'accordion'
				},
				afterFinish : function ()
				{
					close.hide ();
					open.setStyle ({ height: this.maxHeight+"px" });  
				},
			}
		)
	}
});