Thanks to @googletorp advice the solutions is so easy and I want to share it with others.
All I need was to overwrite two Drupal autocomplete object functions: found and select.
My autocomplete works on content profile fields. I pass nid of selected profiles to JS:
PHP:
function messagi_autocomplete($string) {
global $user;
$cache = cache_get('messagi_auto_complete_'.$string);
$items = array();
if ($cache) {
$items = $cache->data;
} else {
$sql = "
SELECT
n.nid AS id
FROM
node as n
WHERE
n.type = 'profile'
AND
u.status = 0
";
$result = db_query($sql);
$disabled = array();
while($obj = db_fetch_object($result)) {
$disabled[] = $obj->id;
}
$items = array();
$sql = "
SELECT
profile.nid AS nid,
profile.field_profile_name_value AS name,
profile.field_profile_surname_value AS surname
FROM
content_type_profile AS profile
WHERE
lower(profile.field_profile_name_value) LIKE lower('%s%%')
OR lower(profile.field_profile_surname_value) LIKE lower('%s%%')
LIMIT 10
";
$result = db_query($sql, $string, $string);
$sql = NULL;
while($obj = db_fetch_object($result)) {
if (! in_array($obj->nid, $disabled)) {
$key = check_plain($obj->name).' '.check_plain($obj->surname);
$items['nids'][$key] = (int) $obj->nid;
$items[$key] = $key.', '.check_plain($obj->venue);
}
}
if (! empty($items)) {
cache_set('messagi_auto_complete_'.$string, $items, 'cache', 1000);
}
$disabled = NULL;
}
print drupal_to_js($items);
exit();
}
JS:
$(document).ready(function() { if
(Drupal.jsEnabled) {
Drupal.jsAC.prototype.found = function (matches) {
if (!this.input.value.length) {
return false;
}
var ul = document.createElement('ul');
var ac = this;
for (key in matches) {
var li = document.createElement('li');
//to avoid of showing nids object in the select list
if (key !== 'nids') {
$(li)
.html('<div>'+ matches[key] +'</div>')
.mousedown(function () {
ac.select(this); })
.mouseover(function () {
ac.highlight(this); })
.mouseout(function () { ac.unhighlight(this); });
li.autocompleteValue = key;
$(ul).append(li);
} else {
//add all nids to prototype
Drupal.jsAC.prototype.nids = matches[key];
}
}
//rest of the code
}
};
Drupal.jsAC.prototype.select = function (node) {
//get selected value nid and put it on form hidden field
$('#edit-nid').val(Drupal.jsAC.prototype.nids[node.autocompleteValue]);
this.input.value = node.autocompleteValue;
};
} })
I hope this help somebody in the future. Thanks for the others advice to this code.