var my = {

    // This ties in bootstrap-table, so the recommended JS isn't needed.
    // This code will search for the tables and apply the JS.
    applyBootstrapTable: function () {

        // Apply library to each table
        $('.table').each(function () {
            var hasTableBordered = $(this).hasClass('table-bordered');
            var hasTableHover = $(this).hasClass('table-hover');

            $(this).bootstrapTable();

            if (!hasTableBordered) $(this).removeClass('table-bordered');
            if (!hasTableHover) $(this).removeClass('table-hover');
            $(this).addClass('original-table');
        });

        // Compatibility with tabs -- reset view when tab is shown
        $('.nav-tabs .nav-link').on('shown.bs.tab', function (e) {
            var table = $(e.target.hash).find('.original-table');
            if (table.length === 1)
                table.bootstrapTable('resetView');
        });
    },

    // Ties in floating-scroll library.
    applyFloatingHScroll: function () {

        // bootstrap-table compatibility
        $('.floating-hscroll.original-table').parent().floatingScroll();
        $(window).resize(function () {
            $('.fixed-columns-right .fl-scrolls').remove();
        });

        // Everything else
        $('.floating-hscroll-container').floatingScroll();

    },

    // Gridviews generate terrible code.  This fixes it up to something more standard.
    // After this function runs, other code that expects standard structures will work.
    // Source: https://weblog.west-wind.com/posts/2010/Nov/18/Adding-proper-THEAD-sections-to-a-GridView
    fixGridviews: function () {
        $('.table').each(function () {
            if ($(this).find('tbody>tr>th').length > 0) {
                var trClass = $(this).find('tbody>tr').attr('class');
                $(this).find('tbody').before('<thead><tr></tr></thead>');
                $(this).find('thead>tr').append($(this).find('th'));
                $(this).find('tbody>tr:first').remove();
                $(this).find('thead>tr').attr('class', trClass);
            } else {
                $(this).find('tbody').before('<thead><tr class="d-none"><th></th></tr></thead>');
            }
        });
    },

    // Copy/paste code to make tabs remember their state
    // Source: https://stackoverflow.com/questions/18999501/bootstrap-3-keep-selected-tab-on-page-refresh
    rememberTabState: function () {
        if (location.hash) {
            $('a[href=\'' + location.hash + '\']').tab('show');
        }
        var activeTab = localStorage.getItem('activeTab');
        if (activeTab) {
            $('a[href="' + activeTab + '"]').tab('show');
        }

        $('body').on('click', 'a[data-toggle=\'tab\']', function (e) {
            e.preventDefault()
            var tab_name = this.getAttribute('href')
            if (history.pushState) {
                history.pushState(null, null, tab_name)
            }
            else {
                location.hash = tab_name
            }
            localStorage.setItem('activeTab', tab_name)

            $(this).tab('show');
            return false;
        });
        $(window).on('popstate', function () {
            var anchor = location.hash ||
                $('a[data-toggle=\'tab\']').first().attr('href');
            $('a[href=\'' + anchor + '\']').tab('show');
        });
    },

    // Make table rows clickable.
    // Add 'clickable-rows' class to header, and add 'data-href' attribute to each row
    // On asp:GridView, you will need to catch the RowDataBound event to add the data-href attribute.
    makeRowsClickable: function () {
        $('.table.clickable-rows').each(function () {
            $(this).on('click-row.bs.table', function (e, row, $element) {
                if (typeof $element.data('href') === 'undefined')
                    return;
                if ($element.data('target') === '_blank')
                    window.open($element.data('href'), $element.data('target'));
                else
                    window.location = $element.data('href');
            });
        })
    },

}

$(document).ready(function () {

    // Fixes come first, so following code will work
    my.fixGridviews();

    // Tie in libraries, which may change html structure more
    my.applyBootstrapTable();
    my.applyFloatingHScroll();

    // Our behavior changes
    my.makeRowsClickable();
    my.rememberTabState();
});
