// 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++;

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

            // create replacement
            var NewHtml = '' +
                '<div id="DatatableFader' + DatatableCounter + '" class="DatatableFader" style="height:0px; opacity:0; overflow-y: hidden;">' +
                '<table id="Datatable' + DatatableCounter + '" class="display table datatable ' + currentClass + '">' +
                $(this).html() +
                '</table>' +
                '</div>';

            // transport code to html
            $(this).replaceWith(NewHtml);

            // render setup parameter
            var counter = 0;
            var sort = [];
            var orderable = [];
            $('#Datatable' + DatatableCounter).find('th').each(function () {
                // define sort
                if ($(this).attr('Sort') != undefined) {
                    sort = [
                        [counter, $(this).attr('Sort')]
                    ];
                }

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

                // searchable
                if ($(this).attr('Searchable') != "true") {
                    orderable.push({
                        "searchable": false,
                        "targets": counter
                    });
                }


                // counter
                counter++;
            });

            // rows
            var rows = $(this).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;
            }

            if($('table').hasClass('downloadbuttons')){

                $(".dt-buttons").removeClass('hideBtn');
                $(".dt-buttons").addClass('showBtn');

            } else {
                $(".dt-buttons").addClass('hideBtn');
            }

            // activate datatable
            $('#Datatable' + DatatableCounter).dataTable({
                'dom': '<"c8tableTools01"Bf><"c8tableBody"t><"c8tableTools02"lipr>',
                "fixedHeader": true,
                'autoWidth': true,
                'pageLength': parseInt(rows),
                "order": sort,
                "paging": Complex,
                "searching": Complex,
                "info": Complex,
                "columnDefs": orderable,
                "buttons": [
                    'csv', 'excel'
                ],
                "language": {
                    "url": "//cdn.datatables.net/plug-ins/1.10.19/i18n/German.json"
                },
                "lengthMenu": [
                    [rows, 10, 25, 50, 100, 500, -1],
                    ['Standard (' + rows + ')', 10, 25, 50, 100, 500, "Alle"]
                ],
                "drawCallback": function (settings) {

                    var hasClass = $('table').hasClass('downloadbuttons');

                    if($('.buttons-excel').length > 0) {
                        $('.buttons-excel')[0].style.visibility = hasClass ? 'visible' : 'hidden';
                    }

                    if($('.buttons-csv').length > 0) {
                        $('.buttons-csv')[0].style.visibility = hasClass ? 'visible' : 'hidden';
                    }


                    // 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);
