// 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 : 8,
			classNames : {
				toggle : 'accordion_toggle',
				toggleActive : 'accordion_toggle_active',
				content : 'accordion_content',
				content_hidden : 'display_none'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		//var accordions = $$('#'+container+' .'+this.options.classNames.toggle);
		var accordions = $(container).select(' .'+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 (this.options.direction == 'horizontal') {
				var options = $H({width: '0px'});
			} else {
				var options = $H({height: '0px'});			
			}
			options.merge({display: 'none'});			
			
			if (accordion.next(0)) this.currentAccordion = $(accordion.next(0)).setStyle(options);			
		}.bind(this));
	},
	
	//
	//  Activate an accordion
	//
	activate : function(accordion) {
		if (this.animating || !accordion.next(0)) {
			return false;
		}
		
		this.effects = [];
	
		this.currentAccordion = $(accordion.next(0));

		/*
		console.dir({
			height: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
			width: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
		});
		*/
		
		//this.currentAccordion.setStyle({
		//	display: 'block',
		//	height: 'auto',
		//	fontSize: '1em',
		//	width: 'auto'
		//	/*,border: 'solid 1px #ff0000'*/
		//});		
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
			
		if (this.currentAccordion == this.showAccordion) {
console.debug('activate #1');
			// this.deactivate();
		} else {
console.debug('activate #2');
			this._handleAccordion();
		}
	},
	// 
	// Deactivate an active accordion
	//
	deactivate : function() {
		var MSIE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
		var MSIE = (navigator.appName == "Microsoft Internet Explorer") ? true : false; 

		var options = $H({
		  duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.none,
			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
			},
			restoreAfterFinish: (MSIE && MSIE7) ? false : true,
			//afterFinishInternal: function(effect) {
			//	effect.element.hide().undoClipping();
			//},
			afterFinish: function() {
				this.showAccordion.setStyle({
          			height: 'auto',
					display: 'none'
				});				
				this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		});    
		options.merge(this.scaling);

		this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);

		//new Effect.Scale(this.showAccordion, 0, options);
console.debug('deactivate: toggle_disabled = ' + this.showAccordion.hasClassName('accordion_toggle_disabled'));
		if (! this.showAccordion.hasClassName('accordion_toggle_disabled')) new Effect.BlindUp(this.showAccordion, options);
		
		//this.showAccordion = null;
	},

  //
  // Handle the open/close actions of the accordion
  //
	_handleAccordion : function() {
		var MSIE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
		var MSIE = (navigator.appName == "Microsoft Internet Explorer") ? true : false; 

		var options = $H({
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.none,
			restoreAfterFinish: (MSIE && MSIE7) ? false : true,
			//afterFinishInternal: function(effect) {
			//	effect.element.hide().undoClipping();
			//},
			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
			}
		});
		options.merge(this.scaling);
		
		if (this.showAccordion == this.currentAccordion) 
		{
				console.debug("currentAccordion == showAccordion");
		}else{
				console.debug("currentAccordion != showAccordion");
		}
		
console.debug('_handleAccordion BlindDown: toggle_disabled = ' + this.currentAccordion.previous(0));
console.debug('_handleAccordion BlindDown: toggle_disabled = ' + this.currentAccordion.previous(0).hasClassName('accordion_toggle_disabled'));
		if (! this.currentAccordion.previous(0).hasClassName('accordion_toggle_disabled'))
			this.effects.push(
				//new Effect.Scale(this.currentAccordion, 100, options)
				new Effect.BlindDown(this.currentAccordion, options)
			);
		
		// hide showAccordion? - remove class _active, call scale to 0%.
		if (this.showAccordion) {
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.none
			});
			options.merge(this.scaling);
console.debug('_handleAccordion BlindUp: toggle_disabled = ' + this.showAccordion.previous(0));
console.debug('_handleAccordion BlindUp: toggle_disabled = ' + this.showAccordion.previous(0).hasClassName('accordion_toggle_disabled'));
			if (! this.showAccordion.previous(0).hasClassName('accordion_toggle_disabled'))
				this.effects.push(
					//new Effect.Scale(this.showAccordion, 0, options)
					new Effect.BlindUp(this.showAccordion, options)
				);				
		}
		
		new Effect.Parallel(this.effects, {
				duration: this.duration, 
				queue: {
					position: 'end', 
					scope: 'accordionAnimation'
				},
				beforeStart: function() {
					this.animating = true;
				}.bind(this),
				//afterFinishInternal: function(effect) {
				//	effect.element.hide().undoClipping();
				//},
				afterFinish: function() {
					if (this.showAccordion) {
						if (! this.showAccordion.previous(0).hasClassName('accordion_toggle_disabled'))
							this.showAccordion.setStyle({
								display: 'none'
							});				
					}
					$(this.currentAccordion).setStyle({
					  	height: 'auto', 
						originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
						originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
					});
					this.showAccordion = this.currentAccordion;
					this.animating = false;
				}.bind(this)
			});
			
	}
}

