$(document).ready(function(){
	var currentQuote = 1;
	
	// fade in quote and client name divs on page load
	$('#quote-holder').fadeIn(600);
	
	window.setInterval(cycleQuotes, 5000);
	
	$('#quiz').click(function(){
		window.location = '/what-we-offer.php';
	});
	
	$('.gradeQuiz').click(function(){
		gradeQuiz();
	});
	
	function cycleQuotes() {
		// cycle through all available quotes, fading in one at a time

		currentQuote++;
		
		// go back to beginning if this quote element doesn't exist
		if (!$('.quote' + currentQuote).length) {
			currentQuote = 1;
		}
		
		$('#quote-holder').fadeOut(600, function() {
			$('.quote' + currentQuote).show();
			$('.quote-single').not('.quote' + currentQuote).hide();
			$('#quote-holder').fadeIn(600);
		});
	}
	
	function gradeQuiz() {
		// Grade the "How Organized Are you?" quiz and display score
	    var trueAnswers = 0;
		var totalQuestions = 16;
		var currentAnswer = null;
		var gradeString = '';
		var answerString = '';
		
		for (i = 1; i <= totalQuestions; i++) {
			var theQuestion = eval("document.organizedQuiz.question" + i);
			
			for (j = 0; j < theQuestion.length; j++) {
				if(theQuestion[j].checked == true) {
					currentAnswer = theQuestion[j].value;
					
					if(currentAnswer == 'True') {
						trueAnswers++;
					}
				}
				
			}
		}
		
		gradeString += 'You had ' + trueAnswers + ' true answer';
		if(trueAnswers != 1) {
			gradeString += 's'
		}
		gradeString += '.' + "\n\n";
		
		if(trueAnswers <= 4) {
			answerString += "Consider hiring LET'S MAKE ROOM to help you.";
		}
		else if(trueAnswers > 4 && trueAnswers < 9) {
			answerString += 'You are having some organizing challenges and could benefit from some help.';
		}
		else if(trueAnswers > 8 && trueAnswers < 13) {
			answerString += 'You are reasonably organized. You may just need to tweak your systems.';
		}
		else {
			answerString += 'You are very organized. Keep up the good work!';
		}
		
		
		
		$('.overlay').overlay({
			load: true,
			closeOnClick: false,
			top: '30%'
		});
		
		var oAPI = $('.overlay').data('overlay');
		oAPI.load();
		
		$("#viewResults").validate({
			submitHandler: function(form) {
				$(form).ajaxSubmit({
					beforeSubmit: function(){
						$('.quizSubmit').val('Processing...');
					},
					success: function(){
						$('.overlay').children().not('.close').remove();
						$('.overlay').append('<h2>Your Results</h2>');
						$('.overlay').append('<div class="grade">' + gradeString + '</div>');
						$('.overlay').append('<div class="result">' + answerString + '</div>');
						$('.overlay .close').click(function(){
							oAPI.close();
						});
						$('.quizSubmit').val('View My Results');
					}
				});
			}
		});
						
		$('.overlay .grade').text(gradeString);
		$('.overlay .result').text(answerString);
	
	}
	
	$('body.success-stories .bothPhotos img[rel]').overlay({
		top: '32%',
		mask: {
			color: '#000',
			opacity: 0.8
		}
	});
	
});
