setup_swap();

var currentStep = 0;
var choice1, choice2, choice3, choice4;
var finalAnswers, choiceName;

window.addEvent('domready', function()
{
	var c1 = $('nonjs_ie6_choice1');
	var c2 = $('nonjs_ie6_choice2');

	if (c1 && c2)
	{
		c1.addEvent('mouseenter', function(){ imageSwapIE6_on(c1); });
		c2.addEvent('mouseenter', function(){ imageSwapIE6_on(c2); });

		c1.addEvent('mouseleave', function(){ imageSwapIE6_off(c1); });
		c2.addEvent('mouseleave', function(){ imageSwapIE6_off(c2); });
	}

	if (!$('take_it')) return;

	$('take_it').addEvent('click', function(event)
	{
		if (window.ie6) return;
		if (!window.ie) event.preventDefault();
		else new Event().preventDefault();

		currentStep = 1;

		loadStep();

		var qsOption1 = $('quiz_step_option_1').getFirst();
		var qsOption2 = $('quiz_step_option_2').getFirst();

		qsOption1.addEvent('mouseenter', function(event){ swapImage_enter(1); });
		qsOption1.addEvent('mouseleave', function(event){ swapImage_leave(1); });

		qsOption2.addEvent('mouseenter', function(event){ swapImage_enter(2); });
		qsOption2.addEvent('mouseleave', function(event){ swapImage_leave(2); });

		qsOption1.getParent().addEvent('click', function(event)
		{
			if (!window.ie) event.preventDefault();
			else new Event().preventDefault();

			if (currentStep == 1) { choice1 = 'a'; }
			else if (currentStep == 2) { choice2 = 'a'; }
			else if (currentStep == 3) { choice3 = 'a'; }
			else if (currentStep == 4) { choice4 = 'a'; }

			currentStep += 1;
			loadStep();
		});

		qsOption2.getParent().addEvent('click', function(event)
		{
			if (!window.ie) event.preventDefault();
			else new Event().preventDefault();

			if (currentStep == 1) { choice1 = 'b'; }
			else if (currentStep == 2) { choice2 = 'b'; }
			else if (currentStep == 3) { choice3 = 'b'; }
			else if (currentStep == 4) { choice4 = 'b'; }

			currentStep += 1;
			loadStep();
		});
	});
});

function imageSwapIE6_on(target)
{
	var filter = target.getStyle('filter');
	filter = filter.replace('.png', '_on.png');
	target.setStyle('filter', filter);

}

function imageSwapIE6_off(target)
{
	var filter = target.getStyle('filter');
	filter = filter.replace('_on.png', '.png');
	target.setStyle('filter', filter);

}

function loadStep()
{
	var qsOption1 = $('quiz_step_option_1').getFirst();
	var qsOption2 = $('quiz_step_option_2').getFirst();

	if (currentStep == 1)
	{
		$('intro').setStyle('visibility', 'hidden');
		$('quiz_steps').setStyle('visibility', 'visible');

		qsOption1.src = "images/quiz_options/quiz_step_flower_shop.png";
		qsOption2.src = "images/quiz_options/quiz_step_fruit_stand.png";

		// Will need IE6 Handler
		$('quiz_step_number').setStyle('background-image', 'url(images/quiz_step_1_4.png)');
		$('quiz_container').setStyle('background-image', 'url(images/quiz_container_background_steps.png)');
	}
	else if (currentStep == 2)
	{
		qsOption1.src = "images/quiz_options/quiz_step_pastry.png";
		qsOption2.src = "images/quiz_options/quiz_step_bread.png";

		// Will need IE6 Handler
		$('quiz_step_number').setStyle('background-image', 'url(images/quiz_step_2_4.png)');
	}
	else if (currentStep == 3)
	{
		qsOption1.src = "images/quiz_options/quiz_step_picnic.png";
		qsOption2.src = "images/quiz_options/quiz_step_spa.png";

		// Will need IE6 Handler
		$('quiz_step_number').setStyle('background-image', 'url(images/quiz_step_3_4.png)');
	}
	else if (currentStep == 4)
	{
		qsOption1.src = "images/quiz_options/quiz_step_log_home.png";
		qsOption2.src = "images/quiz_options/quiz_step_pool.png";

		// Will need IE6 Handler
		$('quiz_step_number').setStyle('background-image', 'url(images/quiz_step_4_4.png)');
	}
	else if (currentStep == 5)
	{
		processResults();
	}
}

function swapImage_enter(quizImageId)
{
	if (window.ie6)
	{
		// To Do
	}
	else
	{
		var imgElement = $('quiz_step_option_' + quizImageId).getFirst();
		var imgSource = imgElement.src;
		imgSource = imgSource.replace('.png', '_on.png');
		imgElement.src = imgSource;
	}
}

function swapImage_leave(quizImageId)
{
	if (window.ie6)
	{
		// To Do
	}
	else
	{
		var imgElement = $('quiz_step_option_' + quizImageId).getFirst();
		var imgSource = imgElement.src;
		imgSource = imgSource.replace('_on', '');
		imgElement.src = imgSource;
	}
}

function processResults()
{
	$('quiz_steps').setStyle('visibility', 'hidden');
	$('quiz_results').setStyle('visibility', 'visible');
	$('quiz_background').setStyle('visibility', 'hidden');

	$('find_your_fragrance_factor').setStyle('background-image', 'url(images/your_fragrance_factor.png)');

	var fruity = ['baaa','baab','baba','babb'];
	var fresh =  ['bbaa','abba','bbba'];
	var floral = ['aaaa','abaa','aaab','abab','bbab'];
	var urbane = ['aaba','aabb','abbb','bbbb'];

	var resultString = choice1 + choice2 + choice3 + choice4;
	finalAnswers = resultString;

	window.location.href = "/us/find_your_fragrance_factor/default.jsp?step=5&answer=" + finalAnswers;

	$('quiz_container').setStyle('margin-top', '40px');
	$('quiz_container').setStyle('height', '324px');

	if (fruity.contains(resultString))
	{
		choiceName = 'Fruity';
		$('quiz_container').setStyle('background-image', 'url(images/quiz_container_background_fruity.png)');
		$('quiz_results').addClass('results_fruity');
		$$('div.case_fruity').setStyle('visibility', 'visible');
	}
	else if (fresh.contains(resultString))
	{
		choiceName = 'Fresh';
		$('quiz_container').setStyle('background-image', 'url(images/quiz_container_background_fresh.png)');
		$('quiz_results').addClass('results_fresh');
		$$('div.case_fresh').setStyle('visibility', 'visible');
	}
	else if (floral.contains(resultString))
	{
		choiceName = 'Floral';
		$('quiz_container').setStyle('background-image', 'url(images/quiz_container_background_floral.png)');
		$('quiz_results').addClass('results_floral');
		$$('div.case_floral').setStyle('visibility', 'visible');
	}
	else if (urbane.contains(resultString))
	{
		choiceName = 'Urbane';
		$('quiz_container').setStyle('background-image', 'url(images/quiz_container_background_urbane.png)');
		$('quiz_results').addClass('results_urbane');
		$$('div.case_urbane').setStyle('visibility', 'visible');
	}
}