(function($){
	$(document).ready(function() {
		//generate a full zend url using / instead of & and =
		$(".search-form").submit(function(){
			var url = '';
            var collection = false;
            var provincie = '';
            var plaats ='';
            var zoekterm = '';
            var container = '';
			$(this).find('input').each(function(){
              if ( $(this).attr('name').length
					&& $(this).attr('alt') != $(this).val()
				) {
                    if ($(this).attr('name') != 'where') {
                        if ($(this).attr('name') == 'city_container') {
                            container = $(this).val();
                        }
                        if ($(this).attr('name') == 'provincie') {
                            provincie = $(this).val();
                        }
                        if ($(this).attr('name') == 'plaats') {
                            plaats= $(this).val();
                        }
                        if ($(this).attr('name') == 'zoekterm') {
                            zoekterm = $(this).val();
                        }
                    }else if ($(this).attr('name') == 'where') {
                        if (($("input[@name='where']:checked").val() == 'result') && (container == plaats)) {
                            collection = true;
                        }
                    }
				}

			});

            var questionmark= false;//(url.indexOf('?') == -1)?false:true ;
            if (provincie != ''){
                if (collection){
                    url = window.location;
                }
                if (container != '' && !collection) {
                    var newCity = plaats.toLowerCase().replace(/\s/g,'-').replace(/[^a-z-]/,'') + '-('+provincie.toLowerCase()+')';
                    url.replace(container, newCity);
                }else {
                    url = url + '/'+plaats.toLowerCase().replace(/\s/g,'-').replace(/[^a-z-]/,'') + '-('+provincie.toLowerCase()+')';
                }

                collection = true;
            } else if (provincie == '' && plaats != ''){
                url = '?plaats='+plaats;
                questionmark = true;
            }

            if(zoekterm != '') {
                if (!questionmark) {
                    url = url + '?zoekterm='+zoekterm;
                } else {
                    url = url + '&zoekterm='+zoekterm;
                }
            }

            if (!collection){
                url = '/'+url;
            };

            url = $(this).attr('action') + url;
			window.location.href = url;
			return false;
		});
		$(".search-form input[name=plaats]").autoComplete({
			cache: true,
			suggest: '/zkn/search/index/?zoekterm={query}'
		});
	});
})(jQuery);
