﻿var HHSM = HHSM || {};

HHSM.initSubmenus = function() {
    $(function() {
        var $curSubmenu;

        var defaultSMWidths = {
            "sm-members": "9.1em",
            "sm-meets": "11.25em",
            "sm-coaches": "9.1em",
            "sm-admin": "11.25em",
            "sm-pub": "11.7em"
        };

        // for IE issues
        if ($.browser.msie && $.browser.version.substr(0, 1) == "6") {
            $.each($('div.submenu'), function(index, item) {
                var $item = $(item)
                $item.width(defaultSMWidths[item.id]);
                $item.children('ul').width(defaultSMWidths[item.id]);
            });
        }

        // This function addresses issues that affect the widths of 
        // drop-down menus with versions of IE prior to IE8.
        var adjustOptionWidth = function($submenu) {
            if (!$submenu.data('visited')) {
                var w, iw, delta, rpad, boundaries;
                // adjust the padding-right of each anchor element so that
                // highlighting extends the full width                
                w = $submenu.width() - 4;
                $.each($submenu.find('li'), function(index, item) {
                    iw = $(item).width();
                    delta = w - iw;
                    if (delta > 0) {
                        $a = $(item).find('a');
                        rpad = parseInt($a.css('padding-right'), 10) + delta;
                        $a.css('padding-right', rpad + 'px');
                    }
                });
                $submenu.data('visited', true);
            }
        }

        var showSubMenu = function($submenu, offset) {
            if ($curSubmenu) {
                $curSubmenu.hide();
            }
            $submenu.css({
                top: offset.top,
                left: offset.left
            })
            .show();
            $curSubmenu = $submenu;

            // IE kludge - see description of adjustOptionWidth function
            if (!$submenu.data('visited')) {
                adjustOptionWidth($submenu);
            }

        }

        // Hides the supplied submenu if the event mouse coordinates are not
        // within the submenu boudaries.
        var safeHideMenu = function(e, $submenu) {
            var offset = $submenu.offset();
            var w = $submenu.width(), h = $submenu.height();
            if (e.pageX < offset.left || e.pageX > offset.left + w ||
                e.pageY < offset.top || e.pageY > offset.top + h) {
                $submenu.hide();
                $curSubmenu = null;
            }
        }

        var $menuOpts = $('#menu li[id]');

        // To enhance performance, store a reference to the appropriate 
        // submenu in the menu option's data collection
        $.each($menuOpts, function(index, item) {
            $(item).data('submenu', $('#sm-' + item.id));
        });

        $menuOpts.hover(function() {
            var $item = $(this);
            var $submenu = $item.data('submenu');
            var $a = $item.children('a');
            var offset = $a.offset();            
            var menuOffset = {
                top:  offset.top + $a.outerHeight(),
                left: offset.left - ($submenu.width() - $a.outerWidth())
            };
            showSubMenu($submenu, menuOffset);
        }, function(e) {
            safeHideMenu(e, $(this).data('submenu'));
        });

        $('div.submenu').mouseleave(function(e) {
            // IE6 will fire the mouseleave event while the cursor is still
            // within the submenu boundaries. Therefore, rather than call
            // the hide method, call safeHideMenu so that the menu doesn't
            // close while the mouse is still within its boundaries
            safeHideMenu(e, $(this));
        });

        // handle <li> click when mouse is outside the anchor text
        $('div.submenu li').click(function() {
            document.location = $(this).find('a').attr('href');
            return false;
        });
    });
}