// Flags JavaScript
// Keith Enevoldsen, thinkzone.wlonk.com

// Get flag size code
sz = "med";
str=window.location.search.substring(1);
if (str != "") { sz = str.replace(/sz=/,""); }

// Write message at top of page
function message()
{
if (sz == "pop") {
	document.writeln('Size = Population');
} else if (sz == "area") {
	document.writeln('Size = Land Area');
} else {
	document.writeln('How many flags can you identify?');
}
}

// Display country's flag
function country(abbrev, country, population, area_km2)
{
// Show Greenland (not a country) only on land area chart
if (abbrev == "gl" && sz != "area") return;

// Calculate flag size
hgtMed=54;
if (sz == "pop") {
	wid = Math.max(Math.round(Math.sqrt(population)/100),4);
	hgt = Math.round(0.75*wid);
} else if (sz == "area") {
	wid = Math.max(Math.round(Math.sqrt(area_km2)/10),4);
	hgt = Math.round(0.75*wid);
} else if (sz == "sml"){
	wid = 0;
	hgt = hgtMed/2;
} else if (sz == "big"){
	wid = 0;
	hgt = hgtMed*2;
} else {
	wid = 0;
	hgt = hgtMed;
}
// Choose flag image
imgDir='https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/';
imgDirLg=imgDir + 'resize/';
imgDirSm=imgDir + 'small/';
if (hgt > hgtMed) {
	imgName = imgDirLg + abbrev + '-lgflag.gif';
} else {
	imgName = imgDirSm + abbrev + '-flag.gif';
}
// Display flag image
document.write('<img src="' + imgName + '" alt="' + country +'" title="' + country +'" align="center" ');
if (sz == "pop" || sz == "area") {
	document.writeln(' width="' + wid + '" height="' + hgt);
} else {
	document.writeln(' height="' + hgt);
}
document.writeln('">');
}