var myAnswers=new Array();
var ets_id;
var sid = '35668BA5E'; 
var currentQuestion = 0;
var questions_groups = [0,0,0,1,2,2,3,4,5,5,5,5,6,7];
var inmotion = false;
var glob_type=false;

function reset_survey(){

	//ets_id = null;
	//reset_ets_id();
	get_id();

	glob_type=false;

	myAnswers = [];
	$('#user_survey input:checked').attr('checked', false);
	
	currentQuestion = 0
	$('#user_survey div.question_group').hide().eq(0).show();
	$("#survey_forward").hide();
	$("#survey_back").hide();	

}

function get_id(){
    var url = "http://survey.elegantthought.com/uuid.php?";
    $.getJSON(url + "&jsoncallback=?", function(resp){
        ets_id = resp.id;
    });
}

function get_ets_id(){
    var url = "http://survey.elegantthought.com/etsid.php?";
    $.getJSON(url + "&jsoncallback=?", function(resp){
        ets_id = resp.etsurveyor_id;
    });
}

function reset_ets_id(){
    var url = "http://survey.elegantthought.com/etsid.php?reset=1";
    $.getJSON(url + "&jsoncallback=?", function(resp){
        ets_id = resp.etsurveyor_id;
    });
}

function get_myAnswers(){

    var params = "sid="+sid+"&";

	var url = "http://survey.elegantthought.com/user_answers.php?"+params;
	$.getJSON(url + "&jsoncallback=?", function(resp){
		myAnswers = resp.answers;
		ets_id = resp.etsurveyor_id;
        
		currentQuestion = questions_groups[myAnswers.length];
		
		for(var i=0; i<myAnswers.length; i++){
			$('#'+myAnswers[i]).attr('checked','checked');
		}
		
		if(myAnswers.length > 4)
			get_globaliser_type();
		
		
	});
	
}

function get_globaliser_type(){
if(glob_type) return;

var params = "etsurveyor_id="+ets_id+"&",
	url = "http://survey.elegantthought.com/globalisers/user_result.php?"+params;
    $.getJSON(url + "&jsoncallback=?", function(resp){
		glob_type = resp.globaliser_type;
		$('.result').hide();
		$('#'+glob_type).show();

	});
}


function submitAnswer(qid, aid){
    
    /*
    var qid = $('.answers.selected input:checked').attr('name');
    var aid = $('.answers.selected input:checked').attr('value');
    */
    
    myAnswers.push(aid);
    
    var params ="";
    params += "etsurveyor_id="+ets_id+"&";
    params += "sid="+sid+"&";
    params += "qid="+qid+"&";
    params += "aid[]="+aid+"&";

    var url = "http://survey.elegantthought.com/submit.php?"+params;
    
    $.getJSON(url + "&jsoncallback=?", function(resp){
        ets_id = resp.etsurveyor_id;
    });
    
    
}

function checkAnswers(){

	$("#survey_forward").hide();
	var $qgroup = $("div.question_group:visible");

	if($qgroup.hasClass("stack") ){
		var checked = true;

		$qgroup.find("table tr.sub_question_group").each(function(){
			if($(this).find("input:checked").length == 0) checked = false;
		});
		checked && $("#survey_forward").show();
	} else if($qgroup.hasClass("bar") ){
		$qgroup.find("input:checked").length && $("#survey_forward").show();
	}
	
	if(myAnswers.length > 4)
		get_globaliser_type();
		
	if(currentQuestion == 7)
		$("#survey_back").hide();	
	
}


function goNext(){
	if(inmotion) return;
	inmotion = true;
	
	var $thisq = $("div.question_group:visible");
	
	if($thisq.hasClass("stack")){
		$thisq.find("tr.sub_question_group").each(function(){
			var qid = $(this).children("td.sub_question").attr("id");
			var aid = $(this).find("input:checked").attr("id");
			submitAnswer(qid, aid);
		});
	} else if($thisq.hasClass("bar")){
		$thisq.find("input:checked").each(function(){
			var aid = this.id;
			var qid = $(this).attr("name");
			submitAnswer(qid, aid);
		});
	}
	
	$thisq.fadeOut(500,function(){
		$(this).next("div.question_group").fadeIn();
		checkAnswers(); 
		inmotion = false;
	});
	
	currentQuestion ++;
	$("#survey_back").show();
	
}

function goBack(){
	if(inmotion) return;
	inmotion = true;
	$("div.question_group:visible").fadeOut(500,function(){
		$(this).prev("div.question_group").fadeIn();
		inmotion = false;
	});
	currentQuestion --;
	currentQuestion || $("#survey_back").hide();
	$("#survey_forward").show();
}

$(function(){

	$('#user_survey').jqm();
	$('#user_survey_opener').click(function(){
		//get_ets_id();
		get_id();
		$("#user_survey div.question_group").eq(currentQuestion).show();
		currentQuestion || $("#survey_back").hide();
		$('#user_survey').jqmShow();
		checkAnswers();
	});
	$('#resetsurvey').click(function(){reset_survey();});
	//get_myAnswers();
	$("div.question_group").click(function(){checkAnswers();});
	$("#survey_forward").click(function(){goNext();});
	$("#survey_back").click(function(){goBack();});
	
	$('.type_changer').click(function(){
		var type = $(this).attr('index');
		$('.result').hide();
		$('#'+type).show();

	});

	$('.type_changer').hover(
		function(){
		$(this).addClass('typehover');

		},
		function(){
		$(this).removeClass('typehover');

		}
	);
	
	
});

