/*	chord_base.js
	copyleft Tancrède Bastié 2004-2008

requires:
	dom.js

index:
	ChordBase()
		add(name, rootString, fingering, [open])
		addChordSetSelector(id, onchange)
		chordSet(setName)
		find(name, rootString)
		randomChord()
*/

function ChordBase() {
/*	prototype of the chord base
	prototype de la base des accords
*/

	function Chord(name, rootString, fingerings, open) {
	/*	prototype of chord
		prototype d'un accord
	*/
		this.name = name;
		this.rootString = rootString;
		this.fingerings = new Array;
		this.open = (open == true);
		var minFingering = 24;
		var maxFingering = 0;
		var fingering;
		for (var i = 0; i < fingerings.length; i++) {
			if (fingerings.charAt(i) == "x") this.fingerings.push(null);
			else {
				this.fingerings.push(fingering = fingerings.charAt(i));
				if (fingering < minFingering) minFingering = fingering;
				if (fingering > maxFingering) maxFingering = fingering;
			}
		}
		this.rootFret = 1*this.fingerings[rootString];
		this.minFingeringOffset = minFingering - this.rootFret;
		this.maxFingeringOffset = maxFingering - this.rootFret;
	}

	/*	first chord set is the union of all others
		chords added to other sets are implicitly added to it
		its name is given explicitely before other sets

		le premier jeu d'accord est l'union de tous les autres
		les accords ajoutés aux autres jeux lui sont implicitement ajoutés
		son nom est donné explicitement avant les autres
	*/
	var allChords = new Array;
	var chordSets = new Array;
	var chordSetNames = new Array;
	var currentChordSet;

	this.chordSet = function(setName) {
	/*	creates a chord set if necessary and change current chord set
		créé un jeu d'accord si nécessaire et change le jeu courant
	*/
		if (typeof chordSets[setName] == "undefined") {
			chordSets[setName] = new Array;
			chordSetNames.push(setName);
		}
		currentChordSet = chordSets[setName];
	}

	this.add = function(name, rootString, fingerings, open) {
	/*	creates and adds a chord to the current chord set
		créé et ajoute un accord au jeu d'accord courant
	*/
		var chord = new Chord(name, rootString, fingerings, open);
		currentChordSet.push(chord);
		allChords.push(chord);
	}

	this.addChordSetSelector = function(id, onchange) {
	/*	creates the selector used to specify the studied chord set
		créé le sélecteur utilisé pour spécifier le jeu d'accord étudié
	*/
		var selector = select();
		for (var i = 0; i < chordSetNames.length; i++) selector.appendChild(option(chordSetNames[i], chordSetNames[i]));
		selector.onchange = function() { currentChordSet = chordSets[this.value]; onchange(); }
		currentChordSet = chordSets[selector.value = chordSetNames[0]] = allChords;
		return appendTo(document.getElementById(id), selector);
	}

	this.randomChord = function() {
	/*	pick a chord at random in the studied set
		tire au sort un accord dans le jeu d'accord étudié
	*/
		return currentChordSet.random();
	}

	this.find = function(name, rootString) {
	/*	find a chord of given name and root string
		trouve un accord du nom donné et dont la fondamentale est sur une corde donnée
	*/
		for (var i = 0; i < currentChordSet.length; i++) {
			if (currentChordSet[i].rootString == rootString) if (currentChordSet[i].name == name) return currentChordSet[i];
		}
		for (var i = 0; i < allChords.length; i++) {
			if (allChords[i].rootString == rootString) if (allChords[i].name == name) return allChords[i];
		}
		return null;
	}
}
