  (function($) {
        $.fn.vmenu = function(settings) {
            var config = {
                width: 189,
                width2: 100,
                fadeIn: 350,
                fadeOut: 100,
                delay: 200
            };

            if (settings) $.extend(config, settings);

            this.each(function() {
                var $mainmenu = $(this);
                var $headers = $mainmenu.find("ul").parent();
                var current_item = null;

                $headers.each(function(i) {
                    var $curobj = $(this); // li
                    var $subul = $(this).find('ul:eq(0)'); // ul.subnav

                    this._menudata = {
                        w: this.offsetWidth,
                        h: this.offsetHeight,
                        ulw: $subul.outerWidth(),
                        ulh: $subul.outerHeight(),
                        sublevel: $curobj.parents("ul").length
                    };

                    // console.log(this._menudata);

                    $curobj.hover(
				    function(e) {
				        if (current_item) { clearTimeout(current_item); current_item = null; }

				        var item = this;
				        current_item = setTimeout(function() {

				            var $targetul = $(item).children("ul:eq(0)");
				            this._offsets = { left: $(item).offset().left, top: $(item).offset().top };

				            var menuleft = config.width;
				            if (!item._menudata.sublevel == 1) {
				                menuleft = menuleft - ((sublevel - 1) * config.width2);
				            }
				            menuleft = menuleft + (item._menudata.sublevel - 1) * config.width2

				            var menutop = 0;
				            var wH = $(window).height();
				            var wS = $(window).scrollTop();

				            if (item._menudata.ulh > wH) {
				                menutop = menutop - (this._offsets.top - wS);
				            } else {
				                var overflow = (this._offsets.top + item._menudata.ulh) - (wH + wS);

				                if (overflow > 0) {
				                    menutop = (menutop - overflow);
				                }
				            }

				            $targetul.css({ left: menuleft + "px", top: menutop + "px" }).fadeIn(config.fadeIn);
				        }, config.delay);
				    },
				    function(e) {
				        if (current_item) { clearTimeout(current_item); current_item = null; }

				        $(this).children("ul:eq(0)").fadeOut(config.fadeOut);
				    }
			    );
                });
            });

            return this;
        };
    })(jQuery);
