/*	rythm2ms.js
	copyleft Thomas Baspeyras 2005

requires :
	dom.js
	js_lib.js

index:
	Rythm2ms()
*/

function Rythm2ms() {
/*	prototype of rythm-milliseconds converter
	prototype de convertisseur rythme-millisecondes
*/

	Figure = function(input, factor) {
	/*	prototype of rythm figure
		prototype de figure rythmique
	*/
		input.onchange = function() {
			tempo.value = Math.round((15000000 * bottomSignature.value * factor) / input.value) / 1000;
			updateFigures();
		}
		this.input = input;
		this.factor = factor;
	}

	var figures = new Array();
	var bar;
	var topSignature;
	var bottomSignature;
	var tempo;

	addLoadEvent(
		function() {
			tempo = document.getElementById("r2msTempo");
			tempo.onchange = updateFigures;

			topSignature = document.getElementById("topSignature");
			topSignature.onchange = updateFigures;
			bottomSignature = document.getElementById("bottomSignature");
			bottomSignature.onchange = updateFigures;

			bar = document.getElementById("bar");
			bar.onchange = function() {
				tempo.value = Math.round(60000000 * topSignature.value / bar.value) / 1000;
				updateFigures();				
			}

			figures.push(new Figure(document.getElementById("fig2"), 2));
			figures.push(new Figure(document.getElementById("fig2d"), 3));
			figures.push(new Figure(document.getElementById("fig4"), 1));
			figures.push(new Figure(document.getElementById("fig4d"), 1.5));
			figures.push(new Figure(document.getElementById("fig8"), 0.5));
			figures.push(new Figure(document.getElementById("fig8d"), 0.75));
			figures.push(new Figure(document.getElementById("fig12"), 1 / 3));
			figures.push(new Figure(document.getElementById("fig2x12"), 2 / 3));
			figures.push(new Figure(document.getElementById("fig16"), 0.25));
			figures.push(new Figure(document.getElementById("fig16d"), 0.375));
			figures.push(new Figure(document.getElementById("fig24"), 1 / 6));
			figures.push(new Figure(document.getElementById("fig2x24"), 2 / 6));
			figures.push(new Figure(document.getElementById("fig32"), 0.125));

			updateFigures();
		}
	)

	updateFigures = function() {
		var beat = 60000 / tempo.value;
		var fourth = bottomSignature.value * beat / 4;
		bar.value = Math.round(topSignature.value * beat);
		for (var i = 0; i < figures.length; i++) figures[i].input.value = Math.round(figures[i].factor * fourth);
	}
}

rythm2ms = new Rythm2ms();

