/**
 * @author djohnston
 */

var yesEL = null;
var noEL = null;
var qNum = 0;

function setupQuiz(mol) {
	var done = false;
	qNum = 1;
	if (yesEL !== null)  { Event.stopObserving('yes', 'click', yesEL); }
	if (noEL !== null)  { Event.stopObserving('no', 'click', noEL); }
	yesEL = checkAnswer.bindAsEventListener(mol);
	noEL = checkAnswer.bindAsEventListener(mol);
	Event.observe('yes', 'click', yesEL);
	Event.observe('no', 'click', noEL);
	$('flowchart').undoPositioned();
	$('feedback').innerHTML = "";
	new Effect.MoveBy('flowchart', 115, 20, {duration:0});
	new Effect.MoveBy('flowchart', -250, 0, {queue: 'end', beforeStart: hideButtons, afterFinish: showButtons});	
}

function showButtons()
{
	$('yesButton').show();
	$('noButton').show();
}

function hideButtons()
{
	$('yesButton').hide();
	$('noButton').hide();
}

function checkAnswer()
{
	var done = false;
	var response = arguments[0].target.id;
	var correctAnswer = t[qNum](this);
	var qaFlag = 0;
	
	if (response == correctAnswer)
		qaFlag += 1;
	if (correctAnswer == 'yes')	{				// the correct answer is YES
		qaFlag += 2;
		if (isString(q[qNum].yes(this)))		// the (yes) result is a point group
			qaFlag += 4;  }
	else {
		if (isString(q[qNum].no(this)))			// the (no) result is a point group
			qaFlag += 4;	}
	
	switch (qaFlag) {
		case 0:				// incorrect response, answer = no, next = question
		case 4:				// incorrect response, answer = no, next = point group	
			$('feedback').innerHTML = 'That is not correct.';
			new Effect.Shake('flowchart', {distance:3}); 
			break;
		case 1:				// correct response, answer = no, next = question
			var nextQuestion = q[qNum].no(this);
			var dx = q[qNum].x - q[nextQuestion].x;
			var dy = q[qNum].y - q[nextQuestion].y;
			qNum = nextQuestion;
			$('feedback').innerHTML = 'That is correct.';
			new Effect.MoveBy('flowchart', dy, dx, {beforeStart: hideButtons, afterFinish: showButtons});			
			break;
		case 2:				// incorrect response, answer = yes, next = question
		case 6:				// incorrect response, answer = yes, next = point group
			$('feedback').innerHTML = "Incorrect";
			h[qNum](this);
			new Effect.Shake('flowchart', {distance:3});
			break;
		case 3:				// correct response, answer = yes, next = question
			var nextQuestion = q[qNum].yes(this);
			var dx = q[qNum].x - q[nextQuestion].x;
			var dy = q[qNum].y - q[nextQuestion].y;
			$('feedback').innerHTML = "Correct";
			h[qNum](this);
			new Effect.MoveBy('flowchart', dy, dx, {beforeStart: hideButtons, afterFinish: showButtons});			
			qNum = nextQuestion;
			break;		
		case 5:				// correct response, answer = no, next = point group
			$('feedback').innerHTML = "Correct - the point group is " + q[qNum].no(this) + ".";
			new Effect.MoveBy('flowchart', -180, 0, {beforeStart: hideButtons});
			done = true;
			break;
		case 7:				// correct response, answer = yes, next = point group
			$('feedback').innerHTML = "Correct";
			h[qNum](this);
			$('feedback').innerHTML += "  The point group is " + q[qNum].yes(this) + ".";
			new Effect.MoveBy('flowchart', 0, -271, {beforeStart: hideButtons});
			done = true;
			break;
		default:
			done = true;
			break;
	}

	if (done) {
		Event.stopObserving('yes', 'click', yesEL);		// turn off buttons
		Event.stopObserving('no', 'click', noEL);	
	}
}

var q = [];		// array of questions
var t = [];		// array of test functions (return true for yes, false for no)
var h = [];		// array of highlight functions

q[0] = {
	"text"	: function(mol) { return "Molecular Structure"; }
};

