/* ==========================================================================
   FHS — Global Mobile Responsive Behaviour (content)
   --------------------------------------------------------------------------
   Pure vanilla JS, self-initialising, load-order independent.

   The sidebar drawer (open/close, overlay, close button) is already handled
   by the customised js/sb-admin-2.min.js via the .mobile-open / #sidebarOverlay
   mechanism, so this file deliberately does NOT touch the sidebar.

   It only wraps every <table> in a horizontal-scroll container so wide tables
   scroll instead of breaking the page layout on small screens.
   ========================================================================== */
(function () {
    'use strict';

    function wrapTables() {
        var tables = document.querySelectorAll('table');
        Array.prototype.forEach.call(tables, function (table) {
            var parent = table.parentElement;
            if (!parent) return;
            if (parent.classList.contains('rk-table-scroll') ||
                parent.classList.contains('table-responsive')) {
                return;
            }
            var wrap = document.createElement('div');
            wrap.className = 'rk-table-scroll';
            parent.insertBefore(wrap, table);
            wrap.appendChild(table);
        });
    }

    function init() {
        wrapTables();
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();
