// geen observer(dom:loaded) meer, zie Mantis 8440.
//var initApplications = function (){

// ###################################################################
// ## FLASH PLACER (AUTOMATED UNOBTRUSIVE)							##
// ###################################################################
var execFlashPlacer = function()
{
	var required = 8;
	
	if (NetEffect.FlashInfo.installed && NetEffect.FlashInfo.version >= required) 
	{
		$$("div.js-flash-embed").each(function(element)
		{
			if(element.up(".nav"))
				return;
			new NetEffect.HtmlFlashEmbed(element);
			element.className.replace("js-flash-embed","");
			return;
		});
	};
};
execFlashPlacer();
//$(document).observe("dom:loaded",execFlashPlacer);


// ###################################################################
// ## GRAFIEK AND FORM FIX											##
// ###################################################################

function newUrl(currentUrl) {
		// new split due to Mantis 8230
		var urlSplitter;
		if(currentUrl.indexOf(";jsessionid") > -1) {
			urlSplitter = ";jsessionid";
		} else {
			urlSplitter = "?";
		}
		return currentUrl.substr(0, currentUrl.indexOf(urlSplitter)) + 'resources/' + currentUrl.substr(currentUrl.indexOf(urlSplitter));
}

var execGrafiekEnFormFix = function()
{
	var koersGrafiek = $("koersVerloopGrafiek");
	if (koersGrafiek){
//		var currentUrl = koersGrafiek.src;
//		koersGrafiek.src = currentUrl.substr(0, currentUrl.indexOf('?')) + 'resources/' + currentUrl.substr(currentUrl.indexOf('?'));
		koersGrafiek.src = newUrl(koersGrafiek.src);
	};
	
	$$("form[name^='mailForm']").each(function(o){
		o.action = newUrl(o.action);
	});
	
	$$("form[id^='filterForm']").each(function(o){
		o.action = newUrl(o.action);
	});
	
	$$("select[id^='faqCategory']").each(function(o){
		var form = o.up("form");
		form.action = newUrl(form.action);
	});
};
execGrafiekEnFormFix();
//$(document).observe("dom:loaded",execGrafiekEnFormFix);

	
// ###################################################################
// ## FORMVALIDATOR (AUTOMATED UNOBTRUSIVE)							##
// ###################################################################
var execFormValidator = function()
{
	var formToolTip = new NetEffect.AltToolTip({
		"style" : {
			"border" : "1px solid #cecece",
			"fontWeight" : "bold"
		}
	});
	
	var preloadAttentionImg = new Image();
	preloadAttentionImg.src = "/docroot/images/attentie_red_ico.gif";
	
	$$("form.js-validator").each(function(o){
		// Validator object
		o.validator = new NetEffect.FormValidator(o);
		// Custom Validator handlers
		// onValid
		o.validator.observe("valid",function(e){
			if(e.field.attentionImg) {
				e.field.parentNode.removeChild(e.field.attentionImg);
				e.field.attentionImg = null;
			}
		});
		// onInvalid
		o.validator.observe("invalid",function(e){
			if(!e.field.attentionImg) {
				e.field.attentionImg = document.createElement("img");
				e.field.attentionImg.src = "/docroot/images/attentie_red_ico.gif";
				e.field.attentionImg = $(e.field.parentNode.insertBefore(e.field.attentionImg,e.field));
				if(e.field.hasClassName("js-validate-radio")){
					e.field.attentionImg.setStyle({
						"position" : "relative",
						"margin" : "3px 0 0 0px",
						"float" : "left",
						"zIndex" : "10",
						"width" : "16px",
						"height" : "15px"
					});
				} else {
					e.field.attentionImg.setStyle({
						"position" : "relative",
						"margin" : "3px 0 0 -22px",
						"float" : "left",
						"zIndex" : "10",
						"width" : "16px",
						"height" : "15px"
					});
				}
				// set alt texts for formToolTip object
				if(e.field.hasClassName("js-validate-string"))
					e.field.attentionImg.alt = "Vul hier alleen tekst in.";
				if(e.field.hasClassName("js-validate-number"))
					e.field.attentionImg.alt = "Vul hier alleen getallen in.";
				if(e.field.hasClassName("js-validate-email"))
					e.field.attentionImg.alt = "Vul hier een geldig e-mail adres in.";
				if(e.field.hasClassName("js-validate-select"))
					e.field.attentionImg.alt = "Selecteer <b>minimaal</b> 1 optie.";
				if(e.field.hasClassName("js-validate-radio"))
					e.field.attentionImg.alt = "Maak hier verplicht een keuze.";
				if(e.field.hasClassName("js-validate-notempty"))
					e.field.attentionImg.alt = "Vul dit verplichte veld in.";
				if(e.field.hasClassName("js-validate-phonenumber"))
					e.field.attentionImg.alt = "Vul hier uw telefoonnummer in.";
				// Bind events to the formToolTip object
				$(e.field.attentionImg).observe("mouseover",formToolTip.showTip.bindAsEventListener(formToolTip));
				$(e.field.attentionImg).observe("mouseout",formToolTip.hideTip.bindAsEventListener(formToolTip));
			}
		});
		// onValidateField
		o.validator.observe("fieldvalidated",function(e){
			if(!$(e.field.parentNode).visible() && !e.field.isValid) {
				if(e.field.attentionImg) {
					e.field.parentNode.removeChild(e.field.attentionImg);
					e.field.attentionImg = null;
				}
				e.validator.errors -=1;
			}
		});
		// onDone
		o.validator.observe("done",function(e){});
	});
};
execFormValidator();
//$(document).observe("dom:loaded",execFormValidator);
	
	
// ###################################################################
// ## ACCORDION (AUTOMATED UNOBTRUSIVE)								##
// ###################################################################
var execAccordion = function()
{
	$$("ul.js-accordion").each(function(o){
		var tmp = new NetEffect.Accordion(o,{
			'panelOpen' : function(obj){
				obj.parentNode.accordionButton.hide();
				obj.setStyle({
					'overflow':'auto',
					'height':'auto'
				});
				obj.show();
			},
			'panelClose': function(obj){
				obj.parentNode.accordionButton.show();
				if(Prototype.Browser.IE) 
					obj.setStyle({'overflow':'hidden','height':'1px'});
				else 
					obj.setStyle({'overflow':'hidden','height':'0px'});
				obj.hide();
			},
			'pageUpdate' : false
		});
	});
	
	// Hover effects
	$$("ul.collapse-list div.js-accordion-button").each(function(o){
		o.observe("mouseover",function(e){
			Event.findElement(e,"li").setStyle({"backgroundColor":"#f2f3f4"});
		});
		o.observe("mouseout",function(e){
			Event.findElement(e,"li").setStyle({"backgroundColor":""});
		});
	});
};
execAccordion();
//$(document).observe("dom:loaded",execAccordion);

