var markersOn = true;

$(document).ready(function(){
	// Initialize flexible form fields
	// Education
	$(".education-record:first").each(function() {
		educationRecord = $(this);
	});
	$(".education-record:last").each(function() {
		educationRecordCount = parseInt(this.id.match(/\d+$/)) + 1;
	});
	$("#addEducationBtn").click(function() {
		// Clone object and events
		$(this).before(educationRecord.clone(true));
		$(".education-record:last, .education-record:last > *").each(function() {
			if(parseInt(this.id.match(/\d+/)) > 0) {
				this.id = this.id.replace(/\d+/, educationRecordCount);
			}
			this.value = "";
		});
		educationRecordCount++;
		addDeleteFunction();
		return false;
	});
	// Courses
	$(".course-record:first").each(function() {
		courseRecord = $(this);
	});
	$(".course-record:last").each(function() {
		courseRecordCount = parseInt(this.id.match(/\d+$/)) + 1;
	});
	$("#addCourseBtn").click(function() {
		// Clone object and events
		$(this).before(courseRecord.clone(true));
		$(".course-record:last, .course-record:last > *").each(function() {
			if(parseInt(this.id.match(/\d+/)) > 0) {
				this.id = this.id.replace(/\d+/, courseRecordCount);
			}
			this.value = "";
		});
		courseRecordCount++;
		addDeleteFunction();
		return false;
	});
	// Experience
	$(".experience-record:first").each(function() {
		experienceRecord = $(this);
	});
	$(".experience-record:last").each(function() {
		experienceRecordCount = parseInt(this.id.match(/\d+$/)) + 1;
	});
	$("#addExperienceBtn").click(function() {
		// Clone object and events
		$(this).before(experienceRecord.clone(true));
		$(".experience-record:last, .experience-record:last > *").each(function() {
			if(parseInt(this.id.match(/\d+/)) > 0) {
				this.id = this.id.replace(/\d+/, experienceRecordCount);
			}
			this.value = "";
		});
		experienceRecordCount++;
		addDeleteFunction();
		return false;
	});
	// Functionality for form record deletion
	addDeleteFunction();
	
	// Initialize formbuilder date components
	$(".input_date").each(function() {
		dateArray = $(this).val().split("-");
		y = dateArray[0] == undefined ? '' : dateArray[0];
		m = dateArray[1] == undefined ? '' : dateArray[1];
		d = dateArray[2] == undefined ? '' : dateArray[2];
		
		input_date = createDateSubBox({
			'id': $(this).attr('id')+'-d',
			'class': 'datesmall',
			'value': d,
			'maxlength': 2
		})
		+createDateSubBox({
			'id': $(this).attr('id')+'-m',
			'class': 'datesmall',
			'value': m,
			'maxlength': 2
		})
		+createDateSubBox({
			'id': $(this).attr('id')+'-y',
			'class': 'datelarge',
			'value': y,
			'maxlength': 4
		});
		
		$(this).after(input_date);
		
		$("#"+$(this).attr('id')+"-d").bind("keydown", numericInput);
		$("#"+$(this).attr('id')+"-m").bind("keydown", numericInput);
		$("#"+$(this).attr('id')+"-y").bind("keydown", numericInput);
		
		$("#"+$(this).attr('id')+"-d").bind("keyup", input_date_update);
		$("#"+$(this).attr('id')+"-m").bind("keyup", input_date_update);
		$("#"+$(this).attr('id')+"-y").bind("keyup", input_date_update);
		
		$(this).css('display', 'none');
	});
	
	// Initialize Google maps functionality
	if(typeof(load) == "function"){
		setTimeout(load, 300);
	}
	
	// Initialise temp gastouder form
	/*$("a").each(function(){
		if($(this).attr("href") == '/nl/kow/inschrijven/gastouder----thuisopvang.htm') {
			$(this).bind("click", function(){
				newwindow=window.open("/gastouder-inschrijven/gastouder_inschrijven_1.php",'gastouder_inschrijven','height=600,width=550,menubar=false,location=false,toolbar=false');
				if (window.focus) {newwindow.focus()}
				return false;
			});
		}
	});*/
	
	/*$("#map").each(function(){
		$("#sidebar").css("position", "relative");
		$("#mapExpl").css("position", "absolute");
		$("#mapExpl").css("left", "0");
		if(jQuery.browser.msie) {
			var offset = 0;
			
			offset = this.offsetParent.offsetTop + this.offsetParent.offsetParent.offsetTop;
			
			$("#mapExpl").css("top", this.offsetTop - offset);
		}
		else {
			$("#mapExpl").css("top", this.offsetTop);
		}
	});*/
});

