/* functions to fade quiz in & out */

// Use these variables to configure the fader
var quizFadeDelay = 50;
var quizFadeInSpeed = 5;
var quizFadeOutSpeed = 10;
var quizPauseDelay = 2000;
var quizMaxLoops = 0;

// Add your messages here
var quizMsg = new Array();
quizMsg[0] = 'How many commercial diets have you tried?';
quizMsg[1] = 'How many times have you tried will power?';
quizMsg[2] = 'Has portion control worked for you?';
quizMsg[3] = 'What about calorie counting and exercise?';
quizMsg[4] = 'Has skipping meals helped?';
quizMsg[5] = 'How many times have you said, "If only I could lose this weight?"';
quizMsg[6] = 'How many weight related health consequences do you have?';
quizMsg[7] = 'Gaining, losing, and regaining...  The cycle continues with predictable certainty.';
quizMsg[8] = 'Are you sick and tired of waiting and weighting?';



// Don't edit anything below here
var quizTimeoutID;
var quizCurrentColor = 255;
var quizFadingIn = true;
var quizFadingOut = false;
var quizLoopCounter = 1;
var quizMsgCounter = 0;
var quizFader;

function initQuizFader() {

	// If this is a non-DOM browser, bail out
	if (!document.getElementById) { 
		return;
	} else {
	quizFader = document.getElementById("fadeQuiz");
		// Initialize the fader
		initFader();
	}
}

// This function sets the initial scroller position and clip region
function initFader() {
//alert('initializing - initFader');

	// Hide the fader
	quizFader.style.visibility = "hidden";

	// Set the message
	quizFader.innerHTML = quizMsg[quizMsgCounter];

	// Start fading
	fade_it();
}

// This function fade the text in and out
function fade_it() {

	if (quizFadingIn || quizFadingOut) {

		// Convert the red, green, and blue values to hexadecimal
		var red_hex = decimal_to_hex(quizCurrentColor);
		var green_hex = red_hex;
		var blue_hex = red_hex;

		// Set the color style
		quizFader.style.color = "#" + red_hex + green_hex + blue_hex;

		// Does the fader have children? 

		// Show the fader
		quizFader.style.visibility = "visible";

		// Adjust the color value for the fade-in portion
		if (quizFadingIn) {
//alert('fading in');
			quizCurrentColor -= quizFadeInSpeed;

			// Has the text faded in completely?
			if (quizCurrentColor < 0) {

				// If so, reset the color
				quizCurrentColor = 0;

				// Set up the flags for the fade out
				quizFadingIn = false;
				quizFadingOut = true;

				// Leave the text onscreeen for quizPauseDelay ms
				quizTimeoutID = setTimeout("fade_it()", quizPauseDelay);
				return;
			}
		}

		// Adjust the color value for the fade-out portion
		if (quizFadingOut) {
//alert('fading out');
			quizCurrentColor += quizFadeOutSpeed;

			// Has the text faded out completely?
			if (quizCurrentColor > 255) {

				// If so, set up the flags for the fade in
				quizFadingIn = false;
				quizFadingOut = false;
			}
		}

		// Do it again
		quizTimeoutID = setTimeout("fade_it()", quizFadeDelay) ;
	} else {

		// Otherwise, the text has faded in and faded out,
		// so reset the color and the flag
		quizCurrentColor = 255;
		quizFadingIn = true;


		// Increment and check quizMsgCounter
		if (++quizMsgCounter == quizMsg.length) {

			// If we've gone past the last message, start over
			quizMsgCounter = 0;
			quizLoopCounter++;
		}

		// Check the loop count
		if (quizLoopCounter <= quizMaxLoops || quizMaxLoops == 0) {

			// If it's okay, start the fader back at the beginning
			initFader();
		}
	}
}
