﻿(function ($) {
    $(function () {
        if (screen.width < 600) {
            return;
        }

        $.get('/navigation', function (response) {
            $('#nav-primary').replaceWith($(response));

            $('#nav-primary li.submenu').each(function () {
                var $this = $(this);
                var mouseout_timer = null;
                var mousein_timer = null;
                var hovered = false;

                function hover() {
                    $this.siblings('.active').removeClass('active');
                    $this.addClass('active');
                }

                $this.hover(function () {
                    clearTimeout(mouseout_timer);
                    mousein_timer = setTimeout(hover, 200);
                    hovered = true;
                }, function () {
                    clearTimeout(mousein_timer);
                    if (!hovered) {
                        return;
                    }
                    mouseout_timer = setTimeout(function () {
                        hovered = false;
                        $this.removeClass('active');
                    }, 800);
                });
            });
        });
    });
})(jQuery);
