
function showsearchdialog(ident, related_class){
    // set identifier (not nice solution, but I can't pass it through closure, dunno why
    $('#searchbox').attr('ident', ident);
    // clear dialog box
    $('#searchtext').attr('value', '');
    $('#opts').empty();
    // open dialog box
    $('#searchbox').dialog({ modal: true, width:650, bgiframe:true, buttons: {
        //"Cancel": function() { $(this).dialog("close"); } ,
        //"Ok":function(){input_search($(this), ident);},
     }});
    $('#searchbox').dialog('open');
    // attach event handler to handle doubleclick
    $('#searchbox').dblclick(function() { input_search($(this)); });
    $('#searchtext').keypress(function(){handle_key(related_class);});
    $('#searchtext').focus();
    return false;
}


function input_search(di){
    ident = $('#searchbox').attr('ident');
    var value = $('#opts').attr('value')
    var id = '#' + ident
    // see if the select has this value
    var c = $(id + ' > option[value="' + value + '"]').length
    if(c==0){
        // find the element in searchbox, get its label (a bit hackish)
        var label = $('#opts > option[value="' + value + '"]').html()
        $(id).append("<option value=" + value + ">" + label + "</option>");
    }
    // put selected value into the field, close dialog, trigger event
    $(id).attr('value', value).trigger('change');
    di.dialog("close");
    di.dialog('destroy');
}


function newshowsearchdialog(ident, related_class){
    $('#searchbox').unbind(); // so that we don't accumlate handlers
    // set identifier (not nice solution, but I can't pass it through closure, dunno why
    $('#searchbox').attr('ident', ident);
    // clear dialog box
    $('#searchtext').attr('value', '');
    $('#opts').empty();
    // open dialog box
    $('#searchbox').dialog({ modal: true, width:650, bgiframe:true, autoOpen:false});
    $('#searchbox').dialog('open');
    // attach event handler to handle doubleclick
    $('#searchbox').dblclick(function() { newinput_search(); });
    $('#searchbox_ok').click(function() { newinput_search(); });
    $('#searchtext').keypress(function(){handle_key(related_class);});
    $('#searchtext').focus();
    return false;
}


function newinput_search(){
	var di = $('#searchbox');
    ident = di.attr('ident');
    //var value = $('#opts').attr('value')
    var sel = $('#' + ident);
    // get selected option
    var opt = $("#opts > option:selected");
    var label = opt.html();
    var value = opt.attr('value');
    // put selected value into the field, close dialog, trigger event
    sel.next().attr('value', label);
    sel.attr('value', value).trigger('change');
    di.dialog("close");
}


function handle_key(related_class){
    // clear previous timeout if any
    // perform search if the user stopped typing for half a second
    try {
        window.clearTimeout(tm);
    }catch(e) {
    }
    tm = window.setTimeout('refresh_search("' + related_class +'")', 500);
}


function refresh_search(related_class){
    // skip if less then 3 chars have been entered
    txt = $('#searchtext').attr('value');
    if(txt.length < 3) return;
    // clear select box
    $('#opts').empty();
    // show message
    $('#opts').append('<option>...searching...</option>');
    var ppath = $("#naszewina").attr('proxypath');
    if(ppath == undefined){
        var path = '/core/' + related_class +'/lista/json/' + txt + '/';
    }else{
        var path = ppath + '?source_path=/core/' + related_class +'/lista/json/' + txt + '/';
    }
    $.getJSON(path,
        function(data){
            $('#opts').empty();
            $.each(data.items, function(i,item){
                var no = new Option(item.label, item.pk)
                $('#opts').append("<option value=" + item.pk + ">" + item.label + "</option>");
            });
            var ln = $('#opts').find('option').length
            // if only one value returned select it
            if(ln == 1){
              $('#opts').attr('selectedIndex', 0);
            }
            // show message if no results
            if(ln == 0){
                $('#opts').append('<option>(no result)</option>');
            }
            // a hack to make IE8 refresh when there is only one item
            $('#opts').append("<option value=0>&nbsp;</option>");
        });
}

