/*******************************************************************************
 * An audio chord quiz.
 ******************************************************************************/

function sortNumber(a, b)
{
  return a - b;
}

function AudioChordQuiz()
{
  this.nNotes = 48; // Number of possible notes.
  this.enabledChords = [0, 1, 4];
  this.nTries = 0;
  this.nCorrect = 0;

  this.UpdateEnabledChords = function() {
    this.enabledChords.sort(sortNumber);
    var choicesText = '';
    choicesText += '<ul class="acqChoicesList">';
    for (var i = 0; i < this.enabledChords.length; i++) {
      choicesText += '<li><a href="#" onClick="return acq.Guess(this)">' +
	chordTypes[this.enabledChords[i]][2] +
	'</a></li>';
    }
    choicesText += '</ul>';
    choicesText += '<p><a href="javascript:playAgain()" class="acqActionButton">' +
    '<img src="../images/listen.gif"> Play again</a><p>';
    document.getElementById('acqChoices').style.display = 'none';
    document.getElementById('acqChoices').innerHTML = choicesText;

    choicesText = '<ul id="acqAllChordsList">';
    for (var i = 0; i < chordTypes.length; i++) {
      var isEnabled = (ArrayIndexOf(this.enabledChords, i) != -1);
      var theClass = isEnabled ? 'acqEnabledChord' : 'acqDisabledChord';
      var checked = isEnabled ? ' CHECKED' : '';
      var actionString = isEnabled ? 'disable' : 'enable';
      var longName = chordTypes[i][2];
      choicesText += '<li class="' + theClass + '" title="' + longName + '">' +
	'<a href="#" id="acqChord' + i +
	'" onClick="return acq.ToggleChord(this)" class="' + theClass + '">' +
	'<input type="checkbox"' + checked + '/>' +
	 longName +
	'</a>' +
	'<a href="#" onClick="return acq.PreviewChord('+i+')" title="Hear a ' + longName + '">' +
	'<img src="../images/listen.gif"/>' +
	'</a></li>';
    }
    choicesText += '</ul>';
    document.getElementById('acqChooseChords').innerHTML = choicesText;
  }

  this.ToggleChord = function(el) {
    chord = parseInt(el.id.replace('acqChord', ''));
    if (el.className == 'acqEnabledChord') {
      ArrayRemove(this.enabledChords, chord);
      el.className = 'acqDisabledChord';
    } else {
      this.enabledChords.push(chord);
      el.className = 'acqEnabledChord';
    }
    el.getElementsByTagName('input')[0].checked = true;
    el.parentNode.className = el.className;
    this.UpdateEnabledChords();
    this.RemoveOptions();
    return false;
  }

  this.MakeChord = function () {
    var chord = RandomChoice(this.enabledChords);

    this.PreviewChord(chord);
    this.answer = chordTypes[chord][2];
  }

  this.Guess = function(el) {
    if (el.innerHTML == this.answer) {
      document.getElementById('acqLastResult').innerHTML = ['Correct!', 'Good job!', 'That\'s right!',
							    'Nice one!', 'Yup!', 'You got it!'][this.nCorrect % 6];
      document.getElementById('acqLastResult').style.color = 'blue';
      this.nCorrect++;
    } else {
      document.getElementById('acqLastResult').innerHTML = ['Incorrect', 'Wrong', 'Sorry',
							    'No', 'That\'s not it', 'Nope'][this.nTries % 6] +
      '. Correct answer was ' + this.answer + '.';
      document.getElementById('acqLastResult').style.color = 'red';
    }
    this.nTries++;
    percentage = Math.round(this.nCorrect / this.nTries * 100);
    document.getElementById('acqScore').innerHTML = this.nCorrect + ' out of ' + this.nTries + ' (' + percentage + '%)';
    document.getElementById('acqScore').style.display = 'block';
    this.MakeChord();
    return false;
  }

  this.EmbedMe = function() {
    loadPlayer();
    this.UpdateEnabledChords();
    return false;
  }

  this.FirstChord = function() {
    this.MakeChord();
    document.getElementById('acqQuestion').innerHTML = 'Name the chord:';
    document.getElementById('acqChoices').style.display = 'block';
    return false;
  }

  // Option class. Potentially reusable in the future.
  function Option(question, choices, inputName) {
    this.GetHtml = function() {
      var ret = '<div class="acqOption" id="id_' + inputName + '">' +
      '<p>' + this.question + '</p>' +
      '<ul>';

      for (var i = 0; i < this.choices.length; i++) {
	ret += '<li><input type="radio" name="' + this.inputName +
	  '" value="' + this.choices[i] +
	  '" onClick="acq.SetOption(this)"' +
	  ((i == 0) ? ' CHECKED' : '') +
	  '/>' + this.choices[i] +
	  '</li>';
      }
      ret += '</ul></div>';
      return ret;
    }

    this.question = question;
    this.choices = choices;
    this.value = this.choices[0];
    this.inputName = inputName;

    document.getElementById('acqOptionsForm').innerHTML += this.GetHtml();
  }

  this.RemoveOptions = function() {
    document.getElementById('acqChoices').style.display = 'none';
    document.getElementById('acqLastResult').innerHTML = '';
    document.getElementById('acqQuestion').innerHTML = '<a href="#" onClick="return acq.FirstChord()" class="acqActionButton"><img src="../images/listen.gif" style="border-style:none"/> Hear chord.</a>';
  }

  this.PreviewChord = function(chord) {
    // ES: Get rid of that 8 when have better samples!
    var rootNoteNum = RandomInt(8, this.nNotes - 12);
    var rootNote = NoteNumToName(rootNoteNum, '#', true);
    mp3File = rootNote;
    for (var i = 0; i < chordTypes[chord][1].length; i++) {
      mp3File += '-' + NoteNumToName(rootNoteNum + chordTypes[chord][1][i], '#', true);
    }
    mp3File += '.mp3'
    playMp3(mp3File, 0.07);
    return false;
  }

  this.SetOption = function(el) {
    for (var i = 0; i < this.options.length; i++) {
      if (el.name == this.options[i].inputName) {
	this.options[i].value = el.value;
	break;
      }
    }
    this.RemoveOptions()
  }


  this.options = [];
}

