$().ready(function() {

	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	
	$("#author").autocomplete(authors, {
		minChars: 1,
		width: 310,
		matchContains: "word",
		autoFill: false,
		formatItem: function(row, i, max) {
			return  row.name;
		},
		formatMatch: function(row, i, max) {
			return row.to + " " + row.name;
		},
		formatResult: function(row) {
			return row.name;
		}
	});
	
	$("#author").result(log).next().click(function() {
		$(this).prev().search();
	});
	
	$("#author").result(function(event, data, formatted) {

		var hidden = $("#author_id");
		hidden.val( data.id);
	});
	
	$("#book").autocomplete(books, {
		minChars: 1,
		width: 310,
		matchContains: "word",
		autoFill: false,
		formatItem: function(row, i, max) {
			a = row.name;
			//if (row.year>0) a = a + ' (' + row.year + ')';
			return  a;
		},
		formatMatch: function(row, i, max) {
			return row.to + " " + row.name;
		},
		formatResult: function(row) {
			a = row.name;
			//if (row.year>0) a = a + ' (' + row.year + ')';
			return  a;
		}
	});
	
	$("#book").result(log).next().click(function() {
		$(this).prev().search();
	});
	
	$("#book").result(function(event, data, formatted) {
		var hidden = $("#book_id");
		hidden.val( data.to);
	});
	
	function log(event, data, formatted) {
		//alert(formatted);
	}
});