var data = { "landcover" : { "34585" : {"de" : "Bebaute Flächen", "en" : "Artificial surfaces"}, "34601" : {"de" : "Landwirtschaftliche Flächen", "en" : "Agricultural areas"}, "34617" : {"de" : "Wälder und naturnahe Flächen", "en" : "Forests and semi-natural areas"}, "34633" : {"de" : "Feuchtflächen", "en" : "Wetlands"}, "34641" : {"de" : "Wasserflächen", "en" : "Water bodies"}, "34649" : {"de" : "Flächen außerhalb des Bearbeitungsgebietes", "en" : "no data"} }, "countries" : { "8449" : {"de" : "Deutschland", "en" : "Germany"}, "8407" : {"de" : "Österreich", "en" : "Austria"}, "8472" : {"de" : "Georgien", "en" : "Georgia"}, "8572" : {"de" : "Polen", "en" : "Poland"}, "8503" : {"de" : "Italien", "en" : "Italy"}, "8470" : {"de" : "Vereinigtes Königreich Großbritannien und Nordirland", "en" : "United Kingdom of Great Britain and Northern Ireland (the)"}, "8582" : {"de" : "Rumänien", "en" : "Romania"}, "8436" : {"de" : "Schweiz", "en" : "Switzerland"}, "8477" : {"de" : "Grönland", "en" : "Greenland"}, "8468" : {"de" : "Frankreich", "en" : "France"} }, "taxongroups" : { "175" : "Diplopoda", "186" : "Collembola", "193" : "Chilopoda", "84976" : "Oribatida", "86683" : "Enchytraeidae", "101721" : "Gamasina", "101771" : "Isopoda", "108931" : "Tardigrada", "141728" : "Fungi", "172653" : "Oomycetes", "173425" : "Bacteria", "203855" : "Crassiclitellata", "207279" : "Nematoda" }, "commondata" : {"obs":493292,"points":39365,"taxa":15293,"updatetime":"2026-04-07"}, "tgdata" : { "175" : {"obs" : 88698, "points" : 14352, "spec" : 2102}, "186" : {"obs" : 117471, "points" : 3235, "spec" : 731}, "193" : {"obs" : 52333, "points" : 9067, "spec" : 1382}, "84976" : {"obs" : 92706, "points" : 5143, "spec" : 1707}, "86683" : {"obs" : 14857, "points" : 349, "spec" : 135}, "101721" : {"obs" : 20612, "points" : 2708, "spec" : 858}, "101771" : {"obs" : 14403, "points" : 3781, "spec" : 62}, "108931" : {"obs" : 427, "points" : 110, "spec" : 101}, "141728" : {"obs" : 3320, "points" : 141, "spec" : 25}, "203855" : {"obs" : 29276, "points" : 2816, "spec" : 133}, "207279" : {"obs" : 48708, "points" : 3738, "spec" : 720} } };
var template = "<h3 translate=\"true\">{\"de\": \"Allgemeine Angaben zum Datenbestand in &lt;a target='_blank' href='https://edaphobase.org/'&gt;Edaphobase&lt;/a&gt;\", \"en\": \"General information about the &lt;a target='_blank' href='https://edaphobase.org/'&gt;Edaphobase&lt;/a&gt; dataset\"}</h3>\n<table border=\"0\">\n\t<tr>\n\t\t<td translate=\"true\">{\"de\": \"Letzte Aktualisierung:\", \"en\": \"Last update\"}</td>\n\t\t<td style=\"padding-left: 10px\" id=\"ebLastRefresh\" />\n\t</tr><tr>\n\t\t<td translate=\"true\">{\"de\": \"Anzahl Beobachtungen:\", \"en\": \"Number of observations\"}</td>\n\t\t<td style=\"padding-left: 10px\" id=\"ebObservations\" />\n\t</tr><tr>\n\t\t<td translate=\"true\">{\"de\": \"Anzahl Taxa:\", \"en\": \"Number of taxa\"}</td>\n\t\t<td style=\"padding-left: 10px\" id=\"ebTaxa\" />\n\t</tr><tr>\n\t\t<td translate=\"true\">{\"de\": \"Anzahl Fundpunkte:\", \"en\": \"Number of sites\"}</td>\n\t\t<td style=\"padding-left: 10px\" id=\"ebSites\" />\n\t</tr>\n</table>\n<div id=\"ebLong\">\n\t<hr /><br />\n\t<h3 translate=\"true\">{\"de\": \"In Edaphobase enthaltene Taxonomische Großgruppen\", \"en\": \"Taxonomic major groups in Edaphobase\"}</h3>\n\t<table border=\"0\" id=\"ebTaxGroups\" >\n\t\t<tr>\n\t\t\t<th style=\"width: 100px\" translate=\"true\">{\"de\": \"Taxonomische Großgruppe\", \"en\": \"Taxonomic major group\"}</th>\n\t\t\t<th translate=\"true\">{\"de\": \"Anzahl Beobachtungen\", \"en\": \"Number of observations\"}</th>\n\t\t\t<th translate=\"true\">{\"de\": \"Anzahl Fundpunkte\", \"en\": \"Number of sites\"}</th>\n\t\t\t<th translate=\"true\">{\"de\": \"Anzahl Arten\", \"en\": \"Number of species\"}</th>\n\t\t</tr>\n\t</table>\n\t<hr /><br />\n\t<h3 translate=\"true\">{\"de\": \"Abfrage von Daten aus Edaphobase\", \"en\": \"Query data from Edaphobase\"}</h3>\n\t<div translate=\"true\">{\"de\": \"(Diese Abfrage leitet Sie auf das Edaphobase-Portal weiter.)\", \"en\": \"(This query will redirect you to the Edaphobase portal.)\"}</div><br />\n\t<form name=\"query\">\n\t\t<table border=\"0\">\n\t\t\t<tr>\n\t\t\t\t<td translate=\"true\">{\"de\": \"Taxonomische Großgruppe\", \"en\": \"Taxonomic major group\"}</td>\n\t\t\t\t<td style=\"padding-left: 10px\"><select style=\"width: 100%\" name=\"taxa\" id=\"ebTaxGroup\"></select></td>\n\t\t\t</tr><tr>\n\t\t\t\t<td style=\"padding-top: 10px\" translate=\"true\">{\"de\": \"Land\", \"en\": \"Country\"}</td>\n\t\t\t\t<td style=\"padding-top: 10px; padding-left: 10px\"><select style=\"width: 100%\" name=\"regions\" id=\"ebCountry\"></select></td>\n\t\t\t</tr><tr>\n\t\t\t\t<td style=\"padding-top: 10px; white-space: nowrap;\" translate=\"true\">{\"de\": \"Landnutzung Ebene 1 (&lt;a target='_blank' href='http://www.umweltbundesamt.de/themen/boden-landwirtschaft/flaechensparen-boeden-landschaften-erhalten/corine-land-cover-clc'&gt;CORINE&lt;/a&gt;)\", \"en\": \"Landcover Level 1 (&lt;a target='_blank' href='http://www.umweltbundesamt.de/en/topics/soil-agriculture/land-use-reduction/the-corine-land-cover-clc-program'&gt;CORINE&lt;/a&gt;)\"}</td>\n\t\t\t\t<td style=\"padding-top: 10px; padding-left: 10px\">\n\t\t\t\t\t<select style=\"width: 100%\" name=\"column\" id=\"ebLandcover\"><option /></select>\n\t\t\t\t</td>\n\t\t\t</tr><tr>\n\t\t\t\t<td colspan=\"2\" style=\"padding-top: 10px\"><input type=\"button\" value=\"OK\" style=\"width: 100%\" onclick=\"submitQuery(document.forms['query'])\"/></td>\n\t\t\t</tr>\n\t\t</table>\n\t</form>\n</div>\n";