// ###################################################################
// ## FAQ (AUTOMATED UNOBTRUSIVE)									##
// ###################################################################
var execFAQ = function()
{
	$$("div.vragen-container").each(function(el){
		var current = null;
		
		el.immediateDescendants("div").each(function(row){
			var button = $(row).down(".js-faq-question");
			var panel = $(row).down(".js-faq-answer").hide();
			if(button) {
				button.href = "javascript:void(0)";
				
				button.observe("click",function(e){
					e.stop();
					panel.show();
					button.addClassName("active");
					if(current)
					{
						current.button.removeClassName("active");
						current.panel.hide();
					};
					current = {
						button : button,
						panel : panel
					};
				});
			}
		});
	});
};
execFAQ();
//$(document).observe("dom:loaded",execFAQ);

// ###################################################################
// ## KLIKMOMENTEN (AUTOMATED UNOBTRUSIVE)							##
// ###################################################################
var execKlikMomenten = function()
{
	if(!window.global) window.global = {"klikmoment":null};
	$$("table.click-table a").each(function(o){
		var cns = o.classNames(),kid=false;
		cns.each((function(n){
			if(n.include("onderliggendewaarde_"))
				kid = n;
		}).bind(this));
		if(kid) {
			o.victim = $(kid).hide();			
			o.observe("click",function(e){
				Event.stop(e);
				if(window.global.klikmoment)
					$(window.global.klikmoment).hide();
				Event.findElement(e,"a").victim.show();
				window.scrollTo(0,Position.cumulativeOffset(Event.findElement(e,"a").victim)[1]);
				window.global.klikmoment = Event.findElement(e,"a").victim;
			});
		} else {
			o.observe("click",function(e){
				Event.stop(e);
			});
		}
	});
};
execKlikMomenten();
//$(document).observe("dom:loaded",execKlikMomenten);

