$(document).ready(function(){
		buttonHoverState();
		populate();
		calculate_people_events();
		count_people();
		check_nmbs_field();
		hide_info_boxes();
});

function hide_info_boxes(){
	if ($("div[id*='holder_']").length > 0) {
		$("div[id*='holder_']").hide(0);
	}
}

function showInfo(id){
 //var intHeight = $(document).height() + 'px';
 //$('#overlay').css('height',intHeight);
 //$('#overlay').css('display','block');
 $('#'+id).show();
}

function hideInfo(id){
 //$('#overlay').css('display','none');
 $('#'+id).hide(0);
}

function buttonHoverState(){
 $('.btn_bevestigen').mouseover(function(){$(this).addClass('btn_hover')});
 $('.btn_bevestigen').mouseout(function(){$(this).removeClass('btn_hover')});
 $('.btn_fs').mouseover(function(){$(this).addClass('btn_hover')});
 $('.btn_fs').mouseout(function(){$(this).removeClass('btn_hover')});
}


function addFields(type, num, id){	
 if ($("#" + id).length > 0) {		
		$('#'+id).load("assets/scripts/controller_add_person.php", {'type': type, 'num': num}, function(){
			populate();
		}); 
	}
}

/**
 * Generate population of province dropdowns
 */
var pop_drop;

function populate(){
	if ($("select[name*='provincie']").length > 0) {
		$("select[name*='provincie']").change(function(){						
			populate_postcodes($(this).attr("name"), $(this).val());
		});
	}
	if ($("select[name*='postcode']").length > 0) {
		$("select[name*='postcode']").change(function(){
			populate_cities($(this).attr("name"), $(this).val());			
		});
	}
}

function populate_postcodes(pop_drop, id, selected){
	pop_drop = pop_drop;
		
	$.getJSON("assets/scripts/controller_populate.php", {
		pid: id,
		ajax: 'true'
	}, function(j){
		
		var options = '';
		
		for (var i = 0; i < j.length; i++) {
			
			if (selected == j[i].optionValue) {
				options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';
			} else {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
		}
		
		var num_id = get_num(pop_drop);
						
		if ($("select[name='"+ num_id +"_postcode']").length > 0) {			
			$("select[name='"+ num_id +"_postcode']").html(options);
		}		
		
		if ($("select[name='"+ num_id +"_plaats']").length > 0) {
			options = '';
			$("select[name='"+ num_id + "_plaats']").html(options);
		}	
	})
}

function populate_cities(pop_drop, id, selected){	
	pop_drop = pop_drop;
			
	$.getJSON("assets/scripts/controller_populate.php", {
		cid: id ,
		ajax: 'true'
	}, function(j){
		var options = '';
		
		for (var i = 0; i < j.length; i++) {
			if (selected == j[i].optionValue) {
				options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';
			} else {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
		}				
		var num_id = get_num(pop_drop);				
		if ($("select[name='"+ num_id +"_plaats']").length > 0) {
			$("select[name='"+ num_id + "_plaats']").html(options);
		}				
	});
}

function get_num(pop_drop){
	var i = pop_drop.match(/[\d\.]+/g);		
	if(i != null){
		return i[0];
	} else {
		return '';
	}	
}

/**
 * Calculate people
 */ 
var total_people = 0; 
function calculate_people_events(){
	if ($("input[name*='groep_num_leeftijd_cat']").length > 0) {
		$("input[name*='groep_num_leeftijd_cat']").keyup(function(){
			count_people();			
		});
	}
}

function count_people(){
	total_people = 0;			
	$("input[name*='groep_num_leeftijd_cat']").each(function(i){
		if(is_numeric(parseInt($(this).val())))	total_people += parseInt($(this).val());			
	});	
	if ($("#total_people").length > 0) {
		$("#total_people").text(total_people);
	}
}

/**
 * Check nmbs field 
 */
function check_nmbs_field(){	
	if ($("#social_check_nmbs").length > 0) {		
		if ($("li[id*='social_extra_field']").length > 0) {
			$("li[id*='social_extra_field']").hide(0);
		}		
		$("#social_check_nmbs").keyup(function(){
			if(is_numeric(parseInt($(this).val())) &&  parseInt($(this).val()) > 0){
				$("li[id*='social_extra_field']").show(0);
			} else {
				$("li[id*='social_extra_field']").hide(0);
			}	
		});
	}
}

function open_nmbs_field(){
	$("li[id*='social_extra_field']").show(0);
}

function is_numeric( mixed_var ) {  
    return !isNaN(mixed_var * 1);
}

function showWandelingInfo(blockId){
 //if(!$('.fs_wandeling_info').hasClass('hidden')){$('.fs_wandeling_info').addClass('hidden');}
 $('.fs_wandeling_info').each(function(){if(!$(this).hasClass('hidden')){$('.fs_wandeling_info').addClass('hidden');}});
	$('#'+blockId).removeClass('hidden');
}

