/*
Required Files: 
	prototype.js
	scriptaculous.js?load=effects
*/
;
var Accordion = {


	init : function() {
	
		var accordions = $$('.accordion');
		
		accordions.each(function(n) {
			new Accordion.Accordion(n);
		});
	}
};

Accordion.Accordion = Class.create({

	// constructor
	initialize : function(accordion) {
		if (!accordion) { return; }

		// properties
		this.accordian = accordion;
		this.panes = [];
		this.openedPanes = [];
		this.is_opening = false;
		
		this.scaling_options = {
			sync : true,
			scaleContent : false
		};
		
		this.options = {
			on_hover : false,
			duration : 0.5,
			direction : "vertical",
			toggle_class : "accordion_toggle",
			content_class : "accordion_content",
			number_of_open_panes : 1,
			default_pane : 1,
			transition_effect : Effect.Transitions.sinoidal
		};	
		
		this.accordion = accordion;
		this.panes = this.accordion.select(".pane");
	
		// set up options
		var options_strings = $(this.accordion).className.match(/\{.*\}/);
		if (options_strings) { 
			Object.extend(this.options, options_strings[0].evalJSON() || {});
		}
		
		if (this.options.direction == "horizontal") {
			Object.extend(this.scaling_options, {
				scaleX: true,
				scaleY: false
			});
		} else {
			Object.extend(this.scaling_options, {
				scaleX: false,
				scaleY: true
			});
		}
		
		this.panes.each(function(pane) {
			
			pane.observe("mouseover", function() { this.addClassName("highlighted"); });
			pane.observe("mouseout", function() { this.removeClassName("highlighted"); });
			
			pane.select("." + this.options.toggle_class).each(function(toggle) {
				var onevent = this.options.on_hover ? "mouseover" : "click";
				toggle.observe(onevent, function() { if(!this.isPaneOpened(pane) && !this.is_opening) { this.openPane(pane); }}.bind(this));
			}.bind(this));
			
		}.bind(this));
		
		// open first pane
		this.openPane(this.panes[this.options.default_pane - 1]);
	},
	
	// methods
	isPaneOpened : function(pane) {
		if (!pane) { return false; }
	
		for(var i = 0; i < this.openedPanes.length; i++) {
			if (this.openedPanes[i] == pane) { return true; }
		}
		
		return false;
	},
	
	closeOpenedPane : function(pane) {
		if (!pane) { return; }
	
		for(var i = 0; i < this.openedPanes.length; i++) {
			if (this.openedPanes[i] == pane) {
				this.openedPanes.splice(i, 1);
			}
		}
	},
	
	closeFirstOpenPane : function() {
		if (this.openedPanes.length < this.options.number_of_open_panes) { return; }
	
		this.closePane(this.openedPanes[0]);
	},

	openPane : function(pane) {
		if (!pane) { return; }
	
		var effects = [];
		
		// set up open pane effect
		open_pane_content = pane.select("." + this.options.content_class)[0];

		var open_scaling_options = Object.extend(this.scaling_options, {
			scaleFrom: 0,
			scaleMode: { 
				originalHeight: open_pane_content.scrollHeight,
				originalWidth: open_pane_content.scrollWidth
			},
			beforeStart : function() {
				this.is_opening = true;
				pane.addClassName("open_pane");
			}.bind(this),
			afterFinish : function() {
				this.openedPanes.push(pane);
				this.is_opening = false;
			}.bind(this)
		});
	
		effects.push(new Effect.Scale(open_pane_content, 100, open_scaling_options));
		
		// set up pane to close effect
		pane_to_close = null;
		
		if (this.openedPanes.length >= this.options.number_of_open_panes) { 
			pane_to_close = this.openedPanes[0];
			if (pane_to_close) {
					
				close_pane_content = pane_to_close.select("." + this.options.content_class)[0];
			
				var close_scaling_options = Object.extend(this.scaling_options, {
					scaleFrom: 100,
					scaleMode: { 
						originalHeight: close_pane_content.scrollHeight,
						originalWidth: close_pane_content.scrollWidth
					},
					beforeStart : function() {
						pane_to_close.removeClassName("open_pane");
					},
					afterFinish : function() {
						this.closeOpenedPane(pane_to_close);
					}.bind(this)
				});	
				
				effects.push(new Effect.Scale(close_pane_content, 0, close_scaling_options));
			}
		}
	
		new Effect.Parallel(effects, {
			duration : this.options.duration,
			transition: this.options.transition_effect
		});
	}
	
});


document.observe("dom:loaded", Accordion.init);
