function queryKorAPpoliqarp(query) {
    window.open('http://korap.ids-mannheim.de/kalamar/?ql=poliqarp&q='+encodeURIComponent(query), 'KorAP');
}


function queryKorAPalternatives(alt1, alt2) {
    queryKorAPpoliqarp("{1:[orth="+ alt1 +"]} | {2:[orth=" + alt2 +"]}");
}

function paradigmatorClick(e, target, paradigmator) {
    if (e.which === 2) {
        e.preventDefault();
        queryKorAPalternatives(target, paradigmator);
        return false;
    }
}


function makeVocabDistanceTable(id, baseURL) {
  var urlprefix =  new URLSearchParams(window.location.search);
  return $(id).DataTable({
    ajax: {
      method: "GET",
      url: baseURL + '/getBiggestVocabDistances',
      dataType: 'json',
      dataSrc: function (result) {
        return result;
      },
      timeout: 30000,
    },
    "initComplete":function(settings, json){
      vocabDistanceTable.columns.adjust().draw();
    },
    "createdRow": function (row, data, rowIndex) {
      $.each($('td.collocator', row), function (colIndex) {
        $(this).attr('title', "f("+data.word+")="+data.f2.toLocaleString("en-GB") + " f1: "+ccResult.f1+ " total: "+ccResult.N);
      });
    },
    "sScrollY": "780px",
    "bScrollCollapse": true,
    "bPaginate": false,
    "bJQueryUI": true,
    "dom": '<"top">rt<"bottom"flp><"clear">',
    "columns": [
      { "data": "rank", type: "allnumeric" },
      { "data": "dist",  render: function ( data, type, row ) {return data.toFixed(3) }},
      { "data": "word",  class: "paradigmator", render: function ( data, type, row ) {
        urlprefix.set("word", data); return  '<a class="' + getMergedClass(row.rank) + '" href="?' + urlprefix + '">' + data + '</a>' 
      }}
    ],
    "columnDefs": [
      { className: "dt-right", "targets": [0,1] },
      { "searchable": false,
        "orderable": false,
        "targets": 0
      },
      { "orderSequence": [ "desc" ], "targets": [ 1 ] },
      { "orderSequence": [ "asc", "desc" ], "targets": [ 2 ] },
    ],
    "oLanguage": {
      "sSearch": "Filter: "
    },
    "order": [[ 1, 'desc' ]]
  });
}

function toFixed(n, d) {
  if(typeof n != "number") {
    return n;
  } else {
    return n.toFixed(d);
  }
}

function toExponential(n, d) {
  if(typeof n != "number") {
    return n;
  } else {
    return n.toExponential(d);
  }
}

function bitmask2window(autofocus, win) {
  var af_str = autofocus.toString(2).padStart(10, "0")
    .replace(/^([0-9]{5})/, '$1 ')
    .replace(/0/g, '·')
    .replace(/1/g, '◼');
  var w_str = win.toString(2).padStart(10, "0")
    .replace(/^([0-9]{5})/, '$1 ')
    .replace(/0/g, '·')
    .replace(/1/g, 'o');
  for (var i=0, l=af_str.length; i<l; i++) {
    if(af_str.charAt(i) == '·' && w_str.charAt(i) == 'o' ) {
      af_str = af_str.substring(0, i) + "◽" + af_str.substring(i+1);
    }
  }
  return af_str;
}

function makeClassicCollocatorTable(id, baseURL, wordIndex) {
  $.fn.dataTable.ext.errMode = 'throw';
  var classicCollocatorTable = $('#classicoloctable').DataTable({
    ajax: {
      method: "GET",
      url: baseURL + '/getClassicCollocators',
      dataType: 'json',
      dataSrc: function (result) {
        ccResult = result;
        $("#rawfreq_tt").attr("title", "Raw frequencies of the co-occurence.\n"
                              + "f(" + ccResult.w1+")=" + ccResult.f1.toLocaleString("en-GB") + ";\n"
                              + "N=" + ccResult.N.toLocaleString("en-GB"));
        return result.collocates;
      },
      warning: function (request, error) {
      },
      timeout: 30000,
      data: { w: wordIndex }
    },
    "initComplete":function(settings, json){
      $("td.collocator").click(function(){
        queryKorAPCII(this.textContent + " /w5 " + CIIsearchWords);
      });
      classicCollocatorTable.columns(".detail").visible(false);
      $("#ccd").css('width', '680px');
      classicCollocatorTable.columns.adjust().draw();
    },
    "createdRow": function (row, data, rowIndex) {
      $.each($('td.collocator', row), function (colIndex) {
        $(this).attr('title', "f("+data.word+")="+data.f2.toLocaleString("en-GB") + " f1: "+ccResult.f1+ " total: "+ccResult.N);
      });
    },
    "sScrollY": "780px",
    "bScrollCollapse": true,
    "bPaginate": false,
    "bJQueryUI": true,
    "dom": '<"top">rt<"bottom"flp><"clear">',
    "columns": [
      //                      { "data": "pos", width: "7%", sClass: "dt-center mono compact", render: function ( data, type, row ) {return bitvec2window(data, row.heat, row.word) }},
      { "data": "llr",  render: function ( data, type, row ) {return toFixed(data, 0) }},
      { "data": "pmi", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "md", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "lfmd", render: function ( data, type, row ) {return toFixed(data, 2) }},
      //             { "data": "fpmi", type: "scientific", render: function ( data, type, row ) {return data.toExponential(2) }  },
      { "data": "npmi", render: function ( data, type, row ) {return toFixed(data, 3) }},
      { "data": "dice", render: function ( data, type, row ) {return toExponential(data, 2) }},
      { "data": "ld", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "af", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "delta", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "afwin", width: "auto", sClass: "dt-center mono compact", render: function ( data, type, row ) {return bitmask2window(data, row.win) }},
      { "data": "lncount", render: function ( data, type, row ) {return toFixed(data, 1) }},
      { "data": "rncount", render: function ( data, type, row ) {return toFixed(data, 1) }},
      { "data": "lnpmi", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "rnpmi", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "f" },
      { "data": "word",  sClass: "collocator" }
    ],
    "columnDefs": [
      { className: "dt-right", "targets": [0,1,2,3,4,5,6,7,8,10,11,12,13,14] },
      { className: "dt-right detail", "targets": [5,10,11,12,13] },
      { "searchable": false,
        "orderable": false,
        "targets": []
      },
      { "orderSequence": [ "desc" ], "targets": [0,1,2,3,4,5,6,7,10,11,12,13,14] },
      { "orderSequence": [ "asc", "desc" ], "targets": [8,9,15] },
    ],
    "oLanguage": {
      "sSearch": "Filter: "
    },
    "order": [[ 7, 'desc' ]],
  });
  return classicCollocatorTable;
}
