var ac_selected_url = false;
var ac_extraParams = { avatar: 1 };

if (typeof ac_minChars == "undefined") ac_minChars = 3;
if (typeof ac_friends != "undefined" && ac_friends) ac_extraParams.friends=1;

$(".ac_user").attr("autocomplete", "off").autocomplete({ url:base_url+'backend/autocomplete_user.php',
	minChars: ac_minChars, matchSubset: 0,
	extraParams: ac_extraParams,
	showResult: function(value, data) {
		return '<img src="'+data[0]+'" width="12" height="12" style="vertical-align:bottom"/>&nbsp;'+value;
	},
	onItemSelect: function(item) {
		if (name.length > 0 && item.data.length > 0 && item.data[0].length > 0) {
			ac_selected_url = item.data[0];
			id = $(this).attr("id");
			$("#"+id+"_avatar").attr("src", ac_selected_url);
			$("#"+id+"_avatar").css('visibility', 'visible');
		} else {
			ac_selected_url = false;
		}
	},
}).change(function (t) {id = $(this).attr("id"); setTimeout(function () {form_add_avatar(id)}, 300) });

function form_add_avatar(id) {
	name = $("#"+id).val();
	if (name.length > 0) {
		if (ac_selected_url != false) url = ac_selected_url;
		else url = base_url+"backend/get_avatar.php?user="+name+"&size=20"
		$("#"+id+"_avatar").attr("src", url);
		$("#"+id+"_avatar").css('visibility', 'visible');
	} else {
		$("#"+id+"_avatar").css('visibility', 'hidden');
	}
	ac_selected_url = false;
}

@@ -1,33 +1,4 @@
-var ac_selected_url = false;%0A
 var 
@@ -207,16 +207,94 @@
  %22off%22).
+each( function () %7B%0A%09var input = $(this);%0A%09input.data(%22url%22, false);%0A%09$(this).
 autocomp
@@ -346,16 +346,17 @@
 r.php',%0A
+%09
 %09minChar
@@ -387,16 +387,17 @@
 set: 0,%0A
+%09
 %09extraPa
@@ -418,16 +418,17 @@
 Params,%0A
+%09
 %09showRes
@@ -458,16 +458,17 @@
 ta) %7B%0A%09%09
+%09
 return '
@@ -559,20 +559,22 @@
 +value;%0A
+%09
 %09%7D,%0A
+%09
 %09onItemS
@@ -593,24 +593,25 @@
 ion(item) %7B%0A
+%09
 %09%09if (name.l
@@ -677,33 +677,34 @@
 0) %7B%0A%09%09%09
-ac_selected_url =
+%09input.data(%22url%22,
  item.da
@@ -712,191 +712,72 @@
 a%5B0%5D
-;%0A%09%09%09id = $(this).attr(%22id%22);%0A%09%09%09$(%22#%22+id+%22_avatar%22).attr(%22src%22, ac_selected_url);%0A%09%09%09$(%22#%22+id+%22_avatar%22).css('visibility', 'visible');%0A%09%09%7D else %7B%0A%09%09%09ac_selected_url =
+).change();%0A%09%09%09%7D else %7B%0A%09%09%09%09input.data(%22url%22,
  false
+)
 ;%0A%09%09
+%09
 %7D%0A
+%09
 %09%7D
-,
 %0A
+%09
 %7D).c
@@ -796,37 +796,11 @@
 on (
-t
 ) %7B
-id = $(this).attr(%22id%22); 
 setT
@@ -810,25 +810,24 @@
 out(function
- 
 () %7Bform_add
@@ -839,18 +839,24 @@
 ar(i
-d
+nput
 )%7D, 
-300) 
+100)%7D);%0A
 %7D);%0A
@@ -882,17 +882,20 @@
 avatar(i
-d
+nput
 ) %7B%0A%09nam
@@ -902,22 +902,70 @@
 e = 
-$(%22#%22+id).val(
+input.val();%0A%09avatar_holder = $(%22#%22+input.attr('id')+%22_avatar%22
 );%0A%09
@@ -993,31 +993,33 @@
 %7B%0A%09%09if (
-ac_selected_
+input.data('
 url
+')
  != fals
@@ -1027,31 +1027,33 @@
 ) url = 
-ac_selected_
+input.data('
 url
+')
 ;%0A%09%09else
@@ -1117,35 +1117,33 @@
 e=20%22%0A%09%09
-$(%22#%22+id+%22_avatar%22)
+if (avatar_holder
 .attr(%22s
@@ -1149,36 +1149,52 @@
 src%22
-,
+) !=
  url)
-;%0A%09%09$(%22#%22+id+%22_avatar%22
+ %7B%0A%09%09%09avatar_holder.attr(%22src%22, url
 ).cs
@@ -1221,16 +1221,20 @@
 ible');%0A
+%09%09%7D%0A
 %09%7D else 
@@ -1241,27 +1241,21 @@
 %7B%0A%09%09
-$(%22#%22+id+%22_avatar%22)
+avatar_holder
 .css
@@ -1282,37 +1282,54 @@
 en')
-;%0A%09%7D%0A%09ac_selected_url =
+.attr(%22src%22, %22%22);%0A%09%7D%0A%09input.data(%22url%22,
  false
+)
 ;%0A%7D%0A

Solved the conflict with several input and avatars in the same form