var questions = {};
var curr_q = 1;
var answers = new Array();

function load_question (question_number) {
	if (curr_q == 1) {
		$('#prev_q').hide();
	} else {
		$('#prev_q').show();
	}
	
	$.each(questions, function(i, items) {
		if (items.q_num == question_number) {
			var html_code = '<p><strong>Question ' + items.q_num + ' of 5</strong><br />' + items.q.question + '</p>';
			if (items.q.question_type == 1) {
				$.each(items.ops, function(j, op) {
					html_code += '<label><input type="radio" class="reco_ans" name="q' + question_number + '" value="' + op.reco_answer_option_id + '" class="radio" />' + op.answer + '</label>';
				});
			} else if (items.q.question_type == 2) {
				$.each(items.ops, function(j, op) {
					html_code += '<label><input type="checkbox" class="reco_ans" name="q' + question_number + '" value="' + op.reco_answer_option_id + '" class="radio" />' + op.answer + '</label>';
				});
			}
			$('#reco_question').html(html_code);
		}
	});
	
	if (isdefined(answers[curr_q])) {
	
		var q_answer = answers[curr_q].split(',');
		
		$('.reco_ans').each(function(i, val) {
			var len = q_answer.length;
			for (var i = 0; i < len; i++) {
				if ($(this).val() == q_answer[i]) {
					$(this).attr('checked','checked');
				}
			}
		});
		
	}
}

function next_question () {
	var cur_answer = '';
	
	$('.reco_ans:checked').each(function(i, val) {
		cur_answer += $(this).val() + ',';
	});
	
	if (cur_answer.length > 0) {
		answers[curr_q] = cur_answer;		
		
		if (curr_q < 5) {
			curr_q++;			
			load_question(curr_q);
		}
		
		if (curr_q == 5) {
			var pos = $('#trigger').position();
			$('#reco_info').overlay({
				expose: { 
			        color: '#efefef', 
			        loadSpeed: 10, 
			        opacity: 0.9 
			    }, 
			    absolute: true,
			    top: pos.top-300,
			    closeOnClick: false,
			    closeOnEsc: false
			}).load();
		}
	}
}

function prev_question () {
	curr_q--;
	load_question(curr_q);
}

function isdefined (variable) {
    return (typeof(variable) !== "undefined") ? true : false;
}

function submit_reco_form () {
	var pass = true;
	
	$('.req_rg').each(function(i, item) {
		if ($(this).val().length == 0) {
			$(this).css('border','1px solid #ff0000');
			pass = false;
		}
	});
	
	var reco_answers = answers.join('|');
	
	if (pass) {
		$.post('/reco_genius/submit_reco_form', $('#reco_genius_info_form').serialize() + '&answers=' + reco_answers,
			function(data) {
				$('#reco_info').css('background-image','url(/images/reco_genius_result_bg.jpg)');

				var html_code = '';

				if ((isdefined(data.type)) && (data.msg.length > 0)) {

					html_code = '<div class="thanks">' + data.msg + '</div>';
					$('#rg_form_code').html(html_code);

				} else {

					html_code = '<div class="thanks">Thank you for using our online recoGENIUS.  Based on your answers, the product(s) below might be of interest to you.  Feel free to read on and learn more - one of our Total Recognition Strategists will be contacting you shortly to further discuss your company\'s recognition needs.';
					html_code += '<br /><br />';
					html_code += '<div class="clear"><!-- --></div>';

					$.each(data, function(i, item) {
						html_code += '<a href="' + item.url + '" target="_blank"><img src="' + item.image_path + '" class="program_icon"></a>';
					});

					html_code += '</div>';

					$('#rg_form_code').html(html_code);

				}
			},
		'json');
	}
}

$(document).ready(function() {
	$.get('/reco_genius/load_ajax_questions',function(data) {
		questions = data;
		load_question(curr_q);
	},'json');
});
