var sppName = 'Current: none';
var tsppName = 'Current: none';
function showPlantSpp(show) {
  if (show) {
    showDiv('plantSpp', true);
    if (xInnerHtml('plantSppInner') == '') {
      xInnerHtml('plantSppInner', 'Loading...');
      var qs = 'fid=-1&ini=' + ini;
      var xhr = new xHttpRequest();
      if (!xhr.send('POST', '/lib/getplantsbyfamily.php', qs, false, false, false, null, fillSpecies))
        gotoError();
      xhr = null;
    } else {
      showSciList();
      showDiv('plantLink', true);
    }
  } else {
    showDiv('plantSpp', false);
    showDiv('sciList', false);
    showDiv('commonList', false);
    showDiv('plantLink', false);
  }
}
function fillSpecies(req, status, data) {
  if (status == 0) {
    var json = JSON.parse(req.responseText);
    if (json.ok) {
      var str = '<a href="javascript:showSciList()">Scientific names</a><br>' +
        '<a href="javascript:showCommonList()">Common names</a><br>' +
        '<a href="javascript:showPlantSpp(false)">Cancel</a>';
      if (mf.tfilters) str += '<div id="plantMapPosCtrl">' +
        '<input type="radio" id="plantmappostop" name="plantmappos" value="top" onclick="setPlantPos()" checked> Top<br>' +
        '<input type="radio" id="plantmapposbottom" name="plantmappos" value="bottom" onclick="setPlantPos()"> Bottom</div>';
      str += '<div id="currentSpp">Current: none</div>' +
        '<div id="plantLink"><a href="/plants" target="_base">Digital Atlas website</a></div>' +
        '<div id="sciList">' + json.sci + '</div><div id="commonList">' + json.common + '</div>';
      xInnerHtml('plantSppInner', str);
      showSciList();
      showDiv('plantLink', true);
    } else alert(json.err);
  } else gotoError();
}
function showSciList() {
  xGetElementById('sciList').style.visibility = 'visible';
  xGetElementById('commonList').style.visibility = 'hidden';
}
function showCommonList() {
  xGetElementById('sciList').style.visibility = 'hidden';
  xGetElementById('commonList').style.visibility = 'visible';
}
function selectSpecies(id) {
  showPlantSpp(false);
  if (mf.tfilters && (mf.plantmappostop.checked)) {
    mf.tfilters.value = '{"plants":"spp_id = ' + id + '"}';
    data = false;
  } else {
    mf.filters.value = '{"plants":"spp_id = ' + id + '"}';
    data = true;
  }
  getMap(false);
  var qs = 'id=' + id + '&ini=' + ini;
  var xhr = new xHttpRequest();
  if (!xhr.send('POST', '/lib/getplantsppinfo.php', qs, false, false, false, data, fillInfo))
    gotoError();
  xhr = null;
}
function fillInfo(req, status, data) {
  if (status == 0) {
    var json = JSON.parse(req.responseText);
    if (json.ok) {
      var s = 'Current: ' + json.info.sci_name + ' (' + json.info.common_name + ')';
      xInnerHtml('currentSpp', s);
      if (data) sppName = s;
      else tsppName = s;
    } else alert(json.err);
  } else gotoError();
}
function setPlantPos() {
  if (mf.plantmappostop.checked) xInnerHtml('currentSpp', tsppName);
  else xInnerHtml('currentSpp', sppName);
}

