﻿(function ($) {
    $.fn.columnizeList = function (settings) {
        settings = $.extend({
            cols: 3,
            constrainWidth: 0
        }, settings);
        // var type=this.getNodeType();
        var container = this;
        if (container.length == 0) { return; }
        var prevColNum = 10000; // Start high to avoid appending to the wrong column
        var size = $('li', this).size();
        var percol = Math.ceil(size / settings.cols);
        var tag = container[0].tagName.toLowerCase();
        var classN = container[0].className;
        var colwidth = Math.floor($(container).width() / settings.cols);
        var maxheight = 0;
        var rows = 4;
        var nowCol = 1;
        var itemNum = -1;
        // Prevent stomping on existing ids with pseudo-random string
        var rand = Math.floor(Math.random().toPrecision(6) * 10e6);
        $('<ul id="container' + rand + '" class="' + classN + '"></ul>').css({ width: $(container).width() + 'px' }).insertBefore(container);
        $('li', this).each(function (i) {
            itemNum++;
            var currentColNum = nowCol;
            var myMod = itemNum % rows;
            if (itemNum > 1 && myMod < 1) {
                nowCol++;
                currentColNum = nowCol;
            }
            if (prevColNum != currentColNum) {
                if ($("#col" + rand + "-" + prevColNum).height() > maxheight) { maxheight = $("#col" + rand + "-" + prevColNum).height(); }
                $("#container" + rand).append('<li class="list-column-processed"><' + tag + ' id="col' + rand + '-' + currentColNum + '"></' + tag + '></li>');
            }
            $(this).attr("value", i + 1).appendTo("#col" + rand + '-' + currentColNum);
            prevColNum = currentColNum;
            /*            var currentColNum = Math.floor(i / percol);
            if (prevColNum != currentColNum) {
            if ($("#col" + rand + "-" + prevColNum).height() > maxheight) { maxheight = $("#col" + rand + "-" + prevColNum).height(); }
            $("#container" + rand).append('<li class="list-column-processed"><' + tag + ' id="col' + rand + '-' + currentColNum + '"></' + tag + '></li>');
            }
            $(this).attr("value", i + 1).appendTo("#col" + rand + '-' + currentColNum);
            prevColNum = currentColNum;*/
        });
        $("li.list-column-processed").css({
            'float': 'left',
            'list-style': 'none',
            'margin': 0,
            'padding': 0
        });
        if (settings.constrainWidth) {
            $(".list-column-processed").css({ 'width': colwidth + "px" });
        };
        $("#container" + rand).after('<div style="clear: both;"></div>');
        $("#container" + rand + " " + tag).height(maxheight);
        // Add CSS to columns
        this.remove();
        return this;
    };
})(jQuery);
