/**
 * BluAccordion
 *
 * Extends Accordion to allow multiple open sections and history.
 * 
 * @package	 BluCommerce
 * @subpackage  FrontendClientside
 */
var BluAccordion = new Class({

	Extends: Accordion,
	 
	history: null,
	togglerIds: [],
	 
	options: {
		historyKey: 'accordion',
		useHistory: false,
		historyIds: false,
		show: false,
		opacity: false,
		duration: 500,
		transition: Fx.Transitions.Cubic.easeOut,
		fps: 100,
		allowMultipleOpen: false,
		onActive: function(toggler, element) { toggler.removeClass('closed').addClass('open'); },
		onBackground: function(toggler, element) { toggler.removeClass('open').addClass('closed'); }
	},
 
	initialize: function(togglers, elements, options) {
		this.setOptions(options);
		
		/* Add history */
		if (this.options.useHistory) {
			this.history = HistoryManager.register(
				this.options.historyKey,
				[this.options.display],
				this.onHistoryMatch.bind(this),
				false,
				false);
		}
		
		return this.parent(togglers, elements, this.options);
	},
	
	onHistoryMatch: function(args) {	
		if (!args[0]) { return; }		
		var index = this.options.historyIds ? this.togglerIds.indexOf(args[0]) : args[0];
		this.display(index, true);
	},
	
	addSection: function(toggler, element, pos) {
		
		/* Add to ID mapping */
		if (this.options.historyIds) {
			var id = toggler.get('id').replace(this.options.historyKey+'-', '');
			toggler.erase('id');
			this.togglerIds.push(id);
		}
	
		/* Add hover events for IE6 */
		if (Browser.Engine.trident4) {
			this.togglers.each(function(el) {
				el.addEvent('mouseover', function() { this.addClass('hover'); });
				el.addEvent('mouseout', function() { this.removeClass('hover'); });
			}, this);
		}
		
		return this.parent(toggler, element, pos);
	},
	
	display: function(index) {
		index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
		if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide && !this.options.allowMultipleOpen)) {
			return this;
		}
		
		var obj = {};
		if (this.options.allowMultipleOpen) {
			if ($type(index) != 'array') { index = [index]; }
			index.each(function(i){
				var el = this.elements[i];
				obj[i] = {};
				var hide = (el.offsetHeight > 0);
				this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
				for (var fx in this.effects) {
					obj[i][fx] = hide ? 0 : el[this.effects[fx]];
				}
			}, this);
		} else {
		
			/* Update history value */
			if (this.history) {
				var val = this.options.historyIds ? this.togglerIds[index] : index;
				this.history.setValue(0, val);
			}
		
			this.previous = index;
			this.elements.each(function(el, i){
				obj[i] = {};
				var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
				this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
				for (var fx in this.effects) {
					obj[i][fx] = hide ? 0 : el[this.effects[fx]];
				}
			}, this);
		}		
		return this.start(obj);
	}
});
