function is_touch_device() {
	try {
		document.createEvent("TouchEvent");
		return true;
	} catch (e) {
		return false;
	}
}

$(document).ready(function(){

	if(is_touch_device()) {
		$('nav > ul > li > ul').prepend('<li class="first close"><a href="#close">Sluiten</a></li>');
		$('nav > ul > li > ul .close').click(function() {
			$(this).parent().closest('li').removeClass('hover').find('a.open').removeClass('open');
			return false;
		});

		$('nav > ul > li > a').click(function() {
			// Make sure first click doesn't follow link immediatly
			if($(this).hasClass('open')) {
				return true;
			} else {
				$(this).addClass('open').parent().siblings().removeClass('hover').find('a').removeClass('open');
				$(this).closest('li').addClass('hover');
				return false;
			}
		});
	} else {
		// navigation
		$('nav li').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}

	// photos
	$('.photos #content section a').prepend('<span class="hover"></span>');

	$(function() {
		$('div.photos .item').each(function(i){
			if($("a.largeImageLink", $(this)).attr('href')) {
				var largeImageLink = $("a.largeImageLink", $(this)).attr('href');
				$("a:first", $(this)).attr('href', largeImageLink);
				$("a.largeImageLink", $(this)).remove();

				$("div.photos a").fancybox({
					'padding'			: '1',
					'transitionIn'		: 'fade',
					'transitionOut'		: 'fade',
					'titlePosition' 	: 'over',
					'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
						if(currentArray.length > 1) {
							return '<span id="fancybox-title-over">Foto ' + (currentIndex + 1) + ' van ' + currentArray.length + (title.length ? ' &ndash; ' + title : '') + '</span>';
						} else {
							return (title.length ? '<span id="fancybox-title-over">' + title + '</span>' : '');
						}
					}
				});

			}
		});
	});

	// Photo galleries and photos
	if($('.photos .noPhotos').length == 1) $('.photos .noPhotos').remove();
	if($('.photos .noPhotos').length == 2) $('.photos .noPhotos').first().remove();

	// Photo galleries
	if($('.photos .photoGalleries .item').length <= 0) $('.photos .photoGalleries').remove();

	// print button
	$('#pageTools #print a').click(function(){
		window.print();
		return false;
	});

});