$(window).unload(function(){
	if(typeof(GUnload) == "function"){
		GUnload();
	}
});

function checkCheckbox(el, type) {
	if(el.checked == 1) {
		toggleMarkers(type, 'on');
	} else {
		toggleMarkers(type, 'off');
	}
}

function toggleMarkers(category, status) {
	for (var i=0; i<markers.length; i++) {
		if (markers[i].mycategory == category) {
			if(status == "on" || status == "off") {
				if(status == "on") {
					markers[i].show();
					markers[i].pleaseHideLater = false;
				}
				else {
					markers[i].hide();
					markers[i].pleaseHideLater = true;
				}
			}
			else {
				if (markersOn) {
					markers[i].hide();
					markers[i].pleaseHideLater = true;
				}
				else {
					markers[i].show();
					markers[i].pleaseHideLater = false;
				}
			}
		}
	}
	markersOn = !markersOn;
}

function createDateSubBox(params){
	input = '<input type="text" ';
	for(attr in params){
		input += attr+'="'+params[attr]+'" ';
	}
	input += '/>';
	
	return input;
}

function numericInput(e){
	var key = e.which;
	if ((key == 8 || key == 46 || key == 16 || key == 35 || key == 36 ||key == 9) || 	// Backspace, Del, Shift, -, End, Home, Tab
		(key >= 48 && key <= 57)  || 	// 0-9 at top of keyboard
		(key >= 96 && key <= 105) || 	// 0-9 at numpad
		(key >= 37 && key <= 40)){   	// arrows
		return true;
	 }
	 return false;
}

function input_date_update(e) {
	master = "#"+$(e.target).attr("id").substr(0,$(e.target).attr("id").length -2);
	dateArray = $(master).val().split("-");
	var key = e.which;
	
	if(key == 9 || key == 16) {
		/*clearSelection();*/
		return;
	}
	
	switch($(e.target).attr("id").substr($(e.target).attr("id").length -1, 1)){
		case "d":
			$(master).val(dateArray[0]+"-"+dateArray[1]+"-"+$(e.target).val());
			
			if($(e.target).val().length == 2){
				$(e.target).next().focus();
				$(e.target).next().select();
				e.stopPropagation();
			}
			break;
		case "m":
			$(master).val(dateArray[0]+"-"+$(e.target).val()+"-"+dateArray[2]);
			
			if($(e.target).val().length == 2){
				$(e.target).next().focus();
				$(e.target).next().select();
				e.stopPropagation();
			}
			break;
		default:
			$(master).val($(e.target).val()+"-"+dateArray[1]+"-"+dateArray[2]);
			break;
	}
}

function clearSelection () {
	if(document.selection)
		document.selection.empty();
	else if (window.getSelection)
		window.getSelection().removeAllRanges();
}

function addDeleteFunction(){
	$(".delete-record").click(function(){
		$(this).parent().remove();
		return false;
	});
}

function initColorSchemaSelector(page, colorSchema, iFrame) {
	$('#'+colorSchema+', #'+page).change(function(){
		jquery_maaklinkvoorkant($('#'+page).val(), colorSchema, iFrame, updateIFrame);
	});
}

function jquery_maaklinkvoorkant(page_id, colorSchema, iFrame, callback) {
	return $.get("/jquery_maaklinkvoorkant.php", { page_id: page_id, colorSchema: colorSchema, iFrame: iFrame}, callback, "json");
}

function updateIFrame(data) {
	$('#'+data.iFrame).attr("src",data.url+"?colorSchema="+$('#'+data.colorSchema).val());
}