//sIFR config
var flux = { src: sDomain + 'flash/sifr.swf' };
sIFR.activate(flux);
var flux2 = { src: sDomain + 'flash/slogan.swf' };
sIFR.activate(flux2);
//functies die standaard moeten worden ingeladen
$(document).ready(function(){
	initSIFR();
	//fixLeft();
	searchInput();
	searchAuto();
	menu();
	goto();
	selectboxes();
	tables();
	tellafriend();
});
//de functies
function initSIFR(){
	sIFR.replace(flux, {
		selector: '#filter h2.flash',
		css: '.sIFR-root { color: #ffffff; font-size: 20px; font-weight: bold; }',
		wmode: 'transparent'
	});	
	sIFR.replace(flux, {
		selector: 'h1.flash, #_d h1',
		css: '.sIFR-root { color: #0b154f; font-size: 24px; font-weight: bold; }',
		wmode: 'transparent'
	});	
	sIFR.replace(flux, {
		selector: 'h2.flash.20px',
		css: '.sIFR-root { color: #0b154f; font-size: 20px; font-weight: bold; }',
		wmode: 'transparent'
	});	
	sIFR.replace(flux, {
		selector: 'h2.flash.18px',
		css: '.sIFR-root { color: #0b154f; font-size: 18px; font-weight: bold; }',
		wmode: 'transparent'
	});
	sIFR.replace(flux2, {
		selector: 'div.slogan div.row',
		css: [
			'.sIFR-root { color: #ffffff; font-size: 37px; font-weight: bold; text-align: center; }',
			'em { font-style: normal; font-weight: normal; font-size: 17px; color: #ffffff; text-align: center; }'
		],
		wmode: 'transparent'
	});
}
function fixLeft(){
	if($('#content #left').length > 0){
		var nHeightLeft = $('#content #left').outerHeight(true);
		var nHeightRight = $('#content #right').outerHeight(true);
		if(nHeightLeft >= nHeightRight){
			$('#content').css('padding-bottom',63);
		}
	}
}
function menu(){
	var nZindex = 50;
	$('#menu ul li.nav').each(function(){
		$(this).css('z-index',nZindex);
		nZindex--;
	});
	
	$('#menu ul li.nav').hover(function(){
		$(this).children('.submenu').show();
		$(this).children('.corner').show();
	}, function(){
		$(this).children('.submenu').hide();
		$(this).children('.corner').hide();
	});
}
function nieuwsbrief(){
	var bClicked = false;
	$('#newsletter_form label.vakgebiedkeuze').click(function() {
		if(!bClicked){
			$(this).find('em').fadeTo(150, 0.33);
			$('.keuze_vakgebieden').show();
			bClicked = true;
		} else {
			$(this).find('em').fadeTo(150, 1);
			$('.keuze_vakgebieden').hide();
			bClicked = false;
		}
	});	
	$('#submit_vakgebieden').click(function() {
		$('.keuze_vakgebieden').hide();
		$(this).parents('.keuze_vakgebieden').prev().find('em').fadeTo(150, 1);
		bClicked = false;
	});
}
function searchInput() {
	$('input.fade').each(function(){
		if ( $(this).val() != '' ) {
			$(this).css('background-repeat', 'repeat');
			$(this).prev().fadeTo(150, 0.33);
		}
	});
	$('input.fade').focus(function() {
		$(this).select();
		$(this).prev().fadeTo(150, 0.33);
		$(this).keydown(function() {
			$(this).css('background-repeat', 'repeat');			 
		});
	});
	$('input.fade').blur(function() {
		if ( $(this).val() == '' ) {
			$(this).css('background-repeat', 'no-repeat');
			$(this).prev().fadeTo(150, 1);
		}
	});
}
function searchAuto(){
	$('input#q').autocomplete('inc/requests.inc.php?function=autocomplete', {
		formatItem: function(data) {
			return '<a href="javascript://">'+data[0]+'</a>';
		},
		minChars: 3,
		max: 10
	}).result(function(e,data,formatted){
		location.href = data[1];
		return false;
	});
}
function goto(){
	$('#goto').hover(function(){
		$(this).children('.goto').addClass('hover');
		$(this).children('.goto_menu').show();
	}, function(){
		$(this).children('.goto').removeClass('hover');
		$(this).children('.goto_menu').hide();
	});	
}
function slideshowHome(){
	$('#sfeer .slideshow').each(function(index){
		$(this).cycle({
			fx: 'fade',
			timeout: 5000,
			speed: 1000,
			pager: 'ul#pager',
			pagerAnchorBuilder: function(idx, slide) { 
				if (index == 0)
					return '<li><a href="#">'+(idx+1)+'</a></li>';
				return 'ul#pager li a:eq('+idx+')';
			},
			after: function(curr, next, opts){
				initSIFR();
			},
			cleartype: 1,
			cleartypeNoBg: true,
			activePagerClass: 'active'
		});
	});
}
function googlemaps(){
	var myLatlng = new google.maps.LatLng(52.141218,5.423795);
	var myOptions = {
	  zoom: 12,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP,	  
	  mapTypeControl: false,
	  scrollwheel: false
	}
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
	
	var image = new google.maps.MarkerImage('images/template/google_marker2.png',
		new google.maps.Size(44, 48),
		// The origin for this image is 0,0.
		new google.maps.Point(0,0),
		// The anchor for this image is the base of the flagpole at 0,32.
		new google.maps.Point(16,42)
	);
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		icon: image
	}); 	
}
function selectboxes(){
	$('div.select select').selectbox();
	var zIndex = 9999;
	$('div.form div.entry').each(function(){
		$(this).css('z-index',zIndex);
		zIndex--;
	});
}
function tables(){
	$('div.table div.row:even, table.stijl tr:even').addClass('odd');
	$('div.table div.row, table.stijl tr').hover(function(){
		$(this).toggleClass('hover');
	}, function(){
	 	$(this).toggleClass('hover');
	});
}
function toggleAccordion(){
	showAccordion($('a#toggle_all'));
	$('a#toggle_all').click(function(){
		$(this).toggleClass('open');
		showAccordion($(this));
	});	
}
function showAccordion(obj){
	if(obj.hasClass('open')){
		obj.html('Sluit alle categorie&euml;n');
		$('div.table div.slide div.cat').addClass('open');
		$('div.table div.slide div.row').slideDown(300);
	} else {
		obj.html('Open alle categorie&euml;n');
		$('div.table div.slide div.cat').removeClass('open');
		$('div.table div.slide div.row').slideUp(300);
	}	
}
function accordion() {
	//check of er cursussen zijn, anders verbergen
	$('div.table div.slide div.cat').each(function(){
		if($(this).parent().children('.row').length == 0){
			$(this).parent().remove();
		}
	});
	var bOpen = true;
	$('div.table div.slide div.cat').click(function(){
		if(bOpen){
			//parent element in var stoppen
			var oSlide = $(this).parent('.slide');
			var oRows = oSlide.children('.row');
		}
		if(oRows.length > 0){
			if($(this).hasClass('open')){
				if(bOpen){
					bOpen = false;
					$(this).removeClass('open');
					oRows.slideUp(300, function(){
						bOpen = true;
					});
				}
			} else {
				if(bOpen){
					bOpen = false;
					$('div.table div.slide div.cat').removeClass('open');
					$('div.table div.slide div.row').slideUp(300);
					$(this).addClass('open');
					oRows.slideDown(300, function(){
						bOpen = true;
					});
				}
			}
		}
	});
}
var nMaxHeight = 0;
var nSiteHeight = 0;
var nButtonHeight = 0;
var nWindowHeight = 0;
var nStart = 0;
var nYLocation = 0;
var nViewPort = 0;
var nOffset = 0;
var offset = 0;
function floatingBar(p_bExtra){	
	offset = 0;
	nStart = 0;
	nYLocation = 0;
	$('#bar').stop(true,true);
	
	nMaxHeight = parseInt($('#right').outerHeight(true)) - 50;
	if(p_bExtra){
		nMaxHeight = nMaxHeight - 30;
	}
	nSiteHeight = parseInt($('#site').outerHeight(true));
	nButtonHeight = parseInt($('#bar').outerHeight(true));
	nWindowHeight = parseInt($(window).height());
	if(nWindowHeight < nSiteHeight){
		nStart = nWindowHeight - 375;
	} else {
		nStart = nMaxHeight;
	}	
	nYLocation = nStart;
	
	if(nWindowHeight < (nSiteHeight - $(document).scrollTop())){
		var nCSS = nStart + $(document).scrollTop();
	} else {
		var nCSS = nMaxHeight;
	}
	$('#bar').css({'top':nCSS+'px'}).show();
}
function scrollBar(){
	nViewPort = nWindowHeight + $(document).scrollTop();
	if( nViewPort > (nYLocation + nButtonHeight) ){
		nOffset = (nViewPort - nYLocation) - nButtonHeight;
		if(nWindowHeight > nYLocation){
			offset = nYLocation + $(document).scrollTop();
		} else {
			offset = nYLocation + nOffset;
		}
		if(offset > nMaxHeight){
			offset = nMaxHeight;
		}
		$('#bar').animate({top:offset+'px'},{duration:500,queue:false});
	}
}
function tabs(p_sAdres){
	//tabjes positioneren
	var nLeft = 13;
	$('#tabs ul li:visible').each(function(){
		var nWidth = $(this).outerWidth(true);
		$(this).css('left',nLeft);
		nLeft += (nWidth+1);
	});
	
	//linken van tabjes regelen
	var sHash = window.location.hash.replace('#!/','');
	if(sHash != ''){
		$('#tab_'+sHash).show();
		$('#tabs ul li a[href*="#!/'+sHash+'"]').addClass('active');
		if(sHash == 'routebeschrijving'){
			$('#bar').hide();
			route(p_sAdres);
		} else {
			floatingBar(false);
		}
	} else {
		$('#tabs ul li:first a').addClass('active');
		$('.tab_content:first').show();
		floatingBar(false);
	}
	
	$('#tabs ul li a').click(function(){
		//$('html,body').animate({scrollTop: 0},0);
		var sId = this.hash.replace('#!/','');
		$('#tabs ul li a').removeClass('active');
		$(this).addClass('active');
		$('.tab_content').hide();
		$('#tab_'+sId).show();
		if(sId == 'routebeschrijving'){
			$('#bar').hide();
			route(p_sAdres);
		} else {
			floatingBar(true);
		}
	});
}
function catUri(p_sCat) {
	p_sCat = p_sCat.toLowerCase();
	p_sCat = p_sCat.replace(/[^a-zA-Z 0-9]+/g,'');
	p_sCat = p_sCat.replace(/ /g,'+');
	return p_sCat;
}
function extraInfoCursus(p_nCursus,p_sEvenementIDs){
	var sHash = window.location.hash.replace('#!/','');
	$('#par_prijs,#tab_brochure').html('<img style="margin: 5px 0px 0px 0px;" src="'+sDomain+'images/loading.gif" />');
	$.ajax({
		type: 'POST',
		url: 'inc/requests.inc.php',
		data: 'function=extraInfoCursus&nCursus='+p_nCursus,
		dataType: 'json',
		success: function(oData){
			//prijsje
			if($('#par_prijs').length > 0){
				$('#par_prijs').html('&euro; '+oData.prijs+' excl. omzetbelasting');
			}
			
			//brochures
			var sBrochures = '';
			if(oData.brochures.length > 0){
				sBrochures += '<div class="par">';
				sBrochures += '<ul>';
				var nCount = 0;
				$.each(oData.brochures, function(){
					var aExt = this.bestand.split('.');
					if(nCount & 1){
						sBrochures += '<li class="odd">';
					} else {
						sBrochures += '<li>';
					}
					sBrochures += '<span class="title">'+this.omschrijving+'</span>';
					sBrochures += '<em class="file">(.'+aExt[aExt.length - 1]+'-bestand)</em>';
					sBrochures += '<a href="'+sDomain+'file.php?file='+this.bestand+'&name='+catUri(this.omschrijving)+'" target="_blank">download</a>';
					sBrochures += '</li>';
					nCount++;
				});
				sBrochures += '</ul>';
				sBrochures += '</div>';
				
				var nTempWidth = $('#tabs ul li.tab_brochure').outerWidth(true)+1;
				var nCount = 0;
				var nBrochureLeft = 0;
				$('#tabs ul li:not(.static)').each(function(){
					if(nCount == 0){
						nBrochureLeft = parseInt($(this).css('left').replace('px'));	
					}
					var nLeft = parseInt($(this).css('left').replace('px'));
					$(this).css('left',(nLeft+nTempWidth)+'px');
					nCount++;
				});
				if(nBrochureLeft == 0){
					nBrochureLeft = 241;	
				}
				$('#tabs ul li.tab_brochure').css('left',nBrochureLeft+'px').show();
				$('#tab_brochure').html(sBrochures);
				if(sHash == 'brochure'){
					floatingBar(true);	
				}
			}
		}
	});	
}
function inschrijven(){
	
	$('#loading').fadeOut(100, function(){
		$('#formuliertje').show();
	});
	
	var nZindexBOP = 9993;
	$('.column.bop .select').each(function(){
		$(this).css('z-index',nZindexBOP);
		nZindexBOP--;
	});
	$('.column.bop .radio label input').click(function(){
		var oCheckboxes = $('.column.bop .checkbox label input');
		var nCount = 0;
		var sVal = $(this).val();
		var nLoop;
		var oInputs = $('.column.bop .select input.selectbox');	
		var oSelects = $('.column.bop .select select');
		var oCursussessies = $('.column.bop .cursussessies');
		switch(sVal){
			case 'bop':
				nLoop = 5;
			break;
			case 'bob':
				nLoop = 8;
			break;
			default:
				nLoop = 0;
			break;
		}
		if($(this).attr('checked')){
			oCheckboxes.attr('checked',false).removeClass('basisopleiding');
			oInputs.val('Kies een startdatum \u2026');
			oSelects.val('Kies een startdatum \u2026');
			oCheckboxes.next('.select').find('li.selected').removeClass('selected');
			oCheckboxes.parents('.checkbox').css('opacity','1');
			oCursussessies.hide();
			oCheckboxes.each(function(){
				if(nCount < nLoop){
					$(this).attr('checked',true).addClass('basisopleiding');
					//faden
					$(this).parents('.checkbox').css('opacity','0.7');
				}
				nCount++;
			});
		}
	});
	$('.column.bop .selectbox-wrapper ul li').click(function(){
		var aValue = $(this).attr('id').split('_');
		var sValue = aValue[aValue.length-1];
		var oCheckbox = $(this).parents('.select').prev().find('input');
		if(sValue != ''){
			oCheckbox.attr('checked',true);
		} else {
			oCheckbox.attr('checked',false);	
		}
	});
	$('.column.bop .checkbox label input').click(function(){
		var oCursussessies = $(this).parents('.checkbox').next().next();
		var sRadio = $('.column.bop .radios .radio label input:checked').val();
		var bConfirm;
		var bHide = true;
		switch(sRadio){
			case 'bop':
			case 'bob':
				bConfirm = true;
			break;
			default:
				bConfirm = false;
			break;
		}
		if(!$(this).attr('checked')){
			if(bConfirm && $(this).hasClass('basisopleiding')){
				if(confirm('Deze module is onderdeel van een basisopleiding. Weet u het zeker?')){
					bHide = true;
					$('.column.bop .radios .radio label input.nop').attr('checked',true);
				} else {
					bHide = false;
					$(this).attr('checked',true);	
				}
			}
			if(bHide){
				var oInput = $(this).parents('.checkbox').next('.select').children('input.selectbox');	
				var oSelect = $(this).parents('.checkbox').next('.select').children('select');
				oInput.val('Kies een startdatum \u2026');
				oSelect.val('Kies een startdatum \u2026');
				$(this).parents('.checkbox').next('.select').find('li.selected').removeClass('selected');
				oCursussessies.hide();
			}
		} else {
			if(oCursussessies.prev('span').length > 0 && oCursussessies.length > 0){
				var nEvenement = $(this).parents('.checkbox').next().attr('id').replace('evenement_','');
				fetchSessies(nEvenement, oCursussessies);
			}
		}
	});
	$('#sWerkgeverOrganisatie').change(function(){
		var obj = $('#sFactuurAdres,#sFactuurHuisnummer,#sFactuurPostcode,#sFactuurPlaats');
		obj.attr('disabled',true).css('background','url('+sDomain+'images/loading.gif) no-repeat center center');
		$.ajax({
			type: 'POST',
			url: 'inc/requests.inc.php',
			data: 'function=getOrganisatie&sNaam='+ $(this).val(),
			dataType: 'json',
			success: function(oData){
				if(oData != 0){
					if(oData.post_huisnummer_toev == undefined){
						oData.post_huisnummer_toev = '';	
					}
					$('#sFactuurAdres').val(oData.post_adres);
					$('#sFactuurHuisnummer').val(oData.post_huisnummer+oData.post_huisnummer_toev);
					$('#sFactuurPostcode').val(oData.post_postcode);
					$('#sFactuurPlaats').val(oData.post_woonplaats);
					$('#nOrganisatie').val(oData.afas_organisatie);
				} else {
					$('#sFactuurAdres').val('');
					$('#sFactuurHuisnummer').val('');
					$('#sFactuurPostcode').val('');
					$('#sFactuurPlaats').val('');
					$('#nOrganisatie').val('');
				}
				obj.attr('disabled',false);	
				obj.css('background','none');
			}
		});
	});
	//cursussessies
	$('.select .selectbox-wrapper ul li').click(function(){
		var aID = $(this).attr('id').split('_');
		var nEvenement = aID[aID.length-1];
		var oCursussessies = $(this).parents('.select').next();
		fetchSessies(nEvenement, oCursussessies);
	});
	//extra validatie
	$('#inschrijfformulier .submit input').click(function(){
		var sRadio = $('.column.bop .radios .radio label input:checked').val();
		var bEmpty = false;
		if($('.column.bop').length > 0){
			var nX = 0;
			$('.column.bop .checkbox label input:checked').each(function(){
				var sSelectboxVal = $(this).parents('.checkbox').next().find('select').val();
				if(sSelectboxVal == ''){
					bEmpty = true;	
				}
				nX++;
			});
			if(nX == 0 || (nX < 10 && sRadio == 'bop')){
				bEmpty = true;
			}
		} else {
			var sSelectboxVal = $('#cursusdatum .select').children('select').val();
			if(sSelectboxVal == ''){
				bEmpty = true;
			}
		}
		if(bEmpty){
			if(nX < 10 && sRadio == 'bop'){
				$('p.modulenerror').html('Voor de \'Basisopleiding Publiekszaken\' dienen in totaal 10 modules geselecteerd te worden.');
			} else if(nX == 0){
				$('p.modulenerror').html('Er dient tenminste &eacute;&eacute;n module geselecteerd te worden.');	
			} else if(sRadio == 'bop' || sRadio == 'bob') {
				$('p.modulenerror').html('Er is niet voor elke geselecteerde module een startdatum gekozen.');
			} else {
				$('p.modulenerror').html('Er is geen startdatum gekozen.');	
			}
			$('p.modulenerror').show();
			$('body,html').animate({scrollTop: 0}, 'slow');
			return false;
		} else {
			$('p.modulenerror').hide();	
		}
	});
}
function fetchSessies(nEvenement, oCursussessies){
	oCursussessies.html('<img src="'+sDomain+'images/loading.gif" />').show();
	$.ajax({
		type: 'POST',
		url: 'inc/requests.inc.php',
		data: 'function=fetchSessies&nEvenement='+ nEvenement,
		success: function(result){
			if(result != ''){
				oCursussessies.html(result);
			} else {
				oCursussessies.hide();	
			}
			if($('p.modulenerror').is(':visible')){
				$('p.modulenerror').hide();	
			}
		}
	});	
}
function filter(){
	$('#filter label.spec input').change(function(){
		var sVakgebied = $(this).val();
		if($(this).attr('checked')){
			var sData = 'sVakgebied='+sVakgebied;
		} else {
			var sData = 'sVakgebied='+sVakgebied+'&del=1';
		}
		$('#ajax').html('<div id="loader"><img src="images/loading.gif" /></div>');
		$.ajax({
			type: 'POST',
			url: 'inc/requests.inc.php',
			data: 'function=filter&'+sData,
			success: function(result){
				$('a#toggle_all').html('Open alle categorie&euml;n').removeClass('open');
				$('#ajax').html(result);
				//accordion();
				tables();
				setOrderAttr();
				showMoreCursus();
			}
		});
	});																  
}
function showMoreCursus(){
	$('div.row div.field span.more_dates').hover(function(){
		$(this).next('.more_overlay').show();
	},function(){
		$(this).next('.more_overlay').hide();
	});
}
function setOrderAttr(){
	$('#ajax div.row').each(function(){
		var aValues = $(this).attr('rel').split(';');
		var sOmschrijving = aValues[0].replace('omschrijving:','');
		var sDatum = aValues[1].replace('datum:','');
		var sLocatie = aValues[2].replace('locatie:','');
		$(this).attr('omschrijving',sOmschrijving);
		$(this).attr('datum',sDatum);
		$(this).attr('locatie',sLocatie);
	});	
}
function orderby(){		
	$('div.table div.head div:not(.details)').click(function(){
		
		$(this).toggleClass('down');
		$(this).toggleClass('up');
		
		var sOrder = $(this).attr('id').replace('sort_','');
		var sDirection = 'desc';
		if($(this).hasClass('down')){
			sDirection = 'asc';	
		}		
		//var sOrderBy = $('div.slide div.row').attr('rel').match(/omschrijving:(.*?);/gi);
		$('#ajax div.row').tsort({
			order: sDirection,
			attr: sOrder
		});
		//'zebra' opnieuw initialiseren
		$('#ajax div.row').removeClass('odd');
		tables();
	});
}
function tellafriend(){
	$('a#tellfriend').click(function(){
		$('#tellafriend').toggle();
		initSIFR();
		
		$('#tellafriend_form').validate({
			rules: {
				sNaam: 'required',
				sNaam2: 'required',
				sEmail: {
					required: true,
					email: true
				},
				sEmail2: {
					required: true,
					email: true
				}
			},
			messages: {
				sNaam: 'Uw naam is verplicht.',
				sNaam2: 'De naam van de ontvanger is verplicht.',
				sEmail: {
					required: 'Uw e-mailadres is verplicht.',
					email: 'Dit is geen geldig e-mailadres.'
				},
				sEmail2: {
					required: 'Het e-mailadres van de ontvanger is verplicht.',
					email: 'Dit is geen geldig e-mailadres.'
				}
			},
			errorPlacement: function(error, element) {
				var obj = element.parent().next('div.icon');
				error.insertAfter(obj);
				obj.attr('class','icon invalid');
			},
			errorElement: 'div',
			success: function(label) {
				label.prev('div.icon').attr('class','icon valid');
				label.remove();
			},
			submitHandler: function(form) {
				form.trigger('submit');
			}
		});
	});
}
function uploadButton() {
	$('input.file').filestyle({ 
		 image: sDomain+'images/template/bg_browse.gif',
		 imageheight : 32,
		 imagewidth : 101,
		 width : 200,
		 padding : 0
	 });
}
function validateForm(p_sForm){
	jQuery.validator.addMethod("dateNL", function(value, element, params) {
		if(value == '00-00-0000' || value == '00-00-00'){
			return false;	
		}
		return this.optional(element) || /^\d\d?\-\d\d?\-\d\d\d?\d?$/.test(value);
	}, "Vul een geldige datum in. (dd-mm-jjjj)");
	$('#'+p_sForm).validate({
		rules: {
			sCV: {
				accept: 'pdf|doc|xls|docx|xlsx'
			},
			sExtra: {
				accept: 'pdf|doc|xls|docx|xlsx'
			},
			sBijlage: {
				accept: 'pdf|doc|xls|docx|xlsx'
			},
			sGeboortedatum: 'dateNL'
		},
		errorPlacement: function(error, element) {
			var obj = element.parent().next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		errorElement: 'div',
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.trigger('submit');
		}
	});	
}
function orderby2(){		
	var aAsc = [];
	$('table.jobs th.down').click(function(){
		
		$(this).toggleClass('down');
		$(this).toggleClass('up');
		
		var nNr = $(this).attr('id').replace('column_','');
		aAsc[nNr] = aAsc[nNr]=="asc"?"desc":"asc";
		
		$('table.jobs>tbody>tr').tsort({
			order: aAsc[nNr]
		});
		//'zebra' opnieuw initialiseren
		$('table tr:odd').removeClass('odd');
		tables();
	});
}
function login(p_sRedirect,p_sWebkey){
	$('a.wachtwoord').click(function(){
		var sVisible = $('.formfade:visible').attr('id');
		var sHidden = $('.formfade:hidden').attr('id');
		$('#'+sVisible).fadeOut(300, function(){
			$('#'+sHidden).fadeIn(300);
		});
	});
	$('#loginform').validate({
		rules: {
			sUsername: {
				required: true,
				email: true
			},
			sPassword: 'required'
		},
		messages: {
			sUsername: {
				required: 'Dit veld is verplicht.',
				email: 'Dit is geen geldig e-mailadres.'
			},
			sPassword: 'Dit veld is verplicht.'
		},
		errorPlacement: function(error, element) {
			var obj = element.parent().next('.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		errorElement: 'div',
		success: function(label) {
			label.prev('.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			$('#loginform p').fadeOut(300,function(){
				$(this).html('<img src="'+sDomain+'images/loading2.gif" /><em class="loading">Moment geduld a.u.b.</em>').fadeIn(300);
			});
			var options = {
				url: 'inc/requests.inc.php?function=login',
				type: 'POST',
				success: function(response) {
					if(response == 1){
						if(p_sWebkey == 'webkey'){
							var sRedirect = 'http://www.webkey6.nl/klant/mijnsegment/index.php?webkey=1';	
						} else {
							if(sDomain.indexOf('webkey6.nl') > 0){
								var sRedirect = sDomain.replace('segment','mijnsegment')+p_sRedirect;	
							} else {
								var sRedirect = sDomain+'mijnsegment/'+p_sRedirect;	
							}
						}
						window.location.href = sRedirect;
					} else {
						$('#loginform p').addClass('error').text('Onjuiste inloggegevens!');	
					}
				}
			}
			$(form).ajaxSubmit(options);
			return false;
		}
	});
	$('#wachtwoordform').validate({
		rules: {
			sEmail: {
				required: true,
				email: true
			}
		},
		messages: {
			sEmail: {
				required: 'Dit veld is verplicht.',
				email: 'Dit is geen geldig e-mailadres.'
			}
		},
		errorPlacement: function(error, element) {
			var obj = element.parent().next('.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		errorElement: 'div',
		success: function(label) {
			label.prev('.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			$('#wachtwoord_form p').fadeOut(300,function(){
				$(this).html('<img src="'+sDomain+'images/loading2.gif" /><em class="loading">Moment geduld a.u.b.</em>').fadeIn(300);
			});
			var options = {
				url: 'inc/requests.inc.php?function=wachtwoord',
				type: 'POST',
				success: function(response) {
					if(response == 1){
						$('#wachtwoord_form').fadeOut(300, function(){
							$(this).html('<p>Succesvol verzonden.</p>').fadeIn(300);
						});
						setTimeout("$('#wachtwoord_form').fadeOut(300,function(){$('#login_form').fadeIn(300);});",3000);
					} else {
						$('#wachtwoord_form p').addClass('error').text('Dit e-mailadres is niet bekend in ons systeem.');	
					}
				}
			}
			$(form).ajaxSubmit(options);
			return false;
		}
	});
}
function route(p_sAdres){
	
	var directionDisplay;
	var directionsService = new google.maps.DirectionsService();
	var map;
	
	directionsDisplay = new google.maps.DirectionsRenderer();
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': p_sAdres}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var myLatlng = results[0].geometry.location;
			var myOptions = {
				zoom:9,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				center: myLatlng,	  
				mapTypeControl: false,
				scrollwheel: false
			}
			map = new google.maps.Map(document.getElementById("map"), myOptions);
			google.maps.event.trigger(map, 'resize');
			map.setZoom(map.getZoom());
			directionsDisplay.setMap(map);
			directionsDisplay.setPanel(document.getElementById("panel"));
		}
	});
	
	$('#sStart').change(function(){
		calculateRoute(directionDisplay, directionsService, map);
	});
	$('#sEind_container ul li').click(function(){
		calculateRoute(directionDisplay, directionsService, map);
	});
}
function calculateRoute(directionDisplay, directionsService, map){
	var start = $('#sStart').val();
	var end = $('#sEind_container ul li.selected').attr('id').replace('sEind_input_','');
	var request = {
		origin:start, 
		destination:end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
			$('#print_route').show();
		}
	});		
}
function vacatureFilter(){
	$('.vacaturefilter ul li a').click(function(){
		$(this).toggleClass('on');
		var sValue = $(this).attr('id');
		if($(this).hasClass('on')){
			var sData = 'categorie='+sValue;
		} else {
			var sData = 'categorie='+sValue+'&del=1';
		}
		$.ajax({
			type: 'POST',
			url: 'inc/requests.inc.php',
			data: 'function=vacaturefilter&'+sData,
			success: function(result){
				$('#ajax_jobs').html(result);
			}
		});
	});
}
