
$(document).ready(function() {
	navigation();
	setNavigation();

	$('.share #social').click(function(){
		$('.tx-wind-shareorprint .media-icon').toggle();
		return false;
	});
});

$(window).resize(function() {
	setNavigation();
});

function setNavigation() {
	if ($('.navbar-toggle').is(':visible')) {
		var windowHeight = $(window).height();
		$('nav>ul').css({'height':windowHeight-75+'px'});
		$('nav>ul>li>ul').css({'height':windowHeight-75+'px'});
		if ($('body').hasClass('fixedBody')) {
			return false;
		} else {
			$('nav').css({'left':100+'%'});
			$('nav>ul>li>ul').css({'left':100+'%'});
		}

		$('nav>ul>li>ul').each(function() {
			$(this).siblings('a').addClass('subNav');
		});

		$('.subNav').click(function() {
			$(this).siblings('ul').animate({'left':25+'%'}, 300);
			$('nav').addClass('subNav');
			return false;
		});
	}
	if ($('.navbar-toggle').is(':hidden')) {
		$('body').removeClass('fixedBody');
		$('.wrapper').css({'left':0+'%'});
		$('nav').removeClass('subNav');
		$('nav>ul').css({'height':'auto','left':0+'%'});
		$('nav>ul>li>ul').css({'height':'auto'});
	}
}

// function to open navigation
function navigation() {
	$('.navbar-toggle').click(function() {
		$('body').toggleClass('fixedBody');
		if($('body').hasClass('fixedBody')) {
			$('.wrapper').animate({'left':'-'+75+'%'}, 300);
			$('nav').animate({'left':25+'%'}, 300);
		} else {
			$('nav').removeClass('subNav');
			$('.wrapper').animate({'left':0+'%'}, 300);
			$('nav').animate({'left':100+'%'}, 300);
			$('nav>ul >li>ul').animate({'left':100+'%'}, 300);
		}
	});
}

$(function () {
	var tag = 'div.tx-windraadsmoelenboek-pi1 div.single div.pi_container div.personal_info div.telefoonnummer span';
	var count = $(tag).text();
	if (count === 'T: ') {
		$(tag).addClass('hide');
	} else {
		$(tag).addClass('show');
	}

	var tagMobile = 'div.tx-windraadsmoelenboek-pi1 div.single div.pi_container div.personal_info div.mobile span';
	var countMobile = $(tagMobile).text();
	if (countMobile === 'M: ') {
		$(tagMobile).addClass('hide');
	} else {
		$(tagMobile).addClass('show');
	}
});

$(function() {
	$("#accordionblocks").accordion({
		active: false,
		collapsible: true,
		heightStyle: 'content'
	});
});

jQuery(function($) {
	var url = window.location.href;

	var solrContainers = $('div#tx-solr-search ol.results-list');
	solrContainers.each(function() {
		var working = false;
		var getNextSolrPage = function() {
			if (!working && lastPage > currentPage) {
				working = true;
				solrContainer.addClass('loading');
				$.ajax(url, {
					data: {
						tx_solr: {
							page: currentPage + 1
						}
					},
					dataType: 'html',
					complete: function(jqXHR, textStatus) {
						solrContainer.append($(jqXHR.responseText).find('div#tx-solr-search ol.results-list').html());
						currentPage++;
						working = false;
						solrContainer.removeClass('loading');
					}
				});
			}
		}

		var solrContainer = $(this);

		var pageBrowser = solrContainer.siblings('div.pagebrowser');
		var currentPage = parseInt($('li.tx-pagebrowse-current', pageBrowser).text()) - 1;
		// Only work when current page is 0
		if (currentPage === 0) {
			var lastPageUrl = decodeURIComponent($('li.tx-pagebrowse-last a', pageBrowser).attr('href'));
			if (lastPageUrl.indexOf('tx_solr[page]=') > -1) {
				var lastPage = parseInt(lastPageUrl.substr(lastPageUrl.indexOf('tx_solr[page]=') + 14));

				// No need to do anything if there is only 1 page
				if (lastPage > 0) {
					$(window).scroll(function() {
						var win = $(window);
						if (win.scrollTop() + win.height() > $(document).height() - 200) {
							getNextSolrPage();
						}
					});
				}
			}

			pageBrowser.hide();
		}
	});
});

