(function($){

	//mock process.env to make firmata-io happy
	window.process = {
		env: {
			IS_TEST_MODE: false,
			NODE_ENV: "production"
		}
	}

	//loadjs
	var loadjs=function(){var h=function(){},c={},u={},f={};function o(e,n){if(e){var r=f[e];if(u[e]=n,r)for(;r.length;)r[0](e,n),r.splice(0,1)}}function l(e,n){e.call&&(e={success:e}),n.length?(e.error||h)(n):(e.success||h)(e)}function d(r,t,s,i){var c,o,e=document,n=s.async,u=(s.numRetries||0)+1,f=s.before||h,l=r.replace(/[\?|#].*$/,""),a=r.replace(/^(css|img)!/,"");i=i||0,/(^css!|\.css$)/.test(l)?((o=e.createElement("link")).rel="stylesheet",o.href=a,(c="hideFocus"in o)&&o.relList&&(c=0,o.rel="preload",o.as="style")):/(^img!|\.(png|gif|jpg|svg|webp)$)/.test(l)?(o=e.createElement("img")).src=a:((o=e.createElement("script")).src=r,o.async=void 0===n||n),!(o.onload=o.onerror=o.onbeforeload=function(e){var n=e.type[0];if(c)try{o.sheet.cssText.length||(n="e")}catch(e){18!=e.code&&(n="e")}if("e"==n){if((i+=1)<u)return d(r,t,s,i)}else if("preload"==o.rel&&"style"==o.as)return o.rel="stylesheet";t(r,n,e.defaultPrevented)})!==f(r,o)&&e.head.appendChild(o)}function r(e,n,r){var t,s;if(n&&n.trim&&(t=n),s=(t?r:n)||{},t){if(t in c)throw"LoadJS";c[t]=!0}function i(n,r){!function(e,t,n){var r,s,i=(e=e.push?e:[e]).length,c=i,o=[];for(r=function(e,n,r){if("e"==n&&o.push(e),"b"==n){if(!r)return;o.push(e)}--i||t(o)},s=0;s<c;s++)d(e[s],r,n)}(e,function(e){l(s,e),n&&l({success:n,error:r},e),o(t,e)},s)}if(s.returnPromise)return new Promise(i);i()}return r.ready=function(e,n){return function(e,r){e=e.push?e:[e];var n,t,s,i=[],c=e.length,o=c;for(n=function(e,n){n.length&&i.push(e),--o||r(i)};c--;)t=e[c],(s=u[t])?n(t,s):(f[t]=f[t]||[]).push(n)}(e,function(e){l(n,e)}),r},r.done=function(e){o(e,[])},r.reset=function(){c={},u={},f={}},r.isDefined=function(e){return e in c},r}();

	//FROM https://plugins.jquery.com/closestDescendant/
	!function(a){a.fn.closestDescendant=function(b,c){if(!b||""===b)return a();c=c?!0:!1;var d=a();return this.each(function(){var e=a(this),f=[];for(f.push(e);f.length>0;)for(var g=f.shift(),h=g.children(),i=0;i<h.length;++i){var j=a(h[i]);if(j.is(b)){if(d.push(j[0]),!c)return!1}else f.push(j)}}),d}}(jQuery);

	// Add support scrolling of page durign drag and drop (DnD)
	// when not supported by the browser. Smart enough to detect
	// scroll support on the fly.
	// Adds invisible scroll areas top and bottom the viewport.
	// When a dragged element enters either area, will scroll if
	// the browser isn't scrolling automatically.
	$.fn.dndPageScroll = function(options) {
		options || (options = {});
		var defaults = {
			topId: 'top_scroll_page',
			bottomId: 'bottom_scroll_page',
			delay: 50,
			height: 20
		};
		var options = $.extend(defaults, options);

		var top_el = $('#'+options.topId);
		if (!top_el.length)
			top_el = $('<div id="top_scroll_page">&nbsp;</div>').appendTo('body');

		var bottom_el = $('#'+options.bottomId);
		if (!bottom_el.length)
			bottom_el = $('<div id="bottom_scroll_page">&nbsp;</div>').appendTo('body');

		var both_el = $('#top_scroll_page, #bottom_scroll_page');
		both_el.hide();
		both_el.css({
			//backgroundColor: '#FFE698',
			position:'fixed', left: 0, right: 0,
			height: options.height, zIndex: 999999
		});
		top_el.css({top:0});
		bottom_el.css({bottom:0});

		// When DnD occurs over a scroll area - scroll the page!
		var lastTop;
		var lastBottom;
		both_el.on('dragenter', function(e) {
			var direction = ($(this).attr('id')==options.topId)?'up':'down';
			//console.log('dragenter '+direction);
			return true;
		});
		both_el.on('dragover', function(e) {
			// Wait a little while before doing stuff here again
			if ($('html,body').is(':animated')) return true;

			var scrollTop = $(window).scrollTop();
			var direction = ($(this).attr('id')==options.topId)?-1:1;
			var last = (direction==-1)?lastTop:lastBottom;
			var current = (direction==-1)?scrollTop:$(document).height()-(scrollTop+$(window).height());
			//console.log('dragover '+direction+' last='+last+' current='+current);

			if (last != undefined && last == current && current > 0) {
				var newScrollTop = scrollTop+direction*50;
				//console.log('scrolling '+newScrollTop);
				$('html,body').animate(
					{scrollTop: newScrollTop},
					options.delay,
					'linear'
				);
			}

			if (direction == -1) lastTop = current; else lastBottom = current;
			return true;
		});

		// Function to hide the scroll areas. Reset everything.
		var _hide = function(e) {
			both_el.hide();
			timestamp = undefined;
			scrolltop = 0;
			scrollbottom = 0;
			return true;
		};

		// When a DND drag event starts, show the scroll areas
		$(document).on('dragstart', function(e) {
			both_el.show();
			return true;
		});
		// When DND ends, hide it.
		$(document).on('dragend', _hide);
		// In IE dragend does not always get triggered.
		// Workaround by hiding areas when the mouse enters one.
		both_el.on('mouseover', _hide);
	};

	window.addEventListener("load", function(){

	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if(pair[0] == variable){return pair[1];}
		}
		return(false);
	}

	if (getQueryVariable('skipconsent') === "true") {
		localStorage.setItem('skipconsent', true)
	}

	if (getQueryVariable('skipconsent') === "false") {
		localStorage.removeItem('skipconsent')
	}


	if (!localStorage.getItem('skipconsent')) {
		window?.cookieconsent?.initialise({
			"palette": {
				"popup": {
					"background": "#231f20",
					"text": "#ffffff"
				},
				"button": {
					"background": "#fccc4a",
					"text": "#231f20"
				}
			},
			"content": {
				"message": "Deze website maakt gebruik van cookies om u een optimale gebruikservaring te bieden.",
				"dismiss": "OK",
				"link": "Meer weten",
				"href": "https://teo.training/privacy"
			}
		})
	}

	});

	$(document).ready(function() {
		$().dndPageScroll();
	});

	function selectAll() {
		document.execCommand('selectAll', false, null);
	}
	var dataLayer = dataLayer || []
	window.dataLayer = dataLayer;
	//window.jsPlumb = jsPlumb;
	//window.FroalaEditor = FroalaEditor;
	//window.tus = tus;
	window.loadjs = loadjs;
	window.$ = $;
  window.Fancybox = Fancybox;
	//window.katex = katex;
	//window.MathQuill = MathQuill;
	//window.google = google;

	$(document).on('onInit.fb', function (e, instance) {
		if ($('.fancybox-toolbar').find('#rotate_button').length === 0) {
			$('.fancybox-toolbar').prepend('<button id="rotate_button" class="fancybox-button" title="Rotate Image"><i class="fa fa-repeat"></i></button>');
		}
		var click = 0;
		$('.fancybox-toolbar').on('click', '#rotate_button', function () {
			let ang = 90 * ++click;

			/*
			let img = $('.fancybox-inner img');
			let width = img[0].naturalWidth
			let height = img[0].naturalHeight

			let rad = ang * Math.PI / 180;
			let sin = Math.sin(rad);
			let cos = Math.cos(rad);
			let newWidth  = Math.abs(width * cos) + Math.abs(height * sin);
			let newHeight = Math.abs(width * sin) + Math.abs(height * cos);

			let aspect = newWidth / newHeight;
			let vpAspet =  $(window).width() / $(window).height();

			if (aspect > vpAspet) {
				newWidth = $(window).width();
				newHeight = newWidth * aspect;
			} else {
				newHeight = $(window).height();
				newWidth = newHeight * aspect;
			}
			*/

			$('.fancybox-image').css('transform', 'rotate(' + ang + 'deg)'); // + ' scale(' + (aspect) + ')'
			//$(".fancybox-container").css({'width': `${$(window).width()}px`, 'height': `${$(window).height()}px`});

			/*
			if (!Math.abs(ang % 180) < 0.01) {
				$(".fancybox-content").css({'width': `${$(window).width()}px`, 'height': `${$(window).height()}px`});
				$(".fancybox-image").css({'object-fit': 'contain'});
			} else {

			}
			*/
			/*
			if (!Math.abs(ang % 180) < 0.01) {
				$(".fancybox-content").css('max-width', '100vh');
				$(".fancybox-content").css('max-height', '100vw');
				let xMargin = ($(window).width() - newWidth) / 2;
				let yMargin = ($(window).height() - newHeight) / 2;
				console.log(`translate(${yMargin},${xMargin})`)
				$(".fancybox-content").css('transform', `translate(${yMargin},${xMargin})`);
			}
			*/

			/*
			if (!Math.abs(ang % 180) < 0.01) {
				$('.fancybox-image').css('max-width', '100vh');
				$('.fancybox-image').css('max-height', '100vw');
				let yOffset = ($(window).height() - newHeight) / 2;
				let xOffset = ($(window).width() - newWidth) / 2;
				$('.fancybox-content').css('transform', `translate(${xOffset}px, ${yOffset}px)`);
			}
			*/
			
			/*
			$('.fancybox-image').css('width', newWidth + 'px');
			$('.fancybox-image').css('height', newHeight + 'px');
			$('.fancybox-content').css('width', newWidth + 'px');
			$('.fancybox-content').css('height', newHeight + 'px');
			*/

		});
	});

	/*
	window.onscroll = function (e) {
		if (window.pageYOffset) {
			document.getElementsByClassName('navigation')[0].classList.add('theme-light');
		} else {
			let node = 	document.getElementsByClassName('navigation')[0];
      if (node) {
        if (node.classList.contains('theme-transparent-light')) {
          document.getElementsByClassName('navigation')[0].classList.remove('theme-light');
        }
      }
		}
	}
	*/

	/*
	$.fn.ignore = function(sel){
		return this.clone().find(sel||">*").remove().end();
	};
	*/
	
})(jQuery);