/*

Editorial by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)

*/

(function($) {

var     $window = $(window),
        $head = $('head'),
        $body = $('body');
// Breakpoints.
        breakpoints({
                xlarge:   [ '1281px',  '1680px' ],
                large:    [ '981px',   '1280px' ],
                medium:   [ '737px',   '980px'  ],
                small:    [ '481px',   '736px'  ],
                xsmall:   [ '361px',   '480px'  ],
                xxsmall:  [ null,      '360px'  ],
                'xlarge-to-max':    '(min-width: 1681px)',
                'small-to-xlarge':  '(min-width: 481px) and (max-width: 1680px)'
        });
// Stops animations/transitions until the page has ...
        // ... loaded.
                $window.on('load', function() {
                        window.setTimeout(function() {
                                $body.removeClass('is-preload');
                        }, 100);
                });
        // ... stopped resizing.
                var resizeTimeout;
                $window.on('resize', function() {
                        // Mark as resizing.
                                $body.addClass('is-resizing');
                        // Unmark after delay.
                                clearTimeout(resizeTimeout);
                                resizeTimeout = setTimeout(function() {
                                        $body.removeClass('is-resizing');
                                }, 100);
                });
// Fixes.
        // Object fit images.
                if (!browser.canUse('object-fit')
                ||      browser.name == 'safari')
                        $('.image.object').each(function() {
                                var $this = $(this),
                                        $img = $this.children('img');
                                // Hide original image.
                                        $img.css('opacity', '0');
                                // Set background.
                                        $this
                                                .css('background-image', 'url("' + $img.attr('src') + '")')
                                                .css('background-size', $img.css('object-fit') ? $img.css('object-fit') : 'cover')
                                                .css('background-position', $img.css('object-position') ? $img.css('object-position') : 'center');
                        });
// Sidebar.
        var $sidebar = $('#sidebar'),
                $sidebar_inner = $sidebar.children('.inner');
        // ALWAYS inactive by default
                $sidebar.addClass('inactive');
        // Hack: Workaround for Chrome/Android scrollbar position bug.
                if (browser.os == 'android'
                &&      browser.name == 'chrome')
                        $('<style>#sidebar .inner::-webkit-scrollbar { display: none; }</style>')
                                .appendTo($head);
        // Toggle.
                $('#sidebar-toggle-button')
                        .on('click', function(event) {
                                // Prevent default.
                                        event.preventDefault();
                                        event.stopPropagation();
                                // Toggle.
                                        $sidebar.toggleClass('inactive');
                        });
                $('#sidebar-search-button')
                        .on('click', function(event) {
                                // Prevent default.
                                        event.preventDefault();
                                        event.stopPropagation();
                                // Toggle.
                                        $sidebar.toggleClass('inactive');
                        });
        // Events.
                // Link clicks.
                        $sidebar.on('click', 'a', function(event) {
                                // Vars.
                                        var $a = $(this),
                                                href = $a.attr('href'),
                                                target = $a.attr('target');
                                                pathname = $a.length > 0 ? $a[0].pathname : "";
                                                hash = href.split('#').length > 1 ? href.split("#")[1] : "";
                                // Prevent default.
                                        event.preventDefault();
                                        event.stopPropagation();
                                // Check URL.
                                        if (!href || href == '#' || href == '')
                                                return;
                                // Hide sidebar.
                                        $sidebar.addClass('inactive');
                                // Redirect to href.
                                        setTimeout(function() {
                                                if (target == '_blank')
                                                        window.open(href);
                                                else {
                                                        // Prevent reloading if the page is the same
                                                        if (pathname && hash && pathname == window.location.pathname) {
                                                                document.getElementById(hash).scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});
                                                        }
                                                        else
                                                                window.location.href = href;
                                                }
                                        }, 500);
                        });
                // Prevent certain events inside the panel from bubbling.
                        $sidebar.on('click touchend touchstart touchmove', function(event) {
                                // Prevent propagation.
                                        event.stopPropagation();
                        });
                // Hide panel on body click/tap.
                        $body.on('click touchend', function(event) {
                                // Deactivate.
                                        $sidebar.addClass('inactive');
                        });
        // Scroll lock.
        // Note: If you do anything to change the height of the sidebar's content, be sure to
        // trigger 'resize.sidebar-lock' on $window so stuff doesn't get out of sync.
                $sidebar_inner
                        .data('locked', 0)
                        .css('position', '')
                        .css('top', '');
// Menu.
        var $menu = $('#menu'),
                $menu_openers = $menu.children('ul').find('.opener');
        // Openers.
                $menu_openers.each(function() {
                        var $this = $(this);
                        $this.on('click', function(event) {
                                // Prevent default.
                                        event.preventDefault();
                                // Toggle.
                                        $menu_openers.not($this).removeClass('active');
                                        $this.toggleClass('active');
                                // Trigger resize (sidebar lock).
                                        $window.triggerHandler('resize.sidebar-lock');
                        });
                });

})(jQuery);