function queryKorAPalternatives(alt1, alt2) {
    queryKorAP("{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;
}
