$._columns_index = 0
$.fn.layout_columns = function (options)
{
    var opts = {
        count: 2,
        gap: 20,
        line_height: 22
    }
    
    $.extend(opts, options)
    
    if (opts.count == 1)
    {
        return
    }
    
    var best_split = null
    
    var find_split_node = function (node, H)
    {
        if (node.nodeType == 1)
        {
            var pos = $(node).offset()
            var height = $(node).height()
            
            if (pos.top < H && pos.top + height > H)
            {
                best_split = node
                
                for (var i=0; i<node.childNodes.length; i++)
                {
                    find_split_node(node.childNodes[i], H)
                }
            }
        }
    }
    
    var split_node = function (node, left_col, right_col, H)
    {
        var curnode = node.lastChild
        
        while (curnode)
        {
            if (curnode.nodeType == 3)
            {
                var text = document.createTextNode()
                var words = curnode.nodeValue.split(' ')
                
                if (words.length > 0)
                {
                    var i = 1
                    
                    while ((left_col.offset().top + left_col.height()) > H)
                    {
                        text.nodeValue = words.slice(words.length - i - 1, words.length).join(' ')
                        curnode.nodeValue = words.slice(0, words.length - i - 1).join(' ')
                        
                        i++
                        
                        if (curnode.nodeValue == '')
                        {
                            break
                        }
                    }
                    
                    text.nodeValue += ' '
                    right_col.prepend(text)
                    
                    if (curnode.nodeValue == '')
                    {
                        curnode.parentNode.removeChild(curnode)
                    }
                    else
                    {
                        break
                    }
                }
            }
            else
            {
                right_col.prepend(curnode)
            }
            
            curnode = node.lastChild
        }
    }
    
    return this.each(function ()
    {
        best_split = null
        var elm = $(this)
        var total_width = elm.width()
        var column_width = (total_width - ((opts.count-1) * opts.gap)) / opts.count
        
        var current_col = E('div').css({
            width: column_width+'px',
            marginRight: opts.gap+'px',
            'float': 'left'
        }).attr('id', 'column-'+$._columns_index)
        
        $._columns_index++
        
        current_col.append(elm.html())
        elm.empty().append(current_col)
        
        var column_height = current_col.height() / opts.count
        var col_num = 1
        var H = current_col.offset().top+column_height
        
        while (col_num < opts.count)
        {
            find_split_node(current_col.get(0), H)
            
            if (!best_split)
            {
                break
            }
            
            var next_col = $(current_col.get(0).cloneNode(false))
            next_col.attr('id', 'column-'+$._columns_index)
            $._columns_index++
            col_num++
            
            if (opts.count <= col_num)
            {
                next_col.css({
                    marginRight: 0
                })
            }
            
            elm.append(next_col)
            
            if (best_split.id != current_col.attr('id'))
            {
                var cursib = best_split
                
                while (cursib && cursib.nextSibling)
                {
                    next_col.append(cursib.nextSibling)
                    cursib = cursib.nextSibling
                }
            }
            
            split_node(best_split, current_col, next_col, H)
            
            current_col = next_col
        }
    })
}
