$(function(){
    /**
     * Splits an unordered list into columns
     * Items which themselves have children will not be split
     */
    var columnizeSitemap = function() {
        // Amount of columns to create
        var columnCount = 4;
        
        // Items that make the columns longer than the column length multiplied by this will be moved to next column if possible
        var longColumn = 1.2;
        
        // Each block will have its children split into columns
        var blocks = $('#sitemap ul.sitemap ul.level2');
        
        blocks.each(function( blockIndex ){
            // Initialize current list as the first column in the columns array
            // New columns will be appended to this array
            var columns = [ $(this) ];
            
            // Get count of all items in current list, including children
            var allChildren = columns[0].find('li');
            var itemCount = allChildren.length;
            
            if( itemCount > 1 ) {
                // Get max length of each column
                var columnLength = Math.ceil( itemCount / columnCount );
                
                // Create the new columns
                for( i = 1; i < columnCount; i++ ) {
                    columns.push( $('<ul class="level2">') );
                }
                
                // Get top level items in list, NOT including their children
                var items = columns[0].find('> li');
                
                var totalIndex = 0; // Index including children
                var currentColumn = 0;
                
                // Loop through each top level item
                items.each( function( index, value ){
                    var currentColumnIndex = totalIndex - ( currentColumn * columnLength );
                    var item = $(this);
                    var itemChildren = item.find('li');
                    var itemChildrenCount = itemChildren.length;
                    var currentColumnLength = currentColumnIndex + itemChildrenCount;
                    
                    // Move item to next column if the current item makes the column extremely long and there are columns to spare
                    if( currentColumnLength / columnLength > longColumn && currentColumn < ( columnCount - 1 ) ) {
                        currentColumn++;
                    } 
                    
                    // All items belong to the first column by default
                    // Only move them if they belong to a new column
                    if( currentColumn > 0 ) {
                        item.detach();
                        columns[currentColumn].append( item );
                    }
                    
                    totalIndex += 1 + itemChildrenCount;

                    // Increase currentColumn if amount of items exceeded limit
                    if( totalIndex > ( ( currentColumn + 1 ) * columnLength ) && currentColumn < ( columnCount - 1 ) ) {
                        currentColumn++;
                    }
                });
    
                // Add new columns to DOM after column1
                columns[0].after( columns[1], columns[2], columns[3] )
            }
        });
    }

    columnizeSitemap();
});
