/**
 * @fileOverview All JS-code for ggzbreburg.nl
 * @author Wouter Bos
 * @since 1.0 - 2010-8-6
 * @version 1.0 - 2010-8-6
 */




if (typeof (nl) == "undefined") {
	/**
	 * @namespace Top Level Domain
	 */
	var nl = {}
}






/**
 * @namespace Domain name and container for simple public functions
 */
nl.ggzbreburg = (function() {
	/* Start public */
	return {
		/**
		 * Add Flash Spotlight to page
		 * @param {String} swfUrl URL of SWF
		 * @param {String} flashContainer ID of the DOM element where the SWF will be placed in
		 * @requires swfobject
		 * @requires jQuery
		 * @example
		 * nl.ggzbreburg.AddSpotlight("/flash/TODO.swf", "FlashSpotlight")
		 */
		AddSpotlight: function(swfUrl, flashContainer) {
			var flashvars = {};
				//flashvars.xmlConfigURL = "/Estate/Flash/Spotlight/config.xml"
			var params = {};
				params.wmode = "transparent";
			var attributes = {};

			if (swfobject.getFlashPlayerVersion().major >= 9) {
				swfobject.embedSWF(swfUrl +"?random=" + Math.random(), flashContainer, "960", "364", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
				if (jQuery.browser.msie == true) {
					Estate.Events.AddEvent(
						window,
						function() {
							setTimeout('jQuery("div.hulpwijzer").css("position", "static"); jQuery("div.hulpwijzer").css("position", "absolute");', 250)
						},
						"onload"
					)
				}
			}
		},
		
		/**
		 * Makes it possible to tab through the links in the menu
		 * @param {String} selector jQuery selector
		 * @requires jQuery
		 * @example
		 * nl.ggzbreburg.MakeMenuAccessible("ul.hoofdmenu")
		 */
		MakeMenuAccessible: function(selector) {
			jQuery(selector).find("li li a").bind('focus', function() {
				jQuery("div.onderliggende-opties").attr("style", "")
				jQuery(this).parents("div.onderliggende-opties").attr("style", "left: -60px;")
			})
			jQuery(selector).find("li li a").bind('blur', function() {
				jQuery("div.onderliggende-opties").attr("style", "")
			})
			jQuery(selector).bind('mouseenter', function() {
				jQuery(this).parents("div.header").css("z-index", "1")
			})
			jQuery(selector).bind('mouseleave', function() {
				jQuery(this).parents("div.header").css("z-index", "")
			})
		},

		/**
		 * Adds accordion widget to a defenition list (dl)
		 * @param {String} selector jQuery selector that selects the dl element
		 * @requires jQuery
		 * @example
		 * nl.ggzbreburg.SetAccordion("dl.informatieaccordeon")
		 */
		SetAccordion: function(selector) {
			jQuery(selector).addClass("js_accordion")
			jQuery(selector).find("dd:gt(0)").hide()
			jQuery(selector).find("dt").click(function() {
				if (jQuery(this).next("dd").is(":hidden")) {
					jQuery(this).siblings("dd").slideUp(400);
					jQuery(this).next("dd").slideDown(600);				
				}
			})
		}		
	}
	/* End public */
})();






/**
 * @namespace Adds watermark to an input box
 */
nl.ggzbreburg.Watermark = (function() {
	var collection
	var config = {
		watermarkText: "Zoeken..."
	}

	function addWatermark(el) {
		jQuery(el).val(config.watermarkText)
		jQuery(el).addClass("watermark")
	}



	/* Start public */
	return {
		/**
		 * Initialize watermark functionality
		 * @param {String|Object} selector jQuery selector
		 * @requires jQuery
		 * @example
		 * nl.ggzbreburg.Watermark.Init("input.searchBox")
		 */
		Init: function(selector) {
			collection = jQuery(selector)

			addWatermark(collection)
			
			jQuery(collection).focus(function() {
				if (jQuery(this).val() == config.watermarkText) {
					jQuery(collection).val("")
					jQuery(collection).removeClass("watermark")
				}
			})
			
			jQuery(collection).blur(function() {
				if (jQuery(this).val() == "") {
					addWatermark(this)
				}
			})
		}
	}
	/* End public */
})();




/**
 * @namespace Adds highlighting to container
 */
nl.ggzbreburg.Highlighting = (function() {

	function DoHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) {

		// the highlightStartTag and highlightEndTag parameters are optional
		if ((!highlightStartTag) || (!highlightEndTag)) {
			highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
			highlightEndTag = "</font>";
		}

		// find all occurences of the search term in the given text,
		// and add some "highlight" tags to them (we're not using a
		// regular expression search, because we want to filter out
		// matches that occur within HTML tags and script blocks, so
		// we have to do a little extra validation)
		var newText = "";
		var i = -1;
		var lcSearchTerm = " " + searchTerm.toLowerCase();
		var lcBodyText = " " + bodyText.toLowerCase();

		searchTerm = " " + searchTerm;
		bodyText = " " + bodyText;

		while (bodyText.length > 0) {
			i = lcBodyText.indexOf(lcSearchTerm, i + 1);
			if (i < 0) {
				newText += bodyText;
				bodyText = "";
			} else {
				// skip anything inside an HTML tag
				if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
					// skip anything inside a <script> block
					if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
						newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
						bodyText = bodyText.substr(i + searchTerm.length);
						lcBodyText = bodyText.toLowerCase();
						i = -1;
					}
				}
			}
		}

		return newText;
	}

	/* Start public */
	return {
		HighlightSearchTerms: function(searchText, treatAsPhrase, highlightStartTag, highlightEndTag, containerClass) {

			// if the treatAsPhrase parameter is true, then we should search for 
			// the entire phrase that was entered; otherwise, we will split the
			// search string so that each word is searched for and highlighted
			// individually
			if (treatAsPhrase) {
				searchArray = [searchText];
			} else {
				searchArray = searchText.split(" ");
			}

			// loop through all the containers
			jQuery("." + containerClass).each(function() {
				var bodyText = jQuery(this).html();

				// loop through all search terms
				for (var i = 0; i < searchArray.length; i++) {
					bodyText = DoHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
				};

				jQuery(this).html(bodyText);
			});

			return true;
		}
	}
	/* End public */
})();





