$(document).ready(function(){

	// reset form on page (re)load
	$(':input','#search_jobs')
	 .not(':button, :submit, :reset, :hidden')
	 .removeAttr('checked')
	 .removeAttr('selected');
		
	$('#search_jobs input:radio').attr('disabled', true);
	$('#search_jobs tr').addClass('disabled');
	$('#search_jobs tr:first input:radio').attr('disabled', false);
	$('#search_jobs tr:first').removeClass('disabled');

	$('#search_jobs input:radio').change(function() {

		// hide result box if already shown
		$('div.search_result_box').hide();

		// find next row
		var nextRow = $(this).parents('tr').next();
				
		$(nextRow).find(':radio').attr('disabled', false);
		$(nextRow).removeClass('disabled');
		
		// keep track of answered questions
		var question_answered_count = $('#search_jobs input:radio:checked').size();
		
		// alert(question_answered_count);
				
		// if count == 4, show result
		if (question_answered_count == 4) {
			
			// get result
			var question_result = $('#search_jobs').getResult();
								
			if (question_result > 0) {
				
				// update number of result shown in result box
				$('span.text_result').text( question_result );
				
				// show result box
				$('div#search_jobs_result_1').fadeIn();
				
				var search_key = '_'+$('#search_jobs input[name=expertise]:checked').val()
							   + '_'+$('#search_jobs input[name=contract]:checked').val()
							   + '_'+$('#search_jobs input[name=bezetting]:checked').val()
							   + '_'+$('#search_jobs input[name=niveau]:checked').val()
							   ;
				$('#search_jobs input[name=search_key]').val(search_key);
				
			} else {
				$('div#search_jobs_result_2').fadeIn();
				
				$('#search_jobs input[name=search_key]').val('');
			}
		}
		
	});
	
	$('#search_jobs').jNice();
	
	$('.search_result_box .cta_button').click(function(e){
		e.preventDefault();		
		$('#search_jobs').submit();
		return false;
	});
	
	$.fn.getSelection = function() {
		var selection = '';		
		$('#search_jobs input:radio').each(function(){
			if ($(this).attr('checked')) {
				selection = selection + "_" + $(this).val();
			}
		});
		return selection;
	}
	
	function rand ( n )
	{
	  return ( Math.floor ( Math.random ( ) * n + 1 ) );
	}	

	$.fn.getResult = function() {
		
		var selection = $('#search_jobs').getSelection();
		return num_results[selection];
		
	}
		
});
