// Copyright (c) 2007 stickmanlabs
// Original Author: Kevin P Miller | http://www.stickmanlabs.com
// Tweaked by: Martin Caron | http://www.rds.ca
// 
// Accordion is freely distributable under the terms of an MIT-style license.
/*-----------------------------------------------------------------------------------------------*/


// IS SCRIPTACULOUS INCLUDED ?
if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

// LETS ADD A LITTLE FUNCTIONALITY TO JS ARRAYS
// INDEX OF VALUE
Array.prototype.indexOf = function(obj) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == obj)
      return i;
  }
  return -1;
}

// HAS VALUE
Array.prototype.has = function(obj) {
  return this.indexOf(obj) >= 0;
} 


var accordion = Class.create();
accordion.prototype = {

	//  Setup the Variables
	showAccordions : new Array(),
	currentAccordion : null,
	duration : null,
	animating : false,
	
	//////////////////////////////////////////////////////////////////
	//  Initialize the accordions
	initialize: function(container, options) {
		 
		// container exists ?
		if (!$(container)) 	{throw(container+" doesn't exist!");return false;}
	  
	  	// instantialte variables
		this.options = Object.extend({
			resizeSpeed : 10,
			classNames : {toggle : 'accordion_toggle', toggleActive : 'accordion_toggle_active', content : 'accordion_content'},
			direction : 'vertical',
			maxOpen : 1,
			onEvent : 'click'
		}, options || {});
		
		this.showAccordions = new Array();
		
		// set effect duration
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		// get toggles
		var accordions = $$('#'+container+' .'+this.options.classNames.toggle);
		
		// foreach toggle -> observe click
		accordions.each(function(accordion) {
			Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
			if (this.options.onEvent == 'click') { accordion.onclick = function() {return false;};}
			this.currentAccordion = $(accordion.next(0));			
		}.bind(this));
	},
	
	
	//////////////////////////////////////////////////////////////////////
	//	ACTIVATE an accordion , this is the action called by the listener
	activate : function(accordion) {
		this.currentAccordion = $(accordion.next(0));
		if (this.animating == this.currentAccordion) {return false;}
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);
		if (this.showAccordions.has(this.currentAccordion)) {
		  this.deactivate();
		} else {
		  this._handleAccordion();
		}
	},
	
	
	
	/////////////////////////////////////////////////////////////////////
	// DEACTIVATE an active accordion
	deactivate : function() {
    	this.currentAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
    	
		// close content
		new Effect.BlindUp(this.currentAccordion, { duration: this.duration, 
													beforeStart:function(){this.animating = this.currentAccordion;}.bind(this),
													afterFinish:function(){this.animating = false;}.bind(this)
												});
		
		// REMOVE FROM ARRAY
		var i = 0;
		var tempArray = new Array();
		while(i < this.showAccordions.length)
		{
			if(this.showAccordions[i] != this.currentAccordion)
			{
				tempArray[tempArray.length] = this.showAccordions[i];
			}
			i++;
		}
		this.showAccordions = tempArray;
	},



	//////////////////////////////////////////////////////////////////////
	// HANDLE the open/close actions of the accordion
	_handleAccordion : function() {
		
		// open content
		Effect.BlindDown(this.currentAccordion, { 	duration: this.duration, 
													beforeStart:function(){this.animating = true;}.bind(this),
													afterFinish:function(){this.animating = false;}.bind(this)
												});
		
		
		this.currentAccordion.style.height = "";
		
		if((this.showAccordions.length+1) > this.options.maxOpen)
		{
			var closeAccordion = this.showAccordions[0];
			
			// close content
			Effect.BlindUp(closeAccordion, {duration: this.duration});
			closeAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			// REMOVE FROM ARRAY
			var i = 0;
			var tempArray = new Array();
			while(i < this.showAccordions.length)
			{
				if(this.showAccordions[i] != closeAccordion)
				{
					tempArray[tempArray.length] = this.showAccordions[i];
				}
				i++;
			}
			this.showAccordions = tempArray;
		}
		
		this.showAccordions.push(this.currentAccordion);
	}
}
	