/**
 * @namespace Adds extra dynamic styling to the webforms for marketers.
 */
nl.ggzbreburg.SetWebforms = (function() {
	/* Start public */
	return {
		/**
		 * Adds extra dynamic styling to the webforms for marketers.
		 * @requires jQuery
		 * @example
		 * nl.ggzbreburg.SetWebforms.Init()
		*/
		Init: function() {
			var scfForm = jQuery("div.scfForm:first")
			jQuery(scfForm).find('input, textarea, select').bind('click blur change', function() {
				// if validation is not ok
				jQuery(scfForm).find('fieldset span[id][style][title]:visible').each( function() {
					jQuery(this).addClass('validationError')
					var parents = jQuery(this).parents("div.scfDropListBorder, div.scfEmailBorder, div.scfMultipleLineTextBorder, div.scfSingleLineTextBorder, div.scfPasswordBorder, div.scfNumberBorder, div.scfDateBorder, div.scfRadioButtonListBorder, div.scfListBoxBorder, div.scfCheckBoxListBorder, div.scfFileUploadBorder, div.scfDateSelectorBorder, div.scfCreditCardBorder, div.scfConfirmPasswordBorder, div.scfCaptchaBorder, div.scfTelephoneBorder, div.scfSmsTelephoneBorder")
					jQuery(parents).addClass("validationError")
				})
				// if validation is ok
				jQuery(scfForm).find('fieldset span[id][style][title]:hidden').each(function() {
					jQuery(this).removeClass('validationError')
					var parents = jQuery(this).parents("div.scfDropListBorder, div.scfEmailBorder, div.scfMultipleLineTextBorder, div.scfSingleLineTextBorder, div.scfPasswordBorder, div.scfNumberBorder, div.scfDateBorder, div.scfRadioButtonListBorder, div.scfListBoxBorder, div.scfCheckBoxListBorder, div.scfFileUploadBorder, div.scfDateSelectorBorder, div.scfCreditCardBorder, div.scfConfirmPasswordBorder, div.scfCaptchaBorder, div.scfTelephoneBorder, div.scfSmsTelephoneBorder")
					jQuery(parents).removeClass("validationError")
				})
			})
		}
	}
	/* End public */
})();






