$(document).ready(function() {

	var searchterm = 'Suchbegriff';
	$("#filmsearch").focus(function () {
		if ($("#filmsearch").val() == searchterm) {
			$("#filmsearch").val('');
		}
	});

	$("#filmsearch").blur(function () {
		if ($("#filmsearch").val() == '') {
			$("#filmsearch").val(searchterm);
		}
	});

	$("#filmsearch").autocomplete102(
		filmtitles,
		{
			delay:0,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:50,
			formatItem:formatItem,
			formatMatch:formatMatch,
			formatResult:formatResult,
			resultsClass: "ac102_results",
			maxItemsToShow:50,
			scroll:false,
			extraParams: { m:'n' }
		}
	);
	
	$("#filmsearch").keyup(function(e) {
		if (e.keyCode == 13) {
			document.location = '/film/' + $("#filmsearch").val();
			//alert($("#urltext").val());
		}
	});

});

function formatItem(row) {
	var result = '<a href="/film/' + row[0] + '">' + row[1];
	if (row[2] != row[1]) result = result + ' (' + row[2] + ')';
	result = result +'</a>';
	return result;
}

function formatMatch(row) {
	return row[1]+';'+row[2];
}

function formatResult(row) {
	//document.location.href = '/film/' + row[0];
	//$("#urltext").val(row[0]);
	return row[0];
	//document.location = '/film/' + $("#filmsearch").val();
}

