// JavaScript Document
jQuery.columns = function(container){
	
}
jQuery.fn.columns = function(options){
	var defaults = {
		columns: 2,
		space: 50,
		type: 'tag_p' // tag_p
	};
	var o = jQuery.extend(defaults, options);
	
	return this.each(function() {
		var e = $(this).html();
		var width = $(this).width();
		var columnwidth = (width - ((o.columns-1) * o.space)) / o.columns;
		
		switch(o.type){
			case 'tag_p':
				var novo;
				var nElements = $(this).children('p').length;
				var novo = document.createElement("div");
				
				for (i=0; i < o.columns; i++){
					if (i < o.columns - 1){
						$(novo).append('<div style="margin-right:' + o.space + 'px; width:'+ columnwidth +'px; position:relative; float:left">');
					} else {
						$(novo).append('<div style="width:'+ columnwidth + 'px; position:relative; float:left">');
					}
				}
				
				var qtd = Math.ceil(nElements / o.columns);
				var indiceDiv = 0;
				
				$(this).children('p').each(function(index){
					if (index != 0 && index % qtd == 0){
						indiceDiv++;
					}
					$(novo).children('div:eq('+ indiceDiv +')').append($(this))
				});
				$(this).html($(novo).html());
			break;
		}
	});
}
