$(function(){

// Equalize #navigation and #main containers
    (function () {
        var $navigation = $('#navigation');
        var $main = $('#main');
        if ($navigation.size() == 0 || $main.size() == 0) return;
        
        var $lastSection = $main.find('.section:last');
        function reviewHeight() {
            $navigation.css('min-height', 0);
            $lastSection.css('min-height', 0);
            setTimeout(function () { // setTimeout gives it a chance to render
                var height = Math.max($navigation.outerHeight(), $main.outerHeight());
                $navigation.css('min-height', height - ($navigation.outerHeight()-$navigation.height()));
                $lastSection.css('min-height', height - ($lastSection.offset().top - $main.offset().top) - ($lastSection.outerHeight()-$lastSection.height()));
            }, 0);
        };
        reviewHeight();
        $('.header-images img').load(reviewHeight);
    })();
    
// #categories-nav
    $('#categories-nav a').append('<span class="hover">');
    $('#categories-nav li').each(function (i) {
        if (i == 0) $(this).addClass('first');
        $(this).addClass('t'+(i+1));
    });
    
// #articles-nav
    $('#articles-nav li:first').addClass('first');
    
// Transform .menu-entries
    $('.menu-entries h4').each(function () {
        var $div = $('<div class="menu-entry">');
        var $h4 = $(this), $e = $().add($h4), $c = $h4.next();
        while ($c.size() > 0 && $c[0].nodeName.toUpperCase() != 'H4') {
            $e = $e.add($c);
            $c = $c.next();
        }
        $h4.after($div);
        $div.append($e);
    });

// Balance .menu-entries
    (function () {
        if ($('.menu-entries > .col').size() == 1) {
            var $col = $('<div class="col">');
            $('.menu-entries').append($col);
            var $mes = $('.menu-entries .first > .menu-entry');
            var n = Math.floor($mes.size()/2);
            while (n > 0) {
                $col.prepend($mes.eq(-1));
                $mes = $mes.not($mes.eq(-1));
                n = n -1;
            }
        }
    })();
});

