/*
LowlanderAccordion is similar to the "accordion" widgets
available for jQuery, but it allows several elements to be
opened at one time. In effect, it is an accordion which does
not enforce Highlander behaviour (i.e. "there can be only one").

This plugin intentionally uses the same HTML structure as Joern
Zaefferer's Accordion plugin so that it is trivial to switch between
the One True Accordion plugin and this one.

///////////////////////////////////////////////////////////////////////
Author:

	http://wanderinghorse.net/home/stephan/

Based off of Karl Swedberg's article:

	http://www.learningjquery.com/2007/02/more-showing-more-hiding

Plugin home page:

	http://wanderinghorse.net/computing/javascript/jquery/togglepane/

License: Public Domain

Revision history:

- 20070911:
  - Added a workaround to allow it to work around missing lt/gt functions
	in jQuery 1.2.x.

- 20070807: initial release

*/
jQuery.fn.initTogglePane = function( props ) {
	props = jQuery.extend({
		// todo: highlander:false,
		headerClassClosed:null,
		startOpened:Infinity,
		speed:'fast'
		},
		props ? props : {});
	if( false === props.startOpened ) props.startOpened = NaN;
	else if( true === props.startOpened ) props.startOpened = Infinity;
	var wrappers = jQuery('> div',this);
	var contents = jQuery('div:last',wrappers);
	var heads = jQuery('input:first',wrappers);
	if( ! heads.lt ) { // accommodate jQuery 1.2 incompatibility...
		heads.lt = function(index) { return heads.slice(0,index); };
		heads.gt = function(index) { return heads.slice(index+1); };
	}
	heads.click( function() {
		var head = jQuery(this);
		head.next().slideToggle(props.speed,
			props.headerClassClosed
			? function(){head.toggleClass( props.headerClassClosed )}
			: undefined);
	});
	var so = props.startOpened;
	if( isNaN(so) ) {
		heads.click(); // close all
	}
	else if( ! isFinite( so ) ) {
		1; // Inifinity: all are opened.
	}
	else if( (so >= 0) && (so < heads.length) ) {
		heads.lt(so).click();
		heads.gt(so).click();
	} else {
		1; // this is an error, but lamely ignore it.
	}
	return this;
};
