// jslint configuration
/*jslint browser: true */
/*global $ window */


function init_submenus() {
    $("ul.menu ul.children").parent().hover(
        function () {
            $('ul.children', this).fadeIn("fast");
        },
        function () {
            $('ul.children', this).fadeOut("fast");
        }
    );
}


function set_horizontal_menu_width() {
    var number_of_menu_items, available_width, width_per_item;
    available_width = 750 - 10;  /* Small safety margin on the right */
    number_of_menu_items = $("#top-menu > li").length;
    width_per_item = available_width / number_of_menu_items;
    $("#top-menu > li").width(width_per_item);
    $("#top-menu ul.children").width(width_per_item - 24);
}


function fix_breadcrumbs() {
    var breadcrumbs, new_breadcrumbs;
    if ($("#breadcrumbs a").length > 1) {
        // Zap second <a> element.
        $("#breadcrumbs a:eq(1)").remove();
        // Collect the breadcrumbs and the last element.
        $("#breadcrumbs a").each(function () {
            $(this).append(' &rarr; ');
        });
    }

}


function expand_height() {
    var available_height;
    available_height = $(window).height() - $("#logo").outerHeight() -
        $("#home-link").outerHeight() - $("#footer").outerHeight();
    if ($("#main").height() < available_height) {
        $("#main").height(available_height);
    }
}

$(document).ready(init_submenus);
$(document).ready(set_horizontal_menu_width);
$(document).ready(fix_breadcrumbs);
$(document).ready(expand_height);

