var curQuestionNum = 1;
var tResult = new Array();

$(document).ready(function(){
	qst_num = ' <td><div>Вопрос:&nbsp;&nbsp;&nbsp;</div></td>';	
	for(var i=1; i<tQuestions.length; i++) {	
		//qst_num +='<td><div id="block_'+i+'" class="num-black">'+i+'</div></td>';	
		qst_num += '<td><div id="block_'+i+'"  class="q-pas"><strong>'+i+'</strong></div></td>';
	}
	$(tConfig['question_num']).html(qst_num);
});


function testShowQuestion() {

	$(".q-act").attr('class' , 'q-pas');
	$('#block_'+curQuestionNum+'').attr('class' , 'q-act');
	
	$("#dnt_know").attr('checked',false);	
	var answersHtml = '';
	var descriptionHtml = '';
	$(tConfig['test_image']).html('');
	//$(tConfig['question_num']).text('Вопрос ' + curQuestionNum + ' из ' + (tQuestions.length - 1) );
	//$(tConfig['title_element']).text('Вопрос ' + curQuestionNum + ' из ' + (tQuestions.length - 1) + '. ' + tQuestions[curQuestionNum][0]);
	$(tConfig['title_element']).text(tQuestions[curQuestionNum][0]);
	if(tDescription[curQuestionNum]) descriptionHtml = '<strong></strong> ' + tDescription[curQuestionNum];
	$(tConfig['description_element']).html(descriptionHtml);
	
	if(tQuestions[curQuestionNum][2]=='img') {
		$(tConfig['test_image']).html('<div class="line-x"></div><img src="tpl/iqwork.ru/images/q'+curQuestionNum+'.png" alt="" />');
	}

	if(tQuestions[curQuestionNum][1] != 4)  {
		if($.isArray(tAnswers[curQuestionNum][1]))	answers_count = tAnswers[curQuestionNum][0].length ; 
		else answers_count = tAnswers[curQuestionNum].length ;
		for(var i=0; i<answers_count; i++) {
			if(tQuestions[curQuestionNum][1] != 3)  {
				if($.isArray(tAnswers[curQuestionNum][1])) {
					answersHtml += '<td align="center"><label for="answer_'+(i+1)+'">';
					answersHtml +='<img src="tpl/iqwork.ru/images/q'+curQuestionNum+'_'+(i+1)+'.png"/><br />';							
				}
				else {
					answersHtml += '<tr><td align="left">';										
				}
					
				if(tQuestions[curQuestionNum][1] == 1) {						
					answersHtml += '<input type="radio" name="test_answer" value="'+(i+1)+'" id="answer_'+(i+1)+'" class="ti_answer">';
				}
				else  
					answersHtml += '<input type="checkbox" name="test_answer_'+i+'" value="'+(i+1)+'" id="answer_'+(i+1)+'" class="ti_answer">';
				if($.isArray(tAnswers[curQuestionNum][1]))	{					
					answersHtml += tAnswers[curQuestionNum][0][i];
					answersHtml += '</label></td>';	
				}
				else {
					answersHtml += tAnswers[curQuestionNum][i];	 			
					answersHtml += '</td></tr>';			
				}
			} else {
				answersHtml += '<option class="ti_answer" value="'+(i+1)+'">' + tAnswers[curQuestionNum][i] + '</option>';
			}
		}
		if(tQuestions[curQuestionNum][1] == 3) answersHtml = '<select name="test_answer" style="width:250px;">' + answersHtml + '</select>';
	} else {
		if($.isArray(tQuestions[curQuestionNum][2])) {
			quest_inf = '<div class="line-x"></div><ul>';
			for(var j=0; j<tQuestions[curQuestionNum][2].length; j++) {
				quest_inf += '<li><strong>'+tQuestions[curQuestionNum][2][j]+'</strong></li>';
			}
			quest_inf += '</ul>';
			$(tConfig['test_image']).html(quest_inf);
		}
		answersHtml += '<input name="test_answer" class="ti_answer">';
	}
	$(tConfig['container_element']).html(answersHtml);
}
	
function ansverClick(target) {
	$(".answer-checked").each(function(indx){
		$(this).attr('class','answer-unchecked');
	});
	$(".answer-checked").attr('class','answer-unchecked');
	$(target).attr('class','answer-checked');
}

function testNextQuestion() {
	var value;
	var tValue = new Array();
	

	$(".ti_answer:input:checked").each(function(indx){
		tValue[tValue.length] = $(this).attr('value');	
	});			
	
	if(tValue.length > 0) value = tValue;
	else {
		$(".ti_answer:input:text").each(function(indx){
			value = $(this).val();			
		});		
		if(!value) {
			$(".ti_answer:selected").each(function(indx){
				value = $(this).val();
			})
		}
	}

	if(tDescription[curQuestionNum] != '' && !value) {
		value =-1;
	}
	
	if(value) {
		tResult[curQuestionNum] = value;
		if(curQuestionNum < (tQuestions.length - 1)) {
			curQuestionNum ++;
			testShowQuestion();
		} else {
			testShowProgress();
			//testResult();
		}		
		return true;
	} else {
		alert('Вы должны заполнить это поле');
		return false;
	}

}
function testShowProgress(){
	$("#test_cont").html("");
	$(tConfig['progress_element']).attr('style','');
		
	$("#progress_title_1").attr('style','');
	$("#progress_outer_1").attr('style','');
	$("#test_progress_1").animate({width: '40%'} ,1500 ).animate( {width: '80%'} ,2000).animate( {width: '100%'} ,2000, function() {
		$("#progress_title_2").attr('style','');
		$("#progress_outer_2").attr('style','');
		$("#test_progress_2").animate({width: '60%'} ,2500 ).animate( {width: '90%'} ,1500).animate( {width: '100%'} ,2000, function() {
			$("#progress_title_3").attr('style','');
			$("#progress_outer_3").attr('style','');
			$("#test_progress_3").animate({width: '30%'} ,2000 ).animate( {width: '50%'} ,2000).animate( {width: '100%'} ,2500, function() {
				testResult();
			});
		});
  	});
}

function testResult() {
	if(tConfig['ajax_result_handler']) {
		var postFileds = '', curValue;		
		for(var key in tResult) {
			curValue = '';
			var value = tResult[key];
			if(typeof value == 'object') {
				for(var iKey in value) {
					var iValue = value[iKey];
					curValue += iValue + '|';
				}
			} else curValue = value;
			postFileds += 'qn' + key + '=' + curValue + '&';
		}
		$.post(
			tConfig['ajax_result_handler'],
			postFileds,
			function(data){location.replace(tConfig['result_url']);}
		);
	} else {
		location.replace(tConfig['result_url']);
	}
	
	return true;	
}


$(function(){
	testShowQuestion();
});
