var examples = [];              // list of all examples connected to lemma
var lexical_unit_examples = []; // lexical_unit_examples[lu.id] = list of examples connected to lexical unit lu
var schema_examples = [];       // lexical_unit_examples["schema_" + schema.id] = list of examples connected to schema
var displayed_examples = [];


function memorizeExamples(examples_data, hard) {
    
    var examples_old = examples;
    examples = examples_data;
    
    if (!hard) {
    var i, j;
        for (i = 0; i < examples_old.length; i++) {
            var id = examples_old[i].id;
            var meanings = examples_old[i].meanings;
            for(j = 0; j < examples.length; j++) {
                if (examples[j].id == id) {
                    examples[j].meanings = meanings;
                    break;
                }
            }
        }
    }
    
    var i;
    for (i = 0; i < lexical_units.length; i++) {
        lexical_unit_examples[lexical_units[i].id] = [];
    }
    
    for (i = 0; i < examples.length; i++) {
        var id = examples[i].id;
        var schema = examples[i].schema;
        schema_examples[schema].push(id);
        for (j = 0; j < examples[i].meanings.length; j++) {
            lexical_unit_examples[examples[i].meanings[j]].push("" + id);
        }
    }
}

function setExamples(luid, new_examples) {
    var j;
    
    for(j = 0; j < new_examples.length; j++) {
        if (lexical_unit_examples[luid].indexOf(new_examples[j]) == -1) {
            var i = indexOfId(examples, new_examples[j]);
            examples[i].meanings.push(parseInt(luid));
            units_operations.push({operation: 'add_example', unit: luid, example: new_examples[j]});
        }
    }
    
    for(j = 0; j < lexical_unit_examples[luid].length; j++) {
        if (new_examples.indexOf(lexical_unit_examples[luid][j]) == -1) {
            var i = indexOfId(examples, lexical_unit_examples[luid][j]);
            examples[i].meanings.splice(indexOfId(examples[i].meanings, luid), 1);
            units_operations.push({operation: 'remove_example', unit: luid, example: lexical_unit_examples[luid][j]});
        }
    }

    lexical_unit_examples[luid] = new_examples
}


function hasExample(luid) {
    for (i = 0; i < examples.length; i++){
        if (examples[i].meanings.length == 1) { 
            if (examples[i].meanings[0] == luid) {
		return true;
	    }
	}
    }
    return false;
}

// get examples connected to lexical unit as table formatted html form
function getExamples(luid) {

    $.ajax({
        dataType: "json",
        url: ajax_examples,
        data: {lemma_id: lemma_id},
        success: function(data){ memorizeExamples(data.examples, false) },
        async: false
    });

    var table = '<table id="nkjp_table" class="ChangeControlTable">';
    
    table += '<tr>';
    table += '<td class="ColumnHeader">Dotyczy:</td>';
    table += '<td class="ColumnHeader">Identyfikator:</td>';
    table += '<td class="ColumnHeader">Przykład:</td>';
    table += '<td class="ColumnHeader">Źródło:</td>';
    table += '<td class="ColumnHeader">Ocena:</td>';
    table += '</tr>';
    
    var i;
    for (i = 0; i < examples.length; i++){
        table += '<tr id="nkjp_' + examples.id + '" class="ExampleRow">';
        if (examples[i].meanings.length == 1) { 
            if (examples[i].meanings[0] == luid) {
                table += '<td class="ColumnHeader"><input type="checkbox" name="connect" value="' + examples[i].id + '" checked></td>';
            } else {
                table += '<td class="ColumnHeader">' + getLexicalUnit(examples[i].meanings[0]) + '</td>';
            }
        } else {
            table += '<td class="ColumnHeader"><input type="checkbox" name="connect" value="' + examples[i].id + '"></td>';
        }
        table += '<td class="ColumnHeader">' + examples[i].id + '</td>';
        table += '<td class="ColumnHeader">' + examples[i].sentence + '</td>';
        table += '<td class="ColumnHeader">' + examples[i].source + '</td>';
        table += '<td class="ColumnHeader">' + examples[i].opinion + '</td>';
        table += '</tr>';
    }
    
    table += '</table>';
    
    return table;
}