/**
* @namespace Code for handling (showing/hiding) div popups
*/
nl.ggzbreburg.Popup = (function() {
	function hidePopup(windowSelector, backgroundSelector) {
		jQuery(windowSelector).hide();
		jQuery(backgroundSelector).hide();
	}

	function getMonthName(index) {
		switch (index) {
			case 0:
				return "januari"
				break;
			case 1:
				return "februari"
				break;
			case 2:
				return "maart"
				break;
			case 3:
				return "april"
				break;
			case 4:
				return "mei"
				break;
			case 5:
				return "juni"
				break;
			case 6:
				return "juli"
				break;
			case 7:
				return "augustus"
				break;
			case 8:
				return "september"
				break;
			case 9:
				return "oktober"
				break;
			case 10:
				return "november"
				break;
			case 11:
				return "december"
				break;
		}
	}

	function openCreateProfile(popupConfig, popup) {
		popup.Open();
		var daysInput = jQuery(popupConfig.windowSelector).find('input.tag_days')

		var initNow = new Date();
		var initThen = new Date(new Date(initNow).setHours(initNow.getHours() + (30 * 24)));

		jQuery('#BerekendeDatum').text("(tot " + initThen.getDate() + " " + getMonthName(initThen.getMonth()) + " " + initThen.getFullYear() + ")")		
		
		//Shows and hides the textarea
		jQuery(daysInput).unbind('keyup blur')
		jQuery(daysInput).bind('keyup blur', function() {
			var days = jQuery(this).val()
			if (days == parseInt(days)) {
			    var now = new Date();
			    var then = new Date(new Date(now).setHours(now.getHours() + (days * 24)));		
				jQuery('#BerekendeDatum').text("(tot " + then.getDate() + " " + getMonthName(then.getMonth()) + " " + then.getFullYear() + ")")
			}
		})
	}

	function openEditProfile(popupConfig, popup) {
		popup.Open();

		var daysInput = jQuery(popupConfig.windowSelector).find('input.tag_days')
		// Calculates date after user supplied a number
		jQuery(daysInput).unbind('keyup blur')
		jQuery(daysInput).bind('keyup blur', function() {
			var days = jQuery(this).val()
			if (days == parseInt(days)) {
				var now = new Date();
				var then = new Date(new Date(now).setHours(now.getHours() + (days * 24)));
				jQuery('#BerekendeDatum').text("(tot " + then.getDate() + " " + getMonthName(then.getMonth()) + " " + then.getFullYear() + ")")
			}
		})

		var forgotPasswordLink = jQuery(popupConfig.windowSelector).find('#ForgotPassword')
		// Opens 'Forgot password' dialogue
		jQuery(forgotPasswordLink).unbind('click')
		jQuery(forgotPasswordLink).bind('click', function() {
			nl.ggzbreburg.Popup.SetForgotPassword(true)
			nl.ggzbreburg.Popup.Hide("#DivPopupWindow_EditProfile", "#DivPopupBackground_EditProfile")
		})
	}



	/* Start public */
	return {
		/**
		* Set events for feedback popup
		* @requires jQuery
		* @example
		* nl.ggzbreburg.Popup.SetFeedback()
		*/
		SetFeedback: function() {
			var popupConfig = {
				animationSpeed: 250,
				closeText: "",
				windowSelector: "#DivPopupWindow_Feedback",
				backgroundSelector: "#DivPopupBackground_Feedback"
			}
			var popup = new Estate.Popup(popupConfig);

			// Hide popup and textarea
			hidePopup(popupConfig.windowSelector, popupConfig.backgroundSelector);
			jQuery(popupConfig.windowSelector).find('div.tag_ifNo').hide();

			// Set click event that opens popup
			jQuery("#Feedback").click(function() {
				popup.Open();
				var radioNo = jQuery(popupConfig.windowSelector).find('span.tag_radio input:radio')
				//Shows and hides the textarea
				jQuery(radioNo).unbind('click change blur')
				jQuery(radioNo).bind('click change blur', function() {
					if (jQuery(this).val() == "nee") {
						jQuery(popupConfig.windowSelector).find('div.tag_ifNo').fadeIn(popupConfig.animationSpeed)
					} else {
						jQuery(popupConfig.windowSelector).find('div.tag_ifNo').fadeOut(popupConfig.animationSpeed)
					}
				})
				return false;
			})
		},

		/**
		* Set events for popup with form to create profile
		* @requires jQuery
		* @example
		* nl.ggzbreburg.Popup.SetCreateProfile()
		*/
		SetCreateProfile: function() {
			var popupConfig = {
				closeReload: true,
				animationSpeed: 250,
				closeText: "",
				windowSelector: "#DivPopupWindow_MakeProfile",
				backgroundSelector: "#DivPopupBackground_MakeProfile"
			}
			var popup = new Estate.Popup(popupConfig);

			hidePopup(popupConfig.windowSelector, popupConfig.backgroundSelector);

			// Set click event that opens popup
			jQuery("#CreateProfile").click(function() {
				openCreateProfile(popupConfig, popup)
				return false;
			})
			
			if (jQuery(popupConfig.windowSelector).parent().hasClass("displayBlock")) {
				openCreateProfile(popupConfig, popup)
			}
		},

		/**
		* Set events for popup with depression self test form
		* @requires jQuery
		* @example
		* nl.ggzbreburg.Popup.DepressionSelfTest()
		*/
		DepressionSelfTest: function() {
			var popupConfig = {
				animationSpeed: 250,
				closeText: "",
				windowSelector: "#DivPopupWindow_DepressieTestResult",
				backgroundSelector: "#DivPopupBackground_DepressieTestResult"
			}
			var popup = new Estate.Popup(popupConfig);

			hidePopup(popupConfig.windowSelector, popupConfig.backgroundSelector);

			// Set click event that opens popup
			jQuery("#DepressieTestResultForm").click(function() {
				popup.Open();
				return false;
			})

			if (jQuery(popupConfig.windowSelector).find("div.scfForm").size() > 0 && jQuery(popupConfig.windowSelector).find("div.scfSubmitButtonBorder").size() == 0) {
				popup.Open();
			}
		},

		/**
		* Set events for popup with form to edit profile
		* @requires jQuery
		* @example
		* nl.ggzbreburg.Popup.SetEditProfile()
		*/
		SetEditProfile: function() {
			var popupConfig = {
				closeReload: true,
				animationSpeed: 250,
				closeText: "",
				windowSelector: "#DivPopupWindow_EditProfile",
				backgroundSelector: "#DivPopupBackground_EditProfile"
			}
			var popup = new Estate.Popup(popupConfig);

			hidePopup(popupConfig.windowSelector, popupConfig.backgroundSelector);

			// Set click event that opens popup
			jQuery("#EditProfile").click(function() {
				openEditProfile(popupConfig, popup)
				return false;
			})
			
			if (jQuery(popupConfig.windowSelector).parent().hasClass("displayBlock")) {
				openEditProfile(popupConfig, popup)
			}
		},

		/**
		* Set events for popup with form to retrieve password
		* @requires jQuery
		* @param {Boolean} openNow Either opens popup window immediately or sets
		*		 event that will trigger popup
		* @example
		* nl.ggzbreburg.Popup.SetForgotPassword()
		*/
		SetForgotPassword: function(argOpenNow) {
			var popupConfig = {
				closeReload: true,
				animationSpeed: 250,
				closeText: "",
				windowSelector: "#DivPopupWindow_ForgotPassword",
				backgroundSelector: "#DivPopupBackground_ForgotPassword"
			}
			var openNow = false
			if (argOpenNow) {
				if (argOpenNow == true) {
					openNow = true
				}
			}
			if (jQuery(popupConfig.windowSelector).parent().hasClass("displayBlock")) {
				openNow = true
			}
			
			var popup = new Estate.Popup(popupConfig);

			hidePopup(popupConfig.windowSelector, popupConfig.backgroundSelector);

			if (openNow == true) {
				// Open popup immediately
				popup.Open();
			} else {
				// Set click event that opens popup
				jQuery("#ForgotPassword").click(function() {
					popup.Open();
					return false;
				})
			}
		},

		/**
		* Hides a popup by providing the jQuery selectors of the popup window and the background
		* @requires jQuery
		* @param {String} windowSelector jQuery selector of popup window (preferably an ID selector)
		* @param {String} backgroundSelector jQuery selector of background div (preferably an ID selector)
		* @example
		* nl.ggzbreburg.Popup.Hide("#WindowID", "#BackgroundID")
		*/
		Hide: function(windowSelector, backgroundSelector) {
			jQuery(windowSelector).hide();
			jQuery(backgroundSelector).hide();
		}
	}
	/* End public */



})();




