
jQuery(document).ready(function() {
	var questionanswered = 0;
	var totalquestions;
	var currentquestion = 1;
	var numright = 0;
	var review = 1;
	var reviewtally = "<p>Here is how you did: </p><br>";
	var correct_answer_text;
	var takername = jQuery('.username').text();
	var takerid = jQuery('.userid').text();
	var getquizid = jQuery('.start-quiz').attr('title');
		jQuery(".start-quiz").click(function() {
			var right = '<img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/correct.png" />';
			var wrong = '<img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/wrong.png" />';
			var load = '<img class="load-img" src="http://www.naymik.com/learn/wp-content/plugins/thequiz/bigrotation2.gif" />';
			jQuery('.welcome').html(load);
				jQuery.ajax({
					type : 'POST',
					url : 'http://www.naymik.com/learn/wp-content/plugins/thequiz/loadquiz.php',
					dataType : 'json',
					data: {
						userid: takerid,
						quizid: getquizid
						
					},
					success : function(data){
						totalquestions = data.total;
					
						jQuery('.welcome').fadeOut('slow',function(){
							jQuery('.quizholder').html(data.quiz).fadeIn('slow',function(){
								if(data.done == 1){
								jQuery('.status').text('1');
								jQuery('.quizholder').prepend(data.previous);
								} else {
								jQuery('.quizholder').prepend('<p><em>You\'ve never taken this quiz.  Complete it and your results will be saved.</em></p>');
								}
								jQuery('.question_order-1').fadeIn(1000);
									jQuery('#thelink').animate({marginLeft: '40px'}, 1000).animate({marginLeft: '0px'}, 1000);
							});

						jQuery('.quizholder li').hover(function(){
						jQuery(this).css({'color' : 'green'});
						},function(){
						jQuery(this).css({'color' : 'black'});
						});
						
						
						jQuery('#ques-container').css({'border' : '0px solid green'});
						
						
						if (review == 1) {
						jQuery('.quiz-link').text('Check');
						} 
						
						
						});
					
						
						

					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
							alert('errer' + XMLHttpRequest + textStatus + errorThrown);
					}
				});
				
				return false;
		});
	
	
	
jQuery(".quiz-link").live('click', function() {
		//var  params = jQuery(this).attr("href").replace('?', '');
		//jQuery(this).html(params);

if (jQuery('.question_order-' + currentquestion + ' input[name=answer]').is(':checked')) {
	jQuery('.load_img').fadeIn('fast',function(){
		jQuery('.quizholder').fadeTo('fast', 0.1);
	});
if (questionanswered == 0) {
  	var getanswerid = jQuery('.question_order-' + currentquestion + ' input[name=answer]:checked').val();
	var getquesid = jQuery('.question_order-' + currentquestion + ' input[name=ques-id]').val();
	var getques = jQuery('.question_order-' + currentquestion + ' .ques-text').text();
	   
	jQuery('.question_order-' + currentquestion).fadeTo('slow', 0.3);
	
	   jQuery.ajax({
			type : 'POST',
			url : 'http://www.naymik.com/learn/wp-content/plugins/thequiz/rate.php',
			dataType : 'json',
			data: {
				action : 'do stuff',
				answer: getanswerid,
				quesid: getquesid
				
			},
			success : function(data){
				questionanswered = 1;
				
				//alert('ans id ' + getanswerid + 'ques id ' + getquesid + 'curr ques ' +currentquestion);
				correct_answer_text = jQuery('#ans-' + data.correct_id).text();
				user_answer_text = jQuery('#ans-' + getanswerid).text();
				if (data.question_grade == 1){
					if (review == 1) {
						jQuery('.question_order-' + currentquestion + ' #ques-container').css({'border' : '3px solid green'});
						jQuery('.question_order-' + currentquestion + ' #ques-container').prepend('<p class="correct-sum">Question ' + currentquestion + ': You said "' + user_answer_text + '." That is correct!.</p>');
						//jQuery('.load_img').html('Question ' + currentquestion + ': You said "' + user_answer_text + '." That is correct!.');
						jQuery('#ans-' + data.correct_id).append('<img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/correct.png" />');
					} else {
						jQuery('#question-' + getquesid + ' #ans-' + getanswerid).append('<img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/correct.png" />');
					}
					
					reviewtally = reviewtally + '<b>' + getques + '</b>' + ' <img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/correct.png" /><br><img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/correct.png" />' + correct_answer_text + '<br><p> </p>';
				numright++;
				} else {
					if (review == 1) {
						jQuery('.question_order-' + currentquestion + ' #ques-container').css({'border' : '3px solid red'});
						jQuery('.question_order-' + currentquestion + ' #ques-container').prepend('<p class="wrong-sum">Question ' + currentquestion + ': You said "' + user_answer_text + '." That is incorrect. The corrrect answer is "' + correct_answer_text + '."</p>');
						//jQuery('.load_img').html('Question ' + currentquestion + ': You said "' + user_answer_text + '." That is incorrect. The corrrect answer is "' + correct_answer_text + '."');
						jQuery('#question-' + getquesid + ' #ans-' + data.correct_id).append('<img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/correct.png" />');
						
					} 
				reviewtally = reviewtally + '<b>' + getques + '</b>' + ' <img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/wrong.png" /><br><img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/correct.png" />' + correct_answer_text + '<br><img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/wrong.png" /> ' + user_answer_text + '<br><p> </p>';
				jQuery('#question-' + getquesid + '  #ans-' + getanswerid).append('<img src="http://www.naymik.com/learn/wp-content/plugins/thequiz/wrong.png" />');
				}
				
				
				jQuery('.talley').show().text(numright + ' correct out of ' + totalquestions + ' questions.' );
					if (review == 1) {
					jQuery('.question_order-' + currentquestion).fadeTo('slow', 1);
					jQuery("input[name=answer]").attr("disabled", "disabled");
					jQuery('.question_order-' + currentquestion + ' #expl').slideDown(1000,function(){
							jQuery('.load_img').fadeOut('fast', function(){
								jQuery('.quizholder').fadeTo('fast', 1);
							});
					});
						
					jQuery('.quiz-link').text('Next');
					} else {
					advanceQuestion(getquizid);
					}
			
					
				
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert('errer' + XMLHttpRequest + textStatus + errorThrown);
			}
		});
		jQuery('#thelink').animate({opacity: 1},1000).animate({marginLeft: '40px'}, 500).animate({marginLeft: '0px'}, 500);
	} else {
	questionanswered = 0;
			advanceQuestion();
			
	}
} else {
alert('select an answer');

}
			return false;
	
	
	
	
	
  });
function advanceQuestion(getquizid) {
			jQuery('.question_order-' + currentquestion).fadeTo(1000, 1);
			jQuery('#ques-container').css({'border' : '0px solid green'});
			jQuery('.question_order-' + currentquestion).fadeOut(1000,function(){
			currentquestion++;
			
				jQuery('.question_order-' + currentquestion).fadeIn(1000);
					jQuery('.question_order-' + currentquestion + ' #ques-container').css({'border' : '0px solid green'});
				jQuery('#expl').slideUp('slow');
				jQuery('.load_img').fadeOut('fast', function(){
				jQuery('.quizholder').fadeTo('fast', 1);
				});
							if (review == 1) {
							//jQuery('.quiz-link').fadeIn('fast');
							jQuery('.quiz-link').text('Check');
							jQuery("input[name=answer]").removeAttr("disabled");
				}
				if(currentquestion > totalquestions){
					
					var percent = (numright / totalquestions) * 100;
					percent = Math.round(percent);
					var stat = jQuery('.status').text();
					if(stat == 1){
					jQuery('.talley').prepend('<b>NOT SAVED</b> ');
					alert('done not saved');
					} else {
					saveQuiz(percent,getquizid);
					}
					var congrats;
					if(percent == 100) {
					congrats = '. Excellent Job!';
					} 
					if(percent < 100 && percent >= 75) {
					congrats = '. Good Work!';
					} 
					if(percent < 75) {
					congrats = '. Good Try!';
					}
					jQuery('.talley').append(' That\'s ' + percent + '%' + congrats);
					
					if (review == 1) {
						jQuery('.review').html(reviewtally).slideDown(3000);
					}
					
					jQuery('#thelink').animate({opacity: 1},1000).animate({marginLeft: '40px'}, 500).animate({marginLeft: '-200px'}, 500);
				} else {
				jQuery('#thelink').animate({opacity: 1},1000).animate({marginLeft: '40px'}, 500).animate({marginLeft: '0px'}, 500);
				}
			});
			
} //end function advance
function saveQuiz(score,getquizid) {
getquizid = jQuery('.quiz-id').text();
			jQuery.ajax({
				type : 'POST',
				url : 'http://www.naymik.com/learn/wp-content/plugins/thequiz/savequiz.php',
				dataType : 'json',
				data: {
					
					thisscore: score,
					thisanswer: reviewtally,
					nameofuser: takername,
					idofuser: takerid,
					quizid: getquizid
				},
				success : function(data){
					if(data.done == 1){
					
					alert(data.quiz);
					
					} else {
					jQuery('.talley').prepend('<b>SAVED</b> ');
					alert(data.username +', your socroe of ' + score + '% was saved.');
					
					}
					
			
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
						alert('errer' + XMLHttpRequest + textStatus + errorThrown);
				}
			});


}
});