var externalLinks = function() {
	$('.article a').each(function (i) {
		if ((this.href.indexOf(document.domain) < 0 && (this.href.indexOf('http://') > -1 || this.href.indexOf('https://') > -1)) || (this.href.indexOf('.pdf') > -1 || this.href.indexOf('.doc') > -1) ) {
			this.target = '_blank';
			this.className += ' external';
			this.title += ' (opent in een nieuw scherm)';
		}
	});
}


function smoothAnchors() {
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			var h = this.hash;
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 750, function(){
					document.location.hash = h;
				});
				return false;
			}
		}
	});
}



function addPrint() {
	$('#extranav').append('<p class="nav-print"><a href="#">Print</a></p>');
	$('#extranav .nav-print a').click(function() {
		if (window.print)
			window.print();
	});
}



function keuzeMenu() {
	if ($('#keuzemenu')) {
		var selectAll = function() { $('#keuzemenu input').attr('checked','checked'); }
		var selectNone = function() { $('#keuzemenu input').attr('checked',''); }
		// select-links onderaan schrijven
		$('#keuzemenu button').before(
			$('<p/>').append(
				$('<a/>').attr('href','#').text('Alles selecteren').click(function(){
					selectAll();
					return false;
				})
			).append(' | ').append(
				$('<a/>').attr('href','#').text('Selectie ongedaan maken').click(function(){
					selectNone();
					return false;
				})
			)
		);
		// hoofddoelgroep selecteren bij selectie subdoelgroep
		$('#keuzemenu ul ul input').click(function(){
			if ($(this).attr('checked')) {
				$(this.parentNode.parentNode.parentNode.getElementsByTagName('input')[0]).attr('checked','checked');
			}
		});
		// subdoelgroepen (de)selecteren bij (de)selectie hoofddoelgroep
		$('#keuzemenu ul h2 input').click(function(){
			$(this.parentNode.parentNode.getElementsByTagName('input')).attr('checked',$(this).attr('checked'));
		});
	}
}



function quiz() {
	if ($('.quiz')) {
		$('.quiz ol input').click(function(){
			$('.quiz ol li').removeClass('selected');
			$(".quiz ol input:checked").parent().parent().addClass('selected');
		});
	}
}



function eou() {
	if ($('form.eou').length > 0) {

		// deselect selector when child checkbox is deselected
		$('form.eou li:not(.select-all) input[type="checkbox"]').click(function(){
			if (!$(this).attr('checked')) {
				$('form.eou li.select-all input[type="checkbox"], form.eou div.select-all input[type="checkbox"]').attr('checked','');
				$(this.parentNode.parentNode.parentNode.parentNode.parentNode).find('li.select-row input[type="checkbox"]').attr('checked','');
			}
		});

		// select all checkboxes in row
		$('form.eou li.select-all input[type="checkbox"], form.eou div.select-all input[type="checkbox"]').click(function(){
			if ($(this).attr('checked')) {
				$('form.eou input[type="checkbox"]').attr('checked','checked');
			} else {
				$('form.eou input[type="checkbox"]').attr('checked','');
			}
		});

		// select all checkboxes in row
		$('form.eou li.select-row input[type="checkbox"]').click(function(){
			if ($(this).attr('checked')) {
				$(this.parentNode.parentNode.parentNode.parentNode.parentNode).find('input[type="checkbox"]').attr('checked','checked');
			} else {
				$(this.parentNode.parentNode.parentNode.parentNode.parentNode).find('input[type="checkbox"]').attr('checked','');
			}
		});

	}
}



function toggle() {
	var toggleOpen = function(el) {
		$(el).children('*:not(.toggle-link)').show('fast');
	}
	var toggleClose = function(el) {
		$(el).children('*:not(.toggle-link)').hide('fast');
	}
	if ($('ul.toggle').length > 0) {
		$('ul.toggle>li').each(function (i) {
			var h = $(this).find('h3').text();
			$(this).find('h3').remove();
			$(this).prepend(
				$('<a/>').text(h).addClass('toggle-link').attr('href','#').click(function(){
					$(this.parentNode).toggleClass('open');
					if ($(this.parentNode).hasClass('open'))
						toggleOpen(this.parentNode);
					else
						toggleClose(this.parentNode);
					return false;
				})
			);
			toggleClose(this);
		});
		$('ul.toggle').addClass('toggle-js')
	}
}



$(document).ready(function() {
	$('#sitetrekTekst').css('display', 'none');
	addPrint();
	/*externalLinks();*/
	smoothAnchors();
	keuzeMenu();
	quiz();
	eou();
	toggle();
});
