/*
 * jQuery Definition List Toggle
 * version: 1.0 (2010/02/10)
 * @requires jQuery v1.3.2 or later
 *
 * Examples at: http://ranjandatta.com/jquery/dlToggle/
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 */
(function($) {

/**
 * dlToggle() converts a definition list to a smooth toggle / accordian
 * 
 * dlToggle() accepts a single argument which is an options object.
 * The following attributes are supported in the options: 
 * 
 *  showSpeed:	number (speed at which showing of <dd> is animated)
 *  
 *  hideSpeed:	number (speed at which hiding of <dd> is animated)
 *  
 *  showEasing:	string (Animation Easing while showing <dd> if using easing plugin)
 *  
 *  hideEasing:	string (Animation Easing while hiding <dd> if using easing plugin)
 *  
 *  toggleAll:	boolean (hide all visible <dd>s before showing chosen <dd>)
 * 
 * @name dlToggle
 * @type jQuery
 * @param options  object literal containing options which control the dlToggle functionaltiy
 * @cat Plugins/Widgets
 * @return jQuery
 */
	$.fn.dlToggle = function(settings) {
		var opt = $.extend({}, $.fn.dlToggle.options, settings);

		return this.each(function() {
			var $dl = $(this);
			if(!$dl.is('dl')) {return;}
			$dt = $dl.find('dt');
			$dt.css('cursor', 'pointer').each(function(){
				var height,
				$dt = $(this),
				$dd = $dt.next();
				
				if($dd.is('dd')) {
					height = $dd.height();
					$dd.hide().css({height : 0});
				} else { return; }
				
				$dt.click(function(){
					$(this).toggleClass('active');
					if(!$dd.is(':visible')) {
						if(opt.toggleAll) {
							$dl.find('dd:visible').animate({height : 0}, {duration : opt.hideSpeed, easing : opt.hideEasing, complete: function(){
								var $active = $(this)
								$active.prev().toggleClass('active');
								$active.hide();
							}});
						} 
						$dd.show().animate({height : height}, {duration : opt.showSpeed, easing : opt.showEasing});
					} else {
						$dd.animate({height : 0}, {duration : opt.hideSpeed, easing : opt.hideEasing, complete: function(){
							$dd.hide();
						}});
					}
				});
			});
		});
	};

	$.fn.dlToggle.options = {
		showSpeed: 200,
		hideSpeed: 200,
		toggleAll: true
	};
})(jQuery);
