(function( $ ){
	
	$.fn.outer = function() {
		return $( $('<div></div>').html(this.clone()) ).html();
	}
	
	$.fn.zColumns = function(options)
	{
		var settings = {
			columns         	: 2,
			columnWidth			: 300,
			columnSeparator		: 0
		};	
		
		if (options)
		{
			$.extend(settings, options);	
		}

		return this.each(function(){
			
			if ($.browser.msie)
			{
			
				var width 	= $(this).width() / settings.columns;
				var pointer	= $('<div />').prependTo(this);
				var peices	= new Array();
				var height	= 0;
				var dWidth	= $(this).width();
				
				// This is to get an accurate width
				$(this).css('width', settings.columnWidth);
				
				while(pointer)
				{
					if (pointer.next().length)
					{
						pointer = pointer.next();	
						
						peices.push(pointer.outer());
						height += pointer.height();
						
					}
					else
					{
						break;	
					}
				}
				
				$(this).empty();
				
				var div = $('<div />').css({
					width 			: settings.columnWidth,
					marginRight		: settings.columnSeparator,
					float			: 'left'
				}).addClass('zColumn');			
					
				var separator 		= peices.length/settings.columns;
				var heightSeparator	= height / settings.columns;
				var offset			= 0;
				
				$('<div />').css({
					position		: 'absolute',
					width			: dWidth,
					height			: heightSeparator
				}).appendTo(this);;
				
				
				for(i=1; i<settings.columns+1; i++)
				{
					var clone = div.clone();
					
					clone.appendTo(this);
					
					if (i==settings.columns)
					{
						clone.css('margin-right', 0);	
					}
					
					for(j in peices)
					{
						if (clone.height() >= heightSeparator - offset)
						{
							offset = clone.height() - heightSeparator;
							break;
						}
						else
						{
							clone.append(peices[j]);
							peices[j] = false;
						}
					}
					
				}
				
				$('<div style="clear:both;" />').appendTo(this);
				
				// Set the width back
				$(this).css('width', dWidth);
			
			}
			
			
		});
	}
	
})( jQuery );
