/**
	izilla_navspacer v1.1
	
	use with div or something containing ul.
	for example
	<div id="the_nav">
		<ul>
			<li><a href=...><span>text</span></a></li>
			<li><a href=...><span>text</span></a>
				<ul><li><a href=...>subnav</a></li></ul>
			</li>
		</ul>
	</div>
	
	and run as
	$('#the_nav').izilla_navspacer({fontsize: 15});
	
	the lis must all contain anchors as direct children; the anchors might have spans in them.
	
	any subnav, set width & height to zero when not visible
	
	arguments:
		fontsize: font size, unit is pixels
**/
(function($) {
	$.fn.izilla_navspacer = function(config) {
		config = $.extend($.fn.izilla_navspacer.defaults, config);
		
		var ul = $(this).find('ul:first');
		if (ul.length) {
			var width = $(this).width();
			var lis = ul.children('li');
			var as = lis.children('a');
			
			var lis_width = 0;
			lis.each(function() {
				lis_width += $(this).width();
			});

			var ratio = width / lis_width;
			var fraction_total = 0;
			lis.each(function() {
				var current = $(this).width();
				var target = current * ratio;
				var increase = target - current;
				var increaseInt = Math.floor(increase);
				fraction_total += (increase - increaseInt);

				var anchor = $(this).children('a');

				anchor.width(anchor.width() + increaseInt);
			});

			// after counting up all the fractional bits we're missing, add that to first one
			$(as[0]).width($(as[0]).width() + Math.floor(fraction_total));
			
			// now adjust heights
			/*var max_ = null;
			var fontSize;
			as.each(function() {
				var height = $(this).height();
				if (max_ == null || height > max_)
					max_ = height;
			});
			var maxem = max_ / config.fontsize;
			maxem += "em";
			as.each(function() {
				$(this).css('min-height',maxem);
				if ($('html').hasClass('ie6'))
						$(this).css('height',maxem);
			});*/

		}
	};

	$.fn.izilla_navspacer.defaults = {
		fontsize: 12
	};
})(jQuery);
