var predefined = [];
var relations = [];
var synsets = [];

function predefinedSelect() {        
    var display = "";
    
    var i;
    for (i = 0; i < predefined.length; i++) {
    	display += "<input type = \"checkbox\" name = \"predef\" value = \"" + predefined[i].id + "\">" + predefined[i].content + "<br>";
    }
    
    return display;
}

function getPredefinedSelections() {
	$.ajax({
        url: ajax_predefined_preferences,
        success: function(data){
        	predefined = data.predefined;
        },
        async: false
    });
}

function memorizeRelations(new_relations) {
    relations = new_relations;
}

function relationsSelect() {
    var display = "<select name = \"rel\">";
    
    var i;
    for (i = 0; i < relations.length; i++) {
        display += "<option value = \"" + relations[i].id + "\">" + relations[i].content + "</option>";
    }
    display += "</select>"
    return display;
}

function argumentSelect(argument) {
    var frame_id = parseInt(argument.split('_')[1]);
    var complement_id = parseInt(argument.split('_')[3]);
    var display = "<select name = \"arg\">";
    
    var i;
    for (i = 0; i < frame_content[frame_id].display.roles.length; i++) {
        var local_complement_id = parseInt(frame_content[frame_id].display.roles[i].csv_class.split('_')[3]);
        if (local_complement_id != complement_id) {
            var list = frame_content[frame_id].display.roles[i].argument;
            
            var text = [];
            var j;
            for (j = 0; j < list.length; j++) {
                text.push(role_name[list[j]]);
            }
            
            display += "<option value = \"" + list + "\">" + text + "</option>";
        }
    }
    
    display += "</select>"
    
    return display;
}

function getSynsets(context, pos) {
    
    if ((typeof pos) == 'undefined') {
        pos = '_';
    }
    
    $.ajax({
        dataType: "json",
        url: ajax_synsets,
        data: {base: context, pos: pos},
        success: function(data){ synsets = data.synsets },
        async: false
    });
    
    var display = "";
    
    var i;
    for (i = 0; i < synsets.length; i++) {
        display += "<input type = \"radio\" name = \"synset\" value = \"" + synsets[i].id + "\"><div>";
        var j;
        for (j = 0; j < synsets[i].content.length; j++) {
            var lexical_unit = synsets[i].content[j];
            display += lexical_unit.base + "-" + lexical_unit.sense + ": <i>" + lexical_unit.glossa + "</i><br>";
         }
         display += "</div>";
    }
    
    return display;
    
}

function relationArgument(argument) {
    return relationsSelect() + "<br><br>" + argumentSelect(argument);
}

function attachPlWNContextAutocomplete() {
	$('#plWN_context_selection').autocomplete({  
	   select: function(event, ui){
 	   },
       source: function(req, add){
            $.getJSON(ajax_plWN_context_lookup, req, function(data) { 
                var suggestions = [];  
                $.each(data['result'], function(i, val){  
                  suggestions.push(val);  
            }); 
            add(suggestions);  
        });  
      },  
	}); 
}
