/////////////////////////////////////////// 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')) {
		enableErrorConsole = true;
	} else {
		enableErrorConsole = false;
	}
	
	// Define wether to use Firebug as the JS debugger (true) or the custom errorconsole (false):
	useFireBugAsErrorConsole = true;
	
	if (window.ie) {
		useFireBugAsErrorConsole = false;
	}
	if (useFireBugAsErrorConsole == false) {
		initConsole();
	}
	
	initRemoveLinkLines();
	initExternalLinks();
	initCustomFunctions();
});



// Check for Internet Explorer for Mac:
var IEMAC = (navigator.userAgent.indexOf('Mac') != -1 && navigator.userAgent.indexOf('MSIE') != -1);
// Check for browser supporting W3CDOM:
var W3CDOM = (document.createElement && document.getElementsByTagName && !IEMAC);

// JavaScript error console for IE:
if (W3CDOM) {
	function initConsole() {
		if (enableErrorConsole) {
			var console = document.createElement('div');
			console.id = 'errorConsole';
			console.style.background = '#ff9999';
			console.style.border = '3px double #ff0000';
			console.style.margin = '1em';
			console.style.padding = '1em 2em 1.5em';
			var errorConsoleTitle = document.createElement('h2');
			errorConsoleTitle.innerHTML = 'JavaScript Error Console:';
			errorConsoleTitle.style.color = '#ff0000';
			errorConsoleTitle.style.fontSize = '1.3em';
			errorConsoleTitle.style.margin = '0 0 0.5em';
			console.appendChild(errorConsoleTitle);
			var errorList = document.createElement('ol');
			errorList.id = 'errorList';
			errorList.style.margin = '0 2em';
			console.appendChild(errorList);
			document.body.appendChild(console);
		}
	}
	function error(message) {
		if (enableErrorConsole && !window.ie && useFireBugAsErrorConsole == true) {
			window.console.log(message);
		} else if (enableErrorConsole) {
			var newMessage = document.createElement('li');
			newMessage.innerHTML = message;
			newMessage.style.margin = '0 0 0.25em';
			var console = document.getElementById('errorList');
			console.appendChild(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 anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			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() {
	//replaceFonts();
	homeBlockInteraction();
	alsoSeeImageCarrousel();
	alsoSeeEmployeeAccordion();
	standardAccordion();
	initTestimonialCarrousels();
	initLinkedIn();
	inputFieldDateOfBirthSkipping();
}



// sIFR font replacing:
function replaceFonts() {
	if(typeof sIFR == "function"){
		//sIFR.replaceElement(named({sSelector:"h4.accordion_head", sFlashSrc:"/fileadmin/templates/flash/sifr_15px.swf", sColor:"#FFFFFF", sBgColor:"#000000", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		//sIFR.replaceElement(named({sSelector:"p.employee_name", sFlashSrc:"/fileadmin/templates/flash/sifr_18px.swf", sColor:"#333333", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		//sIFR.replaceElement(named({sSelector:"p.event_location", sFlashSrc:"/fileadmin/templates/flash/sifr_18px.swf", sColor:"#333333", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		//sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"/fileadmin/templates/flash/sifr_25px_bold.swf", sColor:"#707814", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		//sIFR.replaceElement(named({sSelector:"p.intro", sFlashSrc:"/fileadmin/templates/flash/sifr_16px.swf", sColor:"#b5d333", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		//sIFR.replaceElement(named({sSelector:"h3.carrousel_head", sFlashSrc:"/fileadmin/templates/flash/sifr_18px.swf", sColor:"#d92131", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		//sIFR.replaceElement(named({sSelector:"h3", sFlashSrc:"/fileadmin/templates/flash/sifr_18px.swf", sColor:"#b5d333", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
	};
}



// Defines the interaction within the homeblock:
function homeBlockInteraction() {
	if (!$('homeblock_nav')) return;
	
	homeBlockTimerInterval = 6000;
	homeBlockTimer = null;
	
	arr_homeBlockListItems = $ES('li', 'homeblock_nav');
	arr_homeBlockContents = $ES('.homeblock_content', 'homeblock');
	arr_homeBlockContentsLength = arr_homeBlockContents.length;
	prevHomeBlock = arr_homeBlockContents[0];
	nextHomeBlock = arr_homeBlockContents[1];
	nextHomeBlockNumber = 2;
	
	arr_homeBlockContents.each( function(homeBlockContent) {
		if (prevHomeBlock != homeBlockContent) {
			homeBlockContent.setOpacity(0);
		}
	});

	arr_homeBlockListItems.each( function(homeBlockListItem) {
		homeBlockListItem.addEvent('mouseenter', function() {
			if (prevHomeBlock.getStyle('opacity') == 1) {
				clearTimeout(homeBlockTimer);
	
				arr_homeBlockListItems.each( function(thisHomeBlockListItem, i) {
					thisHomeBlockListItem.removeClass('active');
					if (homeBlockListItem == arr_homeBlockListItems[i]) {
						nextHomeBlock = arr_homeBlockContents[i];
						nextHomeBlockNumber = i + 1;
	
						homeBlockFade();
					}
				});
				homeBlockListItem.addClass('active');
			}
		});
	});
	
	setTimeout("homeBlockFade()", homeBlockTimerInterval);
}

// Adds the fading animation to the homeblocks:
function homeBlockFade() {
	clearTimeout(homeBlockTimer);

	var prevHomeBlockFadeOutFx = new Fx.Styles(prevHomeBlock, {duration: 200, wait: true, transition: Fx.Transitions.Cubic.easeIn});
	var nextHomeBlockFadeInFx = new Fx.Styles(nextHomeBlock, {duration: 300, wait: true, transition: Fx.Transitions.Cubic.easeOut});

	prevHomeBlock = nextHomeBlock;
	
	arr_homeBlockListItems.each( function(thisHomeBlockListItem) {
		if (thisHomeBlockListItem == arr_homeBlockListItems[nextHomeBlockNumber - 1]) {
			thisHomeBlockListItem.addClass('active');
		} else {
			thisHomeBlockListItem.removeClass('active');
		}
	});
	
	if (nextHomeBlockNumber == arr_homeBlockContentsLength) {
		nextHomeBlockNumber = 1;
		nextHomeBlock = arr_homeBlockContents[nextHomeBlockNumber - 1]; 
	} else {
		nextHomeBlockNumber++;
		nextHomeBlock = arr_homeBlockContents[nextHomeBlockNumber - 1]; 
	}
	
	prevHomeBlockFadeOutFx.start({opacity: [1,0]}).chain(function(){
		nextHomeBlockFadeInFx.start({opacity: [0,1]});
	});

	homeBlockTimer = setTimeout("homeBlockFade()", homeBlockTimerInterval);
}



// Defines the image carrousel as alsosee block:
function alsoSeeImageCarrousel() {
	if (!$E('.image_carrousel')) return;
	
	var ic = $E('.image_carrousel');
	var icNav = $E('.ic_nav', ic);
	var icPrevBtn = $E('.ic_prev', icNav);
	var icNextBtn = $E('.ic_next', icNav);
	var icNavDirectList = $E('ul', icNav);
	arr_icDirectBtns = $ES('a', icNavDirectList);
	icContent = $E('.ic_content');
	var arr_icSlides = $ES('.ic_slide');
	var icSlide = $E('.ic_slide');

	var icSlideCounter = 1;
	var icOffsetOld = 0;
	var icOffset = 0;

	var icSlidesLength = arr_icSlides.length;
	icWidth = icSlide.getStyle('width').toInt() + icSlide.getStyle('paddingRight').toInt();
	icContent.setStyle('width', icWidth * icSlidesLength);
	
	var icNavMaxWidth = icNav.getStyle('width').toInt() - (icPrevBtn.getStyle('width').toInt() + icNextBtn.getStyle('width').toInt());
	var icNavWidth = icSlidesLength * 10;
	var icNavOffset = (icNavMaxWidth - icNavWidth) / 2;
	icNavDirectList.setStyle('paddingLeft', icNavOffset);
	
	icPrevBtn.addEvent('click', function(){
		icSlideCounter--;
		icOffsetOld = icOffset;
		if (icSlideCounter <= 0) {
			icSlideCounter = icSlidesLength;
			icOffset = icWidth * ((icSlidesLength - 1) * -1);
		} else {
			icOffset = icOffset + icWidth;
		}
		scrollImageCarrousel(icSlideCounter, icOffsetOld, icOffset);
	});
	
	icNextBtn.addEvent('click', function(){
		icSlideCounter++;
		icOffsetOld = icOffset;
		if (icSlideCounter > icSlidesLength) {
			icSlideCounter = 1;
			icOffset = 0;
		} else {
			icOffset = icOffset - icWidth;
		}
		scrollImageCarrousel(icSlideCounter, icOffsetOld, icOffset);
	});
	
	arr_icDirectBtns.each(function(icDirectBtn){
		icDirectBtn.addEvent('click', function(){
			icSlideCounter = icDirectBtn.innerHTML.toInt();
			icOffsetOld = icOffset;
			icOffset = icWidth * ((icSlideCounter - 1) * -1);
			scrollImageCarrousel(icSlideCounter, icOffsetOld, icOffset);
		});
	});
}

// Defines the scrolling motion of the image carrousel:
function scrollImageCarrousel(icSlideCounter, icOffsetOld, icOffset) {
	var icScrollFx = new Fx.Styles(icContent, {duration: 1000, wait: true, transition: Fx.Transitions.Cubic.easeInOut});
	icScrollFx.start({'left': [icOffsetOld, icOffset]});
	
	updateImageCarrouselDirectNav(icSlideCounter, icOffset);
}

// Updates the direct nav buttons of the image carrousel:
function updateImageCarrouselDirectNav(icSlideCounter, icOffset) {
	for (var i = 0; i < arr_icDirectBtns.length; i++) {
		arr_icDirectBtns[i].removeClass('active');
		if (arr_icDirectBtns[i] == arr_icDirectBtns[icSlideCounter-1]) {
			arr_icDirectBtns[icSlideCounter-1].addClass('active');
		}
	}
}



// Defines the custom accordion sliding of the employees alsosee block:
function alsoSeeEmployeeAccordion() {
	if (!$('employees_accordion')) return;
	
	var employeeVisualHeight = 130;
	var employeeVisualHeightCollapsed = 0;
	var arr_employeeContents = $ES('.employee_content', 'employees_accordion');
	var arr_employeeSlideImages = $ES('.slide_image', 'employees_accordion');
	var employeeAccordionFx = new Fx.Elements(arr_employeeSlideImages, {duration: 600, wait: false, transition: Fx.Transitions.Bounce.easeOut});

	arr_employeeSlideImages[0].setStyle('height', employeeVisualHeight);

	arr_employeeContents.each( function(employeeContent, i) {
										
		employeeContent.addEvent('mouseenter', function(e){
			employeeSlideImage = $E('.slide_image', employeeContent);
			var obj = {};
			obj[i] = {'height': [employeeSlideImage.getStyle('height').toInt(), employeeVisualHeight]};
			arr_employeeSlideImages.each( function(other, j) {
				if (other != employeeSlideImage) {
					var h = other.getStyle('height').toInt();
					if (h != employeeVisualHeightCollapsed) obj[j] = {'height': [h, employeeVisualHeightCollapsed]};
				}
			});
			employeeAccordionFx.start(obj);
		});
	});
}



// Creates the basic functionality of a standard accordion:
function standardAccordion() {
	if (!$('accordion')) return;
	
	var arr_accordionClickables = $ES('.clickable', 'accordion');
	var arr_accordionHideables = $ES('.hideable', 'accordion');
	
	var quickMenuAccordion = new Accordion(arr_accordionClickables, arr_accordionHideables, {
		opacity: false,
		onActive: function(el){activateAccordionButton(el)},
		onBackground: function(el){deactivateAccordionButtons(el)},
		display: -1,
		alwaysHide: true
	});
}

// Activates the accordion button which has been clicked:
function activateAccordionButton(accordionButton) {
	accordionButton.getParent().addClass('active');
}

// Deactivates the accordion buttons which have not been clicked:
function deactivateAccordionButtons(accordionButton) {
	var arr_accordionClickables = $ES('.clickable', 'accordion');
	
	arr_accordionClickables.each(function(accordionClickable){
		if (accordionClickable == accordionButton) {
			accordionButton.getParent().removeClass('active');
		};
	});
}



// Initializes all testimonial carrousels:
function initTestimonialCarrousels() {
	if (!$E('.testimonial_carrousel')) return;
	
	var tcId = 'testimonial_carrousel_';
	var arr_TCs = $$('.testimonial_carrousel');
	
	arr_TCs.each(function(thisTC, t){
		thisTC = new testimonialCarrousel(tcId + (t + 1));
	});
}

// Defines a single testimonial carrousel:
function testimonialCarrousel(thisTestimonialCarrouselId) {
	if (!$(thisTestimonialCarrouselId)) return;
	
	var tc = $(thisTestimonialCarrouselId);
	var tcNav = $E('.tc_nav', tc);
	var tcPrevBtn = $E('.tc_prev', tcNav);
	var tcNextBtn = $E('.tc_next', tcNav);
	var tcNavDirectList = $E('ul', tcNav);
	var arr_tcDirectBtns = $ES('a', tcNavDirectList);
	var tcContent = $E('.tc_content', tc);
	var arr_tcSlides = $ES('.tc_slide', tc);
	var tcSlide = $E('.tc_slide', tc);

	var tcSlideCounter = 1;
	var tcOffsetOld = 0;
	var tcOffset = 0;

	var tcSlidesLength = arr_tcSlides.length;
	var tcWidth = tcSlide.getStyle('width').toInt() + tcSlide.getStyle('paddingRight').toInt();
	tcContent.setStyle('width', tcWidth * tcSlidesLength);
	
	var tcNavMaxWidth = tcNav.getStyle('width').toInt() - (tcPrevBtn.getStyle('width').toInt() + tcNextBtn.getStyle('width').toInt());
	var tcNavWidth = tcSlidesLength * 10;
	var tcNavOffset = (tcNavMaxWidth - tcNavWidth) / 2;
	tcNavDirectList.setStyle('paddingLeft', tcNavOffset);
	
	tcPrevBtn.addEvent('click', function(){
		tcSlideCounter--;
		tcOffsetOld = tcOffset;
		if (tcSlideCounter <= 0) {
			tcSlideCounter = tcSlidesLength;
			tcOffset = tcWidth * ((tcSlidesLength - 1) * -1);
		} else {
			tcOffset = tcOffset + tcWidth;
		}
		scrollTestimonialCarrousel(arr_tcDirectBtns, tcSlideCounter, tcOffsetOld, tcOffset, tcContent);
	});
	
	tcNextBtn.addEvent('click', function(){
		tcSlideCounter++;
		tcOffsetOld = tcOffset;
		if (tcSlideCounter > tcSlidesLength) {
			tcSlideCounter = 1;
			tcOffset = 0;
		} else {
			tcOffset = tcOffset - tcWidth;
		}
		scrollTestimonialCarrousel(arr_tcDirectBtns, tcSlideCounter, tcOffsetOld, tcOffset, tcContent);
	});
	
	arr_tcDirectBtns.each(function(tcDirectBtn){
		tcDirectBtn.addEvent('click', function(){
			tcSlideCounter = tcDirectBtn.innerHTML.toInt();
			tcOffsetOld = tcOffset;
			tcOffset = tcWidth * ((tcSlideCounter - 1) * -1);
			scrollTestimonialCarrousel(arr_tcDirectBtns, tcSlideCounter, tcOffsetOld, tcOffset, tcContent);
		});
	});
}


// Defines the scrolling motion of the testimonial carrousel:
function scrollTestimonialCarrousel(arr_tcDirectBtns, tcSlideCounter, tcOffsetOld, tcOffset, tcContent) {
	var tcScrollFx = new Fx.Styles(tcContent, {duration: 1000, wait: true, transition: Fx.Transitions.Cubic.easeInOut});
	tcScrollFx.start({'left': [tcOffsetOld, tcOffset]});
	
	updateTestimonialCarrouselDirectNav(arr_tcDirectBtns, tcSlideCounter, tcOffset);
}

// Updates the direct nav buttons of the testimonial carrousel:
function updateTestimonialCarrouselDirectNav(arr_tcDirectBtns, tcSlideCounter, tcOffset) {
	for (var i = 0; i < arr_tcDirectBtns.length; i++) {
		arr_tcDirectBtns[i].removeClass('active');
		if (arr_tcDirectBtns[i] == arr_tcDirectBtns[tcSlideCounter-1]) {
			arr_tcDirectBtns[tcSlideCounter-1].addClass('active');
		}
	}
}



// Initializes the LinkedIn popup:
function initLinkedIn() {
	new LinkedIn.CompanyInsiderPopup("linkedin","Hay Group");
}



// When filling out the date of birth inputfields the focus will jump to the next field:
function inputFieldDateOfBirthSkipping() {
	if (!$('date_of_birth') || !$('dob_day')) return;

	var el_dobDay = $('dob_day');
	var el_dobMonth = $('dob_month');
	var el_dobYear = $('dob_year');
	var int_dobDayMax = el_dobDay.maxLength;
	var int_dobMonthMax = el_dobMonth.maxLength;
	var int_dobYearMax = el_dobYear.maxLength;

	el_dobDay.addEvents({
		'keydown': function(){
			if (el_dobDay.value.length == int_dobDayMax) {
				el_dobMonth.focus();
			}
		},
		'focus': function(){
			if (el_dobDay.value.length == int_dobDayMax) {
				el_dobDay.value = '';
			}
		}
	});

	el_dobMonth.addEvents({
		'keydown': function(){
			if (el_dobMonth.value.length == int_dobMonthMax) {
				el_dobYear.focus();
			}
		},
		'focus': function(){
			if (el_dobMonth.value.length == int_dobMonthMax) {
				el_dobMonth.value = '';
			}
		}
	});

	el_dobYear.addEvent('focus', function(){
		if (el_dobYear.value.length == int_dobYearMax) {
			el_dobYear.value = '';
		}
	});
}
/*
 * initF14FormSubmitButton
 *
 * form button submits
 *
 * @author Rob Intema <rob.intema{AT}efocus.nl>
 * @return void
 */
 
function reloadCAPTCHA() {
	 document.getElementById('CAPTCHA').src='typo3conf/ext/captcha/captcha/captcha.php?'+Date();
	 }


