// 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
//

/*-----------------------------------------------------------------------------------------------*/

if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
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 : 7,
			classNames : {
				top : 'accordion_top',
				topActive : 'accordion_top_active',
				bottom : 'accordion_bottom',
				bottomActive : 'accordion_bottom_active',
				toggle : 'accordion_toggle',
				toggleActive : 'accordion_toggle_active',
				content : 'accordion_content',
				hover: 'accordion_hover',
				hoverTop: 'accordion_top_hover',
				hoverBottom: 'accordion_bottom_hover'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.size = $$('#'+container+' .'+this.options.classNames.toggle).length;		
		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);
			//hover
			Event.observe(accordion, 'mouseover', this.mouseOver.bind(this, accordion), false);
			Event.observe(accordion, 'mouseout', this.mouseOut.bind(this, accordion), false);
			
			if (this.options.onEvent == 'click') {
			  accordion.onclick = function() {return false;};
			}
			
			if (this.options.direction == 'horizontal') {
				var options = {width: '0px'};
			} else {
				var options = {height: '0px'};			
			}
			
			Object.extend(options, {display: 'none'});				
			
			$$('#'+container+' .'+this.options.classNames.toggle)[0].addClassName(this.options.classNames.top);
			$$('#'+container+' .'+this.options.classNames.toggle)[this.size-1].addClassName(this.options.classNames.bottom);
			
			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));
		//check if accordion has content, display if true
		if(!this.currentAccordion.innerHTML.empty()){
			this.currentAccordion.setStyle({
				display: 'block'
			});		
		}
		
		if(this.currentAccordion.previous(0).hasClassName(this.options.classNames.top)){
			this.currentAccordion.previous(0).addClassName(this.options.classNames.topActive);
		} else if (this.currentAccordion.previous(0).hasClassName(this.options.classNames.bottom)){
			this.currentAccordion.previous(0).addClassName(this.options.classNames.bottomActive);
		} else {			
			this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);
		}
		
		//clear hover states
		this.mouseOut(accordion);
		
		if (this.options.direction == 'horizontal') {
			this.scaling = {
				scaleX: true,
				scaleY: false
			};
		} else {
			this.scaling = {
				scaleX: false,
				scaleY: true
			};			
		}
		
		//close current accordion if chosen
		if (this.currentAccordion == this.showAccordion) {
		  //this.deactivate();
		} else {
		  this._handleAccordion();
		}
	},
	// 
	// Deactivate an active accordion
	//
	deactivate : function() {
	var options = {
		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: 'auto',
				display: 'none'
			});				
			this.showAccordion = null;
			this.animating = false;
		}.bind(this)
	};    
    Object.extend(options, this.scaling);
	
	if(this.showAccordion.previous(0).hasClassName(this.options.classNames.top)){
		this.showAccordion.previous(0).removeClassName(this.options.classNames.topActive);
	} else if (this.showAccordion.previous(0).hasClassName(this.options.classNames.bottom)){
		this.showAccordion.previous(0).removeClassName(this.options.classNames.bottomActive);
	} else {			
		this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
	}
       
	new Effect.Scale(this.showAccordion, 0, options);
	},

  //
  // Handle the open/close actions of the accordion
  //
	_handleAccordion : function() {
		var options = {
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			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
			}
		};
		Object.extend(options, this.scaling);
		
		this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options)
		);

		if (this.showAccordion) {
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			this.showAccordion.previous(0).removeClassName(this.options.classNames.topActive);
			this.showAccordion.previous(0).removeClassName(this.options.classNames.bottomActive);
			
			options = {
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			};
			Object.extend(options, this.scaling);
			
			this.effects.push(
				new Effect.Scale(this.showAccordion, 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.setStyle({
						display: 'none'
					});				
				}
				$(this.currentAccordion).setStyle({
				  height: 'auto'
				});
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	},	
	
	mouseOver: function(accordion){
		if(!(accordion.hasClassName(this.options.classNames.toggleActive))){
			if(!(accordion.hasClassName(this.options.classNames.topActive))){		
				if(accordion.hasClassName(this.options.classNames.top)){
					accordion.addClassName(this.options.classNames.hoverTop);	
				} else if(accordion.hasClassName(this.options.classNames.bottom)){
					accordion.addClassName(this.options.classNames.hoverBottom);
				} else {
					accordion.addClassName(this.options.classNames.hover);
				}		
			}
		}
	},	
	
	mouseOut: function(accordion){				
		accordion.removeClassName(this.options.classNames.hover);
		accordion.removeClassName(this.options.classNames.hoverBottom);
		accordion.removeClassName(this.options.classNames.hoverTop);
	}
}
	