var selectedTuning = 'EADGBE';

function showFewerOptions(suffix)
{
  if (suffix == null) suffix = '';

  var showOptionsEl = document.getElementById('showOptions' + suffix);
  var optionsEl = document.getElementById('moreOptions' + suffix);
  var optionsDispEl = document.getElementById('id_optionsDisp' + suffix);

  showOptionsEl.style.display = 'inline';
  optionsEl.style.display = 'none';
  optionsDispEl.value = 'none';

  return false;
}


function showMoreOptions(suffix)
{
  if (suffix == null) suffix = '';

  var showOptionsEl = document.getElementById('showOptions' + suffix);
  var optionsEl = document.getElementById('moreOptions' + suffix);
  var optionsDispEl = document.getElementById('id_optionsDisp' + suffix);

  showOptionsEl.style.display = 'none';
  optionsEl.style.display = 'inline';
  optionsDispEl.value = 'inline';

  return false;
}

function setTuning(tuning, suffix)
{
  if (suffix == null) suffix = '';

  var readableTuningEl = document.getElementById("readableTuning" + suffix);
  var tuningNameEl = document.getElementById("tuningName" + suffix);
  var tuningEl = document.getElementById("tuning" + suffix);

  showMoreOptions(suffix);
  for (i = 0; i < tuningNameEl.length; i++) {
    if (tuningNameEl[i].text == tuning) {
      tuningNameEl.selectedIndex = i;
      readableTuningEl.value = tuningNameEl[i].text;
      tuningEl.value = tuningNameEl.value;
      break;
    }
  }
}

function submitting(suffix)
{
  var optionsEl = document.getElementById('moreOptions' + suffix);
  var tuningEl = document.getElementById("tuning" + suffix);
  var tuningNameEl = document.getElementById("tuningName" + suffix);
  var capoEl = document.getElementById("capo" + suffix);
  
  if (optionsEl.style.display == 'none') {
    // Options are not show. Use defaults.
    capoEl.selectedIndex = 0;
    tuningEl.value = 'EADGBE';
    tuningNameEl.selectedIndex = 0;
  }
}

function tuningNameChanged(suffix)
{
  var tuningEl = document.getElementById("tuning" + suffix);
  var tuningNameEl = document.getElementById("tuningName" + suffix);
  var readableTuningEl = document.getElementById("readableTuning" + suffix);

  if (tuningNameEl.value != '<Custom>') {
    tuningEl.value = tuningNameEl.value;
    selectedTuning = tuningNameEl.value;
  } else {
    tuningEl.focus();
    tuningEl.select();
  }
  readableTuningEl.value = tuningNameEl[tuningNameEl.selectedIndex].text;
}


function tuningChanged(suffix)
{
  var tuningEl = document.getElementById("tuning" + suffix);
  var tuningNameEl = document.getElementById("tuningName" + suffix);
  var readableTuningEl = document.getElementById("readableTuning" + suffix);

  tuningValue = '';
  for(i = 0; i < tuningEl.value.length; i++) {
    if (tuningEl.value.charAt(i) != 'b') {
      tuningValue += tuningEl.value.charAt(i).toUpperCase();
    } else {
      tuningValue += tuningEl.value.charAt(i);
    }
  }
  if (tuningValue != selectedTuning) {
    if (tuningNameEl.value != tuningValue) {
      for (i = 0; i < tuningNameEl.length; i++) {
	if (tuningNameEl[i].value == tuningValue) {
	  tuningNameEl.selectedIndex = i;
	  readableTuningEl.value = tuningNameEl[tuningNameEl.selectedIndex].text;
	  return;
	}
      }
      tuningNameEl.selectedIndex = tuningNameEl.length - 1; // custom
      readableTuningEl.value = tuningNameEl[tuningNameEl.selectedIndex].text;
    }
    selectedTuning = tuningEl.value;
  }
}

function reportError(chordName)
{
  
}

function ccSelectTabMenu(tabEl)
{
  allTabEls = document.getElementById('id_ccTabMenuArea').getElementsByTagName('a');
  for (i = 0; i < allTabEls.length; i++) {
    if (allTabEls[i].className == 'activeTab' && allTabEls[i] != tabEl) {
      allTabEls[i].className = 'menuTab';
    }
  }
  tabEl.className = 'activeTab';
  bodyId = tabEl.id.replace('_tab', '');
  bodyEl = document.getElementById(bodyId);
  allBodyEls = document.getElementById('id_ccTabMenuArea').getElementsByTagName('div');
  for (i = 0; i < allBodyEls.length; i++) {
    if (allBodyEls[i].className == 'tabMenuBody' && allBodyEls[i] != bodyEl) {
      allBodyEls[i].style.display = 'none';
    }
  }
  bodyEl.style.display = 'block';
  menuTabEl = document.getElementById('id_menuTab');
  if (menuTabEl) {
    menuTabEl.value = bodyId.replace('id_', '');
  }
  return false;
}

function newChord(anchor)
{
  disp = 'none';
  for (suffix in new Array('', '1')) {
    if ((el = optionsDispEl = document.getElementById('id_optionsDisp' + suffix)) &&
	el.value != 'none') {
      disp = 'inline';
      break;
    }
  }
  anchor.href += encodeURI('&optionsDisp=' + disp);
  return true;
}

var shownButton = null;
function mouseOver(el)
{
  if (tuning != 'EADGBE') return; // ES: Temporary!
  var idNum = el.id.replace('id_chord', '');
  if (shownButton != idNum) {
    if (shownButton) {
      var buttonEl = document.getElementById('id_button' + shownButton);
      buttonEl.style.display = 'none';
    }
    var buttonEl = document.getElementById('id_button' + idNum);
    buttonEl.style.display = 'inline';
    shownButton = idNum;
  }
}

