var $j = jQuery;
$j.noConflict();

jQuery.fn.columnizeList = function(settings){  
    settings = jQuery.extend({  
        cols: 3,  
        width: '13',  
        unit: 'em'  
    }, settings);  
  
    var prevColNum = 0;  
    var size = $j('li',this).size();  
    var computedColHeight = 0;  
    var baseFontSize = parseFloat($j(this).css('font-size'));  
    $j('li',this).each(function(i) {  
        var currentColNum = Math.floor(((i)/size) * settings.cols);  
        $j(this).css('margin-left',(currentColNum*settings.width)+''+settings.unit);
        if(prevColNum != currentColNum) {
            $j(this).css('margin-top','-'+(computedColHeight/baseFontSize)+'em');
            computedColHeight = $j(this).height();
        } else {
            $j(this).css('margin-top','0');
            computedColHeight += $j(this).height();
        }
        prevColNum = currentColNum;
    });

    this.css('height',(size/settings.cols)*(parseFloat($j('li:first',this).height())/baseFontSize)+'em');
    this.after('<br style="clear: left;">');

    var onchange = function(e) {
        if(!e.originalTarget || e.originalTarget.tagName != 'LI') return true;
        var scope = this; // caution: closure
        setTimeout(function() {$j(scope).columnizeList(settings);}, 50);
    };

    this.one('DOMNodeInserted',onchange);
    this.one('DOMNodeRemoved',onchange);

    return this;
};
