function centerPopup(id) {//Cancel the link behavior		
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width(); 
	
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
		   
	//Set the popup window to center
	$(id).css('top',  winH/2-$(id).height()/2)
		.css('left', winW/2-$(id).width()/2) 
		.show();
}
$(function() {
	$('body').append("<div id=\"email-popupcontainer\">\
		<div id=\"email-popup\">\
			<div id=\"email-popuphead\">\
				<h4>Breng een vriend op de hoogte</h4>	\
				<a href=\"#\" onclick=\"$('#email-popupcontainer').hide();return false;\">X</a>\
			</div>\
			<div id=\"email-popupbody\">			\
				<form>\
					<label for=\"email_to\">Naar:  <span>(email)</span></label>\
					<textarea id=\"email_to\" name=\"email_to\" cols=\"30\" rows=\"4\" style=\"resize:none\"></textarea>\
					<label for=\"email_from\">Van:  <span>(jouw email)</span></label>\
					<input id=\"email_from\" name=\"email_from\" type=\"text\"size=\"30\">\
					<label for=\"email_msg\">Uw bericht:  <span>(optioneel)</span></label>\
					<textarea id=\"email_msg\" name=\"email_msg\" style=\"resize:none\" cols=\"30\" rows=\"4\"></textarea>\
					<div style=\"text-align: right;\"><small>255 character limit</small></div>\
					<div>\
						<input class=\"btn\" id=\"mail-send\" type=\"button\" tabindex=\"339\" value=\"Verzend\">\
						<input class=\"btn rse\" type=\"reset\" tabindex=\"340\" value=\"wissen\" onclick=\"$('#email-popupcontainer').hide();return false;\">\
						<img class=\"loading-small\" src=\"images/loading_small.gif\" alt=\"Loading...\" style=\"float: right;\"/>\
					</div>\
				</form>\
			</div>\
		</div>\
	");
		
	//mail-function
	$('.mail-button').click(function() {
		$('#email_to').val('');
		$('#email_msg').val('');
		centerPopup('#email-popupcontainer');
		return false;
	});
	$('#mail-send').click(function() {		
		$('#email-popupbody .loading-small').show();
		$.ajax({
			url: '/mail-ajax.php',
			type: 'POST',
			dataType: 'json',
			data: $('#email-popupbody form').serialize() + '&url=' + $('#res-url').val(),
			success: function(res) {
				$('#email-popupbody .loading-small').hide();
				if(res.error)
					alert(res.code);
				else {
					alert('Bedankt, uw bericht werd verzonden.');
					$('#email-popupcontainer').hide();
				}					
			},
			error: function() {
				$('#email-popupbody .loading-small').hide();
				alert('Sorry! Er was een probleem. Probeer nogmaals.');
			}
		});
		return false;
	});	
});
