/*
 * Toggler: Simple show/hider.
 * Copyright (c) 2007-2010 Isos Media Ltd.
 *
 * Options object:
 * 	head:	    header spec
 * 	body:	    body spec, we'll use $header.next() to choose which one
 * 	open:	    CSS class to add to headers for open bodies
 * 	closed:	    CSS class to add to headers for closed bodies
 *	hover:      CSS class to add to headers when hovering
 *	speed:      how fast should things open/close?
 *	persistent: store open/closed state in a cookie if true, default is true
 *	accordion:  act like an accordion if this is set, this keeps only one panel
 *	            open at a time, the value is a spec we use to find other bodies.
 *
 * If you use "persistent: true" (the default) then you'll want 'id'
 * attributes on your 'head' elements and you should read IsosToggler in
 * the wiki. We use the 'T' cookie to maintain our state; this cookie
 * holds a !-delimited list of 'id's (sans 'tgl' prefixes) and their
 * open/closed states.
 *
 * The 'accordion' value is usually the same as the 'body' value if it
 * is set at all. You can have all panels closed in accordion mode but
 * at most one open.
 */
(function($) {
	$.fn.toggler = function(options) {
		options = $.extend({ }, $.fn.toggler.defaults, options || { });
		return this.each(function() {
			var $head  = $(this);
			var $body  = $head.next(options.body);
			var headId = ($head.attr('id').substring(3) || '');

			//
			// Initialize everything.
			//
			if(!headId)
				options.persistent = false;
			if(options.persistent) {
				var cookie = ($.cookie('T') || '');
				if(cookie.indexOf('!+' + headId + '!') >= 0) {
					$body.show();
				}
				else if(cookie.indexOf('!-' + headId + '!') >= 0) {
					$body.hide();
				}
			}
			$head.addClass($body.is(':hidden') ? options.closed : options.open);

			//
			// And bind our actions.
			//
			$head.bind('click', function() {
				var cookie = null;
				var parts  = { };
				if(options.persistent) {
					//
					// Don't forget to skip the leading and trailing commas.
					//
					cookie = ($.cookie('T') || '');
					var a  = cookie.split('!');
					for(var i = 1; i < a.length - 1; ++i) {
						var c = a[i].charAt(0);
						if(c == '+') {
							parts[a[i].slice(1)] = '+';
						}
						else if(c == '-') {
							parts[a[i].slice(1)] = '-';
						}
						else {
							parts[a[i]] = '+';
						}
					}
				}
				if($body.is(':hidden')) {
					if(options.accordion) {
						//
						// No feedback from the click handlers this way.
						//
						var $others = $(options.accordion).not(':hidden');
						$others.slideToggle(options.speed)
							.prev(options.head)
							.removeClass(options.open)
							.addClass(options.closed);
						if(options.persistent) {
							$others.prev(options.head).each(function() {
								parts[this.id.substring(3)] = '-';
							});
						}
					}
					$head.removeClass(options.closed).addClass(options.open);
					if(options.persistent)
						parts[headId] = '+';
				}
				else {
					$head.removeClass(options.open).addClass(options.closed);
					if(options.persistent)
						parts[headId] = '-';
				}
				if(options.persistent) {
					cookie = '';
					for(var id in parts)
						cookie = cookie + '!' + parts[id] + id;
					cookie = cookie == '' ? null : cookie + '!';
					$.cookie('T', cookie, { expires: 180, path: '/' });
				}
				$body.slideToggle(options.speed);
			});
			if(options.hover) {
				$head.hover(
					function() { $(this).addClass(   options.hover) },
					function() { $(this).removeClass(options.hover) }
				);
			}
		});
	};

	$.fn.toggler.defaults = {
		accordion:  false,
		body:       null,
		closed:      null,
		head:       null,
		hover:      null,
		open:        null,
		persistent: true,
		speed:      'fast'
	};
})(jQuery);

// $HeadURL: http://localhost/svn/isos/tmpl/static/jquery/toggler.js $
// $Id: toggler.js 5862 2010-07-28 16:47:52Z mu $
