/////////////////////////////////////////// BASIC FUNCTIONS, DON'T EDIT ///////////////////////////////////////////

// Starts loading these functions on loading the page:
window.addEvent('domready', function() {
	initRemoveLinkLines();
	initExternalLinks();
	initCustomFunctions();
});

// 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() {
	mainnavDropdownIE6();
	improvementsFFMac();
	improvementsSafariMac();
 	replaceFonts();
	homepageScrolling();
	companyScrolling();
	quickScrollLocation();
	filterByJobTitle();
	terugLink();
	vacatureResetLink();
	
	initShadowBoxLinks();
	initLightbox();
}

// Initializes the Shadowbox lightbox.
function initLightbox() {
	var options = {
		//handleUnsupported: "link"
		flvPlayer: '../flash/flvplayer.swf',
		handleException: "Plugin not installed"
	};

	Shadowbox.init(options);
}

// Main navigation dropdownlist activation in IE6:
function mainnavDropdownIE6() {
	if (window.ie6) {
		var mainbtns = $$('li', 'main_nav');
		mainbtns.each(function(mainbtn){
			mainbtn.addEvents({
				'mouseenter': function(){mainbtn.className = 'hover'},
				'mouseleave': function(){mainbtn.className = ''}
			});
		});
	}
}

// Increase the fontsize by 1px only for Firefox on the Mac:
function improvementsFFMac() {
	var mac = navigator.platform.indexOf("Mac");
	if (window.gecko && (mac > -1)) {
		$$('body').setStyle('fontSize', '13px');
		if ($E('p', 'tagline')) {
			$E('p', 'tagline').setStyle('marginTop', '-0.3em');
		}
//		alert('Browser is Firefox for Mac');
	}
}

// Place the blue HR a little higher only for Safari on the Mac:
function improvementsSafariMac() {
	var mac = navigator.platform.indexOf("Mac");
	if (window.khtml && (mac > -1)) {
		if ($E('hr', 'content')) {
			$E('hr', 'content').setStyle('marginTop', '-6px');
		}
//		alert('Browser is Safari for Mac');
	}
}

// sIFR font replacing:
function replaceFonts() {
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"form.open_application h2", sFlashSrc:"/flash/sifr.swf", sColor:"#263447", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"form#job_application_form h2", sFlashSrc:"/flash/sifr.swf", sColor:"#36414f", sBgColor:"#f4f5f6", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"div#job_application_thanks h2", sFlashSrc:"/flash/sifr.swf", sColor:"#36414f", sBgColor:"#f4f5f6", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"div#tagline h2", sFlashSrc:"/flash/sifr.swf", sColor:"#FFFFFF", sBgColor:"#000000", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=-7", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"/flash/sifr.swf", sColor:"#263447", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:"div.column_alsosee h3", sFlashSrc:"/flash/sifr.swf", sColor:"#FFFFFF", sBgColor:"#000000", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:"h3", sFlashSrc:"/flash/sifr.swf", sColor:"#263447", sBgColor:"#FFFFFF", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"offsetTop=0", sWmode:"transparent"}));
	};
}

// Defines the scrolling content when navigating over the home_nav menu:
function homepageScrolling() {
	
	var scroll = new Fx.Scroll('viewport', {
		wait: false,
		duration: 800,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Circ.easeOut
	});
	
	if ($('home_nav')) {
		$('content').addEvent('mouseleave', function(event) {
			event = new Event(event).stop();
			scroll.toElement('homeBlock0');
			rememberHomeBtn(null);
		});
		$('home_btn1').addEvent('mouseenter', function(event) {
			event = new Event(event).stop();
			scroll.toElement('homeBlock1');
			rememberHomeBtn(this);
		});
		$('home_btn2').addEvent('mouseenter', function(event) {
			event = new Event(event).stop();
			scroll.toElement('homeBlock2');
			rememberHomeBtn(this);
		});
		$('home_btn3').addEvent('mouseenter', function(event) {
			event = new Event(event).stop();
			scroll.toElement('homeBlock3');
			rememberHomeBtn(this);
		});
		$('home_btn4').addEvent('mouseenter', function(event) {
			event = new Event(event).stop();
			scroll.toElement('homeBlock4');
			rememberHomeBtn(this);
		});
	}
}

function rememberHomeBtn(thisHomeBtn) {
	$$('a', 'home_nav').each(function(homebtn){homebtn.className=''});
	if (thisHomeBtn != null) {
		$E('a', thisHomeBtn.id).className = 'active';
	}
}

// Defines the scrolling content when navigating over the company menu:
function companyScrolling() {
	
	var scroll = new Fx.Scroll('viewport_full', {
		wait: false,
		duration: 1000,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Expo.easeInOut
	});
	
	if ($('viewport_full')) {
		$('company1_previous').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock4');
		});
		$('company1_next').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock2');
		});
		$('company2_previous').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock1');
		});
		$('company2_next').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock3');
		});
		$('company3_previous').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock2');
		});
		$('company3_next').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock4');
		});
		$('company4_previous').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock3');
		});
		$('company4_next').addEvent('click', function(event) {
			event = new Event(event).stop();
			scroll.toElement('contentBlock1');
		});
	}
}

