$(function (){
	
	// mail rewrite (href)
	$('a[href*="mailto:"]').each(function() {
		var mailto = $(this).attr('href').split('[at]');
		$(this).attr("href", mailto[0] + "@" + mailto[1]);
	});
	

	// mail rewrite (text node)
	$('a:contains("[at]")').each(function() {
		var mailto = $(this).text().split('[at]');
		$(this).text(mailto[0] + "@" + mailto[1]);
	});


	// FAQ toggle

	$('ul.faq>li').each(function(index) {
		var $trigger = $('>:first', this);
		var $toggle = $('.toggle', this);

		$("h2").css({
			background: "transparent url('layout/plus-icon.gif') no-repeat 0 3px",
			padding: "0 0 0 1.1667em"
		});
		
		$toggle
			.hide();

		$trigger
			.css("cursor", "pointer")
			.hover(function() {
				$(this).css({
					textDecoration: "underline",
					color: "#f31f2d"
				});
			}, function() {
				$(this).css({
					textDecoration: "none",
					color: "#811518"
				});
				
			})
			.click(function() {
				$toggle.slideToggle("", function () {
					if ($toggle.is(':visible')) {
						$(this).prev().css({ background: "transparent url('layout/minus-icon.gif') no-repeat 0 3px" });
					} else {
						$(this).prev().css({ background: "transparent url('layout/plus-icon.gif') no-repeat 0 3px" });
					}
				});
			});
	});

	// Bereken prijs voor jaar abonnement
	$('form.aanvraagformulier').each(function() {
		var abonnement, offline, prijs, vestiging;
				
		$('[name="abonnement"], [name="offline"], [name="vestiging"]').change(function() {
			setPrice();
		});
		setPrice();
		
		function setPrice(args) {
			if ($('[name="abonnement"]:checked').val() == "normaal") {
				abonnement = 195;
				$('[name="offline"]').removeAttr("disabled");
				offline = $('[name="offline"]:checked').val() == "ja" ? 25 : 0;
				vestiging = (parseInt($('[name="vestiging"]:checked').val()) - 1) * 100;
			} else {
				abonnement = 95;
				$('[name="offline"]').attr("disabled", "disabled");
				offline = 0;
				vestiging = (parseInt($('[name="vestiging"]:checked').val()) - 1) * 75;
			}
			
			$('.jaar-abo-prijs').hide().html("<strong>&euro; " + (abonnement + offline + vestiging) + ",&ndash;</strong>").fadeIn();
			
		}
	});
	
	
	// Add arrows to 'more' links
	$('.more, .back, #header .links a').each(function() {
		$(this).append(" <span>&gt;&gt;</a>");
	});
	
	
	// Vertical Centering
	$(window).each(function() {
		$(this).resize(function() {
			var $container = $('#container');
			var wH = $(window).height();
			var dH = $container.height();
			
			if (wH > dH) {
				$container.css("marginTop", (wH-dH)/2 + "px");
			}
		});
		$(this).trigger('resize');
	});
});
