// Pacific Vision jQuery
// by Horn Group, Inc.
// http://www.horngroup.com

var quizAge = '';
var quizUse = '';
var quizCondition = '';
var quizString = '';

function quizPrep(){
	$('#bucket-quiz').removeClass().addClass('open');
	var quizOpenWidth = $('#bucket-quiz').width();
	$('#bucket-quiz').removeClass().addClass('closed');
	var quizClosedWidth = $('#bucket-quiz').width();

	$('#quiz-start').click(function(){
		if( $('#bucket-quiz').attr('class') == 'closed' ){
			
			$('#bucket-quiz').find('input[type="radio"]:checked').each(function(i){
				$(this).removeAttr('checked');
			});
			
			$(this).parent().siblings('div').append('<span class="quiz-loading"></span>').each(function(i){
				if(i == 0){
					$(this).find('.quiz-loading').siblings().hide().eq(1).animate({'opacity':'1'},250,function(){
						$(this).siblings('.quiz-loading').fadeOut(500);
						$(this).parent().children(':not(.quiz-loading)').fadeIn(500);
						$('#bucket-quiz').find('.thisQ').removeClass('thisQ');
						$(this).parent().removeClass('nextQ').addClass('thisQ').next().addClass('nextQ');
						quizBindNext();
					});					
				} else {
					$(this).find('.quiz-loading').siblings().hide();
					$(this).hide();
				}
			});
			
			$('#bucket-quiz').removeClass().addClass('open').css({'width':quizClosedWidth}).animate({'width':quizOpenWidth},500,function(){
				$('#close-quiz').show();
			});
		}
		return false;
	});
	
	$('#close-quiz').click(function(){
		$(this).hide();
		$('#bucket-quiz').find('#results').removeClass('visible-to-all');
		$('#bucket-quiz').find('.thisQ').removeClass('thisQ');
		$('#bucket-quiz').find('.nextQ').removeClass('nextQ');
		$('#bucket-quiz').find('.quiz-loading').remove();
		$('#bucket-quiz').find('.current-answer').removeClass('.current-answer');
		$('#bucket-quiz').find('div').each(function(){
			$(this).attr({'style':''});
		});
		$('#bucket-quiz').animate({'width':quizClosedWidth},500, function(){
			$(this).removeClass().addClass('closed');
		});
		return false;
	});
}


function quizBindNext(){
	var quizResults = $('#bucket-quiz').find('#results');
	
	$('#bucket-quiz').find('.thisQ').children('fieldset').children('p').children('input').unbind().click(function(){

		if( $(this).parent().parent().parent().hasClass('.thisQ') || quizResults.hasClass('visible-to-all')){
	
			var currentQuestion = $(this).attr('name');
			var currentAnswer = $(this).attr('value');
			
			if(currentQuestion == 'age'){
				quizAge = currentAnswer;
			} else if(currentQuestion == 'use') {
				quizUse = currentAnswer;
			} else if(currentQuestion == 'condition') {
				quizCondition = currentAnswer;
			}
			quizString = quizAge + "-" + quizUse + "-" + quizCondition;
			
			$('#bucket-quiz').find('.nextQ').fadeIn(500).animate({'opacity':'1'},250,function(){
																							  
				if( quizResults.hasClass('visible-to-all') ){
					/*
					quizResults.children('ul.current-answer').removeClass('current-answer').hide(500, function(){
						$(this).parent().children('ul#' + quizString).addClass('current-answer').show(500);
					});
					*/
					quizResults.children('ul').hide(250).filter('ul#' + quizString).show(250);
				} else {
				
					$(this).prev().removeClass('thisQ');
					$(this).removeClass('nextQ').addClass('thisQ').next('div').addClass('nextQ');
					
					if( $('#bucket-quiz').find('.nextQ').length <= 0 ){
						
						$('#bucket-quiz').find('div.question').eq(0).addClass('nextQ');
						$(this).find('.quiz-loading').remove();
						$(this).removeClass().addClass('visible-to-all');
						$(this).children(':not(ul)').fadeIn(500);
						$(this).children('ul#' + quizString).addClass('current-answer').fadeIn(500);
						
					} else {
						
						$(this).find('.quiz-loading').remove();					
						$(this).children(':not(.quiz-loading)').fadeIn(500);
						
					}
								
					quizBindNext();
				}
			});
		}
	});
}


$(document).ready(function(){
	
	quizPrep();

});