(function($) {
	$.extend( {
		
		config: (function() {
			// Put config settings in configObj (private):
			var confObj = {
				printLabel:		"Print",
				siteSearch:		{
									overLabel:		"Zoek in deze site",
									emptySearch:	"U heeft geen zoekterm opgegeven.\nVul een of meerdere zoektermen in en klik op het vergrootglas, of druk op Enter."
								},
				zwSearch:		{
									form:	"siteSearch",
									input:	"keyword",
									flyout:	"keywordSuggest"
								}
			};
			
			// Private methods:
			var extend = function(conf, callbacks) {
				if (!conf || typeof conf !== "object") { return; }
				var proceed;
				for (var key in conf) {
					proceed = true;
					if (callbacks) {
						// Call appropriate callback function, if specified:
						if (confObj[key] && callbacks.onAlter) { proceed = callbacks.onAlter(key, conf[key], confObj[key]); }
						else if (!confObj[key] && callbacks.onAdd) { proceed = callbacks.onAdd(key, conf[key]); }
					}
					// Set value, unless callback function returned false:
					if (proceed || proceed == null) { confObj[key] = conf[key]; }
				}
			};
			var get = function(key) {
				return key ? confObj[key] : confObj;
			};
			
			// Interface:
			return {extend: extend, get: get};
		})(),
		
		
		
		keywordSuggest: function() {
			var zwForm = $("#" + $.config.get().zwSearch.form);
			var zwInput = $("#" + $.config.get().zwSearch.input);
			var swSuggestID = $.config.get().zwSearch.flyout;
			
			var xhr;
//			var url = $("#zwSearch").attr("action");
			var url = zwForm.attr("action");
			
			zwInput.focus(zwFocus);
			zwInput.blur(zwBlur);
			
			// add suggest container:
			zwInput.wrap('<div></div>').after('<div id="' + swSuggestID + '"></div>');
			var suggest = $("#" + swSuggestID);
			if (suggest.length == 0) { return; }
			
			suggest.find("a").live("click", function(e){
				e.preventDefault();
				zwInput.val($(this).text());
				zwForm.submit();
			}); 
			
			// empty zwSuggest when focus goes elsewhere:
			$(document.body).bind("click", elsewhere);
			$("#headerContent.zoekwijzer *").bind("focus", elsewhere);
			
			function elsewhere(e) {
				if (!$(e.target).is("#" + $.config.get().zwSearch.form + " *")) {
					zwBlur();
					updateSuggest();
				}
			};
			
			function zwFocus() {
				zwForm.addClass("jsFocus");
			};
			
			function zwBlur() {
				zwForm.removeClass("jsFocus");
			};
			
			function updateSuggest(html, prop) {
				suggest.empty();
				if (html) {
					// append suggestions:
					suggest.append(html);
					if (prop) {
						// cache suggestions:
						suggest.data(prop, html);
					}
				}
			};
			
			function passFocus(jNode, next) {
				var toItem;
				
				if (jNode && jNode.filter("a").length > 0) {
					// focus is on link
					var fromItem = jNode.closest("li");
					toItem = next ? fromItem.next() : fromItem.prev();
				}
				else if (next) {
					// focus is on keywords input
					toItem = suggest.find("li:first");
				}
				
				if (toItem && toItem.length > 0) {
					zwInput.blur();
					toItem.find("a:first").focus();
				}
				else if (!next) {
					zwInput.focus();
				}
			};
			
			zwForm.keyup(function(e){
					var jTarget = $(e.target);
					
					if (e.keyCode == 38 || e.keyCode == 40) {
						// arrow keys
						var next = (e.keyCode == 38) ? false : true;
						passFocus(jTarget.filter("input:text, a"), next);
					}
					else if (jTarget.attr("id") == $.config.get().zwSearch.input) {
						// focus is on keywords input
						var val = jTarget.val();
						
						// make suggestion only if input contains 2 or more characters:
						if (val.length > 1) {
							var cache = suggest.data(val);
							if (cache) {
								// update from cache:
								updateSuggest(cache);
							} else {
								if (xhr) {
									// abort previous ajax call:
									xhr.abort();
								}
								
								var data = { ajax: true,type: 'keyword' };
								data[$.config.get().zwSearch.input] = val;
								// make ajax call:
								xhr = $.ajax({
									url:		url,
									type:		"POST",
									data:		data,
									timeout:	1000,
									dataType:	"html",
									error:		function() {
													updateSuggest();
												},
									success:	function(data) {
													updateSuggest(data, val);
												}
								});
							}
						} else {
							updateSuggest();
						}
					}
				});
		}
		
		
	} );
} )(jQuery);


jQuery(function($) {
	
	// extend $.config with window.config (if any):
	$.config.extend(window.config);

	// keywordSuggest:
	$.keywordSuggest();

	$('.submit').hide();
	
	$('#select_zorg, #select_aard, #straal, #pc, #zoek_zorgaanbieder_home2 input:checkbox, #zoek_zorgaanbieder_home input:checkbox').change(function(e) {
		// de onclick uitschakelen in javascript aan staat
		e.preventDefault();

		if (this.id == 'pc' && this.value.length != 4) {
			return;
		}
		// zoek_zoegaanbieder_home == form-id van pgb-item op homepage 
		// zoek_zoegaanbieder_home2 == form-id van startpagina van vraagenaanbod
		// serialize bouwt een query-string van alle aanwezige form velden (handig!) 
		var data = "type=count_aanbieders&ajax=true&" + $('form#zoek_zorgaanbieder_home, form#zoek_zorgaanbieder_home2').serialize();
		
		$.ajax({
			type: "POST",
			url: "pgb_vraagenaanbod_ajax.php",
			data: data,
			success: function(html) {
				$('#aantal').empty();
				$('#aantal').append(html);
			}
		});
	});
	

	$('#submitlink').click(function(e) {
		// de onclick uitschakelen in javascript aan staat
		e.preventDefault();
		
		// submit het formulier
		$('form[name=venaForm]').submit();
	});
	
});
