﻿/// <reference path="D:\Workspace\WebCommBack\Main\IedersXX\Sources\Presentation\Web\Scripts\jquery-vsdoc.js" />

/// <summary>
///		Zorgt ervoor dat de $(document).ready() functie nog steeds wordt aangeroepen 
///		indien er een async postback via een updatepanel is.
///		Bron: http://bit.ly/XBhJh
/// </summary>

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);	
function EndRequestHandler(sender, args) {
	if (args.get_error() == undefined) {
		jQueryFunctions();
	}
}

function jQueryFunctions() {

	$(document).ready(function () {
		// * Icons toevoegen aan links * 	

		// - Favicons:
		$('a', 'ul.linklijst').not(".customLink").filter(function () {
			return this.hostname && this.hostname !== location.hostname; // check of het een externe link is
		}).each(function () {
			var $link = $(this);
			var linkhref = $link.attr('href');
			var favImg = new Image();

			// eerst kijken of het favicon toevallig gewoon in de root staat
			if (linkhref.substring(0, 5) == "https") { // https anders gaat zorgverleners deel fout
				var iconURL = linkhref + "/favicon.ico";
				favImg.src = iconURL;
			}

			if (favImg.width == 0) { // als width van image 0  is bestaat icon niet in de root
				var iconURL = "https://getfavicon.appspot.com/" + linkhref // gebruik dan externe website om favicon op te halen...
				+ "?defaulticon=https://www.google.com/s2/favicons?domain=" + linkhref.split('//')[1]; // ... en Google's versie als fallback			

				var favImg = new Image();
				favImg.src = iconURL; // nieuwe src voor plaatje				
			}

			var faviconURL = 'url(' + iconURL + ')';
			favImg.onload = function () { // als het image is opgehaald zetten we het als CSS style
				if (favImg.width > 0 && favImg.width <= 16 && favImg.height <= 16) // alleen gebruiken indien 16x16 icon
					$link.removeClass().parent().css("background-image", faviconURL).addClass("externelinklifav");
			};
		});

		// - Andere icons:
		/*	NB: Het volgende kan ook via CSS */
		$('p > a[target^=_blan], li > a[target^=_blan]').filter(function () {
			return $(this).find('img').length == 0; // check of het niet om een <img> in een <a> gaat, dan willen we geen icon er bij!
		}).addClass("nieuwvensterlink"); // "blan" zodat zowel "blanc" als "blank" getarget worden
		$('p > a[href$=\\.pdf], p > a[href$=\\.PDF]').filter(function () {
			return $(this).find('img').length == 0;
		}).addClass("pdflinkAchter");

		$("a[target^=_blan]", "ul.pdflijst, ul.linklijst").removeClass().parent().addClass("nieuwvensterlinkli");
		$("a[href*='.pdf'], a[href*='.PDF']", "ul.pdflijst, ul.linklijst").removeClass().parent().addClass("pdfli");


		// * Opmaken tabel * 
		//   Voegt de juiste classes toe aan de elementen van een contentTable
		$("table.contentTable").each(function () {
			$(this).attr({
				cellspacing: 0,
				cellpadding: 0
			});
			$("tr:odd", this).addClass("kleur");
			//$("tr:first-child > td", this).addClass("koprow"); //niet nodig aangezien contentTable template een <thead> bevat
			$("tr td:nth-child(1)", this).addClass("firstCol");

		});


		// * Innerpage scrolling *
		//	 Based on http://oncemade.com/animated-page-scroll-with-jquery/
		//	 Werkt alleen als href begint met #. Alleen # scrollt naar top.
		$("#content").on('click', 'a[href^=#]', function () {
			var elementClicked = "#" + $(this)[0].href.split("#")[1]; // (ingewikkelde constructie voor IE7)
			if (elementClicked === "#") {
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0 }, 300);
			}
			else {
				var destination = $(elementClicked).offset().top;
				var scrollspd = destination / 5 + 200;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination - 40 }, scrollspd);
			}
		});
		// Als laatste: href='#' verwijzing van asp:menu items weghalen, zodat pagina niet gaat scrollen
		$("div.tabMenu").find("a[href=#]").removeAttr('href');

		// * In- en uitklapelementen *
		$("#expandbutton").click(function () {
			var c = $(this).attr('class');
			$(this).parent().fadeOut();
			$("#collapsebutton." + c).fadeIn();
			$("div." + $(this).attr('class')).slideDown(500, 'easeInExpo');
		});

		$("#collapsebutton").click(function () {
			var c = $(this).attr('class');
			$(this).fadeOut();
			$("#expandbutton." + c).parent().fadeIn(750, 'easeInExpo');
			$("div." + c).slideUp(500, 'easeInExpo');
		});


		// * Info-i *

		/// <summary>
		///		Handelt animatie voor een informatie pop-up af voor elke info-i op de pagina
		/// </summary>	
		$("span.popupTooltip").each(function () {

			/* Elementen zoeken en opslaan */
			var $infoI = $(this);
			var $icon = $infoI.find("a.infoI");
			var $popup = $infoI.find("div.popupWrapper");
			var $closebtn = $infoI.find("div.closeButton");
			var $pijltje = $infoI.find("div.popupPijltje");

			/* Variabelen */
			var fadeinTimer;
			var isBreed = $infoI.hasClass("popupBreed");
			var rightOffset = 27;

			/* Events */
			$infoI.hover(function () {
				if (!$infoI.hasClass("isopen")) {
					fadeinTimer = setTimeout(showPopup, 750);
				}
			}, function () {
				if (!$infoI.hasClass("isopen")) {
					clearTimeout(fadeinTimer);
					hidePopup(false);
				}
			});

			$infoI.click(function () {
				if (!$infoI.hasClass("isopen")) {
					clearTimeout(fadeinTimer);
					lockPopup();
				}
			});

			$closebtn.click(function () {
				hidePopup(true);
			});

			$icon.click(function () {
				if ($infoI.hasClass("isopen")) {
					hidePopup(true);
				}
			});


			/* Support functies */

			/// <summary>
			///    Laat popup zien (100% opacity)
			/// </summary>
			function showPopup() {
				$infoI.focus();
				$popup.stop(true, false).css('display', 'block').animate({ 'opacity': '1.0' }, 300);
			}

			/// <summary>
			///    Geeft semi-transparante "ghost" van de popup weer.
			/// </summary>
			function showGhostPopup() {
				$popup.stop(true, false).css('display', 'block').animate({ 'opacity': '0.4' }, 300, function () {
					fadeinTimer = setTimeout(lockPopup, 750);
				});
			}

			/// <summary>
			///    Laat popup zien met 100% opacity; popup blijft staan, ook na hoverOut. Andere popups worden gesloten.
			/// </summary>
			function lockPopup() {
				$popup.removeClass("popupHovermode").css('display', 'block').animate({ 'opacity': '1.0' }, 300);

				$("span.popupTooltip.isopen").each(function () {
					var $otherinfoI = $(this);
					var $otherpopup = $otherinfoI.find("div.popupWrapper");

					$otherpopup.stop(true, false).animate({ 'opacity': '0' }, 200, function () {
						$otherinfoI.removeClass("isopen");
						$otherpopup.addClass("popupHovermode").css('display', 'none');
					});


				});

				$infoI.addClass("isopen");
			}

			/// <summary>
			///     Verbergt de popup: zet opacity naar 0% en display op 'none'.
			/// </summary>
			/// <param name="slide" type="Boolean">
			///     Bepaald of de slideUp animatie moet worden gebruikt ipv. fadeOut.
			/// </param>
			function hidePopup(slide) {
				slide == true ?
				$popup.stop(true, false).hide('puff', 200, function () { //origineel: .slideUp(100,
					$infoI.removeClass("isopen");
					$popup.addClass("popupHovermode").css({ 'opacity': '0', 'display': 'none' })
				})
				:
				$popup.stop(true, false).animate({ 'opacity': '0' }, 200, function () {
					$infoI.removeClass("isopen");
					$popup.addClass("popupHovermode").css('display', 'none');
				});
			}

			/// <summary>
			///     Past positie van pop-up aan indien deze rechts buiten scherm dreigt te vallen.
			/// </summary>
			function setRightOffset() {
				var $winWidth = $(window).width();
				var $offsetPopup = $infoI.offset();
				var $margin;

				isBreed ? $margin = ($offsetPopup.left + 390) - $winWidth : $margin = ($offsetPopup.left + 250) - $winWidth;

				if ($margin > 0) {
					rightOffset = $margin;
					$popup.css({ 'marginLeft': (0 - rightOffset) });
					$pijltje.css({ 'backgroundPosition': (rightOffset - 2) + 'px top' });
				}
			}

			setRightOffset();
		});		

	});
} jQueryFunctions(); //roept de bovenstaande functies aan


