// version 1.4.0
// http://welcome.totheinter.net/columnizer-jquery-plugin/
// created by: Adam Wulf adam.wulf@gmail.com

(function($){

 $.fn.columnize = function(options) {
	var defaults = {
		height: 150,
		columnCount: 10000,
		parent: $(this).parent()
	};
	var options = $.extend(defaults, options);

	$("> p:last-child", this).append("<span style='float: right'>❖</span>");
	
	var h2 = $("h2", this);
	
	h2.each(function() {
		var el = $(this);
		var offset = el.position();
		var bottom = offset.top + el.outerHeight(true);
		
		if (bottom % options.height < 24) {
			el.addClass("threeLine");
		} else if ((offset.top % options.height) > (bottom % options.height)) {
			el.addClass("twoLine");
		}
	})

	var sourceDiv = $(document.createElement("div"));
	var maxHeight = $(this).height();
	
	sourceDiv.height(options.height)
		.css("overflow", "hidden")
		.css("float", "left")
		.css("text-align", "justify");
	
	$(this).appendTo(sourceDiv)
		.css("position", "relative");
	
	var heightProcessed = 0;
	var columnCount = 0;
	
	while (heightProcessed < maxHeight) {
		var currentDiv = $(sourceDiv.clone(true));
		
		$("div", currentDiv).css("top", -heightProcessed + "px");
		currentDiv.appendTo(options.parent);
		
		heightProcessed += options.height;
		columnCount += 1;
	}

	return $(this);
 };
})(jQuery);
