
var hideCollapsed = function() {
	$('fieldset.collapsed:not(.search-advanced)').each(function (i) {
		var fs = $(this);
		$(this).children('*:not(legend)').hide();
		$(this).find('legend').css({
			cursor: 'pointer'
		}).click(function(){
			fs.children('*:not(legend)').slideToggle();
			fs.toggleClass('collapsed');
		});
	});
}



var sitemap = function() {
	$('.article li.expanded').each(function (i) {
		$(this).css('position', 'relative').append(
			$('<a/>').attr('title', 'Open en sluit dit deel van de sitemap').css({
				display: 'block',
				position: 'absolute',
				left: '0',
				top: '0',
				cursor: 'pointer',
				width: '12px',
				height: '15px'
			}).click(function(){
				$(this.parentNode).children('ul').slideToggle();
				$(this.parentNode).toggleClass('expanded');
			})
		);
	});
}



var externalLinks = function() {
	$('#main 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').prepend('<li class="nav-print"><a href="#">Print</a></li>');
	$('#extranav .nav-print a').click(function() {
		if (window.print)
			window.print();
	});
}



function checkSize() {
	if ($(window).width() < 956)
		$('body').addClass('smallscreen');
	else
		$('body').removeClass('smallscreen');
}



function focusLogin() {
	$('#edit-name').focus();
	$('#edit-name').select();
}



$(document).ready(function() {
	$('body').addClass('js-active');
	checkSize();
	addPrint();
	externalLinks();
	smoothAnchors();
	hideCollapsed();
	sitemap();
	focusLogin();
	$('.slideshow').cycle({
		timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         1000,  // speed of the transition (any valid fx speed value) 
		pause:         1     // true to enable "pause on hover" 
	})
});
$(window).resize(function() {
	checkSize();
});

