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"flpB><"clear">',
    buttons: [
      { extend: 'copyHtml5',  text: '<i class="fa fa-files-o"></i>',  titleAttr: 'Copy' },
      { extend: 'csvHtml5', text: '<i class="fa fa-file-text-o"></i>', titleAttr: 'CSV', filename: currentWords+'_syn_neighbours' },
      { extend: 'excelHtml5', text: '<i class="fa fa-file-excel-o"></i>', titleAttr: 'Excel', filename: currentWords+'_sny_neighbours',
      }
    ],
    "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 + " /w1:5,s0 " + 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"flpB><"clear">',
    buttons: [
      { extend: 'copyHtml5',  text: '<i class="fa fa-files-o"></i>',  titleAttr: 'Copy' },
      { extend: 'csvHtml5', text: '<i class="fa fa-file-text-o"></i>', titleAttr: 'CSV', filename: currentWords+'_collocates' },
      { extend: 'excelHtml5', text: '<i class="fa fa-file-excel-o"></i>', titleAttr: 'Excel', filename: currentWords+'_collocates', exportOptions: { columns: ':visible' } ,
      }
    ],
    "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": "ldaf", 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": "ln_count", render: function ( data, type, row ) {return toFixed(data, 1) }},
      { "data": "rn_count", render: function ( data, type, row ) {return toFixed(data, 1) }},
      { "data": "ln_pmi", render: function ( data, type, row ) {return toFixed(data, 2) }},
      { "data": "rn_pmi", 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;
}