//var lang = navigator.language;
var portal = "https://portal.edaphobase.org/?qxenv:action:extquery&";
//var portal = "http://172.30.138.21/portal-source/PortalNeu/proj/source/?qxenv:action:extquery&";
defaultCountry=8449;


data.commondata.updatetime = new Date(data.commondata.updatetime);

var node = document.getElementById("ebcontent");
node.innerHTML = template;


var node = document.getElementById("ebLastRefresh");
//node.textContent = data.commondata.updatetime.toLocaleFormat("%d.%m.%Y");
node.textContent = data.commondata.updatetime.toLocaleString(lang, {year: "numeric", month: "numeric", day: "numeric"});

var node = document.getElementById("ebObservations");
node.textContent = data.commondata.obs;

var node = document.getElementById("ebTaxa");
node.textContent = data.commondata.taxa;

var node = document.getElementById("ebSites");
node.textContent = data.commondata.points;

var node = document.getElementById("ebTaxGroups");
var groupname, row, cell;
for(var key in data.tgdata) {
	groupname = data.taxongroups[key];
	row = document.createElement("tr");
	
	cell = document.createElement("td");
	cell.textContent = groupname;
	row.appendChild(cell);
	
	cell = document.createElement("td");
	cell.textContent = data.tgdata[key].obs;
	row.appendChild(cell);
	
	cell = document.createElement("td");
	cell.textContent = data.tgdata[key].points;
	row.appendChild(cell);
	
	cell = document.createElement("td");
	cell.textContent = data.tgdata[key].spec;
	row.appendChild(cell);
	
	node.appendChild(row);
}

