Ext.onReady(function(){
    Ext.select('.header>ul>li>ul>li>ul').each(function(a,b,c){
        var d = a.select('>li>ul');

        if(d.getCount() <= 1){return;}

        var cols = [{
            count: 0,
            els: [],
            width: 0,
            height: 0,
            elsh: []
        },{
            count: 0,
            els: [],
            width: 0,
            height: 0,
            elsh: []
        }];

        d.each(function(e,f,g){
            var pos = 0;

            if(g > 0){
                pos = 1;
            }

            var w = 0;
            var y = 0;
            e.parent('li').select('span').each(function(li){
                if(w < li.getWidth()){
                    w = li.getWidth();
                }
                y += li.getHeight();
            });

            cols[pos].count += e.select('li').getCount();
            if(cols[pos].width < w){
                cols[pos].width = w;
            }
            cols[pos].height += y;
            var x = cols[pos].els.push(e.dom);
            cols[pos].elsh[x] = y;
        });

        a.setStyle({
            width: (cols[0].width + cols[1].width + (a.getPadding('lr') * 2)) + 'px',
            height: (Math.max(cols[0].height, cols[1].height) + (a.getPadding('b') * 2)) + 'px'
        });

        var lo = a.getPadding('l');
        Ext.each(cols, function(h,i){
            var to = a.getPadding('t');
            if(i > 0){
                lo += cols[i-1].width + a.getPadding('lr');
            }
            Ext.each(h.els, function(j,k){
                if(k > 0){
                    to += cols[i].elsh[k] + a.getPadding('t');
                }

                Ext.fly(j).parent('li').setStyle({
                    position: 'absolute',
                    top: to + 'px',
                    left: lo + 'px'
                });
            });
        });
    });
});
