
// Position
var documentWidth = Math.max($(document).width(),$(window).width());
var documentHeight = $(window).height();

// Elements
var progressBar = null;
var nextButton = null;
var startButton = null;
var downloadButton = null;
var closeButton = null;
var stageQuestions = [];
var stageNumbers = [];

// Data
var numberStages = 14;
var currentStage = 0;

function showNextButton () {
	nextButton.show().fadeTo(300, 1.0);
}

function showAnswer () {
	var answer = $(stageQuestions[currentStage]).find('.answer');
	if (answer.length > 0) {
		answer.show().fadeTo(100, 0).fadeTo(300, 1.0);
	}
}

function hookupAnswers () {
	stageQuestions.each(hookupAnswersForQuestion);
}

function hookupAnswersForQuestion () {
	
	var answer = $(this).find('.answer').fadeTo(0,0);
	
	$(this).find('.options .option').each(function () {
		var option = $(this);
		option.click(function () {
			option.addClass('selected').siblings().removeClass('selected');
			showNextButton();
			showAnswer();
		});
	});
	
	$(this).find('.multiple-options .option').each(function () {
		var option = $(this);
		option.click(function () {
			option.toggleClass('selected');
			showNextButton();
			showAnswer();
		});
	});
	
	$(this).find('.text-area-options textarea').change(function () {
		showNextButton();
	});
}

function hookupContactForm () {
	$('#evaluationLightbox .contact-form form input').change(function () {
		showNextButton();
	});
	$('#evaluationLightbox .contact-form form').validate({
		errorElement: "div",
		messages: {
			required: 'Required',
			email: {
				required: 'Required'
			}
		}
	});
}

function onNextButtonClick () {
	
	var cancelNextStage = false;
	
	// Contact Form?
	if ($(stageQuestions[currentStage]).find('.contact-form').length > 0) {
		if ($(stageQuestions[currentStage]).find('.contact-form form').valid()) {
			var fields = $(stageQuestions[currentStage]).find('.contact-form form').find('input, select');
			var values = {};
			fields.each(function () {
				values['evaluation[' + $(this).attr('name') + ']'] = $(this).val();
			});
			$.post('/evaluation/create.json', values, function (result) {
				//console.log(result);
			});
		} else {
			cancelNextStage = true;
		}
		
		
	}
	
	if ($(stageQuestions[currentStage]).hasClass('save-answer')) {
		var question = $(stageQuestions[currentStage]).find('.question-title').html();
		
		var answers = [];
		
		// Options
		$(stageQuestions[currentStage]).find('.option.selected').each(function () {
			answers.push($(this).html());
		});
		
		// Text Area
		$(stageQuestions[currentStage]).find('.text-area-options textarea').each(function () {
			answers.push($(this).attr('name') + ': ' + $(this).val());
		});
		
		$.post('/evaluation/update_answer.json', {question:question, "answers[]":answers}, function (result) {
			//console.log(result);
		});
	}
	
	if ($(stageQuestions[currentStage]).hasClass('conditional-answer')) {
		var answer = $(stageQuestions[currentStage]).find('.option.selected').html();
		if (answer == "Yes") {
			$('#question-4, #question-5').removeClass('alternate');
			$('#question-4').html($('#question-4-original').html());
			$('#question-5').html($('#question-5-original').html());
		} else {
			$('#question-4, #question-5').addClass('alternate');
			$('#question-4').html($('#question-4-alternate').html());
			$('#question-5').html($('#question-5-alternate').html());
		}
		$('#question-4, #question-5').each(hookupAnswersForQuestion);
	}
	
	if (cancelNextStage == false) {
		goToStage(currentStage+1);
	}
	this.blur();
	return false;
}

function onDownloadButtonClick () {
	// Save it
	$.post('/evaluation/update_pdf_download.json', {}, function (result) {
		window.location = "/pdf/Interactive-Patient-Care-Handout.pdf";
	});
	this.blur();
	return false;
}

function hookupSliders () {
	
	$(".slider").slider({
		value:3,
		min: 1,
		max: 5,
		step: 1,
		slide: function(event, ui) {
			showNextButton();
			showAnswer();
			var stage = $(stageQuestions[currentStage]);
			if (stage.length > 0) {
				stage.find('.option.selected').html(ui.value);
			}
		}
	});
	
}

