// Preload the menu images
$(window).bind('load', function(){
    var preload = [
        '/images/toggle_open.gif',
        '/images/toggle_closed.gif',
    ];
    $(document.createElement('img')).bind('load', function(){
        if(preload[0]) this.src = preload.shift();
    }).trigger('load');
});

// Build the LessonMenu navigation
function LessonsMenu() {
    var _showText = 'Show Description';
    var _showAllText = 'Expand All';
    var _hideText = 'Hide Description';
    var _hideAllText = 'Collapse All';

    $('.pageMenuTopLevelItemDesc').hide();
    $('.pageMenuTopLevel li.pageMenuItem').css({listStyle: 'none'}).prepend('<span class="bullet">&#8226;</span>');
    $('.pageMenuTopLevel li.hasPageMenuTopLevelItemDesc span').addClass('toggle').attr({title: _showText}).html('+');

    $('.toggle').click(function() {
        $(this).siblings('.pageMenuTopLevelItemDesc').toggle();
        if ($(this).html() == '+') {
            $(this).attr({ alt: _hideText }).html('-');
        } else {
            $(this).attr({ alt: _showText }).html('+');
        }
    });

    if ($('li.hasPageMenuTopLevelItemDesc').size()) {
        $('.pageMenuTopLevel').before('<p id="toggleAll"><a class="expand" href="#">'+_showAllText+'</a><a class="collapse" href="#">'+_hideAllText+'</a></p>');
        $('a.expand').click(function() {
            $('.pageMenuTopLevelItemDesc').show();
            $('.toggle').attr({ alt: _hideText }).html('-');
        }); 
        $('a.collapse').click(function() {
            $('.pageMenuTopLevelItemDesc').hide();
            $('.toggle').attr({ alt: _showText }).html('+');
        }); 
    }
}
