/////////////////////////////////////////// BASIC FUNCTIONS, DON'T EDIT ///////////////////////////////////////////

// Starts loading these functions on loading the page:
window.addEvent('domready', function() {

	// Enables the JS errorconsole when on the internal testing area:
	if (document.location.toString().match('ontwikkelding')) {
		bln_enableErrorConsole = true;
	} else {
		bln_enableErrorConsole = false;
	}
	
	// Define wether to use Firebug as the JS debugger (true) or the custom errorconsole (false):
	if (Browser.Engine.gecko) {
		bln_useFireBugAsErrorConsole = true;
	} else {
		bln_useFireBugAsErrorConsole = false;
	}
	if (bln_useFireBugAsErrorConsole == false) {
		initConsole();
	}
	
	initRemoveLinkLines();
	initExternalLinks();
	initCustomFunctions();
});



// Check for Internet Explorer in general:
var IE = navigator.userAgent.indexOf('MSIE') != -1;
// Check for Internet Explorer for Mac:
var IEMAC = (navigator.userAgent.indexOf('Mac') != -1 && (IE == true));
// Check for browser supporting W3CDOM:
var W3CDOM = (document.createElement && document.getElementsByTagName && !IEMAC);

// JavaScript error console for IE:
if (W3CDOM) {
	function initConsole() {
		if (bln_enableErrorConsole) {
			var el_console = document.createElement('div');
			el_console.id = 'errorConsole';
			el_console.style.background = '#ff9999';
			el_console.style.border = '3px double #ff0000';
			el_console.style.margin = '1em';
			el_console.style.padding = '1em 2em 1.5em';
			var el_errorConsoleTitle = document.createElement('h2');
			el_errorConsoleTitle.innerHTML = 'JavaScript Error Console:';
			el_errorConsoleTitle.style.color = '#ff0000';
			el_errorConsoleTitle.style.fontSize = '1.3em';
			el_errorConsoleTitle.style.margin = '0 0 0.5em';
			el_console.appendChild(el_errorConsoleTitle);
			var el_errorList = document.createElement('ol');
			el_errorList.id = 'errorList';
			el_errorList.style.margin = '0 2em';
			el_console.appendChild(el_errorList);
			document.body.appendChild(el_console);
		}
	}
	function error(message) {
		if (bln_enableErrorConsole && IE == false && bln_useFireBugAsErrorConsole == true) {
			window.console.log(message);
		} else if (bln_enableErrorConsole) {
			var el_newMessage = document.createElement('li');
			el_newMessage.innerHTML = message;
			el_newMessage.style.margin = '0 0 0.25em';
			var el_console = document.getElementById('errorList');
			el_console.appendChild(el_newMessage);
		}
	}
}

// This function is used in order for certain links to open in a new window without the direct target attribute:
function initExternalLinks() { 
	if (!document.getElementsByTagName) return; 
	var arr_anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<arr_anchors.length; i++) { 
		var el_anchor = arr_anchors[i]; 
		if (el_anchor.getAttribute("href") && el_anchor.getAttribute("rel") == "external") {
			el_anchor.target = "_blank";
		}
	}
}

// This function removes the dotted lines around links, but breaks the Tab button on the keyboard: less accessability:
function initRemoveLinkLines() {
	$$('a').addEvent('focus', function(){this.blur()});
}





/////////////////////////////////////////// EDIT CUSTOM JAVASCRIPT BELOW THIS LINE ///////////////////////////////////////////

// Add all your custom function which need to be initialized to this function:
function initCustomFunctions() {
	languageDropDownMenu();
	mainNavDropDownIE6();
	homeAccordionInteraction();
	homeDirectServicesNavigation();
	slideshowInteraction();
	generalAccordionInteraction();
	initVideoPlayer();
	formInputFocus();
	contactTabbedNavigation();
	alsoseeBlocksLinking();
}



