// accordion.js v2.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Accordion is freely distributable under the terms of an MIT-style license.
//
// I don't care what you think about the file size...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//
/*-----------------------------------------------------------------------------------------------*/
var accordion = Class.create();
accordion.prototype = {
	//  Setup the Variables
	showAccordion : null,
	currentAccordion : null,
	duration : null,
	startHeight : 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 : 'accordion_content',
				toggleInActive : 'accordion_toggle_inactive'
			},
			defaultSize : {
				height : null,
				width : null
			},
			onEvent : 'click',
			startHeight : 0
		}, options || {});
		this.duration = ((11-this.options.resizeSpeed)*0.15);
		var accordions = $$('#'+container+' .'+this.options.classNames.toggle);
		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;};
			}
			if (accordion.next(0).scrollHeight > this.options.startHeight) {
				var options = {height: this.options.startHeight + 'px', display: 'block'};
			} else {
				var options = {height: accordion.next(0).scrollHeight + 'px', display: 'block'};
				accordion.addClassName(this.options.classNames.toggleInActive);
			}  
			this.currentAccordion = $(accordion.next(0)).setStyle(options);
		}.bind(this));
	},
	//  Activate an accordion
	activate : function(accordion) {
		if (this.animating) {
			return false;
		}
		this.effects = [];
		this.currentAccordion = $(accordion.next(0));
		this.currentAccordion.setStyle({
			display: 'block'
		});
		if (this.currentAccordion.scrollHeight > this.options.startHeight) {
			this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);
		}
		this.scaling = $H({
			scaleX: false,
			scaleY: true
		});
		if (this.currentAccordion == this.showAccordion) {
		  this.deactivate();
		} else {
		  this._handleAccordion();
		}
	},
	// Deactivate an active accordion
	deactivate : function() {
		// Get start height as percentage of total accordion height
		var startHeight = (this.options.startHeight / this.currentAccordion.scrollHeight) * 100;
		var doScale = this.showAccordion.scrollHeight > this.options.startHeight ? true : false;
		var options = $H({
		  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.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			},
			afterFinish: function() {
				this.showAccordion.setStyle({
					//height: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
					height: this.options.startHeight,
					display: 'block'
				});
				this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		});
		var startHeight = this.options.startHeight / this.currentAccordion.scrollHeight * 100;
		this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
		if (doScale) {
			new Effect.Scale(this.showAccordion, startHeight, options.update(this.scaling).toObject());
		}
	},
	// Handle the open/close actions of the accordion
	_handleAccordion : function() {
		var origHeight = this.currentAccordion.scrollHeight;
		var startHeight = this.options.startHeight / this.currentAccordion.scrollHeight * 100;
		var doScale = this.currentAccordion.scrollHeight > this.options.startHeight ? true : false;

		var options = $H({
			sync: true,
			scaleFrom: startHeight,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: {
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				//originalHeight: targetHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			}
		});
		options.merge(this.scaling);
		if (doScale) {
			this.effects.push(new Effect.Scale(this.currentAccordion, 100, options.update(this.scaling).toObject()));
		}
		if (this.showAccordion) {
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);

			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			});
			options.merge(this.scaling);
			var startHeight = (this.options.startHeight / this.showAccordion.scrollHeight) * 100;
			if (this.showAccordion.scrollHeight > this.options.startHeight) {
				this.effects.push(new Effect.Scale(this.showAccordion, startHeight, options.update(this.scaling).toObject()));
			}
		}
		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.setStyle({
						display: 'block'
					});
				}
				$(this.currentAccordion).setStyle({
				  height: doScale ? 'auto' : origHeight
				});
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	}
}

/*-----------------------------------------------------------------------------------------------*/
// SLIDEDOWN

// slidedown.js v1.0
//
// Heavily based on accordion.js
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Accordion is freely distributable under the terms of an MIT-style license.
//
/*-----------------------------------------------------------------------------------------------*/

var slidedown = Class.create();
slidedown.prototype = {
	//  Setup the Variables
	currentSlidedown : null,
	duration : null,
	effects : [],
	animating : false,
	//  Initialize the slidedowns
	initialize: function(container, options) {
		if (!$(container)) {
	    	throw(container+" doesn't exist!");
	    	return false;
		}
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'slidedown_toggle',
				toggleActive : 'slidedown_toggle_active',
				content : 'slidedown_content'
			},
			defaultSize : {
				height : null,
				width : null
			},
			onEvent : 'click'
		}, options || {});
		this.duration = ((11-this.options.resizeSpeed)*0.15);
		this.scaling = $H({scaleX: false, scaleY: true});	
		var slidedowns = $$('#'+container+' .'+this.options.classNames.toggle);
		slidedowns.each(function(slidedown) {
			Event.observe(slidedown, this.options.onEvent, this.activate.bind(this, slidedown), false);
			if (this.options.onEvent == 'click') {
			  slidedown.onclick = function() {return false;};
			}
			var options = {height: '0px', display: 'none'};
			this.currentAccordion = $(slidedown.next(0)).setStyle(options);
		}.bind(this));
	},
	//  Activate a slidedown
	activate : function(slidedown) {
		if (this.animating) return false;
		this.effects = [];
		this.currentSlidedown = $(slidedown.next(0));
		// slidedown is open : close slidedown
		if (this.currentSlidedown.getStyle('display') == 'block') this.close();
		else this.open();
	},
	// Close an open slidedown
	close : function() {
		var options = $H({
			duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end',
				scope: 'slidedownAnimation'
			},
			scaleMode: {
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentSlidedown.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentSlidedown.scrollWidth
			},
			afterFinish: function() {
				this.currentSlidedown.setStyle({
					height: '0px',
					display: 'none'
				});
				this.animating = false;
			}.bind(this)
		});
		options.merge(this.scaling);
		this.currentSlidedown.previous(0).removeClassName(this.options.classNames.toggleActive);
		new Effect.Scale(this.currentSlidedown, 0, options.update(this.scaling).toObject());
	},
	// Open a closed slidedown
	open : function() {
		this.currentSlidedown.setStyle({display: 'block'});
		var options = $H({
			duration: this.duration,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: {
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentSlidedown.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentSlidedown.scrollWidth
			},
			afterFinish: function() {
				$(this.currentSlidedown).setStyle({
					height: 'auto',
					display: 'block'
				});
				this.animating = false;
			}.bind(this)
		});
		options.merge(this.scaling);
		this.currentSlidedown.previous(0).addClassName(this.options.classNames.toggleActive);
		new Effect.Scale(this.currentSlidedown, 100, options.update(this.scaling).toObject());
	}
}
