// preload
$.fn.preloader=function(options){var defaults={delay:0,preload_parent:this,check_timer:300,ondone:function(){},oneachload:function(image){},fadein:300};var options=$.extend(defaults,options),root=$(this),images=root.find("img").css({"visibility":"hidden",opacity:0}),timer,counter=0,i=0,checkFlag=[],delaySum=options.delay,init=function(){timer=setInterval(function(){if(counter>=checkFlag.length){clearInterval(timer);options.ondone();return}for(i=0;i<images.length;i++){if(images[i].complete==true){if(checkFlag[i]==false){checkFlag[i]=true;options.oneachload(images[i]);counter++;delaySum=delaySum+options.delay}$(images[i]).css("visibility","visible").delay(delaySum).animate({opacity:1},options.fadein,function(){$(this).parent().removeClass("preloader")})}}},options.check_timer)};images.each(function(){if($(this).parent(options.preload_parent).length==0)$(this).wrap("<div class='preloader' />");else $(this).parent().addClass("preloader");checkFlag[i++]=false});images=$.makeArray(images);var icon=jQuery("<img />",{id:'loadingicon',src:'/images/load.gif'}).hide().appendTo("body");timer=setInterval(function(){if(icon[0].complete==true){clearInterval(timer);init();icon.remove();return}},100)}
// resize functie (deel code van supersized.js)
$.fn.resizenow=function(){return this.each(function(){var imgH=$(this).children().height(),imgW=$(this).children().width(),biwH=$(window).height(),biwW=$(window).width(),ratio=imgH/imgW;if((biwH/biwW)>ratio){$(this).height(biwH);$(this).width(biwH/ratio);$(this).children().height(biwH);$(this).children().width(biwH/ratio)}else{$(this).width(biwW);$(this).height(biwW*ratio);$(this).children().width(biwW);$(this).children().height(biwW*ratio)}$(this).children().css('left',(biwW-$(this).width())/2);$(this).children().css('top',(biwH-$(this).height())/2);return false})};
// equal heights on floating divs
$.fn.equalHeights=function(){var currentTallest=0,currentRowStart=0,rowDivs=new Array();function setConformingHeight(el,newHeight){el.data("originalHeight",(el.data("originalHeight")==undefined)?(el.height()):(el.data("originalHeight")));el.height(newHeight)}function getOriginalHeight(el){return(el.data("originalHeight")==undefined)?(el.height()):(el.data("originalHeight"))}return this.each(function(){var $el=$(this);var topPosition=$el.position().top;if(currentRowStart!=topPosition){for(currentDiv=0;currentDiv<rowDivs.length;currentDiv++)setConformingHeight(rowDivs[currentDiv],currentTallest);rowDivs.length=0;currentRowStart=topPosition;currentTallest=getOriginalHeight($el);rowDivs.push($el)}else{rowDivs.push($el);currentTallest=(currentTallest<getOriginalHeight($el))?(getOriginalHeight($el)):(currentTallest)}for(currentDiv=0;currentDiv<rowDivs.length;currentDiv++)setConformingHeight(rowDivs[currentDiv],currentTallest)})}

	$(function(){
// screenshot	geef de class aan in de plugin!!!
		screenshotPreview();
// png voor IE
		$('body').pngFix();
// warning if using IE6
	if (($.browser.msie) && ($.browser.version == "6.0")){
		$('#wrap').prepend('<div style="position:relative;left:0;top:0;height:54px;width:100%;display:block;background: pink ;font-weight:bold;border-bottom:5px solid red;"><p style="padding:10px;">U bekijkt deze site in Internet Explorer 6. Sommige functies werken niet met deze browser. Daarom adviseren wij u de browser te updaten.<br /><a href="http://www.mozilla-europe.org/nl/firefox/">"Firefox"</a>, <a href="http://www.apple.com/safari/">"Safari"</a>, <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx">"Internet Explorer"</a>, <a href="http://www.google.com/chrome">"Google Chrome"</a> en <a href="http://www.opera.com/">"Opera"</a> zijn allemaal uitstekende opties om uw verouderde en onveilige browser te vervangen.</p></div>');
	}
// enter does submit if IE
	if ($.browser.msie){
		$('input').keydown(function(e){
			if (e.keyCode == 13) {
				$(this).parents('form').submit();
				return false;
			}
		});
	}


		var currentTime = new Date(),
			day = currentTime.getDate(),
			month = currentTime.getMonth() + 1,
			year = currentTime.getFullYear();
		if(day < 10){ day = '0'+day; }
		if(month < 10){ month = '0'+month; }
		var de_datum = year+''+month+''+day;
// alert(de_datum);
		$('#listview_nu tbody tr').each(function(){
			var tabeldatum = parseInt($(this).children('td').slice(0,1).children('span').text());
			var tabeldatumeind = $(this).children('td').slice(1,2).children('span').size();
			if(tabeldatumeind == 1){
				var tabeldatumeind_ja = parseInt($(this).children('td').slice(1,2).children('span').text());
			} else {
				var tabeldatumeind_ja = 0;
			}
			if(de_datum > tabeldatum && tabeldatumeind_ja == 0){ // datum is eerder dan vandaag, maar heeft geen einddatum
				$(this).remove();
			}
			if(de_datum > tabeldatumeind_ja){ // datum is eerder dan vandaag, en de einddatum is groter dan vandaag
				$(this).remove();
			}
			if(de_datum < tabeldatum){ // datum is eerder dan vandaag, en de einddatum is groter dan vandaag
				$(this).remove();
			}
		});
		$('#listview tbody tr').each(function(){
			var tabeldatum = parseInt($(this).children('td').slice(0,1).children('span').text());
			if(de_datum > tabeldatum){
				$(this).remove();
			}
		});

		$('.block_view_nu').each(function(){
			var blockdatum = parseInt($(this).children('.order_begin').text());
			var blockdatumeind = $(this).children('.order_eind').size();
			if(blockdatumeind == 1){
				var blockdatumeind_ja = parseInt($(this).children('.order_eind').text());
			} else {
				var blockdatumeind_ja = 0;
			}
			if(de_datum > blockdatum && blockdatumeind_ja == 0){ // datum is eerder dan vandaag, maar heeft geen einddatum
				$(this).remove();
			}
			if(de_datum > blockdatumeind_ja){ // datum is eerder dan vandaag, en de einddatum is groter dan vandaag
				$(this).remove();
			}
			if(de_datum < blockdatum){ // datum is eerder dan vandaag, en de einddatum is groter dan vandaag
				$(this).remove();
			}
		});
		$('.block_view').each(function(){
			var blockdatum = parseInt($(this).children('.order_begin').text());
			if(de_datum > blockdatum){
				$(this).remove();
			}
		});

// tablesorter
		$("#listview").tablesorter({sortList: [[0,0]], widgets: ['zebra']});
		$("#listview_nu").tablesorter({sortList: [[0,0]], widgets: ['zebra']});
// sortelements
		$('.related section, .block_view, .block_view_nu').sortelements(function(a, b){
			return $(a).attr('rel') > $(b).attr('rel') ? 1 : -1;
		});
// dotted linkline
		$('a').focus(function() {this.blur();});
		$('a img').hover(function(){
			$(this).stop().animate({'opacity':.7}, 100);
		}, function(){
			$(this).stop().animate({'opacity':1}, 400);
		})
// als links niet in markup mogen ivm worst-in-blik.
		$('a.verborgen').each(function(){
			var hiddenLink = $(this).text(),
				splitLink = hiddenLink.split('$'),
				emailLink = splitLink[0]+'@'+splitLink[1]+''+splitLink[2]+'.'+splitLink[3];
			$(this).attr('href', 'mailto:'+emailLink);
			$(this).text(emailLink);
			$(this).removeClass('verborgen');
		});
// form als t-shirt
		$('label.hidden').each(function(){
			var tekst = $(this).text();
			$(this).next('input').val(tekst);
			$(this).hide();
		});
// focus op input
		$('textarea, input[type="text"]').addClass("idleField");
		// textarea
		$('textarea').focus(function() {
			$(this).removeClass("idleField").addClass("focusField");
		});
		$('textarea').blur(function() {
			$(this).removeClass("focusField").addClass("idleField");
		});
		//input type=text
		$('input[type="text"], textarea').live('focus', function() {
			$(this).removeClass("idleField").addClass("focusField");
			var defVal = $(this).prev('label').text();
			if (this.value == this.defaultValue){ 
				this.value = '';
			}
			if(this.value != this.defaultValue){
				this.select();
			}
		});
		$('input[type="text"], textarea').live('blur', function() {
			$(this).removeClass("focusField").addClass("idleField");
			if ($.trim(this.value) == ''){
				this.value = (this.defaultValue ? this.defaultValue : '');
			}
		});
// printbutton
		$('#printwindow').click(function(){
			window.print();
			return false;
		});
// slider
			$('.slider').cycle({ 
				fx:'fade', 
				speed:300, 
				timeout: 4000,
				pause:1, 
				next:'.slider img', 
				pager:'.f0', 
				cleartype:1, 
				height:'auto', 
				random:0 
			});
			$('.slider1').cycle({ 
				fx:'fade', 
				speed:300, 
				timeout: 4000,
				pause:1, 
				next:'.slider img', 
				pager:'.f1', 
				cleartype:1, 
				height:'auto', 
				random:0 
			});
			$('.slider2').cycle({ 
				fx:'fade', 
				speed:300, 
				timeout: 4000,
				pause:1, 
				next:'.slider img', 
				pager:'.f2', 
				cleartype:1, 
				height:'auto', 
				random:0 
			});
			$('.slider3').cycle({ 
				fx:'fade', 
				speed:300, 
				timeout: 4000,
				pause:1, 
				next:'.slider img', 
				pager:'.f3', 
				cleartype:1, 
				height:'auto', 
				random:0 
			});
			$('.slider4').cycle({ 
				fx:'fade', 
				speed:300, 
				timeout: 4000,
				pause:1, 
				next:'.slider img', 
				pager:'.f4', 
				cleartype:1, 
				height:'auto', 
				random:0 
			});
			$('.slider5').cycle({ 
				fx:'fade', 
				speed:300, 
				timeout: 4000,
				pause:1, 
				next:'.slider img', 
				pager:'.f5', 
				cleartype:1, 
				height:'auto', 
				random:0 
			});
// Equal heights
		$('.een3e, .een4e, aside section').equalHeights();
// Image bottom align
		$('.een3e, .een4e, aside section, .related section').each(function(){
			var een3eH = $(this).height(), een3eImgH = $(this).find('img').height(), een3ePH = $(this).find('p').height(), marginT = een3eH - (een3eImgH + een3ePH);
			$(this).children('img').css('margin-top', marginT);
		});
// Jouwbuis duimnagel
		$('a.fancyoutube').each(function(){
			var thumb_YT = $.jYoutube($(this).text()); 
			$(this).text('').append('<img src="'+thumb_YT+'" alt="youtube video" height="173" width="230" />');
		});
		
// fancybox
		$('a.fancy').fancybox({
			'titleShow' : false
		});
		$("a.fancypopup").fancybox({
			//autoDimensions:false,
			width:730
		});
		$('nav ul li a[href="/?#zoekmodal"]').attr('id', 'zoekendan').attr('href', '#zoekmodal');
		$('nav ul li a[href="/?#nieuwsmodal"]').attr('id', 'inschrijvendan').attr('href', '#nieuwsmodal');
		$("#zoekendan").fancybox({
			'modal' : false, 
			'autoDimensions' : false,
			'height':35,
			'width':525
		});
		$("#inschrijvendan").fancybox({
			'modal' : false, 
			'autoDimensions' : false,
			'height':355,
			'width':525
		});
		$("a.fancyvideo").live('click',function() {
			$.fancybox({
				'overlayOpacity': 0.7,
				'overlayColor'  : '#000',
				'padding'	: 0,
				'margin'	: 0,
				'autoScale'	: false,
				'showCloseButton': true,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'		: this.title,
				'width'		: 600,
				'height'	: 339,
				'href'		: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
				'type'		: 'swf'
			});
			return false;
		});
		$('#fancybox-content').children('div').css('overflow','hidden');

		$("a.fancyoutube").live('click',function() {
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 640,
				'height'		: 385,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

			return false;
		});

		$('#fancybox-overlay').live('click', function(){
			$.fancybox.close();
		});
// scrolltotop
		$(".scroll").click(function(event){
			event.preventDefault();

			var full_url = this.href;

			var parts = full_url.split("#");
			var trgt = parts[1];

			var target_offset = $("#"+trgt).offset();
			var target_top = target_offset.top;

			$('html, body').animate({scrollTop:target_top}, 500);
		});
// tooltip
		$('.logo, .slider img, .social img, .een3e img, , .archief .een3e p, #logo a, #lijstweergave tr, .related section p, .related section a, .listview tr, #actueelmodal p')
			.tooltip({opacity: 0.5, track: true, delay: 0, showURL: false, fade: 100});
		// tablesorter
		$("#lijstweergave").tablesorter();
// taalswitcher
// gebruikt switcher.tmpl, switcher.php en knoppen in menu.tmpl
		$('#lang_NL').live('click', function(){
			var taalkeus = 'nl';
			$.post("/switcher.php", {lang: taalkeus}, function(data){
				location.reload();
			});
		});
		$('#lang_FR').live('click', function(){
			var taalkeus = 'fr';
			$.post("/switcher.php", {lang: taalkeus}, function(data){
				location.reload();
			});
		});
		$('#mod_NL').live('click', function(){
			var taalkeus = 'nl';
			$.post("/switcher.php", {lang: taalkeus}, function(data){
				location.reload();
			});
		});
		$('#mod_FR').live('click', function(){
			var taalkeus = 'fr';
			$.post("/switcher.php", {lang: taalkeus}, function(data){
				location.reload();
			});
		});
		$('.archief .een3e img').lazyload({
		    effect : "fadeIn"
		});
	});
// na browser resize, afbeelding ook resizen
	$(window).bind("resize", function(){
		$('.bg-image-wrap').resizenow();
		$('.een3e, .een4e, .related section').equalHeights();
// Image bottom align
		$('.een3e, .een4e, aside section, .related section').each(function(){
			var een3eH = $(this).height(),
			een3eImgH = $(this).find('img').height(),
			een3ePH = $(this).find('p').height(),
			marginT = een3eH - (een3eImgH + een3ePH);
			$(this).children('img').css('margin-top', marginT);
		});
	});
