/*
* 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);
	});
	
	
	if(!Modernizr.input.placeholder) {
		$("input[placeholder], textarea[placeholder]").each(function() {
			if($(this).val()==""){
				$(this).val($(this).attr("placeholder"));
				$(this).focus(function(){
					if($(this).val()==$(this).attr("placeholder")) {
						$(this).val("");
						$(this).removeClass('placeholder');
					}
				});
				$(this).blur(function(){
					if($(this).val()==""){
						$(this).val($(this).attr("placeholder"));
						$(this).addClass('placeholder');
					}
				});
			}
		});
		$('form').submit(function(){
			// first do all the checking for required element and form validation.
			// Only remove placeholders before final submission
			var placeheld = $(this).find('[placeholder]');
			for(var i=0; i<placeheld.length; i++){
				if($(placeheld[i]).val() == $(placeheld[i]).attr('placeholder')) {
					// if not required, set value to empty before submitting
					$(placeheld[i]).attr('value','');
				}
			}
		});
	}
	// iPad swipe
	
	
	$('#container').bind('swiperight swiperightup swiperightdown',function(){
		//alert('right');
		var prev = $("#btnPrevious");
		if (prev.length) {
			var prevurl = $(prev).attr("href");
			window.location.href = prevurl;				
		}
	});
	$('#container').bind('swipeleft swipeleftup swipeleftdown',function(){
		//alert('left');
		var next = $("#btnNext");
		if (next.length) {
			var nexturl = $(next).attr("href");
			window.location.href = nexturl;
		}
	});
	
	
	/*
	$(document).bind("mobileinit", function(){
		alert('asdf');
		$.extend(  $.mobile , {
			linkBindingEnabled: false,
			hashListeningEnabled: false,
			ajaxEnabled: fale,
			subPageUrlKey: false
		});
	});
	
	$('#container').live('swipeleft swiperight',function(event){
		if (event.type == "swiperight") {
			var prev = $("#btnPrevious");
			if (prev.length) {
				var prevurl = $(prev).attr("href");
				window.location.href = prevurl;				
			}
		}
		if (event.type == "swipeleft") {
			var next = $("#btnNext");
			if (next.length) {
				var nexturl = $(next).attr("href");
				window.location.href = nexturl;
			}
		}
		event.preventDefault();
	});
	*/
	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;
}






