
    var hoveringMenu = null;
    var hoveringParent = null;
    
    function checkHover(parentID, menuID)
    {
        if((hoveringMenu != menuID) && (hoveringParent != parentID))
        {
            $(menuID).hide();
        }
    }
    
    function makeSubmenu(parentID, menuID, align)
    {
        var pos = $(parentID).offset();
        height = $('#block-menu-73').height();
        $(menuID).css('top', pos.top + height - 4);
        
        // position the menu in reference to the parent
        if(align == 'left')
        {
            $(menuID).css('left', pos.left - 3);
        }
        else
        {
            $(menuID).css('left', pos.left + $(parentID).width() - $(menuID).width() + 8);
            $(menuID).css('text-align', 'right');
        } 
        
        // this prevents the parent link from going anywhere
        /*
        $(parentID).click(function()
        {
            return false;
        });
        */
        
        // following logic is to keep the menu open whether
        // the mouse is hovering over the parent link
        // or the menu itself
        
        $(parentID).hover(function()
        {
            hoveringParent = parentID;
            checkHover(parentID, menuID);
            $(menuID).fadeIn('fast');
        },
        function()
        {
            hoveringParent = null;
            setTimeout('checkHover("' + parentID + '", "' + menuID + '")', 200);
        });
        
        $(menuID).hover(function()
        {
            hoveringMenu = menuID;
            checkHover(parentID, menuID);
            $(menuID).fadeIn('fast');
        },
        function()
        {
            hoveringMenu = null;
            setTimeout('checkHover("' + parentID + '", "' + menuID + '")', 200);
        });
    }
    
    $(document).ready(function()
    {
        makeSubmenu('#mid-95', '.block-AdultServicesBranch', 'left');
        makeSubmenu('#mid-96', '.block-AdolescentServicesBranch', 'left');
        makeSubmenu('#mid-118', '.block-ChildrensServicesBranch', 'left');
        makeSubmenu('#mid-180', '.block-SuccessStoriesBranch', 'left');
        makeSubmenu('#mid-119', '.block-ContributeBranch', 'right');
        makeSubmenu('#mid-120', '.block-ClientAlumniBranch', 'right');
        makeSubmenu('#mid-121', '.block-NewsEventsBranch', 'right');
        makeSubmenu('#mid-122', '.block-AboutUsBranch', 'right');

        // this sets the class to 'active' of the Main Nav link
        // corresponding to current document
        $('#block-menu-73').find('a').each(function()
        {
            href = this.href;
            if(href.match(/.html$/))
            {
                href = href.substr(0, href.length - 5);
            }
            
            // skip root ... better way to do this?
            if(href.charAt(href.length - 1) == '/')
            {
                return true;
            }
            
            if(document.location.href.substring(0, href.length) == href)
            {
                $(this).addClass('active');
                
                return false;
            }
            
            return true;
        });
    });