function loadEvaluation () {
	
	if ($('#evaluationLightbox').length > 0) {
		for (i = 0; i < stageQuestions.length; i++) {
			$(stageQuestions[i]).fadeTo(0, 0);
		}
		goToStage(0);
		showEvaluation();
	} else {
		$('#loadableContainer').load('/evaluation/index.html', {}, function () {
			
			// Initial Setup
			progressBar = $('#evaluationProgressBar div');
			nextButton = $('#evaluationNextButton');
			startButton = $('#evaluationStartButton');
			downloadButton = $('#evaluationDownloadButton');
			closeButton = $('#evaluationCloseButton');
			stageQuestions = $('#evaluationContent li');
			stageNumbers = $('#evaluationStages li');
			
			startButton.click(function () {
				goToStage(1);
				this.blur();
				return false;
			});
			
			nextButton.click(onNextButtonClick);
			downloadButton.click(onDownloadButtonClick);
			
			closeButton.click(function () {
				hideEvaluation();
				this.blur();
				return false;
			});
			
			for (i = 0; i < stageQuestions.length; i++) {
				$(stageQuestions[i]).hide().fadeTo(0, 0);
			}
			hookupAnswers();
			hookupContactForm();
			hookupSliders();
			
			$('.animated-button').hover(function () {
				$(this).find('span').fadeTo(0, 0).show().stop().fadeTo(200, 1.0);
			}, function () {
				$(this).find('span').stop().fadeTo(200, 0);
			});
			
			// Show
			goToStage(0);
			showEvaluation();
		});
	}
	
}

function showEvaluation () {
	
	documentWidth = Math.max($(document).width(),$(window).width());
	documentHeight = $(window).height();
	
	$('#evaluationLightboxInner').css({
		left:Math.round(documentWidth/2 - 363) + 'px',
		top:Math.round(documentHeight/2 - 350) + 'px'
	});
	$('#evaluationLightbox').fadeIn('fast', function () {
		$('#evaluationTab').animate({ 
			top:"4px"
		}, 500);
	});
}

function hideEvaluation () {
	$('#evaluationLightbox').fadeOut('fast', function () {
		$('#evaluationTab').css({top:'50px'});
		$('#evaluationLightbox .option.selected').removeClass('selected');
		$('#evaluationLightbox .answer').fadeTo(0,0);
	});
}

function moveProgressBar (percent) {
	progressBar.animate({ 
		width: percent + "%"
	}, 300);
}


function goToStage (stage) {
	
	currentStage = stage;
	
	for (i = 0; i < stageNumbers.length; i++) {
		if (i+1 == currentStage) {
			$(stageNumbers[i]).removeClass('next').removeClass('stage-' + (i+1) + '-next').addClass('current').addClass('stage-' + (i+1) + '-current');
		} else if (i+1 < currentStage) {
			$(stageNumbers[i]).removeClass('next').removeClass('stage-' + (i+1) + '-next').removeClass('current').removeClass('stage-' + (i+1) + '-current');
		} else {
			$(stageNumbers[i]).addClass('next').addClass('stage-' + (i+1) + '-next').removeClass('current').removeClass('stage-' + (i+1) + '-current');
		}
	}
	
	var itemID = -1;
	
	for (i = 0; i < stageQuestions.length; i++) {
		itemID = parseInt($(stageQuestions[i]).attr('id').replace('question-','').replace('-a','').replace('-b',''), 10);
		if (itemID == currentStage) {
			$(stageQuestions[i]).show().fadeTo('fast', 1);
		} else if ($(stageQuestions[i]).is(':visible')) {
			$(stageQuestions[i]).fadeTo('fast', 0, function () {
				$(this).hide();
			});
		}
	}
	
	var progressPercent = 0;
	switch (currentStage) {
		case 2:
			progressPercent = 11.5;
			break;
		case 3:
			progressPercent = 19;
			break;
		case 4:
			progressPercent = 27;
			break;
		case 5:
			progressPercent = 34;
			break;
		case 6:
			progressPercent = 42;
			break;
		case 7:
			progressPercent = 49;
			break;
		case 8:
			progressPercent = 57;
			break;
		case 9:
			progressPercent = 64.5;
			break;
		case 10:
			progressPercent = 71.5;
			break;
		case 11:
			progressPercent = 79;
			break;
		case 12:
			progressPercent = 86.5;
			break;
		case 13:
			progressPercent = 94.5;
			break;
		case 14:
			progressPercent = 100;
			break;
		case 1:
			progressPercent = 5;
			break;
		case 0:
		default:
			progressPercent = 0;
			break;
	}
	moveProgressBar(progressPercent);
	
	if (currentStage === 0) {
		startButton.show();
		nextButton.hide();
		downloadButton.hide();
	} else if (currentStage === 14) {
		startButton.hide();
		nextButton.hide();
		downloadButton.fadeTo(0, 0).show().fadeTo(300, 1.0);
	} else {
		startButton.hide();
		nextButton.fadeTo(300, 0);
		downloadButton.hide();
	}
	
	
}

$(function () {
	$('#promoEvaluationButton, #promoEvaluationTopTab, #promoEvaluationBottomTab').click(function () {
		loadEvaluation();
		this.blur();
		return false;
	});
});

