$.root_ = $('body');
$.extend(true, $.fn.dataTable.defaults, {
    bLengthChange: false,
    pageLength: 10,
    "language": {
        "sEmptyTable": "Keine Daten in der Tabelle vorhanden",
        "sInfo": "_START_ bis _END_ von _TOTAL_ Eintr&auml;gen",
        "sInfoEmpty": "0 bis 0 von 0 Eintr&auml;gen",
        "sInfoFiltered": "(gefiltert von _MAX_ Eintr&auml;gen)",
        "sInfoPostFix": "",
        "sInfoThousands": ".",
        "sLengthMenu": "_MENU_ Eintr&auml;ge anzeigen",
        "sLoadingRecords": "Wird geladen...",
        "sProcessing": "Bitte warten...",
        "sSearch": "Suchen: ",
        "sZeroRecords": "Keine Eintr&auml;ge vorhanden.",
        "oPaginate": {
            "sFirst": "Erste",
            "sPrevious": "Zur&uuml;ck",
            "sNext": "N&auml;chste",
            "sLast": "Letzte"
        },
        "oAria": {
            "sSortAscending": ": aktivieren, um Spalte aufsteigend zu sortieren",
            "sSortDescending": ": aktivieren, um Spalte absteigend zu sortieren"
        }
    },
    responsive: true
});

function convertTimestamp(UNIX_TS) {
    var date = new Date(UNIX_TS * 1000);
    var year = date.getFullYear();
    var month = (date.getMonth()+1) < 10 ? '0'+(date.getMonth()+1) : (date.getMonth()+1);
    var day = date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
    var hour = date.getHours() < 10 ? '0'+date.getHours() : date.getHours();
    var min = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes();
    var sec = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds();
    var fulltime = day + '.' + month + '.' + year + ' - ' + hour + ':' + min + ':' + sec;
    return fulltime;
}

function anonIP(ipdata) {
    var split = ipdata.split(".", 2);
    return split[0]+'.'+split[1]+'.xxx.xxx';
}

function refLink(referrer) {
    if(referrer != 'direct') {
        return '<a href="'+referrer+'" target="_blank">Link zum Referer</a><p class="note">'+referrer+'</p>';
    } else {
        return 'Browsereingabe oder Anonymisierter aufruf';
    }
}

$(document).ready(function() {
    $('#dlLists').DataTable({
        "ajax": "ajax/downloadOverview.php?ssp=1",

        columns: [
            {"data": "guid"},
            {"data": function(data) {
                    return anonIP(data.ip_adress);
                }
            },
            {"data": "user_agent"},
            {"data": function(data) {
                    return convertTimestamp(data.timestamp);
                }
            },
            {"data": "dl_version"}
        ]
    });

    $('#visitorLists').DataTable({
        "ajax": "ajax/visitorOverview.php?ssp=1",

        columns: [
            {"data": "guid"},
            {"data": function(data) {
                    return anonIP(data.ip_adress);
                }
            },
            {"data": "user_agent"},
            {"data": function(data) {
                    return refLink(data.referrer);
                }
            },
            {"data": function(data) {
                    return convertTimestamp(data.timestamp);
                }
            }
        ]
    });
});