/*
 * Supersubs v0.2b - jQuery plugin
 * Copyright (c) 2008 Joel Birch
 *
 ******************** MODIFIED ************************
 */

;(function($){
	$.fn.supersubs = function(options){
		var opts = $.extend({}, $.fn.supersubs.defaults, options);
		return this.each(function() {
			var $$ = $(this);
			var o = $.meta ? $.extend({}, opts, $$.data()) : opts;

			$ULs = $$.find('ul');
			$ULs.each(function(i) {	
				var $ul = $ULs.eq(i);
				var $LIs = $ul.children();
				var $As = $LIs.children('a');
				var liFloat = $LIs.css('white-space','nowrap').css('float');
				var emWidth = $ul.add($LIs).add($As).css({
					'float' : 'none',
					'width'	: 'auto'
				})
				.end().end()[0].clientWidth;
				
				emWidth += o.extraWidth;
				if (emWidth > o.maxWidth)		{ emWidth = o.maxWidth; }
				else if (emWidth < o.minWidth)	{ emWidth = o.minWidth; }
				
				$ul.css('width',emWidth+'px');
				$LIs.css({
					'float' : liFloat,
					'width' : '100%',
					'white-space' : 'normal'
				})
				.each(function(){
					var $childUl = $('>ul',this);
					var offsetDirection = $childUl.css('left')!==undefined ? 'left' : 'right';
					$childUl.css(offsetDirection,(emWidth - 1) + 'px');
				});
			});
			
		});
	};

	$.fn.supersubs.defaults = {
		minWidth		: 9,
		maxWidth		: 25,
		extraWidth		: 0
	};
	
})(jQuery);
