function setupTopStories() {
    $list = $('#top-stories-items');

    $list.find('img').each(function () {
        $(this).closest('li').css('background',
            'url("' + $(this).attr('src') + '") no-repeat center center');
        $(this).remove().appendTo('body');
        $(this).css({
            display: 'block',
            position: 'absolute',
            width: '1px',
            height: '1px',
            top: '0px',
            left: '0px'
        });
    });

    $list.before('<div id="top-stories-pager"></div>')
        .cycle({
            fx: 'scrollLeft',
            timeout: 7000,
            pager: '#top-stories-pager',
            pauseOnPagerHover: true
        });

    // Don't change the slide if the user is trying to click.
    $list.find('a').mouseover(function () {
            $('#top-stories-items').cycle('pause');
        })
        .mouseout(function () {
            $('#top-stories-items').cycle('resume');
        });
}

function setupCategoriesNav() {
    var $navbar = $('#categories-nav');
    var anyOpen = false;

    $navbar.bind('showing-menu', function (evnt, $title) {
        $('#categories-nav > ul > li').each(function () {
            if($(this).not($title).length) {
                $(this).children('ul.children').hide();
            }
        });
    });

    $('#categories-nav > ul > li').each(function () {
        var $title = $(this);
        var $menu = $title.children('ul.children');
        if($menu.length == 0) {
            return;
        }

        var hovered = {title: false, menu: false};

        $title.mouseover(function () {
            hovered.title = true;

            // Show immediately if switching from one to another; delay otherwise.
            setTimeout(function () {
                if(!hovered.title) {
                    return;
                }

                $menu.css({
                    top: $navbar.offset().top + $navbar.outerHeight() -
                        $navbar.offsetParent().offset().top - 1,
                    left: $title.offset().left -
                        $title.offsetParent().offset().left - 1
                });

                $menu.fadeIn('fast');
                anyOpen = true;
                $navbar.trigger('showing-menu', $title);
            }, anyOpen ? 0 : 200);
        });

        $menu.mouseover(function () {
            hovered.menu = true;
        });

        function setMenuVisibility() {
            setTimeout(function () {
                if(!(hovered.title || hovered.menu)) {
                    if($menu.is(':visible')) {
                        // If a visible menu times out, no menus are open.
                        anyOpen = false;
                        $menu.fadeOut();
                    }
                }
            }, 1000);
        }

        $(this).mouseout(function () {
            hovered.title = false;
            setMenuVisibility();
        });

        $menu.mouseout(function () {
            hovered.menu = false;
            setMenuVisibility();
        });
    });
}
