/*
 * 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.
 */
$.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 = {
	open:       '',
	closed:     '',
	speed:      'fast',
	persistent: true
};
// $HeadURL: http://192.168.1.66/svn/isos/tmpl/static/jquery/toggler.js $
// $Id: toggler.js 5325 2010-02-23 21:42:55Z mu $