// Detect a class to start scrolling to a chosen company:
function quickScrollLocation() {
	if ($('viewport_full')) {
		var quickscroll = new Fx.Scroll('viewport_full', {
			wait: false,
			duration: 0,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Expo.easeInOut
		});


		if ($('contentBlockWrapper').className == 'block1_selected') {
			quickscroll.toElement('contentBlock1');
		} else if ($('contentBlockWrapper').className == 'block2_selected') {
			quickscroll.toElement('contentBlock2');
		} else if ($('contentBlockWrapper').className == 'block3_selected') {
			quickscroll.toElement('contentBlock3');
		} else if ($('contentBlockWrapper').className == 'block4_selected') {
			quickscroll.toElement('contentBlock4');
		}
	}
}

// A filter showing only the jobtitles in the overview and hiding the rest of the information:
function filterByJobTitle() {
	if ($('job_title_filter')) {
		$('job_title_filter').addEvent('click', function(){
			if ($('job_title_filter').checked == true) {
				
				$('content').className = 'jobtitles_only';
				
				var a = new EfocusAjax('/ajax_server');
				a.zetActie('toonAlleenVacatureTitel', { 'checkbox' : 'aan'});
				a.doeRequest();
				
			} else {
				
				$('content').className = '';
				
				var a = new EfocusAjax('/ajax_server');
				a.zetActie('toonAlleenVacatureTitel', { 'checkbox' : 'uit'});
				a.doeRequest();
				
			}
		});
	}
}

// teruglink
function terugLink() {
	if ($('terug_link')) {
		$('terug_link').addEvent('click', function(){
			history.go(-1);
		});
	}
}

// resetlink
function vacatureResetLink() {
	if ($('vacature_reset_link')) {
		$('vacature_reset_link').addEvent('click', function(){
			var a = new EfocusAjax('/ajax_server');
			a.zetActie('resetVacatureZoeken');
			a.doeRequest();
		});
	}
}

// resetlink (bedrijf en vakgebied)
function resetVacatureZoekOpties(link) {
	
	var a = new EfocusAjax('/ajax_server');
	
	a.zetAsynchroon(true, 'verwerkVacatureZoekActie');
	a.zetActie('resetVacatureZoekOpties', { 'link' : link});	
	a.doeRequest();
	
}


// zoekactie vacatures
function vacatureZoeken(type,link) {
	
	var a = new EfocusAjax('/ajax_server');
	a.zetAsynchroon(true, 'verwerkVacatureZoekActie');
	
	if(type=='bedrijf') {
		
		bedrijf = $('zoek_bedrijf').value;
		a.zetActie('zoekVacature', { 'bedrijf' : bedrijf});	
	
	} else if(type=='vakgebied') {
		
		vakgebied = $('zoek_vakgebied').value;		
		a.zetActie('zoekVacature', { 'vakgebied' : vakgebied, 'link' : link});
	}
	
	a.doeRequest();
	
}

// verwerk het de vacature zoekactie
function verwerkVacatureZoekActie(ajaxResultaat) {
	
	if(ajaxResultaat['gelukt']) {
		if(ajaxResultaat['koppeling']) {
			document.location.href = ajaxResultaat['koppeling'];	
		} else {
		 location.reload(true);
		}
	} else {
		alert('Er gaat iets fout bij het verwerken van de zoekactie.');
	}
}

// zoekactie vacatures
function vacatureOverzichtBedrijf(bedrijf_id) {
	
	var a = new EfocusAjax('/ajax_server');
	a.zetAsynchroon(true, 'verwerkVacatureZoekActie');
	a.zetActie('zoekVacature', { 'bedrijf' : bedrijf_id, 'link' : 'vacatures'});
	
	a.doeRequest();
	
}

// Checks all links for videos with the wmv extension and adds the shadowbox rel attribute dynamically:
function initShadowBoxLinks() {
	var intDefaultVideoWidth = 320;
	var intDefaultVideoHeight = 240;
	var strVideoPlayer = '';

	$$('a').each(function(el){
		if (el.href.contains('.flv') || el.href.contains('.mpg') || el.href.contains('.wmv')) {
			if (el.href.contains('.flv')) {
				strVideoPlayer = 'flv';
			} else if (el.href.contains('.mpg')) {
				strVideoPlayer = 'qt';
			} else if (el.href.contains('.wmv')) {
				strVideoPlayer = 'wmp';
			}
			
			var arrVideoName = el.href.split('_');
			
			if (arrVideoName.length != 1) {
				var arrVideoNameEnd = arrVideoName[arrVideoName.length - 1].split('.');
				var arrVideoSizes = arrVideoNameEnd[0].split('x');
				if (arrVideoSizes.length != 1) {
					var intVideoWidth = arrVideoSizes[0];
					var intVideoHeight = arrVideoSizes[1];
				} else {
					var intVideoWidth = intDefaultVideoWidth;
					var intVideoHeight = intDefaultVideoHeight;
				}
			} else {
				var intVideoWidth = intDefaultVideoWidth;
				var intVideoHeight = intDefaultVideoHeight;
			}
			
			el.setProperty('rel', 'shadowbox;player=' + strVideoPlayer + ';width=' + intVideoWidth + ';height=' + intVideoHeight);
		}
	});
}