nl.ggzbreburg.subsites = {};



/**
 * @namespace Manages vacature popup
 */
nl.ggzbreburg.subsites.Popup = (function() {
  /* Start public */
  return {
    Init: function(windowSelector, button) {
      var popupConfig = {
        animationSpeed: 250,
        closeText: '',
        windowSelector: windowSelector,
        backgroundSelector: '.divPopupBackground'
      }
      var popup = new Estate.Popup(popupConfig);
      var iframe;
      
      jQuery(button).click(function() {
        var href = jQuery(this).attr('href') + "&iframe=true";
        jQuery('iframe.form').attr('src',href).load(function(){
          iframe = this.contentWindow;
          frameHeight = iframe.document.body.scrollHeight + 'px';
          jQuery('iframe.form').height(frameHeight);
          jQuery('iframe.form').contents().find('input').change(function(){
            frameHeight = iframe.document.body.scrollHeight + 'px';
            jQuery('iframe.form').height(frameHeight);
          });
          jQuery('iframe.form').contents().find('input[type=submit]').click(function(){
            frameHeight = iframe.document.body.scrollHeight + 'px';
            jQuery('iframe.form').height(frameHeight);
          });
        });
        popup.Open();
        return false;
      });
      
      jQuery(windowSelector).find('div.close').click(function() {
        popup.Close();
        jQuery(windowSelector).find('iframe.form').attr('src', 'about:blank');
      });
      
      jQuery('.divPopupBackground').click(function() {
        jQuery(windowSelector).find('iframe.form').attr('src', 'about:blank');
      });
      
    }
    
  }
  /* End public */
})();






/**
 * @namespace Manages zoekbox watermark
 */
nl.ggzbreburg.subsites.zoekbox = (function() {
  /* Start public */
  return {
    Init: function() {
      jQuery('input.searchBox_SearchTerm').val("Zoeken...");
      jQuery('input.searchBox_SearchTerm').focusin(function() {
        if(jQuery(this).val() == "Zoeken...") {
          jQuery(this).val("");
        }
      }).focusout(function() {
        if(jQuery(this).val() == "") {
          jQuery(this).val("Zoeken...");
        }
      });
    }
  }
  /* End public */
})();






nl.ggzbreburg.subsites.data = {};