node = document.getElementById("ebTaxGroup");
var option, att;
for(var key in data.taxongroups) {
	option = document.createElement("option");
	option.value = key;
	option.textContent = data.taxongroups[key];
	node.appendChild(option);
}

node = document.getElementById("ebCountry");
for(var key in data.countries) {
	option = document.createElement("option");
	option.value = key;
	option.setAttribute("translate", "true");
	option.textContent = JSON.stringify(data.countries[key]);
	node.appendChild(option);
	if((key*1) == (defaultCountry*1)) defaultCountry = option;
}

node = document.getElementById("ebLandcover");
for(var key in data.landcover) {
	option = document.createElement("option");
	option.value = "485-"+key;
	option.setAttribute("translate", "true");
	option.textContent = JSON.stringify(data.landcover[key]);
	node.appendChild(option);
}

if(short) {
	node = document.getElementById("ebLong");
	node.parentElement.removeChild(node);
}


translate(document.getElementById("ebcontent"));
function translate(node) {
	var trans = node.getAttribute("translate");
	if(trans == "true") {
		trans = JSON.parse(node.textContent);
		node.innerHTML = lang == "de" ? trans.de : trans.en;
		return;
	}
	for(var i = 0; i < node.children.length; i++) {
		translate(node.children[i]);
	}
}

orderSelect(document.getElementById("ebTaxGroup"));
orderSelect(document.getElementById("ebCountry"));
orderSelect(document.getElementById("ebLandcover"));

defaultCountry.defaultSelected=true;

function orderSelect(select) {
	var arr = [];
	for(var i = 0; i < select.children.length; i++) {
		arr.push(select.children[i]);
	}
	arr.sort(function(a,b) {
		if(a.textContent < b.textContent) return -1;
		if(a.textContent > b.textContent) return 1;
		return 0;
	});
	
	for(var i = 0; i < arr.length; i++) {
		select.appendChild(arr[i]);
	}
	select.children[0].selected = true;
}


function submitQuery(form) {
	var erg = [];
	for(var i = 0; i < form.length-1; i++) {
		if(form[i].value != "")
			erg.push("qxenv:"+form[i].name+":"+form[i].value);
	}
	var link = portal+erg.join("&");
	var win = window.open(link, '_blank');
  	win.focus();
}



