/*
* Custom jQuery functions
**/

$(document).ready(function() {	
	// searchField
	var searchFieldText = 'Zoeken...';
	resetValue('searchField', searchFieldText);
	
	$('#searchField').bind('focus', function(event) {
		emptyValue('searchField', searchFieldText);
	});
	$('#searchField').bind('blur', function(event) {
		resetValue('searchField', searchFieldText);
	});
	$('#searchForm').bind('submit', function(event) {
		emptyValue('searchField', searchFieldText);
	});
	
	externalLinks();
});


/**
 * Empty the value string of an inputfield, but only if the value equals a certain (standard) text
 */
function emptyValue(elm, text){
	if($('#'+elm).val()==text)
		$('#'+elm).val('');	
}

/**
 * Set the value string of an inputfield to a certain (standard) text, but only if the value is empty
 */
function resetValue(elm, text){
	if($('#'+elm).val()=='')
		$('#'+elm).val(text);
}

/**
 * Adds target="_blank" to links with rel="external" and rel="erternal nofollow"
 */
function externalLinks() {
	$('a[rel*="external"]').attr('target', '_blank');
}

/* Sitemap */
function toggleSitemap(id, aId) {
	if($('#'+id).css('display') == 'none') {
		$('#'+id).css('display','block');
		$('a#'+aId+' img.toggleButton').attr('src',imgPath+'sitemap/min.png');
	}else{
		$('#'+id).css('display','none');
		$('a#'+aId+' img.toggleButton').attr('src',imgPath+'sitemap/plus.png');
	}
}

/**
 * Fill values of a jsonString into a selectlist
 * 
 * @param valuesJson 	-> json array with values [value (id),text]
 * @param selector		-> selectlist to fill, ie 'select#myList'
 * @param valueToSelect -> Select this value in the selectlist
 */
function fillValuesInSelectlist(valuesJson,selector,valueToSelect) {
	var optionsHTML = '';
	for(var i in valuesJson) {
		optionsHTML += '<option value="'+valuesJson[i][0]+'">'+addSlashes(valuesJson[i][1])+'</option>';
	}
	$(selector).append(optionsHTML);
	if(selectedValue != '') {
		$(selector).val(valueToSelect);
	}
}

/**
 * Select a value in a selectlist
 * @param selectlistId	->	ID of the selectlist
 * @param valueToSelect	->	The value to select
 */
function selectValueInSelectlist(selectlistId,valueToSelect) {
	$('select#'+selectlistId).val(valueToSelect);
}

/**
 * Escape quotes with leading slashes
 */
function addSlashes(input) {
	var output = input;
	output = output.replace("'","\'");
	output = output.replace('"','\"');
	return output;
}
