﻿(function($) {
    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;
        }
    };

    $.fn.dropmenu = function(options) {
        $.extend(settings, options);

        var items = this.find('li');
        var menus = items.find('ul');
        var width = 0;
        var widthVisible = this.innerWidth();

        methods.init(menus);
        methods.initFrame(menus);

        if (settings.hideDelay > 0) {
            items.live({
                mouseenter: function() {
                    var element = $(this);
                    element.siblings().removeClass('hover');
                    element.parents('li').andSelf().each(function() {
                        window.clearTimeout($(this).addClass('hover').data(settings.hideTimeoutKey));
                    });
                },

                mouseleave: function() {
                    var element = $(this);
                    element.data(settings.hideTimeoutKey, window.setTimeout(function() {
                        element.removeClass('hover');
                    }, settings.hideDelay));
                }
            });
        }
        else {
            items.live('hover', function(e) {
                $(this).toggleClass('hover');
            });
        }

        this.children('li').each(function() {
            width += $(this).outerWidth();
        });

        this.css({ width: width + 'px' }).find('li > ul').each(function() {
            var menu = $(this);
            var menuOffset = Math.min(widthVisible - (menu.parent('li').position().left + menu.outerWidth()), 0);
            menu.css({ left: menuOffset + 'px' });
        });

        return this;
    };
})(jQuery);

