﻿$(document).ready(function () {
	var txtZoeken = $("input[id*=txtZoekTerm]");
	var position = txtZoeken.position();
	txtZoeken.autocomplete({ source: getData });

	var txtHeaderZoeken = $("input[zoekGsa=true]");
	if (txtHeaderZoeken.length > 0) {
		txtHeaderZoeken.autocomplete({ source: getHeaderdata });
	}
});

function getHeaderdata(req, add) {
	addSuggestions('', req, add);
}

function getData(req, add) {
	var collection = "";
	var selectedCollection = $("input[name*=rblZoekCategorie]:checked");
	if (selectedCollection.length > 0) {
		collection = selectedCollection.val();
	}
	addSuggestions(collection, req, add);
}

function addSuggestions(collection, req, add) {
	$.getJSON("GsaAutoComplete.ashx?subsite=" + $("input[id*=hfSubsite]").val() + "&time=" + new Date().getMilliseconds() + "&collection=" + collection, req, function (data) {
		data = eval(data);
		//create array for response objects  
		var suggestions = [];

		//process response  
		$.each(data, function (i, val) {
			suggestions.push(val);
			//alert(val);
		});
		add(suggestions);
	});
}