q[1] = {
	"text"  : function(mol) { return "Is the molecule linear?"; },
	"yes"   : function(mol) { return 2; },
	"no"    : function(mol) { return 3; },
	"x"		: 111,
	"y"		: 259
}
t[1] = function(mol) { return 'no'; }		// we don't have any linear molecules!

q[2] = {
	"text"  : function(mol) { return "Does the molecule contain an inversion center?"; },
	"yes"   : function(mol) { return "<i>D</i><sub>&infin;h</sub>"; },
	"no"	: function(mol) { return "<i>C</i><sub>&infin;v</sub>"; },
	"x"		: 435,
	"y"		: 259
}
t[2] = function(mol) { 
	if (mol.ea.inversion0 != null) 
		return 'yes'; 
	else 
		return 'no';
}

q[3] = {
	"text"  : function(mol) { return "Does the molecule contain two or more unique <i>C</i><sub>3</sub> axes?"; },
	"yes"   : function(mol) { return 4; },
	"no"	: function(mol) { return 10; },
	"x"		: 111,
	"y"		: 583
}
t[3] = function(mol) {
	try {
		var n3 = 0;
		for (var i = 0; i < mol.properArray.length; i++)
			if (mol.ea["proper" + i].elementData.order == 3)
				n3 += 1;
		if (n3 > 2)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[3] = function(mol) {
	var axisCount = 0;
	for (var i = 0; i < mol.properArray.length; i++) {
		if (mol.ea["proper" + i].elementData.order == 3) {
			axisCount += 1;
			jmolScript(mol.ea["proper" + i].showElementString);
		}
	}
	$('feedback').innerHTML += " - there are " + axisCount + " unique <i>C</i><sub>3</sub> axes.";
}

q[4] = {
	"text"  : function(mol) { return "Does the molecule contain two or more unique <i>C</i><sub>5</sub> axes?"; },
	"yes"   : function(mol) { return 5; },
	"no"    : function(mol) { return 6; },
	"x"		: 435,
	"y"		: 583
}
t[4] = function(mol) {
	try {
		var n5 = 0;
		for (var i = 0; i < mol.properArray.length; i++)
			if (mol.ea["proper" + i].elementData.order == 5)
				n5 += 1;
		if (n5 > 2)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[4] = function(mol) {
	var axisCount = 0;
	for (var i = 0; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 3)
			jmolScript(mol.ea["proper" + i].hideElementString);
	for (var i = 0; i < mol.properArray.length; i++) {
		if (mol.ea["proper" + i].elementData.order == 5) {
			axisCount += 1;
			jmolScript(mol.ea["proper" + i].showElementString);
		}
	}
	$('feedback').innerHTML += " - there are " + axisCount + " unique <i>C</i><sub>5</sub> axes.";
}

q[5] = {
	"text"  : function(mol) { return "Does the molecule contain an inversion center?"; },
	"yes"   : function(mol) { return "<i>I</i><sub>h</sub>"; },
	"no"    : function(mol) { return "<i>I</i>"; },
	"x"		: 759,
	"y"		: 583
}
t[5] = function(mol) {
	try {
		if (mol.ea.inversion0 != null)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[5] = function(mol) {
	for (var i = 0; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 5)
			jmolScript(mol.ea["proper" + i].hideElementString);
	jmolScript(mol.ea.inversion0.showElementString);
	$('feedback').innerHTML += " - the molecule contains an inversion center.";
}

q[6] = {
	"text"  : function(mol) { return "Does the molecule contain two or more unique <i>C</i><sub>4</sub> axes?"; },
	"yes"   : function(mol) { return 7; },
	"no"    : function(mol) { return 8; },
	"x"		: 435,
	"y"		: 907
}
t[6] = function(mol) {
	try {
		var n4 = 0;
		for (var i = 0; i < mol.properArray.length; i++)
			if (mol.ea["proper" + i].elementData.order == 4)
				n4 += 1;
		if (n4 > 2)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[6] = function(mol) {
	for (var i = 0; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 3)
			jmolScript(mol.ea["proper" + i].hideElementString);
	var axisCount = 0;
	for (var i = 0; i < mol.properArray.length; i++) {
		if (mol.ea["proper" + i].elementData.order == 4) {
			axisCount += 1;
			jmolScript(mol.ea["proper" + i].showElementString);
		}
	}
	$('feedback').innerHTML += " - there are " + axisCount + " unique <i>C</i><sub>4</sub> axes.";
}

q[7] = {
	"text"  : function(mol) { return  "Does the molecule contain an inversion center?"; },
	"yes"   : function(mol) { return "<i>O</i><sub>h</sub>"; },
	"no"    : function(mol) { return "<i>O</i>"; },
	"x"		: 759,
	"y"		: 907
}
t[7] = function(mol) {
	if (mol.ea.inversion0 != null)
		return 'yes'; 
	else 
		return 'no';
}
h[7] = function(mol) {
	for (var i = 0; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 4)
			jmolScript(mol.ea["proper" + i].hideElementString);
	jmolScript(mol.ea.inversion0.showElementString);
	$('feedback').innerHTML += " - the molecule contains an inversion center.";
}

q[8] = {
	"text"  : function(mol) { return  "Does the molecule contain one or more reflection planes?"; },
	"yes"   : function(mol) { return  9; },
	"no"    : function(mol) { return "<i>T</i>"; },
	"x"		: 435,
	"y"		: 1231
}
t[8] = function(mol) {
	try {
		if (mol.reflectionArray.length > 1)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[8] = function(mol) {
	for (var i = 0; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 3)
			jmolScript(mol.ea["proper" + i].hideElementString);
	var planeCount = 0;
	for (var i = 0; i < mol.reflectionArray.length; i++) {
		planeCount +=1;
		jmolScript(mol.ea["reflection" + i].showElementString);
	}
	$('feedback').innerHTML += " - there are " + planeCount + " reflection planes.";
}

q[9] = {
	"text"  : function(mol) { return  "Does the molecule contain an inversion center?"; },
	"yes"   : function(mol) { return "<i>T</i><sub>h</sub>"; },
	"no"    : function(mol) { return "<i>T</i><sub>d</sub>"; },
	"x"		: 759,
	"y"		: 1231
}
t[9] = function(mol) {
	if (mol.ea.inversion0 != null)
		return 'yes'; 
	else 
		return 'no';
}
h[9] = function(mol) {
	for (var i = 0; i < mol.reflectionArray.length; i++)
		jmolScript(mol.ea["reflection" + i].hideElementString);
	jmolScript(mol.ea.inversion0.showElementString);
	$('feedback').innerHTML += " - the molecule contains an inversion center.";
}

q[10] = {
	"text"  : function(mol) { return  "Does the molecule contain a proper rotation axes (<i>C</i><sub>n</sub>)?"; },
	"yes"   : function(mol) { return  11; },
	"no"    : function(mol) { return  13; },
	"x"		: 111,
	"y"		: 1555
}
t[10] = function(mol) {
	try {
		if (mol.ea["proper0"] != null)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[10] = function(mol) {
	jmolScript(mol.ea.proper0.showElementString);
	var order = mol.ea["proper0"].elementData.order;	
	$('feedback').innerHTML += " - the molecule contains a <i>C</i><sub>" + order + "</sub> axis.";
}

q[11] = {
	"text"  : function(mol) { return  "Identify the highest order <i>C</i><sub>n</sub>.  Are there <i>n</i> perpendicular <i>C</i><sub>2</sub> axes?"; },
	"yes"   : function(mol) { return  12; },
	"no"    : function(mol) { return  14; },
	"x"		: 759,
	"y"		: 1555
}
t[11] = function(mol) {
	try {
		var order = mol.ea["proper0"].elementData.order;	
		if (mol.ea["proper" + order].elementData.order == 2)
			return 'yes'; 
		else 
			return 'no';
		}
	catch (ex) { return 'no'; }
}
h[11] = function(mol) {
	jmolScript(mol.ea.proper0.hideElementString);
	var axisCount = 0;  var start = 1;
	var order = mol.ea["proper0"].elementData.order;
	if (order == 4) start = 2;		// skip extra C2 axis
	if (order == 6) start = 3;
	if (order == 8) start = 3;
	for (var i = start; i < mol.properArray.length; i++) {
		if (mol.ea["proper" + i].elementData.order == 2) {
			axisCount += 1;
			jmolScript(mol.ea["proper" + i].showElementString); }
	}
	$('feedback').innerHTML += " - the molecule contains " + axisCount + " <i>C</i><sub>2</sub> axes perpendicular to a <i>C</i><sub>" + order + "</sub> axis.";
}

q[12] = {
	"text"  : function(mol) { return  "Does the molecule contain a horizontal reflection plane (&sigma;<sub>h</sub>)?"; },
	"yes"   : function(mol) {
				var n = mol.ea["proper0"].elementData.order;
				return "<i>D</i><sub>"+ n + "h</sub>"; },
	"no"    : function(mol) { return  15; },
	"x"		: 1407,
	"y"		: 1555
}
t[12] = function(mol) {
	var present = false;
	for (var i = 0; i < mol.reflectionArray.length; i++) {
		if ((mol.reflectionArray[i].label == 'h') || (mol.reflectionArray[i].label == 'xy'))
			present = true;
	}
	if (present)
		return 'yes'; 
	else 
		return 'no';
}
h[12] = function(mol) {
	for (var i = 1; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 2)
			jmolScript(mol.ea["proper" + i].hideElementString);
	for (var i = 0; i < mol.reflectionArray.length; i++)
		if ((mol.reflectionArray[i].label == 'h') || (mol.reflectionArray[i].label == 'xy'))
			jmolScript(mol.ea["reflection" + i].showElementString);
	$('feedback').innerHTML += " - the molecule contains a horizontal reflection plane (&sigma;<sub>h</sub>).";
}

q[13] = {
	"text"  : function(mol) { return  "Does the molecule contain a reflection plane?"; },
	"yes"   : function(mol) { return "<i>C</i><sub>s</sub>"; },
	"no"    : function(mol) { return  16; },
	"x"		: 111,
	"y"		: 1879
}
t[13] = function(mol) {
	try {
		if (mol.ea.reflection0 != null)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[13] = function(mol) {
	jmolScript(mol.ea.reflection0.showElementString);
	$('feedback').innerHTML += " - the molecule contains a reflection plane (&sigma;).";
}

q[14] = {
	"text"  : function(mol) { return  "Does the molecule contain a horizontal reflection plane (&sigma;<sub>h</sub>)?"; },
	"yes"   : function(mol) {
				var n = mol.ea["proper0"].elementData.order;
				return "<i>C</i><sub>"+ n + "h</sub>"; },
	"no"    : function(mol) { return  17; },
	"x"		: 759,
	"y"		: 1879
}
t[14] = function(mol) {
	var present = false;
	for (var i = 0; i < mol.reflectionArray.length; i++) {
		if ((mol.reflectionArray[i].label == 'h') || (mol.reflectionArray[i].label == 'xy'))
			present = true;
	}
	if (present)
		return 'yes'; 
	else 
		return 'no';
}
h[14] = function(mol) {
	jmolScript(mol.ea.proper0.hideElementString);
	for (var i = 0; i < mol.reflectionArray.length; i++)
		if ((mol.reflectionArray[i].label == 'h') || (mol.reflectionArray[i].label == 'xy'))
			jmolScript(mol.ea["reflection" + i].showElementString);
	$('feedback').innerHTML += " - the molecule contains a horizontal reflection plane (&sigma;<sub>h</sub>).";
}

q[15] = {
	"text"  : function(mol) { return  "Does the molecule contain <i>n</i> dihedral reflection planes (&sigma;<sub>d</sub>)?"; },
	"yes"   : function(mol) {
				var n = mol.ea["proper0"].elementData.order;
				return "<i>D</i><sub>"+ n + "d</sub>"; },
	"no"    : function(mol) {
				var n = mol.ea["proper0"].elementData.order;
				return "<i>D</i><sub>"+ n + "</sub>"; },
	"x"		: 1407,
	"y"		: 1879
}
t[15] = function(mol) {
	var present = false;
	for (var i = 0; i < mol.reflectionArray.length; i++)
		if (mol.reflectionArray[i].label == 'd')
			present = true;
	if (present)
		return 'yes'; 
	else 
		return 'no';
}
h[15] = function(mol) {
	for (var i = 1; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 2)
			jmolScript(mol.ea["proper" + i].hideElementString);
	var planeCount = 0;
	for (var i = 0; i < mol.reflectionArray.length; i++)
		if (mol.reflectionArray[i].label == 'd') {
			planeCount += 1;
			jmolScript(mol.ea["reflection" + i].showElementString); }
	$('feedback').innerHTML += " - the molecule contains " + planeCount + " dihedral reflection planes (&sigma;<sub>d</sub>).";
}

q[16] = {
	"text"  : function(mol) { return  "Does the molecule contain an inversion center?"; },
	"yes"   : function(mol) { return "<i>C</i><sub>i</sub>"; },
	"no"    : function(mol) { return "<i>C</i><sub>1</sub>"; },
	"x"		: 111,
	"y"		: 2203
}
t[16] = function(mol) {
	if (mol.ea.inversion0 != null)
		return 'yes'; 
	else 
		return 'no';
}
h[16] = function(mol) {
	jmolScript(mol.ea.inversion0.showElementString);
	$('feedback').innerHTML += " - the molecule contains an inversion center.";
}

q[17] = {
	"text"  : function(mol) { 
				var n = mol.ea["proper0"].elementData.order;
				return  "Does the molecule contain " + n + " vertical planes (&sigma;<sub>v</sub> or &sigma;<sub>d</sub>?)"; },
	"yes"   : function(mol) { 
				var n = mol.ea["proper0"].elementData.order;
				return "<i>C</i><sub>"+ n + "v</sub>"; },
	"no"    : function(mol) { return  18; },
	"x"		: 759,
	"y"		: 2203
}
t[17] = function(mol) {
	var present = false;
	for (var i = 0; i < mol.reflectionArray.length; i++) {
		if ((mol.reflectionArray[i].label == 'v') || (mol.reflectionArray[i].label == 'yz') || (mol.reflectionArray[i].label == 'xz'))
			present = true;
	}
	if (present)
		return 'yes'; 
	else 
		return 'no';
}
h[17] = function(mol) {
	jmolScript(mol.ea.proper0.hideElementString);
	var planeCount = 0;
	for (var i = 0; i < mol.reflectionArray.length; i++) {
		if ((mol.reflectionArray[i].label == 'v') || (mol.reflectionArray[i].label == 'd') || (mol.reflectionArray[i].label == 'yz') || (mol.reflectionArray[i].label == 'xz'))
			planeCount += 1;
			jmolScript(mol.ea["reflection" + i].showElementString); }
	$('feedback').innerHTML += " - the molecule contains " + planeCount + " vertical reflection planes.";
}

q[18] = {
	"text"  : function(mol) { 
				var n = mol.ea.proper0.elementData.order;
				return  "Does the molecule contain a " + 2*n + "-fold improper rotation axis (S<sub>" + 2*n + "</sub>)?"; },
	"yes"   : function(mol) { 
				var n = mol.ea.proper0.elementData.order;
				return "<i>S</i><sub>"+ 2*n + "</sub>"; },
	"no"    : function(mol) {
				var n = mol.ea.proper0.elementData.order;
				return "<i>C</i><sub>"+ n + "</sub>"; },
	"x"		: 759,
	"y"		: 2527
}
t[18] = function(mol) {
	try {
		if (mol.ea.improper0.elementData.order >= 4)
			return 'yes'; 
		else 
			return 'no';
	}
	catch (ex) { return 'no'; }
}
h[18] = function(mol) {
	for (var i = 1; i < mol.properArray.length; i++)
		if (mol.ea["proper" + i].elementData.order == 2)
			jmolScript(mol.ea["proper" + i].hideElementString);
	var order = mol.ea.improper0.elementData.order;
	jmolScript(mol.ea.improper0.showElementString);
	$('feedback').innerHTML += " - the molecule contains an <i>S</i><sub>" + order + "</sub> axis.";
}
