// define function
function limitless_init_datatable() {

    // counter for datatables
    var DatatableCounter = 0;

    // scann for each datatable
    $('.LimitlessTable').each(function () {

        // count table
        DatatableCounter++;

        // loading bar
        $(this).before('<div id="DatatableLoader' + DatatableCounter + '"><br/><center><b>[ LADE DATEN... ]</b></center><br/></div>');

    });

    setTimeout(function () {
        // reset counter for datatables
        var DatatableCounter = 0;

        // scann for each datatable
        $('.LimitlessTable').each(function () {

            // count table
            DatatableCounter++;

            // element
            let elementTable = $(this);

            var currentClass = elementTable.attr('class');
            currentClass = currentClass.replace('LimitlessTable','');

            // create replacement
            var NewHtml = '' +
                '<div id="DatatableFader' + DatatableCounter + '" class="DatatableFader">' +
                '<table id="Datatable' + DatatableCounter + '" class="display table datatable ' + currentClass + '">' +
                elementTable.html() +
                '</table>' +
                '</div>';

            // transport code to html
            elementTable.replaceWith(NewHtml);


            // render setup parameter
            var counter = 0;
            var sort = [];
            var orderable = [];

            $('#Datatable' + DatatableCounter).find('th').each(function () {

                let tableHeaderElement = $(this);

                // define sort
                if (tableHeaderElement.attr('Sort') != undefined) {
                    sort = [
                        [counter, tableHeaderElement.attr('Sort')]
                    ];
                }

                // orderable
                if (tableHeaderElement.attr('Sortable') == "false") {
                    orderable.push({
                        "orderable": false,
                        "targets": counter
                    });
                }

                // searchable
                if (tableHeaderElement.attr('Searchable') != undefined && tableHeaderElement.attr('Searchable') == "false") {
                    orderable.push({
                        "searchable": false,
                        "targets": counter
                    });
                }


                // counter
                counter++;
            });

            // rows
            var rows = elementTable.attr('rows');
            if (rows == undefined) {
                rows = 10;
            }

            // check entry count
            if ($('#Datatable' + DatatableCounter).children('tbody').children('tr').length > rows) {
                var Complex = true;
            } else {
                var Complex = false;
            }

            // export buttons
            let showExportButtons = elementTable.attr('exportable') != undefined && (elementTable.attr('exportable') == "true" || elementTable.attr('exportable') == true);
            let exportButtons = ['copy', 'csv', 'excel', 'pdf', 'print'];

            // Define the table control elements to appear on the page and in what order.
            // https://datatables.net/reference/option/dom
            let dom = showExportButtons ? 'Blfrtip' : 'lfrtip';


            // length menu
            let lengthMenu = [
                [10, 25, 50, 100, 500, -1],
                ['Standard (10)', 25, 50, 100, 500, "Alle"]
            ];

            // activate datatable
            $('#Datatable' + DatatableCounter).dataTable({
                'dom': dom,
                "fixedHeader": true,
                'autoWidth': true,
                'pageLength': parseInt(rows),
                "order": sort,
                "paging": Complex,
                "searching": Complex,
                "info": Complex,
                "columnDefs": orderable,
                "buttons": exportButtons,
                "language": {
                    "url": "//cdn.datatables.net/plug-ins/1.10.19/i18n/German.json"
                },
                "lengthMenu": lengthMenu,
                "drawCallback": function (settings) {

                    // calculate height for animation
                    var height = $('#' + settings.sTableId).parents('.dataTables_wrapper').first().height() + 10;

                    // add classes to filter and select dropdown
                    $('.dataTables_filter input').addClass('DatatableSearch');
                    $('.dataTables_length label select').addClass('DatatableFilter');
                    $('.dataTables_info').addClass('d-none d-lg-block d-md-block');
                    $('.dataTables_length').addClass('d-none d-lg-block d-md-block');

                    // get int from callback
                    var callbbackid = settings.sTableId.replace(/[^\d.]/g, '');

                    // remove loader
                    $('#DatatableLoader' + callbbackid).remove();

                    // break after inti
                    if ($('#DatatableFader' + callbbackid).css("opacity") == 1) {
                        return false;
                    }

                    // scale up table
                    $('#DatatableFader' + callbbackid).animate({
                        'opacity': '1',
                        'min-height': height
                    }, 300, function () {

                        // fix height for responsible design
                        $('#DatatableFader' + callbbackid).css("height", "auto");
                        $('#DatatableFader' + callbbackid).css("min-height", "auto");

                    });

                }
            });
        });

    }, 300);
}

$(document).ready(limitless_init_datatable);