// Defines the language selection dropdown list:
function languageDropDownMenu() {
	if (!$('language_selection')) return;
	
	var el_languageSelection = $('language_selection');
	
	el_languageSelection.addEvents({
		'mouseenter': function(){el_languageSelection.removeClass('closed')},
		'mouseleave': function(){el_languageSelection.addClass('closed')}
	});
}



// Adds the dropdown navigation to IE6 (faking :hover on a li-element):
function mainNavDropDownIE6() {
	if (!$('main_nav') || !Browser.Engine.trident4) return;
	
	var el_mainNav = $('main_nav');
	var arr_mainNavBtns = el_mainNav.getElements('li');
	
	arr_mainNavBtns.each(function(el_mainNavBtn){
		el_mainNavBtn.addEvents({
			'mouseenter': function(){el_mainNavBtn.addClass('hover')},
			'mouseleave': function(){el_mainNavBtn.removeClass('hover')}
		});
	});
}



// Defines the interaction of the home accordion:
function homeAccordionInteraction() {
	if (!$('home_accordion')) return;
	
	var int_homeAccNormalWidth = 80;
	var int_homeAccExtendedWidth = 479;
	var int_openedHomeAccSlide = 5;
	var int_homeAccSlideCount = 0;
	var arr_homeAccSlides = $$('.home_accordion_slide');
	var arr_homeAccImgs = $('home_accordion').getElements('img');
	
	arr_homeAccSlides.each(function(){
		int_homeAccSlideCount++;
		if (int_homeAccSlideCount <= int_openedHomeAccSlide) {
			arr_homeAccSlides[int_homeAccSlideCount - 1].setStyle('left', (int_homeAccNormalWidth * (int_homeAccSlideCount - 1)));
		} else {
			arr_homeAccSlides[int_homeAccSlideCount - 1].setStyle('left', ((int_homeAccNormalWidth * (int_homeAccSlideCount - 2)) + int_homeAccExtendedWidth));
		}
	});
	
	arr_homeAccImgs.each(function(el_homeAccImg, i){
		el_homeAccImg.addEvent('mouseenter', function(){
			var int_homeAccSlideCounter = 0;
			int_homeAccSlideCounter++;

			arr_homeAccSlides.each(function(el_homeAccSlide, n){
				if (i <= (n - 1)) {
					el_homeAccSlide.tween('left', el_homeAccSlide.getStyle('left'), ((int_homeAccNormalWidth * (n - 1)) + int_homeAccExtendedWidth));
				} else {
					el_homeAccSlide.tween('left', el_homeAccSlide.getStyle('left'), (int_homeAccNormalWidth * n));
				}
			});
		});
	});
}



// Defines the rollover navigation of the direct services on the homepage:
function homeDirectServicesNavigation() {
	if (!$('home_direct_services')) return;
	
	var int_homeDirectServiceId = 1;
	var el_homeDirectServices = $('home_direct_services')
	var arr_homeDirectServicesLinks = el_homeDirectServices.getElements('a');
	var arr_homeDirectServiceInfos = $$('.home_direct_service_info');
	
	arr_homeDirectServicesLinks.each(function(el_homeDirectServiceLink){
		el_homeDirectServiceLink.addEvent('mouseenter', function(){
			int_homeDirectServiceId = el_homeDirectServiceLink.id.substring(20,21);
			arr_homeDirectServicesLinks.each(function(el_thisHomeDirectServiceLink){
				el_thisHomeDirectServiceLink.removeClass('active');
			});
			el_homeDirectServiceLink.addClass('active');
			
			arr_homeDirectServiceInfos.each(function(el_homeDirectServiceInfo){
				el_homeDirectServiceInfo.addClass('hidden');
			});
			arr_homeDirectServiceInfos[int_homeDirectServiceId - 1].removeClass('hidden');
		});
	});
}



