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