$(document).observe("dom:loaded",function(){
	
	// Fix for navigation list.
	(function(){
		
		$$("div#navigation ul ul li").each(function(element){
			
			if(element.hasClassName("active")) {
				
				var topUl = element.up("ul",1) || null;
				
				if(topUl) {
					topUl.className = "mainlevel-parent";
				};
				
			};
			
		})
		
	})();
	
	
	
	// +--------------------------------------------------------+
	// | Form Validator											|
	// +--------------------------------------------------------+
	(function(){ 
		
		var validationMessages = {
			STRING : "Dit veld dient ingevuld te worden.",
			EMPTY : "Dit veld dient ingevuld te worden.",
			RADIO : "Kies 1 optie.",
			EMAIL : "Uw e-mail is vereist.",
			NUMBER : "Vul een correct getal in.",
			PHONE : "Vul hier 10 getallen in ZONDER streepje.",
			ZIP : "Vul een correcte postcode in (bv.: 1234AB)",
			DATE : "Vul een correcte datum in (bv.: 22/03/1977)",
			CHECK : "Aanvinken vereist!"
		};
		
		var validationTooltip = document.createElement("div");
		validationTooltip = $(document.body.appendChild(validationTooltip));
		validationTooltip.addClassName("validation-tooltip");
		validationTooltip.hide();
		
		$$("form.js-validate").each(function(formElm,index){
			
			var validationHandler = function(event) {
				// event.valid Boolean
				// event.element FormElement
				// event.validationType String
				var elm = event.element;
				if(!event.valid) {
					if(!elm.alertImage) {
						elm.alertImage = document.createElement("span");
						elm.alertImage = $(document.body.appendChild(elm.alertImage));
						elm.alertImage.addClassName("validation-alert");
						var offset = elm.cumulativeOffset();
						elm.alertImage.setStyle({
							left : (offset.left-20)+"px",
							top : offset.top+"px"
						});
						
						// attach tooltip message
						elm.alertImage.observe("mouseover",function(e){
							validationTooltip.setStyle({
								top: offset.top+"px",
								left: offset.left+"px"
							});
							validationTooltip.innerHTML = validationMessages[event.validationType];
							validationTooltip.show();
						});
						
						elm.alertImage.observe("mouseout",function(e){
							validationTooltip.hide();
						})
						
						
					} else {
						elm.alertImage.show();
					};
				} else {
					if(elm.alertImage) {
						elm.alertImage.hide();
					};
				};
			};
			
			formElm.validator = new neteffect.FormValidator(formElm,{
				onValidate : validationHandler
			});

		});
		
		$$("form.js-validate-left").each(function(formElm,index){
			
			var validationHandler = function(event) {
				var elm = event.element;
				if(!event.valid) {
					if(!elm.alertImage) {
						elm.alertImage = document.createElement("span");
						elm.alertImage = $(document.body.appendChild(elm.alertImage));
						elm.alertImage.addClassName("validation-alert");
						var offset = elm.cumulativeOffset();
						
						elm.alertImage.setStyle({
							left : (offset.left + elm.getWidth() + 10)+"px",
							top : offset.top+"px"
						});
						
						offset = elm.alertImage.cumulativeOffset();
						
						// attach tooltip message
						elm.alertImage.observe("mouseover",function(e){
							validationTooltip.setStyle({
								top: offset.top+"px",
								left: (offset.left + elm.alertImage.getWidth())+"px"
							});
							validationTooltip.innerHTML = validationMessages[event.validationType];
							validationTooltip.show();
						});
						
						elm.alertImage.observe("mouseout",function(e){
							validationTooltip.hide();
						})
						
						
					} else {
						elm.alertImage.show();
					};
				} else {
					if(elm.alertImage) {
						elm.alertImage.hide();
					};
				};
			};
			
			formElm.validator = new neteffect.FormValidator(formElm,{
				onValidate : validationHandler
			});
			
		});
		
	})();
	
	// +--------------------------------------------------------+
	// | PopUp Layer for Video									|
	// +--------------------------------------------------------+
//	(function(){
//		
//		if(location.href.indexOf("/home.html") > 0) {
//			
//			var movieId = 'kncwxvnf';
//			var popDelay = 1000; // Miliseconds
//			
//			setTimeout(function(){
//				var pop = new neteffect.PopInBox({
//					boxClass : "video-formevideo",
//					closeable : true,
//					draggable : true,
//					y : 147,
//					x : 165
//				});
//				pop.setContent('<iframe id="VideoIFrame" style="border:0px; width:455px; height:280px;" src="http://player.formevideo.nl/'+movieId+'/?view_id=7514454" scrolling="no" marginheight="0" marginwidth="0" frameborder="0"></iframe>');
//				pop.open();
//			},popDelay);
//			
//		};
//		
//	})();
//	
	// +--------------------------------------------------------+
	// | PopUp Layer for Poll									|
	// +--------------------------------------------------------+
	(function(){
		
		
		$$(".js-flashpoll-link").each(function(element){

			element.observe("click",function(e){
				e.stop();
				
				var src = this.href;
				
				var pop = new neteffect.PopInBox({
					boxClass : "flash-poll",
					closeable : true,
					draggable : true,
					y : 147,
					x : 165
				});
				pop.setContent('<iframe id="VideoIFrame" style="border:0px; width:550px; height:328px;" src="'+src+'" scrolling="no" marginheight="0" marginwidth="0" frameborder="0"></iframe>');
				pop.open();
				
			});
			
		});
		
	})();
	
	// +--------------------------------------------------------+
	// | PopUp Layer for Testimonials							|
	// +--------------------------------------------------------+
	(function(){
		
		$$("a.testimonial-link").each(function(element){
			
			$(element).observe('click', function(event){
				
				event.stop();
				
				var src = this.href;
				
				new Ajax.Request(src,{
					onSuccess : function(transport) {
						if(!element.testimonialPopLayer) {
							element.testimonialPopLayer = new neteffect.PopInBox({
								boxClass : "content-testimonial",
								closeable : true,
								draggable : true,
								y : 236,
								x : 170
							});
							element.testimonialPopLayer.setContent(transport.responseText);
						};
						element.testimonialPopLayer.open();
					}
				});
				
			});
			
		});
		
	})();
	
	// +--------------------------------------------------------+
	// | Input string replace on focus							|
	// +--------------------------------------------------------+
	(function(){
		
		$$("input.js-string-replace").each(function(element){
		
			if(!element.originalValue) {
				
				// store original value
				element.originalValue = $F(element);
				
				// add focus listener to clear original value
				element.observe("focus",function(e){
					
					if(this.value == this.originalValue) {
					
						this.value = "";
						
					};
					
				});
				
				// add blur listener to restore original value if empty
				element.observe("blur",function(e){
					
					var re = /\b\w+\b/i; // Regular expression
					
					if(!re.test(this.value)) {
						
						this.value = this.originalValue;
						
					};
					
				});
				
			};
		});
		
		
		
	})()
	
});	