/*--- Plugins ---*/

(function ($) {
	/// <summary>
	///    Zet een element "vast" op (een bepaalde plek op) het scherm indien voorbij een bepaalde offset is gescrollt
	/// </summary>
	/// <param name="offsetFromTop" type="int">
	///     Geeft aan vanaf hoeveel pixels vanaf de bovenkant van de viewport het element moet vastgezet worden
	/// </param>
	/// <param name="posX" type="int">
	///     Het aantal pixels vanaf de linkerkant
	/// </param>
	/// <param name="posY" type="int">
	///      Het aantal pixels vanaf de bovenkant
	/// </param>
	$.fn.makeSticky = function (offsetFromTop, posX, posY) {
		return this.each(function () {
			var element = $(this);			
			var topOffset = 0;
			var X = 0;
			var Y = 0;

			if ( offsetFromTop ) topOffset = offsetFromTop;
			if ( posX ) X = posX;
			if ( posY ) Y = posX;

			$(window).on('scroll', function () {
				setSticky(element,topOffset, X, Y);
			});
		});
	};
})(jQuery);

function setSticky(element, topOffset, X, Y)
{
	var pageOffset = $(window).scrollTop();
	if (pageOffset > topOffset) {
		element.css({ 'position': 'fixed', 'top': Y, 'left': X });
	}
	else element.css({ 'position': 'static' });
}

