﻿// creates a new drop-down menu
DropMenu = function(id, options) {
    var settings = {
        hideDelay: 500,
        hideTimeoutKey: 'dropmenu-hide-timeout'
    };

    var methods = {
        init: function(query) {
            query.each(function() {
                $(this).css({ width: Math.max(methods.maxWidth(this), $(this).parent().width()) + 'px' });
            });
        },

        initFrame: function(query) {
            if (document.all) {
                query.each(function() {
                    var frameMenu = $(this);
                    var frame = $('<iframe/>').attr({
                        frameBorder: '0',
                        scrolling: 'no',
                        src: 'about:blank'
                    }).css({
                        filter: 'progid:DXImageTransform.Microsoft.Alpha(style=0, opacity=0)',
                        left: '0px',
                        height: frameMenu.height() + 'px',
                        position: 'absolute',
                        top: '0px',
                        visibility: 'hidden',
                        width: frameMenu.width() + 'px',
                        zIndex: '-1'
                    });

                    frameMenu.css({ zIndex: 101 }).append(frame);
                });
            }
        },

        maxWidth: function(element) {
            var menu = $(element);
            var menuWidth = 0;

            menu.children('li').each(function() {
                var link = $(this);
                link.css({ display: 'inline', whiteSpace: 'nowrap', width: 'auto' });
                menuWidth = Math.max(menuWidth, $(this).width());
                link.removeAttr('style');
            });

            return menuWidth;
        }
    };

    if (typeof (options) == 'object') {
        $.extend(settings, options);
    }

    var mroot = $('#' + id);
    var items = mroot.find('li');
    var menus = items.find('ul');
    var width = mroot.innerWidth();

    methods.init(menus);
    methods.initFrame(menus);

    if (settings.hideDelay > 0) {
        items.bind('mouseenter', function() {
            var element = $(this);
            element.siblings().removeClass('hover');
            element.parents('li').andSelf().each(function() {
                window.clearTimeout($(this).addClass('hover').data(settings.hideTimeoutKey));
            });
        }).bind('mouseleave', function() {
            var element = $(this);
            element.data(settings.hideTimeoutKey, window.setTimeout(function() {
                element.removeClass('hover');
            }, settings.hideDelay));
        });
    }
    else {
        items.bind('hover', function(e) {
            $(this).toggleClass('hover');
        });
    }

    mroot.find('li > ul').each(function() {
        var menu = $(this);
        var menuOffset = Math.min(width - (menu.parent('li').position().left + menu.outerWidth()), 0);
        menu.css({ left: menuOffset + 'px' });
    });

    mroot.find('li li > ul').each(function() {
        var menu = $(this);
        var menuOffset = menu.parent('li').outerWidth();
        menu.css({ left: menuOffset + 'px' });
    });

    return this;
}

