$( document ).ready(function() {

	var s,
	QuizApp = {
	
		settings: {
			answeredQuestions: [],
			resultIndex: [],
			answers : [],
			flag : true,
			response: $(".response_wrapper"),
			button:  $('.question').find('.response'),
			failAnswers: 0,
			totalQuestions: $('.question').length
		},
		
			init: function() {
			// kick things off
			console.log("Init 48");
			s = this.settings;
			this.bindUI();
		},
		
		bindUI: function() {
			s.button.change(function(){QuizApp.onChange($(this));});
			s.response.click(function(){
				$('.response', this).prop('checked', true).trigger("change");
				QuizApp.selectAnswers($('.response', this));
			});
		},
		
		selectAnswers: function(response) {
			
			var question = response.closest('.question');
			var selected = response.closest('.response_wrapper')
			var resultIndex = [];
			
			response.prop('checked', true).trigger("change");
			
			if (question.children().find('.selected').length > 0) {
				question.children().find('.selected').removeClass('selected');
				$(selected).addClass("selected");
			
			}
			else {$(selected).addClass("selected");}
		}, 
		
		onChange: function (input) {
		
			var answer = input.attr('name');
			
			if (s.answers.indexOf(answer) > -1 ) {
				/* Do nothing */
			}
			else {
				s.totalQuestions--;
				s.answers.push(answer);
			}
		
		
			if (s.totalQuestions == 0) {QuizApp.pullResults();}
		},
		
		pullResults: function() {

			$( ".question input:checked" ).each(function() {s.answeredQuestions.push($(this).val());});	
		
			$( ".result" ).each(function() {
				if ($(this).data('result-key') != undefined) {
					var retrieved_string = $(this).data('result-key').split(",");
					s.resultIndex.push(retrieved_string);	
				}
			});	
			
			QuizApp.calculateResults(); 
		
		},
		
		calculateResults: function() {
			var results = [];

			(function($) {
					$.rand = function(arg) {
						if ($.isArray(arg)) {
							return arg[$.rand(arg.length)];
						} else if (typeof arg === "number") {
							return Math.floor(Math.random() * arg);
						} else {
						return;  // chosen by fair dice roll
					}
				};
			})(jQuery);

			$( s.resultIndex ).each(function(i) {
				
				s.failAnswers = 0;
				
				for (var j = 0; j < s.answeredQuestions.length; j++) {
			
					if ( s.resultIndex[i][j] == s.answeredQuestions[j] ) {
						/* Don't add to fail count */
					}
					else if ( s.resultIndex[i][j] =="any" ) {
						/* Don't add to fail count */
					}
					else {s.failAnswers++;}
					
				}
				if (s.failAnswers == 0) {
					var correctAnswer =  String(s.resultIndex[i]);
					console.log("Fail Answer" + s.failAnswers);
					
					if (s.flag == true) {
						$('html, body').animate({scrollTop: $("#results").offset().top}, 400);
						s.flag = false;	
					}
					
					results.push(correctAnswer);
				}
			});
			
			jQuery.rand(results);
			if ($('.result:not(.result-visible)')) {
				$('.result').addClass("result-hidden").removeClass("result-visible");
				$('div[data-result-key="'+results[0]+'"]').removeClass("result-hidden").addClass("result-visible");
			}
			if (s.failAnswers > 0) {
				console.log("Fail Answer is =" + s.failAnswers);
				$("#result-default").removeClass("result-hidden");
				$("#result-default").addClass("result-visible");
			}
			s.answeredQuestions = [];
		}
	};
	
	QuizApp.init();
});