/*!
* jQuery replaceText - v1.1 - 11/21/2009
* http://benalman.com/projects/jquery-replacetext-plugin/
* 
* Copyright (c) 2009 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/

// Script: jQuery replaceText: String replace for your jQueries!
//
// *Version: 1.1, Last updated: 11/21/2009*
// 
// Project Home - http://benalman.com/projects/jquery-replacetext-plugin/
// GitHub       - http://github.com/cowboy/jquery-replacetext/
// Source       - http://github.com/cowboy/jquery-replacetext/raw/master/jquery.ba-replacetext.js
// (Minified)   - http://github.com/cowboy/jquery-replacetext/raw/master/jquery.ba-replacetext.min.js (0.5kb)
// 
// About: License
// 
// Copyright (c) 2009 "Cowboy" Ben Alman,
// Dual licensed under the MIT and GPL licenses.
// http://benalman.com/about/license/
// 
// About: Examples
// 
// This working example, complete with fully commented code, illustrates one way
// in which this plugin can be used.
// 
// replaceText - http://benalman.com/code/projects/jquery-replacetext/examples/replacetext/
// 
// About: Support and Testing
// 
// Information about what version or versions of jQuery this plugin has been
// tested with, and what browsers it has been tested in.
// 
// jQuery Versions - 1.3.2, 1.4.1
// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.1.
// 
// About: Release History
// 
// 1.1 - (11/21/2009) Simplified the code and API substantially.
// 1.0 - (11/21/2009) Initial release

(function ($) {
	'$:nomunge'; // Used by YUI compressor.

	// Method: jQuery.fn.replaceText
	// 
	// Replace text in specified elements. Note that only text content will be
	// modified, leaving all tags and attributes untouched. The new text can be
	// either text or HTML.
	// 
	// Uses the String prototype replace method, full documentation on that method
	// can be found here: 
	// 
	// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Replace
	// 
	// Usage:
	// 
	// > jQuery('selector').replaceText( search, replace [, text_only ] );
	// 
	// Arguments:
	// 
	//  search - (RegExp|String) A RegExp object or substring to be replaced.
	//    Because the String prototype replace method is used internally, this
	//    argument should be specified accordingly.
	//  replace - (String|Function) The String that replaces the substring received
	//    from the search argument, or a function to be invoked to create the new
	//    substring. Because the String prototype replace method is used internally,
	//    this argument should be specified accordingly.
	//  text_only - (Boolean) If true, any HTML will be rendered as text. Defaults
	//    to false.
	// 
	// Returns:
	// 
	//  (jQuery) The initial jQuery collection of elements.

	$.fn.replaceText = function (search, replace, text_only) {
		return this.each(function () {
			var node = this.firstChild,
		val,
		new_val,

			// Elements to be removed at the end.
		remove = [];

			// Only continue if firstChild exists.
			if (node) {

				// Loop over all childNodes.
				do {

					// Only process text nodes.
					if (node.nodeType === 3) {

						// The original node value.
						val = node.nodeValue;

						// The new value.
						new_val = val.replace(search, replace);

						// Only replace text if the new value is actually different!
						if (new_val !== val) {

							if (!text_only && /</.test(new_val)) {
								// The new value contains HTML, set it in a slower but far more
								// robust way.
								$(node).before(new_val);

								// Don't remove the node yet, or the loop will lose its place.
								remove.push(node);
							} else {
								// The new value contains no HTML, so it can be set in this
								// very fast, simple way.
								node.nodeValue = new_val;
							}
						}
					}

				} while (node = node.nextSibling);
			}

			// Time to remove those elements!
			remove.length && $(remove).remove();
		});
	};

})(jQuery);

var zoekterm;

function getQueryParameter(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return zoekterm;
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(document).ready(function () {
	var zoekwoord = getQueryParameter("query");
	if (zoekwoord != null && zoekwoord != "") {
		$("#content *").replaceText(new RegExp(zoekwoord, "gi"), function (str) { return '<span class="zoekterm">' + str + '</span>'; });
	}

/* Lettergrootte popup laten verschijnen en verdwijnen met uitleg hoe bezoekers kunnen inzoomen */
	$("#lettergrootteMenu").click(function () {
		$(".lettergroottePopUp").toggle();
		$("#fullpageOverlayAchtergrond").css('display', 'block');
	});

	$("#lettergrootteSluiten").click(function () {
		$(".lettergroottePopUp").toggle();
		$("#fullpageOverlayAchtergrond").css('display', 'none');
	});

	$("#fullpageOverlayAchtergrond").click(function () {
		$(".lettergroottePopUp").toggle();
		$("#fullpageOverlayAchtergrond").css('display', 'none');
	});


});

	
