/*

Slidinglist class: lists (ul, o)l collapsing with mootools effects
Author: Lennart Pilon
Collapse function stolen from crazydave's Nested.js (http://www.clanccc.co.uk/moo/sortlist.html)

*/
var Slidinglist = new Class({
	getOptions: function() {
		return {
			childTag: 'LI',
			closedClass: 'closed', 
			openClass: 'open',
			transition: Fx.Transitions.expoOut,
			duration: 250,
			state: 'closed'
		};
	},

	initialize: function(list, options) {
		this.setOptions(this.getOptions(), options);
		this.list = $(list);
		this.options.parentTag = this.list.nodeName;
		this.bound = {};
		this.bound.collapse = this.collapse.bindWithEvent(this);
		this.list.addEvent('click', this.bound.collapse);
		this.list.getElements(this.options.parentTag).each(function(el) {
			
			if (options.state == 'closed') {
				//alert(el.getElementsBySelector('li.current'));
				// close all nodes, except 'current'
				if (el.getElementsBySelector('.current') != "" ){	
					el.getParent().addClass(options.openClass);
				} else {
					el.getParent().addClass(options.closedClass); 
					new Fx.Height(el, {duration: options.duration, transition: options.transition}).hide();
					//new Fx.Elements(el).start({duration: options.duration, transition: options.transition});
				}
			}else {
				// leave all nodes open, add openClass to nodes with children
				el.getParent().addClass(options.openClass);		
			}
		});
		/*$$('.current').removeClass(this.options.closedClass);
		$$('.current').addClass(this.options.openClass);
		$$('.current').each(function(elem) {
			new Fx.Height(elem.getElement('UL')).hide();
		});*/
		

	},
	
	collapse: function(event) {
		var el = $(event.target);
		
		while (el.nodeName != this.options.childTag && el != this.list) {
			el = el.getParent();
		}
		if (el == this.list) return;
		el = $(el);

		// loop through all parents to set their heights to 100%
		function setAncestorsHeight(node, id) {
			var parent = node;
			while(parent.id != id) {
				parent = parent.getParent();
				parent.setStyle('height', 'auto');
			}
		}
		/*alert($E(this.options.parentTag, el).getParent().innerHTML);*/
		var sub = ($E(this.options.parentTag, el).getParent()) ? $E(this.options.parentTag, el).getParent() : false;		
		//new Fx.Elements(sub.getElement(this.options.parentTag)).start({duration: this.options.duration, transition: this.options.transition});

		//alert(sub.getParent().innerHTML);
		if (sub) {
			new Fx.Height(sub.getElement(this.options.parentTag), {duration: this.options.duration, transition: this.options.transition}).toggle();
			if (el.hasClass(this.options.closedClass)) {
				el.removeClass(this.options.closedClass);
				el.addClass(this.options.openClass);
				
				el.setStyle('height', 'auto');
				el.getParent().setStyle('height', 'auto');
				setAncestorsHeight(el.getElement(this.options.parentTag), 'hsb_brand_nav');
			} else {
				el.removeClass(this.options.openClass);
				el.addClass(this.options.closedClass);
			}
		}
		//window.location = el.getElement('a').href;
		event.stop();
	}
});

Slidinglist.implement(new Events);
Slidinglist.implement(new Options);