function Dialog(QA)
{
	this.QA = QA;
}

function QA(questions, answers)
{
	this.questions = questions;
	this.answers = answers;
}

function Answer(answer, prescription, QA)
{
	this.answer = answer;
	this.prescription = prescription;
	this.QA = QA;
}

function Prescription(id, prescription)
{
	this.id = id;
	this.prescription = prescription;
}

var dialog = new Dialog(new QA(
	new Array(
		"How are you feeling today?"
	),

	new Array(
		new Answer("I feel great!", null, new QA(
			new Array(
				"I'm glad to hear that, tell me why."
			),

			new Array(
				new Answer("I'm getting married.", new Array(new Prescription("apachesong", "Congratulations! _Try this poem_.")), null),

				new Answer("I'm in love.", new Array(new Prescription("firstlove", "Congratulations! _Try this poem_.")), null),

				new Answer("I'm just enjoying life.", new Array(new Prescription("myheartleapsupwhenibehold", "Great, _try this poem, by William Wordsworth_.")), null)

			)

		)),

		new Answer("I feel stressed out!", null, new QA(
			new Array(
				"Can you tell me why?"
			),

			new Array(
				new Answer("I don't know what to do with my life.", new Array(new Prescription("youwakeupinthemorning", "_This poem_ will remind you how precious time is.")), null),

				new Answer("I'm having an affair.", new Array(new Prescription("anexpenseofspirit", "Hmmm, _try this poem_.")), null),

				new Answer("I'm overworked.", new Array(new Prescription("theworldistoomuch", "_Try this poem_.")), null)

			)

		)),

		new Answer("I'm feeling down.", null, new QA(
			new Array(
				"Oh I'm sorry, why is that?"
			),

			new Array(
				new Answer("I lost someone.", new Array(new Prescription("donotstandatmygraveandweep", "Perhaps _this poem_ will help a little.")), null),

				new Answer("I'm just depressed.", new Array(new Prescription("odeonmelancholy", "_This poem_ shows how you can accept depression as an essential part of life.")), null),

				new Answer("I got dumped.", new Array(new Prescription("gonow", "I'm sorry about that, _try this poem_.")), null)

			)

		))

	)
));

var dialogPanel = document.getElementById("dialogPanel");
var dialogState = dialog;
var prevDialog = "";

renderDialog();

function answerQuestion(answer)
{
	var answerIndex = answer.selectedIndex;

	if(answerIndex != 0)
	{
		dialogState = dialogState.QA.answers[answerIndex - 1];

		renderDialog();
	}
}

function renderDialog()
{
	if(dialogState.answer)
	{
		prevDialog += "<p class='answer'><strong>Patient:</strong> " +
			dialogState.answer + "</p>";
	}

	if(dialogState.prescription)
	{
		var randomIndex = Math.round(Math.random() * (dialogState.prescription.length - 1));
		var prescription = dialogState.prescription[randomIndex];
		var prescriptionText = prescription.prescription;
		var linkStart = prescriptionText.indexOf("_");
		var linkEnd = prescriptionText.lastIndexOf("_");

		prescriptionText = prescriptionText.substr(0, linkStart) + "<a href='/poems/" +
			prescription.id + "'>" + prescriptionText.substring(linkStart + 1, linkEnd) +
			"</a>" + prescriptionText.substr(linkEnd + 1);
		dialogStr = prevDialog + "<p class='prescription'>" + prescriptionText + "</p>";
	}
	else
	{
		var randomIndex = Math.round(Math.random() * (dialogState.QA.questions.length - 1));
		var dialogStr = "";
		
		prevDialog += "<p class='question'><strong>Doctor:</strong> " +
			dialogState.QA.questions[randomIndex] + "</p>";
		dialogStr = prevDialog + "<p class='answer'><strong>Patient:</strong> " +
			"<select onchange='answerQuestion(this);'><option></option>";

		for(var count = 0; count < dialogState.QA.answers.length; count++)
		{
			var answer = dialogState.QA.answers[count];

			dialogStr += "<option>" + answer.answer + "</option>";
		}
		dialogStr += "</select></p>";
	}

	dialogPanel.innerHTML = dialogStr;
}