// ###################################################################
// ## KLIKMOMENT TABLE (AUTOMATED UNOBTRUSIVE)						##
// ###################################################################
var execKlikmomentTable = function()
{
	$$("img.js-toggle-table").each(function(o,i) {
		o.observe("click",function(e){
			$("klikmomenttable").toggle();
		});
	});
};
execKlikmomentTable();
//$(document).observe("dom:loaded",execKlikmomentTable);
	

// ###################################################################
// ## TOOLTIPS (AUTOMATED UNOBTRUSIVE)								##
// ###################################################################
var execInfoToolTip = function()
{
	var infoToolTip = new NetEffect.AltToolTip({
		"style" : {
			"border" : "1px solid #cecece",
			"fontWeight" : "bold"
		}
	});
	$$("img.js-tooltip-button").each(function(o){
		o.observe("mouseover",infoToolTip.showTip.bindAsEventListener(infoToolTip));
		o.observe("mouseout",infoToolTip.hideTip.bindAsEventListener(infoToolTip));
	});
};
execInfoToolTip();
//$(document).observe("dom:loaded",execInfoToolTip);
	
// ###################################################################
// ## TEKSTGROOTTE (AUTOMATED UNOBTRUSIVE)							##
// ###################################################################
var execTekstGrootte = function()
{
	$$("div.tekstgrootte").each(function(el){
		var fls = ["2_1","2_2","2_3"];
		var reStr = fls.join("|");
		var re = new RegExp("("+reStr+")\.css");
		
		var cssSheet = $$("head link").find(function(ss){
			return re.test(ss.href);
		});
		
		var switchSize = function(el){
			current.removeClassName("active");
			current = el.addClassName("active");
			new Ajax.Request(el.href);
		};
		
		var current = el.down("a.active");
		
		el.down("a.small").observe("click",function(e){
			e.stop();
			cssSheet.href = cssSheet.href.replace(re,fls[0]+".css");
			switchSize(this);
		});
		
		el.down("a.middle").observe("click",function(e){
			e.stop();
			cssSheet.href = cssSheet.href.replace(re,fls[1]+".css");
			switchSize(this);
		});
		
		el.down("a.large").observe("click",function(e){
			e.stop();
			cssSheet.href = cssSheet.href.replace(re,fls[2]+".css");
			switchSize(this);			
		});
	});
};
execTekstGrootte();
//$(document).observe("dom:loaded",execTekstGrootte);

// ###################################################################
// ## GOOGLE SEARCH FORM											##
// ###################################################################
var execGoogleSearchForm = function()
{
	var SearchForm = $("google-search");
	if(SearchForm)
	{
		new NetEffect.SearchApplication(SearchForm);
	};
};
// moet ff uit namens W&vO
//execGoogleSearchForm();
//$(document).observe("dom:loaded",execGoogleSearchForm);

// ###################################################################
// ## RADIO TOGGLER													##
// ###################################################################
var execRadioToggler = function()
{
	$$(".js-radio-toggle").each(function(el){
		
		var target = el.down("div.js-radio-toggle-target");
		
		new NetEffect.RadioToggler(el,target);
		
	});	
};
execRadioToggler();
//$(document).observe("dom:loaded",execRadioToggler);
