/* REQUIRE: jquery.bgiframe.min.js */
/* REQUIRE: jquery.ajaxQueue.js */
/* REQUIRE: thickbox-compressed.js */
/* REQUIRE: jquery.autocomplete.js */
$(document).ready(function () {
	$(".JSuggest_input").focus(jsuggestFocus);
	$(".JSuggest_input").change(jsuggestFocus);
});

function jsuggestFocus() {
	jSuggestId = $(this).parent().children("input.JSuggest_id").val();
	jSuggestLink = $(this).parent().children("input.JSuggest_url").val();
	jSuggestFreeChoice = Boolean($(this).parent().children("input.JSuggest_fc").val());
	
	$(this).autocomplete(jSuggestLink, {
		multiple: false,
		scrollHeight: 200,
		mustMatch: !jSuggestFreeChoice,
		max: 15,
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row.id,
					result: row.value
				}
			});
		},
		formatItem: function(item) {
			return item.value;
		}
	}).result(function(e, item) {
		$(this).parent().children("input.JSuggest_result").val(item.id);
		
		if(item.value2 == undefined)
			item.value2 = -1;
		
		$('input[name=JSuggest_value2]').val(item.value2);
		
		if($('input[name=JSuggest_value2]').val() == undefined) {
			$('.Suggest_container').append('<input name="JSuggest_value2" type="hidden" value="' + item.value2 + '" />');
		}
	});
}