/*	interval_reading.js
	copyleft Tancrède Bastié 2006

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

index:
	IntervalReading(clef)
*/

function IntervalReading(clef) {

	var drillSelect;
	var staff;
	var marksGauge;
	var intervalKeyboard;
	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("Tous les intervalles", 0));
			drillSelect.appendChild(option("Jusqu'à l'octave", 1));
			drillSelect.appendChild(option("Jusqu'à la quinte", 2));
			drillSelect.appendChild(option("Jusqu'à la tierce", 3));
			drillSelect.onchange = intervalReading.fillStaff;
			addTo("drill", drillSelect);

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

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

			intervalKeyboard = new Keyboard(checkAnswer, "b2 (b9)", "2 (9)", "3m", "3", "4 (11)", "b5 (#11)", "5", "6m (b13)", "6 (13)", "7", "7M", "8va");
			intervalKeyboard.addKeyboard("intervalKeyboard", 5);
		}
	);

	this.fillStaff = function() {
		switch (drillSelect.value) {
			case "0": staff.fillStaff(21); break;
			case "1": staff.fillStaff(7); break;
			case "2": staff.fillStaff(4); break;
			case "3": staff.fillStaff(2); break;
		}
		staff.resetCursor();
	}

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

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

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

	addLoadEvent(this.fillStaff);
}

intervalReading = new IntervalReading();