// Defines the carrousel navigation in the right column:
function slideshowInteraction() {
	if (!$('slideshow')) return;
	
	var el_slideshow = $('slideshow');
	var el_slideshowContainer = $('slideshow_container');
	var arr_slideshowImages = el_slideshowContainer.getElements('img');
	var int_slideshowImagesMaxNumber = arr_slideshowImages.length;
	
	var el_slideshowNav = $('slideshow_nav');
	if(el_slideshowNav) {
		var arr_slideshowNavLinks = el_slideshowNav.getElements('a');
	}
	
	var bln_slideEffectReady = true;
	var int_slideshowActiveImage = 1;
	var int_slideshowContainerOffset = 0;
	var int_slideshowImageWidth = arr_slideshowImages[0].getStyle('width').toInt();
	el_slideshowContainer.setStyle('width', (int_slideshowImageWidth * int_slideshowImagesMaxNumber));
	
	var fx_slideshowMorph = new Fx.Morph(el_slideshowContainer, {duration: 800, transition: Fx.Transitions.Quint.easeOut});
	
	if(el_slideshowNav) {
		arr_slideshowNavLinks.each(function(el_slideshowNavLink, n){
			el_slideshowNavLink.addEvent('click', function(){
				if (bln_slideEffectReady == true) {
					arr_slideshowNavLinks.each(function(el_thisSlideshowNavLink){
						el_thisSlideshowNavLink.removeClass('active');
					});
					
					if (el_slideshowNavLink.id == 'slideshow_prev') {
						int_slideshowActiveImage--;
						if (int_slideshowActiveImage <= 0) {
							int_slideshowActiveImage = int_slideshowImagesMaxNumber;
						}
						int_slideshowContainerOffset = ((int_slideshowImageWidth * (int_slideshowActiveImage - 1)) * -1);
					} else if (el_slideshowNavLink.id == 'slideshow_next') {
						int_slideshowActiveImage++;
						if (int_slideshowActiveImage > int_slideshowImagesMaxNumber) {
							int_slideshowActiveImage = 1;
						}
						int_slideshowContainerOffset = ((int_slideshowImageWidth * (int_slideshowActiveImage - 1)) * -1);
					} else {
						int_slideshowActiveImage = n;
						int_slideshowContainerOffset = ((int_slideshowImageWidth * (int_slideshowActiveImage - 1)) * -1);
					}
		
					arr_slideshowNavLinks[int_slideshowActiveImage].addClass('active');
					
					bln_slideEffectReady = false;
					fx_slideshowMorph.start({'left': int_slideshowContainerOffset}).chain(function(){bln_slideEffectReady = true});
				}
			});
		});
	}
}



// Defines the interaction of an accordion:
function generalAccordionInteraction() {
	if (!$('accordion')) return;
	
	var el_accordion = $('accordion');
	var arr_accordionTogglers = $$('.accordion_toggler');
	var arr_accordionPanels = $$('.accordion_panel');
	
	var obj_generalAccordion = new Accordion(el_accordion, arr_accordionTogglers, arr_accordionPanels, {
		show: 0,
		onActive: function(el_thisAccordionToggler){el_thisAccordionToggler.addClass('active')},
		onBackground: function(el_thisAccordionToggler){el_thisAccordionToggler.removeClass('active')}
	});
}



// Defines the videoplayer functionality:
function initVideoPlayer() {
	if (!$('videoplayer')) return;
	
	var str_videoLocation = '/';
	var el_videoPlayer = $('videoplayer');
	var str_videoFileName = el_videoPlayer.innerHTML;
	var int_videoPlayerWidth = 282;
	var int_videoPlayerHeight = 212;
	
	var obj_videoPlayer = new Swiff('/flash/mediaplayer.swf', {
		id: 'flash_videoplayer',
		container: $('videoplayer'),
		width: int_videoPlayerWidth,
		height: int_videoPlayerHeight,
		params: {
			allowfullscreen: 'true'
		},
		vars: {
			width: int_videoPlayerWidth,
			height: int_videoPlayerHeight,
			frontcolor: '0x333333',
			lightcolor: '0x75ac2d',
			screencolor: '0x333333',
			file: str_videoLocation + str_videoFileName
		}
	});
}



