
Event.observe(window, 'load', function() {
    //initSifr();
    attachSubMenu();
    attachNieuwsBrief();
    attachTellafriend();
    attachNieuwsFlitsAnim();
    replaceLogo();
	attachPhotoViewer();
});

var SifrInitialized = false;

function initSifr() {
	if (typeof(sIFR)=='function') {
		sIFR.replaceElement("h1", named({
		sFlashSrc: "/swf/fonts/fontname.swf",
		sColor: "#000000",
		sWmode: 'transparent'
		}));
	}
}

function attachSubMenu() {
	$A($$('.subMenu .JS')).each(function(el) {
			el.observe('click', function(e) {
				Event.stop(e);
				if ($('div_'+el.id).visible()) {
					$('div_'+el.id).blindUp({duration:0.15});
				} else {
					$('div_'+el.id).blindDown({duration:0.15});
				}
			});
	});
}

function attachNieuwsFlitsAnim() {
	if($('nieuwsheadanim')) {
		//nieuwsflitsAnim(false);
		new PeriodicalExecuter(nieuwsflitsAnim, 2);
	}
}

var nieuwsflitsAnimCounter = 1;

function nieuwsflitsAnim() {
	if (nieuwsflitsAnimCounter==5) {
		nieuwsflitsAnimCounter = 1;
		return;
	}
	new Effect.Morph('nieuwsheadanim', {
		style: 'color: #939393',
		duration: 0.1,
		afterFinish: function() {
			new Effect.Morph('nieuwsheadanim', {
				style: 'color: #C11B27',
				duration: 0.1,
				afterFinish: function() {
					nieuwsflitsAnimCounter++;
					nieuwsflitsAnim();
				}
			});
		}
	});
}

function nieuwsflitsAnimOUD(FadeIN) {
	var anim_duration = (Math.floor(Math.random() * (20 - 1 + 1)) + 1)/10;
	var anim_delay = (Math.floor(Math.random() * (5 - 1 + 1)) + 1)/10;

	
	if (FadeIN==true) {
		new Effect.Morph('nieuwsheadanim', {
			style: 'color: #C11B27',
			duration: anim_duration,
			delay: anim_delay,
			afterFinish: function() {
				nieuwsflitsAnim(false);
			}
		});
	} else {
		new Effect.Morph('nieuwsheadanim', {
			style: 'color: #939393',
			duration: anim_duration,
			delay: anim_delay,
			afterFinish: function() {
				nieuwsflitsAnim(true);
			}
		});
	}

}

function attachNieuwsBrief() {
	$('nieuwsbrief_ja').observe('click', function(e) {
		Event.stop(e);
		
		$('nieuwsbrief_aanmelden').toggle();

		if ($('nieuwsbrief_aanmelden').visible()) {
			Form.focusFirstElement('nieuwsbrief_aanmelden');
			$('nieuwsbrief_ja').setStyle({
				"fontStyle": "italic"
			});
			$('nieuwsbrief_nee').setStyle({
				"fontStyle": "normal"
			});
		} else {
			$('nieuwsbrief_nee').setStyle({
				"fontStyle": "normal"
			});
			$('nieuwsbrief_ja').setStyle({
				"fontStyle": "normal"
			});
		}
		if ($('nieuwsbrief_afmelden').visible()) {
			$('nieuwsbrief_afmelden').hide();
		}
	});
	$('nieuwsbrief_nee').observe('click', function(e) {
		Event.stop(e);

		$('nieuwsbrief_afmelden').toggle();
		if ($('nieuwsbrief_afmelden').visible()) {
			Form.focusFirstElement('nieuwsbrief_afmelden');
			$('nieuwsbrief_nee').setStyle({"fontStyle": "italic"});
			$('nieuwsbrief_ja').setStyle({"fontStyle": "normal"});
		} else {
			$('nieuwsbrief_nee').setStyle({"fontStyle": "normal"});
			$('nieuwsbrief_ja').setStyle({"fontStyle": "normal"});
		}
		if ($('nieuwsbrief_aanmelden').visible()) {
			$('nieuwsbrief_aanmelden').hide();
		}
	});
	$('nieuwsbrief_afmelden').observe('submit', function(e) {
		Event.stop(e);
		if (validateForm('nieuwsbrief_afmelden')) {
			$('nieuwsbrief_afmelden').request({
				onComplete: function(transport) {
					if (transport.responseText=="true") {
						$('nieuwsbrief_afmelden').innerHTML = '<p>U bent afgemeld voor de digitale nieuwsbrief.</p>';
					} else {
						window.alert(transport.responseText);
					}
				}
			});
		}
	});
}


function attachTellafriend() {
	
	if ($('tellafriend')) {
		
		$('tellafriend').observe('submit', function(e) {
			Event.stop(e);
			
			if ($('tellafriend_feedback').visible()) {
				$('tellafriend_feedback').blindUp({duration:0.15});
			}
			
			if (validateForm('tellafriend')) {
				$('tellafriend').request({
					onComplete: function(transport) {
						if (transport.responseText=="true") {
							$('tellafriend_feedback').innerHTML = 'De website is naar uw vriend of kennis verstuurd!';
							$('tellafriend').reset();
							$('tellafriend_feedback').blindDown({duration:0.15});
						} else {
							$('tellafriend_feedback').innerHTML = transport.responseText;
							$('tellafriend_feedback').blindDown({duration:0.15});
						}
					}
				});
			}
		});
	}
}

function replaceLogo() {
	var so = new SWFObject("/swf/logo.swf?update=1", "logo", "143", "141", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("wmode", "transparent");
	so.write("logoanim");
}


function attachPhotoViewer() {
	// Point 'n click
	$$('#photoviewer_thumbs img').each(function(el) {
		el.observe('click', function(e) {
			var img_name = el.id.replace('thumb_', 'img_');
			$$('#photoviewer img').invoke('hide');
			$(img_name).show();
		});
	});
	
	// Thumbs control
	if ($('photonav_left')) {
		$('photonav_left').observe('click', function(e) {
			Event.stop(e);
			PhotoViewerGo(-1);
		});
	}
	if ($('photonav_right')) {
		$('photonav_right').observe('click', function(e) {
			Event.stop(e);
			PhotoViewerGo(1);
		});
	}
}

var photoviewer_position = 1;
var photoviewer_items = null;
var photoviewer_animationinprogress = false;

function PhotoViewerGo(direction) {

	if (photoviewer_animationinprogress) {
		return;
	}
	
	photoviewer_animationinprogress = true;

	if (photoviewer_items==null) {
		photoviewer_items = $A($$('#photoviewer_scroll img'));
	}
	
	var new_position = photoviewer_position + direction;
	
	var newX = direction * -140;
	
	if (new_position < 0) {
		//console.log('min');
		photoviewer_animationinprogress = false;
	} else if (new_position > photoviewer_items.length-2) {
		//console.log('max');		
		photoviewer_animationinprogress = false;
	} else {
		new Effect.Move('photoviewer_scroll', {
			x: newX,
			y: 0,
			mode: 'relative',
			delay: 0,
			duration: 0.25,
			afterFinish: function() {
				photoviewer_position = new_position;
				photoviewer_animationinprogress = false;
			}
		});
	}
}

