﻿(function($) {

    $.fn.dropDownMenu = function() {

        return this.each(function() {
            function menuHovered() {
                $Menu.children('li').each(function() {
                    if ($(this).hasClass('hover'))
                        return true;
                });
                return false;
            }

            var $Menu = $(this);

            $Menu.children('li').hover(
                function() {
                    $(this).addClass('hover');
                },
                function() {
                    $(this).removeClass('hover');
                }
            );

            $('*').click(function(e) {
                if ((this == e.target) && menuHovered()) {
                    var clickInMenu = (event.target == $Menu[0] || ($(event.target).parents().index($Menu[0]) >= 0));
                    if (!clickInMenu) {
                        $Menu.children('li').removeClass('hover');
                        return false;
                    }
                }
            });
        });

    };
})(jQuery);