// Changes the look of an inputfield when focussed on:
function formInputFocus() {
	if (!$('application_form') && !$('contact_form')) return;
	
	if ($('application_form')) {
		var el_form = $('application_form');
	} else if ($('contact_form')) {
		var el_form = $('contact_form');
	}
	
	arr_formInputs = el_form.getElements('input');
	arr_formTextareas = el_form.getElements('textarea');
	arr_formInputs.extend(arr_formTextareas);
	
	arr_formInputs.each(function(el_formInput){
		el_formInput.addEvent('focus',function(){
		   	arr_formInputs.each(function(el_thisFormInput){
				el_thisFormInput.removeClass('focus');
			});

			if (!el_formInput.className.test('error')) {
				el_formInput.addClass('focus');
			}
		});
	});
}



// Defines the navigation of contact tabs:
function contactTabbedNavigation() {
	if (!$('contact_nav')) return;
	
	var el_contactNav = $('contact_nav');
	var arr_contactNavLinks = el_contactNav.getElements('a');
	var arr_contactPanels = $$('.contact_panel');

	arr_contactNavLinks.each(function(el_contactNavLink, i){
		el_contactNavLink.addEvent('click',function(){
			arr_contactNavLinks.each(function(el_thisContactNavLink, j){
				el_thisContactNavLink.removeClass('active');
				arr_contactPanels[j].addClass('hidden');
			});
			el_contactNavLink.addClass('active');
			arr_contactPanels[i].removeClass('hidden');
		});
	});
	
	initGoogleMapsNL();
	initGoogleMapsBE();
	
	arr_contactPanels[arr_contactPanels.length - 1].addClass('hidden');
	
}

// Initalizes Google Maps for the Netherlands:
function initGoogleMapsNL() {
	if (!$('map_nl')) return;

	if (GBrowserIsCompatible()) { 
		var gmap = new GMap2(document.getElementById("map_nl")); 
		var point = new GLatLng(52.173603,5.409005); 
		var centerpoint = new GLatLng(52.167000,5.404000); 
		var marker = new GMarker(point); 
		gmap.setCenter(centerpoint, 13, G_NORMAL_MAP) 
		
		gmap.addControl(new GLargeMapControl()); 
		gmap.addOverlay(marker); 
	}
}

// Initalizes Google Maps for the Netherlands:
function initGoogleMapsBE() {
	if (!$('map_be')) return;

	if ($('map_be') && GBrowserIsCompatible()) { 
		var gmap = new GMap2(document.getElementById("map_be")); 
		var point = new GLatLng(50.919053,5.352762); 
		var centerpoint = new GLatLng(50.923300,5.347600); 
		var marker = new GMarker(point); 
		gmap.setCenter(centerpoint, 13, G_NORMAL_MAP) 
		
		gmap.addControl(new GLargeMapControl()); 
		gmap.addOverlay(marker); 
	} 
}


// Creates a link of the entire alsosee block based on the anchor data of the head:
function alsoseeBlocksLinking() {
	if (!$$('.alsosee_block')) return;

	var arr_alsoseeBlocks = $$('.alsosee_block');
	
	arr_alsoseeBlocks.each(function(el_alsoseeBlock){
		var el_alsoseeBlockHeadAnchor = el_alsoseeBlock.getElement('.head');
		if (el_alsoseeBlockHeadAnchor != null) {
			el_alsoseeBlock.setStyle('cursor', 'pointer');
			el_alsoseeBlock.addEvent('click', function(){
				var att_alsoseeBlockHeadAnchorTitle = el_alsoseeBlockHeadAnchor.getProperty('title');
				var att_alsoseeBlockHeadAnchorLink = el_alsoseeBlockHeadAnchor.getProperty('href');
				document.location = att_alsoseeBlockHeadAnchorLink;
			});
		}
	});
}
