/*-------------------------------------------
	jquery-list-columns Version 0.1
	Michigan State University
	Virtual University Design and Technology
	Creator:  Nathan Lounds
	Project Page: http://code.google.com/p/jquery-list-columns/
	Dependencies:  jquery.js (http://jquery.com)
	Copyright (c) 2010 Michigan State University Board of Trustees
	MIT License: http://www.opensource.org/licenses/mit-license.php
--------------------------------------------*/
(function($) {
	jQuery.fn.list_columns = function(options) {
		settings = jQuery.extend({
			 width: "",
			 selector: ".list_columns",
			 percentage: 48,
			 embedCSS: true,
			 minimum: 3
		}, options);
		if(settings.embedCSS===true) {
			$("<style type='text/css'>"+settings.selector+" li { width:"+settings.percentage+"%; float:left; clear:left; } "+settings.selector+" li.right { float:right; clear:right; position:relative; } span.clearfix { display:block; clear:both; }</style>").appendTo("head");
		}
		$(this).each(function() {
			if(settings.width.length>0) {
				$(this).css("width",settings.width);
			}
			var li = $(this).children("li"), half = Math.round(parseFloat($(li).length/2));
			if(settings.minimum===0 || $(li).length >= settings.minimum) {
				$(li).each(function(i) {
					if(i >= half) { $(this).addClass("right"); }
				});	
				var elem1_Offset = $(this).children("li:first").offset();
				var elem2_Offset, diff, tmp_height = 0;
				$(this).children("li.right").each(function() {
					diff = elem1_Offset.top - $(this).offset().top + tmp_height;
					$(this).css("margin-top", diff+"px");
					tmp_height += $(this).height();
				});
			}
			$(this).after('<span class="clearfix" />');
		});
	}
})(jQuery);
