/*
-----------------------------------------------
HearthstoneStoves.com
Script: product-wizard.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 12 Feb 2008
----------------------------------------------- */

productWizard = {
	config: {
		currentClass : 'current',
		hideClass : 'hide'
	},
	current: 1,
	init:function() {
		
		// Add html and classes to styling overlay and set current quesiton
		$jq('#stove_selection_wizard').addClass(productWizard.config.hideClass);
		$jq('#stove_selection_wizard').addClass('wizard_overlay');
		$jq('#stove_selection_wizard').wrapInner('<div class="wrapper"></div>');
		$jq('#stove_selection_wizard').append('<div class="bottom"></div>');
		$jq('#stove_selection_wizard ol li:nth-child(' + productWizard.current + ')').addClass(productWizard.config.currentClass);
		
		// Add the pagination navigation
		pagination_config = {
			pages : $jq('#stove_selection_wizard ol li').size(),
			id : 'wizard_navigation',
			idPrefix : 'page_',
			prevTxt : '< Previous',
			nextTxt : 'Next >',
			showTxt : 'Show Results >',
			prevPageFunc : 'productWizard.getPrevQuestionId',
			nextPageFunc : 'productWizard.getNextQuestionId',
			changePageFunc : 'productWizard.getQuestion',
			current : productWizard.current
		}
		$jq(productWizard.buildPagination(pagination_config)).appendTo('#stove_selection_wizard .wrapper');
		$jq('#page_prev').addClass('disabled');
		
		// Show results events
		$jq('#show_results').addClass('hide');
		$jq('#stove_selection_wizard ol li:last-child input').click(function() {
			if ($jq(this)[0].checked) {
				productWizard.toggleShowResultsLink();
			}
		});
		
		// Add a close button
		$jq('<a href="#" id="close_wizard">X</a>').prependTo('#stove_selection_wizard .wrapper');
		$jq('#close_wizard').click(function(event) {
			event.preventDefault();
			$jq('#stove_selection_wizard').toggleClass(productWizard.config.hideClass);
		});
		
		// Attach show wizard function to the wizard teaser button
		$jq('#wizard_teaser a').click(function(event) {
			event.preventDefault();
			$jq('#stove_selection_wizard').toggleClass(productWizard.config.hideClass);
		});

	},
	buildPagination:function(conf) {
	
		// Previous, next and show results links
		var prevHref = 'javascript:' + conf.prevPageFunc + '()';
		var prevId = conf.idPrefix + 'prev';
		var prevA = '<a id="' + prevId + '" href="' + prevHref  + '">' + conf.prevTxt + '</a>';
		var nextHref = 'javascript:' + conf.nextPageFunc + '()';
		var nextId = conf.idPrefix + 'next';
		var nextA = '<a id="' + nextId + '" href="' + nextHref  + '">' + conf.nextTxt + '</a>';
		var showHref = 'javascript:productWizard.showResults()';
		var showId = 'show_results';
		var showA = '<a id="' + showId + '" href="' + showHref  + '">' + conf.showTxt + '</a>';
		
		var str = '<div id="' + conf.id + '">';
		str += prevA;
		str += ' | ';
		str += nextA;
		str += showA;
		return str;
		
	},
	getNextQuestionId:function() {
		
		// Determine next question id based on current question, set to 0 if the value is greater than the number of questions
		var nextQuestionId = ((productWizard.current + 1) > $jq('#stove_selection_wizard ol li').size()) ? 0 : productWizard.current + 1;
		
		// Skip the square footage question (#2) if the stove is not a primary heating source
		if ((nextQuestionId == 3) && (vdwUtil.getRadioValue($jq("input[name='q2']").get()) == 'Secondary')) {
			nextQuestionId = 4;
		}
		
		if (nextQuestionId != 0) { productWizard.getQuestion(nextQuestionId); }
		
	},
	getPrevQuestionId:function() {
		
		// Determine the previous question id based on current question, set to 0 if value is less than 1
		var prevQuestionId = ((productWizard.current - 1) == 0) ? 0 : productWizard.current - 1;
		
		// Skip the square footage question (#2) if the stove is not a primary heating source
		if ((prevQuestionId == 3) && (vdwUtil.getRadioValue($jq("input[name='q2']").get()) ==  'Secondary')) {
			prevQuestionId = 2;
		}
		
		if (prevQuestionId != 0) { productWizard.getQuestion(prevQuestionId); }
		
	},
	getQuestion:function(questionId) {
	
		// Make sure the question has been answered if we are trying to move forward
		if (questionId < productWizard.current || vdwUtil.getRadioValue($jq("input[name='q" + productWizard.current + "']").get())) {
		
			// Show the current question
			$jq('#stove_selection_wizard ol li:nth-child(' + productWizard.current + ')').removeClass(productWizard.config.currentClass);
			$jq('#stove_selection_wizard ol li:nth-child(' + questionId + ')').addClass(productWizard.config.currentClass);
			productWizard.current = questionId;
			
			// Make sure the show results link is visible if it needs to be (hide the next link if showing the show results link)
			productWizard.toggleShowResultsLink();
			
			// Disable prev/next as necessary
			if (productWizard.current == 1) {
				$jq('#page_prev').addClass('disabled');
			} else if (productWizard.current == $jq('#stove_selection_wizard ol li').size()) {
				$jq('#page_next').addClass('disabled');
			} else {
				$jq('#page_prev').removeClass('disabled');
				$jq('#page_next').removeClass('disabled');
			}
			
		} else {
		
			alert('Please complete this question before moving to the next question.');
			
		}
		
	},
	prevQuestionsAnswered:function(questionId) {
	
		// Loop through all previous radio buttons and return false if we find a group of them with no selection
		var i = 1;
		while (i < questionId) {
			var inputs = document.getElementById('stove_selection_wizard').getElementsByTagName('li')[i - 1].getElementsByTagName('input');
			var answered = productWizard.valRadio(inputs);
			if (!answered) { return false; }
			i++;
		}
		return true;
		
	},
	toggleShowResultsLink:function() {
		var inputs = document.getElementById('stove_selection_wizard').getElementsByTagName('li')[$jq('#stove_selection_wizard ol li').size() - 1].getElementsByTagName('input');
		var answered = productWizard.valRadio(inputs);
		var last_question = (productWizard.current == $jq('#stove_selection_wizard ol li').size()) ? true : false;
		if (answered && last_question) {
			$jq('#show_results').removeClass('hide');
			$jq('#page_next').addClass('hide');
			return true;
		} else {
			$jq('#show_results').addClass('hide');
			$jq('#page_next').removeClass('hide');
			return false;
		}	
	},
	valRadio:function(inputs) {
		var answered = false;
		for (var j = 0; j < inputs.length; j++) {
			if (inputs[j].checked) {
				answered = true;
			}
		}
		return (!answered) ? false : true;
	},
	showResults:function() {
		$jq('#stove_selection_wizard form').submit();
	}
}

$jq(document).ready(productWizard.init);