// accordion.js v1.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// ListScroller 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) {
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'accordion_toggle',
				toggleActive : 'accordion_toggle_active',
				trActive : 'active',
				trUnactive : 'unactive',
				content : 'accordion_content'
			},
			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);
		accordions.each(function(accordion) {
			Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
			accordion.onclick = function() {return false;};
			
			if (this.options.direction == 'horizontal') {
				var options = $H({width: '0px'});
			} else {
				var options = $H({height: '0px'});			
			}			
			this.currentAccordion = $(accordion.next(0)).setStyle(options);			
			this.container = container ;
		}.bind(this));
	},

	// Refresh an accordion
	refresh : function() {
		var options = $H({
			scaleX: false,
			scaleY: true,
			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
			}
		});
		
		new Effect.Scale(this.currentAccordion, 100, options);
	},
	
	//
	// Deactivate an accordion
	//
	deactivate : function(accordion)
	{
		if (this.animating) {
			//return false;
		}
		if (accordion) {
			accordion.removeClassName(this.options.classNames.toggleActive);
			
			this.effects = [];
			
			if (this.options.direction == 'horizontal') {
			var adjustments = $H({
				scaleX: true,
				scaleY: false
				});
			} else {
				var adjustments = $H({
					scaleX: false,
					scaleY: true
				});			
			}
		
			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			});
			options.merge(adjustments);
			
			this.effects.push(
				new Effect.Scale(accordion.next(0), 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 == accordion.next(0))
					this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		});
	},
	
	//
	//  Activate an accordion
	//
	activate : function(accordion) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
		
		this.currentAccordion = $(accordion.next(0));	
		if (this.currentAccordion == this.showAccordion) {
			return this.deactivate(this.showAccordion.previous(0));
		}
	
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);	
		
		if (this.options.direction == 'horizontal') {
			var adjustments = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			var adjustments = $H({
				scaleX: false,
				scaleY: true
			});			
		}
			
		var options = $H({
			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
			}
		});
		options.merge(adjustments);
		
		this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options)
		);

		if (this.showAccordion) {
//			var ullist=$('accordeon_menu_container').getElementsByTagName("tr");		
//			for (var i=0; i<ullist.length; i++) {
//				var search_active = $$(ullist[i].id + ' tr ');
//				var search_active=$(ullist[i]).getElementsByTagName("h1");
//				alert(ullist[i].id+' :'+this.showAccordion.previous(0).className);
//				if ( ullist[i] != this.showAccordion.previous(0) ) {
//					ullist[i].removeClassName(this.options.classNames.trActive);
//				}
//				for (var j=0; j<search_active.length; j++) {
//					if ( search_active[j].className.indexOf(this.options.classNames.toggleActive) > 0 ) {
//						ullist[i].addClassName(this.options.classNames.trActive);	
//					}				
//				}			
//			}				
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);		

			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			});
			options.merge(adjustments);
			
			this.effects.push(
				new Effect.Scale(this.showAccordion, 0, options)
			);				
		}
		
		// Special agreement made by gdelarue to manage all <tr> css of an active element
		var ullist=$('accordeon_menu_container').getElementsByTagName("tr");	
		var current_id = '';
		for (var i=0; i<ullist.length; i++) {
			var search_active=$(ullist[i]).getElementsByTagName("h1");
			for (var j=0; j<search_active.length; j++) {
				if ( current_id != ullist[i].id ) {
					if ( search_active[j].className.indexOf(this.options.classNames.toggleActive) > 0 ) {
						ullist[i].addClassName(this.options.classNames.trActive);			
					} else {
						ullist[i].removeClassName(this.options.classNames.trActive);	
					}
				}
				current_id = ullist[i].id;
			}			
		}			
		// -- Special agreement made by gdelarue to manage all <tr> css of an active element		
		
		new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	}
}
	
//
// Prototype style Accordion master class
//
var Accordion = {
	Preload : function(content_class)
	{
		if(content_class != '')
		{
			elements = $$('.' + content_class);
			elements.each(function(e) {
				e.setStyle({height: '0px'});
			});
		}
	}
}