$(document).ready(function() {
	// functie
	autoFill('snelzoeken_functie', 'Functie of referentienummer...', '.');
	
	// plaats
	autoFill('snelzoeken_plaats', 'Plaats of postcode...', '.');
	
	$('#sorteer_vacatures').change(function() {
		window.location = cur_url + sorteerVacatures();
	});
	
	$('#snelzoeken input:checkbox').click(function() {
		window.location = cur_url + filterOptie(this);
	});
	
	// emailservice
	autoFill('form_email', 'gebruiker@domein.nl', '#');
	
	// controleer emailservice
	$('.emailrss').submit(function () {
		if ($('#form_email').val() == 'gebruiker@domein.nl') {
			alert('Dit is geen geldig e-mailadres');
			
			return false;
		} else {
			$('.emailrss').submit();
		}
	});
});

function autoFill(veld, invoer, type) {
	if (type != '.') {
		type = '#';
	}
	
	$(type+veld).each(function() {
		// standaard tekst
		if ($(this).val() == invoer || $(this).val() == '') {
			$(this).val(invoer).css('color','gray');
		}
		
		// onfocus
		$(this).focus(function() {
			if ($(this).val() == invoer) {
				$(this).val('').css('color','black');
			}
		});
		
		// onblur
		$(this).blur(function() {
			if ($(this).val() == '') {
				$(this).val(invoer).css('color','gray');
			}
		});
	});
}


function sorteerVacatures() {
	// declare vars
	var url = window.location.search.substring(1);
	var foundSorteer = 0;

	// haal huidige value op van de select
	var select_value = $('#sorteer_vacatures').val();

	url_array = url.split('&');
	
	for (var sorteer in url_array) {
		// splitten van het '=' teken
		var split = url_array[sorteer].split('=');
		
		if (split[0] == 'sorteer') {
			url_array[sorteer] = split[0] + '=' + select_value;
			foundSorteer = 1;
		}
		if (split[0] == 'sortering') {
			url_array[sorteer] = split[0] + '=aflopend';
			foundSorteer = 1;
		}
		
		if (foundSorteer == 1)
			break;
	}
	
	if (foundSorteer == 0) {
		url_array.push('sorteer=' + select_value);
		url_array.push('sortering=aflopend');
	}
	
	var new_url = url_array.join('&');

	return new_url;
}

function filterOptie(checkbox) {
	// declare vars
	var url = window.location.search.substring(1);
	var foundFacet = false;
	var checkBox = $(checkbox);
	
	if (checkBox.parent().prev().prev().attr('nodeName') == 'DIV') {
		facet = checkBox.parent().prev().prev().prev().html();
	} else {
		facet = checkBox.parent().prev().prev().html();
	}
	
	//facet = facet.replace(/\s/g, '+');
	
	// items voor in de url
	var value = checkBox.attr('name');
	var set = checkBox.is(':checked');

	facet = facet.replace(/\s/g, '+');
	value = encodeURIComponent(value);

	url_array = url.split('&');
	if (set) {
		url_array.push(facet + '=' + value);
	} else {
		for (var sorteer in url_array) {
			if ((facet + '=' + value) == url_array[sorteer]) {
				url_array.splice(sorteer, 1);
			}
		}
	}
	
	for (var sorteer in url_array) {
		var facetArray = url_array[sorteer].split('=');
		if (facetArray[0] == 'facet') {
			url_array.splice(sorteer, 1);
		}
	}
	var new_url = url_array.join('&') + '&facet=' + facet;

	return new_url;
}

function toggleVerfijn(obj) {
	
	var _parent;
	var _label;
	
	obj = $(obj);
	_label = obj.parent().prev();
	_parent = obj.parent().next().next();
	
	if (_parent.is(':visible')) {
		_parent.hide();
		obj.html('+');
	} else {
		_parent.show();
		obj.html('-');
	}
}
