$(document).ready(function(){
    // Init menus
    bbMenuInit();
});

// Fix position of flyout menu if it overlaps the right edge of the content well
bbMenuPosFix = function($curTab)
{
    //get width of current tab and subsequent tabs (distance to edge of body)
    var tabOffset = $('#outer').offset();
    var outerOffset = $($curTab).offset();
    var innerWidth = $('#inner').outerWidth();
    var menuWidth = $('.submenu',$($curTab)).outerWidth();
    var overlap = (innerWidth + (tabOffset.left - outerOffset.left));
    if ( overlap > 0)
    {
        $('.submenu',$($curTab)).css('margin-left', '-'+(menuWidth - overlap) + 'px');
    }
};

// Init top and left menus
bbMenuInit = function()
{
    // Flyout menus
    var flyouts = $('li.bb-flyout');
    flyouts.each(function(){
        $(this).mouseover(function(){
            $('div.submenu',this).removeClass('hide');
            $('div:first',this).addClass('active');
            bbMenuPosFix($(this));
        }).mouseout(function(){
            $('div.submenu',this).addClass('hide');
            $('div:first',this).removeClass('active');
        });

        $('.submenu .top h3', this).mouseover(function(){
            $('#header .sub').each(function(){$(this).addClass('hide')});
            $('#header .submenu .top h3 a').each(function(){$(this).removeClass('active')});
            $('#sub-' + $(this).attr('id').substring($(this).attr('id').indexOf('-')+1)).removeClass('hide');
            $('a',this).addClass('active');
            bbMenuPosFix($(this).parents('li.bb-flyout'));
        });
    });

    // Left Nav Sliders
    $('#nav-left .slide h4').click(function() {
        $('#nav-left h4').each(function(){
            $(this).removeClass('active');
            if ( $(this).parent().hasClass('slide') )
            {
                $(this).next().slideUp();
            }
        });
        $(this).next().animate({height: 'toggle', opacity: 'linear'}, 200, 'linear');
        $(this).addClass('active');
        return false;
    }).next().hide();

    // Go ahead and expand the active category on page load
    $('#nav-left .slide h4.active').next().animate({height: 'toggle', opacity: 'linear'}, 200, 'linear');

    // Left Nav Link Behaviors
    $('#nav-left .link h4').click(function(){
        $('#nav-left h4').each(function(){
            $(this).removeClass('active');
        });
        
        $('#nav-left .slide h4').each(function(){
            $(this).next().slideUp();
        });

        $(this).addClass('active');
        $('a',this).blur();

        return false;
    });

/*
    $('#nav-left ul.sublist a').click(function(){
        $('#nav-left ul.sublist li').each(function(){
            $(this).removeClass('active');
        });

        $(this).parent().addClass('active');
        $(this).blur();

        return false;
    });
*/
    // Left Nav Hover Behaviors
    $('#nav-left h4').hover(
        function() {
            $(this).addClass('hover');
            return false;
        },
        function() {
            $(this).removeClass('hover');
            return false;
        }
    );
};

