$(function(){
    top_margin = $("#top-menu ul#topnav ul").css("margin-top");
    $("#top-menu ul#topnav ul").css({marginTop: "-30000px", display: "block"});
    $("#top-menu ul#topnav")
        .addClass('sf-menu')
        .supersubs(
            {
                minWidth:    8,   // minimum width of sub-menus in em units
                maxWidth:    30,   // maximum width of sub-menus in em units
                extraWidth:  1     // extra width can ensure lines don't sometimes turn over
                                   // due to slight rounding differences and font-family
            }
        )
        .superfish({ delay:400, autoArrows:true, dropShadows:false, animation:{opacity:'show', height:'show'} })
        ;
    
    $("#top-menu ul#topnav ul").css("margin-top", top_margin);

    alignTopMenu();
    
    if($("#news-section").length) {
        resizeNewsSection();
    }

});

function alignTopMenu()
{
    var width = $("#header").width() - $("#language").width() - 10;
    var count = $("#top-menu>ul#topnav>li").length;
    var total = 0;
    $("#top-menu>ul#topnav>li:last").css({ paddingRight: 0, marginRight: 0});

    $("#top-menu>ul#topnav>li").each(function(){
        total += $(this).width()
            + parseInt($(this).css("margin-left"))
            + parseInt($(this).css("margin-right"))
        ;
    });

    var extra = Math.max(0, Math.floor((width - total)/ (count - 1)) - 2);
    
    $("#top-menu>ul#topnav>li:lt("+(count-1)+")").css("padding-right", extra+"px");
}

function resizeNewsSection(){
}
