/*	note_reading.js
	copyleft Tancrède Bastié 2006-2008

requires:
	dom.js
	js_lib.js
	gauge.js
	staff.js
	keyboard.js

index:
	NoteReading()
*/

function NoteReading() {

	var drillSelect;
	var staff;
	var marksGauge;
	var noteKeyboard;
	var numberOfNotes = 64;

	if (getURLParameterByName("clef") == "fa") clef = 1;
	else clef = 0;

	key = 7;
	keyParameter = getURLParameterByName("tonalite");
	if ((keyParameter != null) && (keyParameter != "")) for (i = 0; i <= 14; i++) if (keyParameter == i) key = i;

	addLoadEvent(
		function() {
			drillSelect = select();
			drillSelect.appendChild(option("Toutes les notes", 0));
			drillSelect.appendChild(option("Quatre notes", 1));
			drillSelect.appendChild(option("Deux notes", 2));
			drillSelect.onchange = noteReading.fillStaff;
			addTo("drill", drillSelect);

			staff = new Staff(clef, key, numberOfNotes, "Quel est cette note ?");
			staff.addClefAndKeySelect("clefSelect", "keySelect", function() { if (drillSelect.value > 0) noteReading.fillStaff(); } );
			staff.addStaff("staff");

			marksGauge = new Gauge("jauge_", ".gif");
			marksGauge.addGauge("marksGauge");

			noteKeyboard = new Keyboard(checkAnswer, "C (do)", "C# Db", "D (ré)", "D# Eb", "E (mi)", "F (fa)", "F# Gb", "G (sol)", "G# Ab", "A (la)", "A# Bb", "B (si)");
			noteKeyboard.addKeyboard("noteKeyboard", 5);
		}
	);

	var twoNotesMask = new Array(true, false, false, false, true, false, false);
	var fourNotesMask = new Array(true, false, true, false, true, false, true);

	this.fillStaff = function() {
		switch (drillSelect.value) {
			case "0": staff.fillStaff(7); break;
			case "1": staff.fillStaff(7, fourNotesMask); break;
			case "2": staff.fillStaff(14, twoNotesMask); break;
		}
		staff.resetCursor();
	}

	function checkAnswer() {
		var answer = this.index;
		var question = staff.currentNote();
		if (answer == question) {
			marksGauge.sum(1, 0);
			staff.setCursorColor(3);
			setTimeout("noteReading.proceed()", 500);
			return;
		}
		marksGauge.sum(0, 1);
		staff.setCursorColor(1);
		setTimeout("noteReading.clear()", 600);
	}

	this.proceed = function() {
		if (staff.stepCursor() == 0) noteReading.fillStaff();
	}

	this.clear = function() {
		staff.setCursorColor(2);
	}

	addLoadEvent(this.fillStaff);
}

noteReading = new NoteReading();
