if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

if (typeof DROW == 'undefined') var DROW = {};

DROW.accordion = Class.create();
DROW.accordion.prototype = {

	//
	//  Setup the Variables
	//
	showAccordion : null,
	currentAccordion : null,
	duration : null,
	effects : [],
	animating : false,
	
	//  
	//  Initialize the accordions
	//
	initialize: function(container, options) {
		if (!$(container)) {
			throw(container+" doesn't exist!");
			return false;
		}
	  
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'accordion-toggle',
				toggleActive : 'accordion-toggle-active',
				content : 'acc-section',
				button : 'btn-toggle'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click',
			titleSelector: 'h3'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);
		this.aItems = new Array();
		var childs = $(container).childElements();
		
        var i=s=0;
        for(i;i<childs.length;i++) {
        	var item = childs[i];

        	//filter text-Nodes
        	if(item.nodeType!=3 && item.select('div.'+this.options.classNames.content)[0]) {
        		
	        	this.aItems[s] = {};
	        	this.aItems[s].titleDom = item.select(this.options.titleSelector)[0];
	        	if (!this.aItems[s].titleDom.hasClassName(this.options.classNames.toggle)) this.aItems[s].titleDom.addClassName(this.options.classNames.toggle);
	        	this.aItems[s].contentDom = item.select('div.'+this.options.classNames.content)[0];

	        	this.aItems[s].toogleBtn = this.aItems[s].titleDom.select('div.'+this.options.classNames.button)[0];
	        	this.aItems[s].toogleBtn.observe(this.options.onEvent, this.activate.bind(this, this.aItems[s]));
	        	if (this.options.onEvent == 'click') {
	        		this.aItems[s].toogleBtn.onclick = function() {return false;};
	  			}
	        	
	        	if (this.options.direction == 'horizontal') {
					var options = {width: '0px'};
				} else {
					var options = {height: '0px'};			
				}
	        	options = Object.extend({display: 'none'}, options);
	        	this.aItems[s].contentDom.setStyle(options);
	        	
	        	if (this.aItems[s].titleDom.hasClassName(this.options.classNames.toggleActive)) {
	        		this.activate(this.aItems[s]);
	        	}
				s++;
        	}
        }
	},
	//
	//  Activate an accordion
	//
	activate : function(accordion) {
		if (this.animating) {
			return false;
		}

		this.effects = [];
	
		this.currentAccordion = accordion;
		this.currentAccordion.contentDom.setStyle({
			display: 'block'
		});
		
		this.currentAccordion.titleDom.addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = {scaleX: true, scaleY: false};
		} else {
			this.scaling = {scaleX: false, scaleY: true};			
		}
			
		if (this.currentAccordion == this.showAccordion) {
		  this.deactivate();
		} else {
		  this._handleAccordion();
		}
	},
	// 
	// Deactivate an active accordion
	//
	deactivate : function() {
		var options = Object.extend({
			duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.contentDom.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.contentDom.scrollWidth
			},
			afterFinish: function() {
				this.showAccordion.contentDom.setStyle({
					height: 'auto',
					display: 'none'
				});				
				this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		}, this.scaling);
		this.showAccordion.titleDom.removeClassName(this.options.classNames.toggleActive);
		new Effect.Scale(this.showAccordion.contentDom, 0, options);
	},
	
	activateIndex: function(index) {
		this.activate(this.aItems[index]);
	},
	//
	// Handle the open/close actions of the accordion
	//
	_handleAccordion : function() {
		var options = Object.extend({
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.contentDom.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.contentDom.scrollWidth
			}
		}, this.scaling);
		this.effects.push(new Effect.Scale(this.currentAccordion.contentDom, 100, options));

		if (this.showAccordion) {
			this.showAccordion.titleDom.removeClassName(this.options.classNames.toggleActive);
			
			options = Object.extend({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			}, this.scaling);
			
			this.effects.push(
				new Effect.Scale(this.showAccordion.contentDom, 0, options)
			);				
		}
		
		new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showAccordion) {
					this.showAccordion.contentDom.setStyle({
						display: 'none'
					});				
				}
				$(this.currentAccordion.contentDom).setStyle({
				  height: 'auto'
				});
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	}
}
