/**
 * columns.jquery.js
 * @author Jason Desrosiers
 */
(function ($)
{
        $.fn.toColumns = function (num_columns)
        {
                this.each(function (ndx, container)
                {
                        // If the container is hidden Then make if visible so we can determine sizes
                        var is_hidden = $(container).css('display') == 'none';
                        if (is_hidden) { $(container).show(); }

                        // Determine the hieght and width of the columns
                        var column_height = Math.ceil(container.offsetHeight / num_columns);
                        var column_width = container.offsetWidth;

                        // The column height can not be larger than the largest element
                        $(container).children().each(function (ndx, element)
                        {
                            if (element.offsetHeight > column_height) { column_height = element.offsetHeight; }
                        });

                        // Brute force: loop until we find a column height that works
                        var current_column_height = 0;
                        var column_count;
                        do
                        {
                                column_height++;
                                column_count = 1;
                                $(container).children().each(function (ndx, element)
                                {
                                        current_column_height += element.offsetHeight;
                                        if (current_column_height > column_height)
                                        {
                                                column_count++;
                                                current_column_height = element.offsetHeight;
                                        }
                                });
                        } while (column_count > num_columns);

                        // Transform to columns
                        var on_column = 0;
                        var max_column_height = 0;
                        current_column_height = 0;
                        $(container).children().each(function (ndx, element)
                        {
                                $(element).css('position', 'absolute');
                                var element_height = element.offsetHeight;
                                if (current_column_height + element_height > column_height)
                                {
                                        on_column++;
                                        if (current_column_height > max_column_height) { max_column_height = current_column_height; }
                                        current_column_height = 0;
                                }

                                $(element).css('margin-left', column_width * on_column + 'px');
                                $(element).css('margin-top', current_column_height + 'px');
                                current_column_height += element.offsetHeight;
                        });

                        // Set the new width and height of the container
                        $(container).css('width', column_width * (on_column + 1) + 'px');
                        $(container).css('height', max_column_height + 'px');

                        // If the container was hidden, Then hide it again
                        if (is_hidden) { $(container).hide(); }
                });

                return this;
        };
})(jQuery);

