/*****************************************************************************************
 *
 *	Collapsible blocks (requires jquery.js)
 *
 ****************************************************************************************/
// $$Revision: 1 $

if (typeof(jQuery) != "undefined") {
	$.fn.nextUntil = function(expr) {
		var match = [];
	
		// We need to figure out which elements to push onto the array
		this.each(function(){
			// Traverse through the sibling nodes
			for( var i = this.nextSibling; i; i = i.nextSibling ) {
				// Make sure that we're only dealing with elements
				if ( i.nodeType != 1 ) continue;
	
				// If we find a match then we need to stop
				if ( jQuery(i).is(expr)) break;
	
				// Otherwise, add it on to the stack
				match.push( i );
			}
		});
	
		return this.pushStack( match, arguments );
	};
	
	$.fn.wrapAll = function() {
		// There needs to be at least one matched element for this to work
		if ( !this.length ) return this;
	
		// Find the element that we're wrapping with
		var b = jQuery.clean(arguments)[0];
	
		// Make sure that its in the right position in the DOM
		this[0].parentNode.insertBefore( b, this[0] );
	
		// Find its lowest point
		while ( b.firstChild ) b = b.firstChild;
	
		// And add all the elements there
		return this.appendTo(b);
	};
		
	arb.functions.collapsible = function(heading, options) {
		options = $.extend(
			{
				cookieName: function(query, heading, index) {
					query = query.replace(" ", "_");
					return document.location.pathname + "#collapsible-section:" + query + ":" + index;
				}
			},
			options
		);
		
		$(heading)
			.each(function(i){
				var a = document.createElement('a');
				a.name = options.cookieName(heading, this, i);
				a.href = "javascript:";
				$(a)
					.html(this.innerHTML)
					.click( function() {
						var container = this.parentNode.parentNode;
						if ($(container).is(".collapsed")) {
							$(container)
								.removeClass("collapsed")
								.find(".collapsible-body")
								.animate({height: "show"}, 'fast', function(){
									$(this).height("auto");
								});
							arb.cookie.set(this.name, 'open');
						}
						else {
							$(".collapsible-body", container)
								.animate({height: "hide"}, 'fast', function() {
									$(container).addClass("collapsed");
									$(this).css("display", "block");
								});
							arb.cookie.set(this.name, 'closed');
						}
						this.blur();
						return false;
					});
					
				$(this)
					.empty()
					.append(a)
					.addClass("collapsible-heading")
					.nextUntil(heading).wrapAll("<div class='collapsible-body'></div>");
					
				if(arb.cookie.get(a.name) == 'closed') {
					$(a.parentNode.parentNode).addClass("collapsed");
				}
				else if (arb.cookie.get(a.name) == 'open') {
					$(a.parentNode.parentNode).removeClass("collapsed");
				}
			});
	}

	// auto attach a form collapsible 
	$(document).ready(function() {
		arb.functions.collapsible("fieldset.collapsible > h2.legend");	  
		arb.functions.collapsible("fieldset.collapsible > h3.legend");	  
		arb.functions.collapsible("fieldset.collapsible > h4.legend");	  
		arb.functions.collapsible("fieldset.collapsible > legend");	  
	});
}  // end of jQuery test