jQuery(document).ready(function() {
	jQuery('a.colorbox').colorbox({
		maxWidth: '100%',
		maxHeight: '100%'
	});

	jQuery('.inline').colorbox({
		inline: true,
		width: '50%'
	});

	$('div.news-item div.image a.news-colorbox').each(function() {
		var el = $(this);
		var elTitle = el.parent().next();

		el.colorbox({
			maxWidth: '100%',
			maxHeight: '100%',
			title: elTitle.text()
		});
	});
});
jQuery(function() {
	jQuery('body').addClass('jsenabled');

	if (jQuery('.tx-wind-shareorprint').length != 0) {
		var shareorprint = jQuery('.tx-wind-shareorprint');
		var sharebuttons = jQuery('.buttonContainer .sharebuttons', shareorprint).clone();

		/* Add extra buttons and links */
		jQuery('.buttonContainer .buttons', shareorprint).append('<a href="#" id="sharethispage"><span>Dit artikel delen</span></a>');
		jQuery('form', shareorprint).append('<input type="hidden" value="1" name="tx_windshareorprint_shareorprint[javascriptSubmit]" />');
		jQuery('body').append('<div id="hiddenshareoptions"><div id="sharecontent" /></div>');

		/* Hide default share options and move this to a hidden div */
		jQuery('#hiddenshareoptions').hide();
		jQuery('#hiddenshareoptions #mailformcontent').prepend(jQuery('.buttonContainer .sharebuttons, .currenturl, form', shareorprint));

		/* Add link to mailform and select link button */
		var sharecontent = jQuery('#mailformcontent');
		jQuery('.sharebuttons', sharecontent).append('<a class="mail" href="#mailform">Stuur door via e-mail</a>');
		jQuery('.currenturl', sharecontent).append('<button id="selectdirectlink">Link selecteren</button>');

		/* Select value in the input field for direct link */
		jQuery('#selectdirectlink').click(function() {
			jQuery('.currenturl input', sharecontent).select();
		});

		/* Show popup with the share options */
		jQuery('#sharethispage').click(function() {
			jQuery('form', sharecontent).hide();
			jQuery('div.sharebuttons, div.currenturl', sharecontent).show();
		});

		jQuery('#sharethispage').colorbox({
			width: "540px",
			height: "250px",
			opacity:0.25,
			inline: true,
			href: '#mailformcontent',
			onLoad: function() {
				jQuery('#cboxTopCenter').addClass('showtitle').html('<div class="title">Deel deze pagina</div>');
			}
		});

		/* Actions in popup. A link that starts with http is opening in a popup and a link with # remains in the popup. */
		jQuery('.sharebuttons a', sharecontent).click(function(e) {
			var href = jQuery(this).attr('href');

			if (href.substring(0,1) == '#') {
				jQuery('form', sharecontent).show();
				jQuery('div.sharebuttons, div.currenturl', sharecontent).hide();
				parent.$.fn.colorbox.resize({innerWidth: 540, innerHeight: 440});

			} else if (href.substring(0,4) == 'http') {

				var windowWidth = 540;
				var windowHeight = 500;

				var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
				var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;

				var left = ((screen.width / 2) - (windowWidth / 2)) + dualScreenLeft;
				var top = ((screen.height / 2) - (windowHeight / 2)) + dualScreenTop;

				var newWindow = window.open(href, '', 'scrollbars=yes, width=' + windowWidth + ', height=' + windowHeight + ', top=' + top + ', left=' + left);

				parent.$.fn.colorbox.close();
			}

			e.preventDefault();
			return false;
		});

		/* Catch the submit of the mailform and send in through AJAX. */
		jQuery('#mailformcontent form').submit(function(e) {
			jQuery.ajax({
				type: 'POST',
				url: jQuery(this).attr('action'),
				data: jQuery(this).serialize(),
				success: function(data) {
					var message = 'Het versturen is niet gelukt. Controleer de velden.';
					var messageField = jQuery('#mailformcontent form div.buttons div.message');
					if (data == 1) {
						var message = 'Het versturen is gelukt.';
					}

					if (messageField.length == 0) {
						jQuery('#mailformcontent .from_info').prepend('<div class="message" />');
					}

					jQuery('#mailformcontent .from_info div.message').html(message);
				}
			});

			e.preventDefault();
			return false;
		});
	}
});
jQuery(function() {
	if (jQuery('#mapLink').length > 0) {
		var mapLink = document.getElementById('mapLink');
		jQuery('<iframe src="' + mapLink.href + '" style="width: ' + mapLink.style.width + '; height: ' + mapLink.style.height + ';" scrolling="no">').appendTo('#mapDiv');
		jQuery('#mapLink').remove();
	}
});
jQuery(document).ready(function(){

	// Change back to the old behavior of auto-complete
	// http://jqueryui.com/docs/Upgrade_Guide_184#Autocomplete
	jQuery.ui.autocomplete.prototype._renderItem = function( ul, item ) {
			return jQuery( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a>" + item.label + "</a>" )
				.appendTo( ul );
	};

	var req = false;

	jQuery('.tx-solr-q').autocomplete({
		source: function(request, response) {
			if (req) {
				req.abort();
				response();
			}

			req = jQuery.ajax({
				url: tx_solr_suggestUrl,
				dataType: 'json',
				data: {
					termLowercase: request.term.toLowerCase(),
					termOriginal: request.term,
					L: jQuery('div.tx-solr input[name="L"]').val()
				},
				success: function(data) {
					req = false;

					var rs     = [],
						output = [];

					jQuery.each(data, function(term, termIndex) {
						output.push({
							label : term.replace(new RegExp('(?![^&;]+;)(?!<[^<>]*)(' +
										jQuery.ui.autocomplete.escapeRegex(request.term) +
										')(?![^<>]*>)(?![^&;]+;)', 'gi'), '<strong>$1</strong>'),
							value : term
						});
					});

					response(output);
				}
			});
		},
		select: function(event, ui) {
			this.value = ui.item.value;
			jQuery(event.target).closest('form').submit();
		},
		delay: 0,
		minLength: 3
	});
});


jQuery(function() {
			jQuery.ias({container: "div.news-list",scrollContainer: jQuery(window),item: "div.news_block",pagination: "div.browseLinksWrap",next: "div.browseLinksWrap a:contains(Volgende)",loader: "<img src='/typo3conf/ext/infinitescrolling/Resources/Public/JavaScript/Ias/images/loader.gif' alt='ajax-loader' />",triggerPageThreshold: 20,trigger: "Toon meer nieuws",loaderDelay: 0,thresholdMargin: -300,history: 1});
		});
