/*
* Custom jQuery functions
**/

$(document).ready(function() {	
	// searchField
	var searchFieldText = 'zoek hele site';
	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); });
	
	var vacancySearchFieldText = 'Wat? (bv functie of vakgebied)';
	resetValue('vacancySearchField', vacancySearchFieldText);
	$('#vacancySearchField').bind('focus', function(event) { emptyValue('vacancySearchField', vacancySearchFieldText); });
	$('#vacancySearchField').bind('blur', function(event) { resetValue('vacancySearchField', vacancySearchFieldText); });
	$('#vacancySearch').bind('submit', function(event) { emptyValue('vacancySearchField', vacancySearchFieldText); });
	
	var nameFieldText = 'naam';
	resetValue('nameField', nameFieldText);
	$('#nameField').bind('focus', function(event) { emptyValue('nameField', nameFieldText); });
	$('#nameField').bind('blur', function(event) { resetValue('nameField', nameFieldText); });
	
	var telephoneFieldText = 'telefoon';
	resetValue('telephoneField', telephoneFieldText);
	$('#telephoneField').bind('focus', function(event) { emptyValue('telephoneField', telephoneFieldText); });
	$('#telephoneField').bind('blur', function(event) { resetValue('telephoneField', telephoneFieldText); });
	
	var emailFieldText = 'e-mail';
	resetValue('emailField', emailFieldText);
	$('#emailField').bind('focus', function(event) { emptyValue('emailField', emailFieldText); });
	$('#emailField').bind('blur', function(event) { resetValue('emailField', emailFieldText); });
	
	$('#vacancyForm').bind('submit', function(event) {
		emptyValue('nameField', nameFieldText);
		emptyValue('telephoneField', telephoneFieldText);
		emptyValue('emailField', emailFieldText);
	});
	
	$('.backlink').bind('click', function(event) {
		history.go(-1);
	});

	equalHeights();
	externalLinks();
	if($.browser.msie && $.browser.version=='6.0') {
		parseLists('menu');
	}
});


/**
 * 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;
}



function equalHeights(){
	var bodyHeight = $('#vacancyBody #contentHolder').height();
	var blockHeight = $('#vacancyBody #vacanciesBlock').outerHeight();
	if(bodyHeight>blockHeight){
		$('#vacancyBody #vacanciesBlock').height(bodyHeight+14);
	}else{
		if($.browser.msie && $.browser.version=='7.0') {
			$('#vacancyBody #contentHolder').height(blockHeight+50);
		}else{
			$('#vacancyBody #contentHolder').height(blockHeight);
		}
	}
	
}

function checkCV() {
	if(document.getElementById('cv_ja').checked){
		document.getElementById('upload_cv').style.display = 'none';
	}else{
		document.getElementById('upload_cv').style.display = 'block';
	}
	equalHeights();
}

function createRSSlink(){
	var sel = document.getElementById('rss_select');
	myvars = '';
	for (i = 1; i < sel.length; i++) {
		if (sel[i].selected) {
			if(myvars==''){
				myvars = '?vakgebied_ids=';
			}
			myvars = myvars + sel[i].value + ',';
		} 
	}
	myvars = myvars.substring(0,myvars.length-1);
	if (sel[0].selected) { myvars = ''; }
	rsslink = rootPath+'rss/vacatures.php' + myvars;
	document.getElementById('rss_link').innerHTML = rsslink;
	document.getElementById('rss_link').href = rsslink;
}

