var inCountryView = false;
var inChartView = false;
var data = null;
var googleChartData = null;
var countrySelect = null;
var chartTitle = null;
var xAxisTitle = null;
var yAxisTitle = null;
var yAxisTitle2 = null;
var yAxisTitle3 = null;
$(document).ready(function () {
$('#subtitle').html(cloud.poweredBy);
$("#search-text").on("keyup", function (e) {
if (e.keyCode == 13) {
search();
}
});
$(".accordion").on("click", ".accordion-header", function () {
$(this).toggleClass("active").next().slideToggle();
});
$(window).resize(function () {
onResize();
});
});
function onResize() {
if (inChartView && googleChartData) {
chartSelectionChanged();
}
}
// Country selection changed - load and display country data
function countrySelectionChanged() {
$("body").css("cursor", "progress");
$('#loading').css('visibility', 'visible');
setCountryView();
var countryName = $('#country').val();
if (countryName === '') {
setAllContent('No content');
$('#preamble').removeClass('optional');
$('h2').addClass('optional');
$('#country-flag').css('visibility', 'collapse');
$('#country-name').text('');
$('#country-flag').attr('src', null);
data = null;
$("body").css("cursor", "default");
}
else {
$('h2').removeClass('optional');
var countryKey = CountryKey(countryName);
setAllContent('Loading...');
if (haveFlag(countryName)) {
$('#country-flag').attr('src', cloud.flagImageUrl(countryKey));
$('#country-flag').css('visibility', 'visible');
}
else {
$('#country-flag').css('visibility', 'collapse');
$('#country-flag').attr('src', null);
}
$('#country-name').text(countryName);
// Retrieve JSON country record
var url = null;
url = cloud.countryRecordUrl(countryName, countryKey);
$.ajax({
type: 'GET',
url: url,
accepts: "json",
}).done(function (result) {
data = cloud.resultToJson(result);
if (data) {
// If nothing is open, open first accordion (Introduction)
if ($('.active').length === 0) {
$('#content-header-introduction').toggleClass("active").next().slideToggle();
}
// Load content: Introduction
var intro = '';
if (haveFlag(countryName)) {
var flagImageUrl = cloud.flagImageUrl(countryKey);
intro = intro + '
Flag ';
}
if (data.introduction && data.introduction.background) {
intro += 'Background
' + data.introduction.background + '
';
}
$('#content-introduction').html(intro);
// Load content: Geography
var geo = '';
var mapImageUrl = cloud.mapImageUrl(countryKey);
geo = geo + 'Map ';
if (data.geography) {
var geography = data.geography;
if (geography.geographic_coordinates && geography.geographic_coordinates.latitude) {
var lat = geography.geographic_coordinates.latitude.degrees + '° ' + geography.geographic_coordinates.latitude.hemisphere;
var long = geography.geographic_coordinates.longitude.degrees + '° ' + geography.geographic_coordinates.longitude.hemisphere;
geo += 'Coordinates
' + lat + ', ' + long + '
';
}
if (geography.location) {
geo += 'Location
' + geography.location + '
';
}
if (geography.climate) {
geo += 'Climate
' + geography.climate + '
';
}
if (geography.terrain) {
geo += 'Terrain
' + geography.terrain + '
';
}
if (geography.overview) {
geo += 'Overview
' + geography.overview + '
';
}
}
$('#content-geography').html(geo);
// Load content: People
var people = '';
if (data.people) {
if (data.people.population && data.people.population.total) {
people += 'Population
' + numberWithCommas(data.people.population.total) + '
';
}
if (data.people.population && data.people.population.rank) {
people += 'Global Rank
' + data.people.population.global_rank + '
';
}
if (data.people.nationality && data.people.nationality.adjective) {
people += 'Nationality
' + data.people.nationality.adjective + '
';
}
if (data.people.ethnic_groups && data.people.ethnic_groups.ethnicity) {
var ethnic_groups = data.people.ethnic_groups;
people += 'Ethnic Groups
'
for (var i = 0; i < ethnic_groups.ethnicity.length; i++) {
var pct = ethnic_groups.ethnicity[i].percent;
var elem = ethnic_groups.ethnicity[i].name;
var note = ethnic_groups.ethnicity[i].note;
if (pct)
elem += " (" + pct + '%)';
if (note)
elem += " note: " + note;
if (i == 0)
people += elem;
else
people += ", " + elem;
}
people += '
';
}
if (data.people.languages && data.people.languages.language) {
var languages = data.people.languages;
people += 'Languages
'
for (var i = 0; i < languages.language.length; i++) {
var pct = languages.language[i].percent;
var elem = languages.language[i].name;
if (pct)
elem += " (" + pct + '%)';
if (i == 0)
people += elem;
else
people += ", " + elem;
}
people += '
';
}
if (data.people.religions && data.people.religions.religion) {
var religions = data.people.religions;
people += 'Religions
'
for (var i = 0; i < religions.religion.length; i++) {
var pct = religions.religion[i].percent;
var elem = religions.religion[i].name;
if (pct)
elem += " (" + pct + '%)';
if (i == 0)
people += elem;
else
people += ", " + elem;
}
people += '
';
}
if (data.people.life_expectancy_at_birth && data.people.life_expectancy_at_birth.total_population && data.people.life_expectancy_at_birth.total_population.value && data.people.life_expectancy_at_birth.total_population.units) {
people += 'Life Expectancy at Birth
' + data.people.life_expectancy_at_birth.total_population.value + ' ' + data.people.life_expectancy_at_birth.total_population.units + '
';
}
if (data.people.population_growth_rate && data.people.population_growth_rate.growth_rate && data.people.population_growth_rate.date) {
people += 'Population Growth Rate
' + data.people.population_growth_rate.growth_rate + ' (' + data.people.population_growth_rate.date + ')
';
}
if (data.people.birth_rate && data.people.birth_rate.births_per_1000_population && data.people.birth_rate.date) {
people += 'Birth Rate
' + data.people.birth_rate.births_per_1000_population + ' births per thousand (' + data.people.birth_rate.date + ')
';
}
if (data.people.death_rate && data.people.death_rate.deaths_per_1000_population && data.people.death_rate.date) {
people += 'Death Rate
' + data.people.death_rate.deaths_per_1000_population + ' deaths per thousand (' + data.people.death_rate.date + ')
';
}
if (data.people.demographic_profile) {
people += 'Demographic Profile
' + data.people.demographic_profile + '
';
}
}
$('#content-people').html(people);
// Load content: Government
var govt = '';
if (data.government) {
var government = data.government;
if (government.government_type) {
govt += 'Type
' + government.government_type + '
';
}
if (government.executive_branch && government.executive_branch.chief_of_state) {
govt += 'Chief of State
' + government.executive_branch.chief_of_state + '
';
}
if (government.executive_branch && government.executive_branch.head_of_government) {
govt += 'Head of Government
' + government.executive_branch.head_of_government + '
';
}
if (government.legal_system) {
govt += 'Legal System
' + government.legal_system + '
';
}
if (government.capital && government.capital.name) {
govt += 'Capital
' + government.capital.name + '
';
}
if (government.national_holidays) {
govt += 'National Holidays
'
for (var i = 0; i < government.national_holidays.length; i++) {
var elem = government.national_holidays[i].name + ' (' + government.national_holidays[i].day + ')';
if (i == 0)
govt += elem;
else
govt += ", " + elem;
}
govt += '
';
}
if (government.national_symbol && government.national_symbol.symbols && government.national_symbol.symbols[0].symbol) {
govt += 'National Symbol
' + government.national_symbol.symbols[0].symbol + '
';
}
if (government.national_symbol && government.national_anthem.name) {
var anthem = 'National Anthem
' + government.national_anthem.name;
if (government.national_anthem.audio_url) {
anthem += ' Listen';
}
govt += '' + anthem + '
';
}
}
$('#content-government').html(govt);
// Load content: Economy
var econ = '';
if (data.economy) {
var economy = data.economy;
if (economy.gdp && economy.gdp.official_exchange_rate && economy.gdp.official_exchange_rate.date && economy.gdp.official_exchange_rate.USD) {
econ += 'Gross Domestic Product
' + economy.gdp.official_exchange_rate.date + ': ' + numberWithCommas(economy.gdp.official_exchange_rate.USD) + ' USD
';
}
if (economy.inflation_rate && economy.inflation_rate.annual_values && economy.inflation_rate.annual_values[0].value && economy.inflation_rate.annual_values[0].units && economy.inflation_rate.annual_values[0].date) {
econ += 'Inflation Rate
' + economy.inflation_rate.annual_values[0].value + economy.inflation_rate.annual_values[0].units + ' (' + economy.inflation_rate.annual_values[0].date + ')
';
}
if (economy.exports && economy.imports.total_value && economy.imports.total_value.annual_values && economy.imports.total_value.annual_values[0]) {
econ += 'Imports
' + numberWithCommas(economy.imports.total_value.annual_values[0].value) + ' ' + economy.imports.total_value.annual_values[0].units + ' (' + economy.exports.total_value.annual_values[0].date + ')
';
}
if (economy.exports && economy.exports.total_value && economy.exports.total_value.annual_values && economy.exports.total_value.annual_values[0]) {
econ += 'Exports
' + numberWithCommas(economy.exports.total_value.annual_values[0].value) + ' ' + economy.exports.total_value.annual_values[0].units + ' (' + economy.exports.total_value.annual_values[0].date + ')
';
}
if (economy.agriculture_products && economy.agriculture_products.products) {
econ += 'Agricultural Products
'
for (var i = 0; i < economy.agriculture_products.products.length; i++) {
var elem = economy.agriculture_products.products[i];
if (i == 0)
econ += elem;
else
econ += ", " + elem;
}
econ += '
';
}
if (economy.industries && economy.industries.industries) {
econ += 'Industries
'
for (var i = 0; i < economy.industries.industries.length; i++) {
var elem = economy.industries.industries[i];
if (i == 0)
econ += elem;
else
econ += ", " + elem;
}
econ += '
';
}
if (economy.overview) {
econ += 'Overview
' + economy.overview + '
';
}
}
$('#content-economy').html(econ);
// Load content: Energy
var energy = '';
if (data.energy) {
if (data.energy.electricity && data.energy.electricity.access && data.energy.electricity.access.total_electrification) {
energy += 'Total Electrification
' + data.energy.electricity.access.total_electrification.value + '%
';
}
if (data.energy.crude_oil && data.energy.crude_oil.production && data.energy.crude_oil.production.bbl_per_day) {
energy += 'Crude Oil Production
' + numberWithCommas(parseInt(data.energy.crude_oil.production.bbl_per_day)) + ' bbl/day
';
}
if (data.energy.refined_petroleum_products && data.energy.refined_petroleum_products.production && data.energy.refined_petroleum_products.production.bbl_per_day) {
energy += 'Refined Petroleum Production
' + numberWithCommas(parseInt(data.energy.refined_petroleum_products.production.bbl_per_day)) + ' bbl/day
';
}
if (data.energy.natural_gas && data.energy.natural_gas.production.cubic_metres) {
energy += 'Natural Gas Production
' + numberWithCommas(parseInt(data.energy.natural_gas.production.cubic_metres)) + ' cubic metres
';
}
}
$('#content-energy').html(energy);
// Load content: Communications
var comm = '';
if (data.communications) {
if (data.communications.telephones) {
var telephones = data.communications.telephones;
if (telephones && telephones.fixed_lines && telephones.fixed_lines.total_subscriptions) {
comm += 'Telephone Fixed-line Subcriptions
' + numberWithCommas(telephones.fixed_lines.total_subscriptions) + '
';
}
if (telephones.mobile_cellular && telephones.mobile_cellular.total_subscriptions) {
comm += 'Mobile-Cellular Subcriptions
' + numberWithCommas(telephones.mobile_cellular.total_subscriptions) + '
';
}
}
if (data.communications.broadcast_media) {
comm += 'Broadcast Media
' + data.communications.broadcast_media + '
';
}
if (data.communications.internet) {
var internet = data.communications.internet;
if (internet.country_code) {
comm += 'Internet Country Code
' + internet.country_code + '
';
}
if (internet.users && internet.users.total && internet.users.percent_of_population) {
comm += 'Internet Users
' + numberWithCommas(internet.users.total) + ' (' + internet.users.percent_of_population + '%)
';
}
}
}
$('#content-communications').html(comm);
// Load content: Transportation
var trans = '';
if (data.transportation) {
if (data.transportation.air_transport && data.transportation.air_transport.national_system && data.transportation.air_transport.national_system.number_of_registered_air_carriers) {
trans += 'Registered Air Carriers
' + numberWithCommas(data.transportation.air_transport.national_system.number_of_registered_air_carriers) + '
';
}
if (data.transportation.air_transport && data.transportation.air_transport.national_system && data.transportation.air_transport.national_system.inventory_of_registered_aircraft_operated_by_air_carriers) {
trans += 'Registered Air Carrier Aircraft
' + numberWithCommas(data.transportation.air_transport.national_system.inventory_of_registered_aircraft_operated_by_air_carriers) + '
';
}
if (data.transportation.air_transport && data.transportation.air_transport.airports && data.transportation.air_transport.airports.total && data.transportation.air_transport.airports.total.airports) {
trans += 'Airports
' + numberWithCommas(data.transportation.air_transport.airports.total.airports) + '
';
}
if (data.transportation.railways && data.transportation.railways.total && data.transportation.railways.total.length && data.transportation.railways.total.units) {
trans += 'Railways
' + numberWithCommas(data.transportation.railways.total.length) + ' ' + data.transportation.railways.total.units + '
';
}
if (data.transportation.roadways && data.transportation.roadways.total && data.transportation.roadways.total.value && data.transportation.roadways.total.units) {
trans += 'Roadways
' + numberWithCommas(data.transportation.roadways.total.value) + ' ' + data.transportation.roadways.total.units + '
';
}
}
$('#content-transportation').html(trans);
// Load content: Military and Security
var mil = '';
if (data.military_and_security) {
if (data.military_and_security.branches && data.military_and_security.branches.by_name) {
mil += 'Military Branches
'
for (var i = 0; i < data.military_and_security.branches.by_name.length; i++) {
var elem = data.military_and_security.branches.by_name[i];
if (i == 0)
mil += elem;
else
mil += "; " + elem;
}
mil += '
';
}
if (data.military_and_security.expenditures && data.military_and_security.expenditures.annual_values) {
var expenditures = data.military_and_security.expenditures;
mil += 'Annual Expenditures
'
for (var i = 0; i < expenditures.annual_values.length; i++) {
var value = numberWithCommas(expenditures.annual_values[i].value);
var unit = expenditures.annual_values[i].units;
if (unit === 'percent_of_gdp') {
unit = '% GDP';
}
var elem = expenditures.annual_values[i].date + ': ' + value + ' ' + unit;
if (i == 0)
mil += elem;
else
mil += "
" + elem;
}
mil += '
';
}
}
$('#content-military').html(mil);
// Load content: Transnational Issues
var transnat = '';
if (data.transnational_issues) {
if (data.transnational_issues.disputes) {
transnat += 'Disputes
' + data.transnational_issues.disputes + '
';
}
if (data.transnational_issues.refugees_and_iternally_displaced_persons && data.transnational_issues.refugees_and_iternally_displaced_persons.refugees && data.transnational_issues.refugees_and_iternally_displaced_persons.refugees.by_country) {
transnat += 'Refugees by Country'
for (var i = 0; i < data.transnational_issues.refugees_and_iternally_displaced_persons.refugees.by_country.length; i++) {
var elem = data.transnational_issues.refugees_and_iternally_displaced_persons.refugees.by_country[i].country_of_origin + ' : ' + numberWithCommas(data.transnational_issues.refugees_and_iternally_displaced_persons.refugees.by_country[i].people);
transnat += elem + '
';
}
if (data.transnational_issues.refugees_and_iternally_displaced_persons.refugees.by_country.length > 0) transnat += '
';
}
if (data.transnational_issues.illicit_drugs && data.transnational_issues.illicit_drugs.note) {
transnat += '
Illicit Drugs
' + data.transnational_issues.illicit_drugs.note + '
';
}
}
$('#content-transnational').html(transnat);
$('#loading').css('visibility', 'collapse');
$("body").css("cursor", "default");
}
else {
setAllContent('No content');
}
});
}
}
// Perform a search.
function search() {
var term = $('#search-text').val();
if (!term) return;
$("body").css("cursor", "progress");
$('#loading').css('visibility', 'visible');
$('#country').val('');
$('#chart-select').val('');
$('h2').removeClass('optional');
$('#country-view').css('visibility', 'collapse');
inCountryView = false;
var url = cloud.searchUrl(term);
$.ajax({
type: 'GET',
url: url,
accepts: "json",
}).done(function (response) {
var results = cloud.resultToJson(response);
var html = '
';
var count = 0;
var countryKey = null;
if (results) {
for (var i = 0; i < results.length; i++) {
countryKey = CountryKey(results[i].name);
html += '';
if (haveFlag(results[i].name)) {
var flagImageUrl = cloud.flagImageUrl(countryKey);
html += ' | ';
}
else {
html += ' | ';
}
html += ' | ' + results[i].name + ' |
';
count++;
}
}
if (count == 0) {
html += 'No matches |
';
}
html += '
';
$('#results-list').html(html);
$('#country-flag').css('visibility', 'collapse');
$('#chart-view').css('visibility', 'collapse');
$('#results-view').css('visibility', 'visible');
$('#loading').css('visibility', 'collapse');
$("body").css("cursor", "default");
});
}
// Return to country view and auto-select a country
function selectCountry(name) {
$('#country').val(name).trigger('change');
inCountryView = true;
}
// Ensure we are in country view and not chart view.
function setCountryView() {
if (!inCountryView) {
$('#results-view').css('visibility', 'collapse');
$('#chart-view').css('visibility', 'collapse');
$('#country-view').css('visibility', 'visible');
$('#select-chart').val('');
inChartView = false;
inCountryView = true;
}
}
// Ensure we are in chart view and not country view.
function setChartView() {
if (!inChartView) {
$('#country-flag').css('visibility', 'collapse');
$('#country-view').css('visibility', 'collapse');
$('#results-view').css('visibility', 'collapse');
$('#chart-view').css('visibility', 'visible');
inCountryView = false;
inChartView = true;
}
}
function setAllContent(content) {
$('#country-flag').attr('src', content);
$('#content-introduction').html(content);
$('#content-geography').html(content);
$('#content-people').html(content);
$('#content-government').html(content);
$('#content-economy').html(content);
$('#content-energy').html(content);
$('#content-communications').html(content);
$('#content-transportation').html(content);
$('#content-military').html(content);
$('#content-transnational').html(content);
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function replace(value, oldChar, newChar) {
if (!value) return null;
return value.split(oldChar).join(newChar);
}
function CountryKey(countryName) {
var countryKey = countryName.toLowerCase();
countryKey = replace(countryKey, ' ', '_');
countryKey = replace(countryKey, '-', '_');
countryKey = replace(countryKey, '(', '');
countryKey = replace(countryKey, ')', '');
countryKey = replace(countryKey, ',', '');
countryKey = replace(countryKey, "'", '');
return countryKey;
}
function haveFlag(name) {
switch (name) {
case 'World':
case 'Antarctica':
case 'Arctic Ocean':
case 'Atlantic Ocean':
case 'Indian Ocean':
case 'Pacific Ocean':
case 'Paracel Islands':
case 'Southern Ocean':
case 'Spratly Islands':
return false;
default:
return true;
}
}
function gotoChart() {
// if chart selector is hidden, switch from search mode to chart mode
if ($('#select-chart').css('visibility') === 'collapse') {
setChartView();
$('#search-text').css('visibility', 'collapse');
$('#btn-search').css('visibility', 'collapse');
$('#btn-goto-chart').css('display', 'none');
$('#btn-goto-search').css('display', 'inline-block');
$('#select-chart').css('visibility', 'visible');
}
}
function gotoSearch() {
// if chart selector is hidden, switch from search mode to chart mode
if ($('#search-text').css('visibility') === 'collapse') {
setCountryView();
$('#select-chart').css('visibility', 'collapse');
$('#btn-search').css('visibility', 'collapse');
$('#btn-goto-search').css('display', 'none');
$('#btn-goto-chart').css('display', 'inline-block');
$('#search-text').css('visibility', 'visible');
$('#btn-search').css('visibility', 'visible');
}
}
// Chart selection changed - retrieve chart data and draw chart.
function chartSelectionChanged() {
var chartName = $('#select-chart').val();
if (!chartName) {
$('#chart_div').html('');
return;
}
if (!cloud.columnChartAvailable(chartName)) {
$('#chart_div').html('');
return;
}
var reportId = chartName;
if (!inChartView) {
setChartView();
}
$('#chart_div').html('');
$('#country').val('');
$("body").css("cursor", "progress");
$('#loading').css('visibility', 'visible');
xAxisTitle = null;
yAxisTitle = null;
yAxisTitle2 = null;
yAxisTitle3 = null;
var url = cloud.reportUrl(reportId);
// Get the report data
googleChartData = [];
var chartData = null;
$.ajax({
type: 'GET',
url: url,
accepts: "json",
}).done(function (result) {
// Convert report data to Google Charts structure and call function to draw bar chart
chartData = cloud.resultToJson(result);
googleChartData = cloud.columnChartData(reportId, chartData);
google.charts.load('current', { packages: ['corechart', 'bar'] });
google.charts.setOnLoadCallback(drawColumnChart);
});
}
// Draw column chart with Google Charts
//
// Inputs:
// googleChartData .. rows of data
// xAxisTitle ....... title of horizontal chart axis
// yAxisTitle........ title of vertical chart axis, and first series
// yAxisTitle2 ...... title of second series, or null
// yAxisTitle3 ...... title of third series, or null
function drawColumnChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', xAxisTitle);
data.addColumn('number', yAxisTitle);
if (yAxisTitle2) {
data.addColumn('number', yAxisTitle2);
}
if (yAxisTitle3) {
data.addColumn('number', yAxisTitle3);
}
data.addRows(googleChartData);
var options = {
title: chartTitle,
hAxis: {
title: xAxisTitle,
},
vAxis: {
title: yAxisTitle
},
};
var chart = new google.visualization.ColumnChart(
document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'select', function () {
selection = chart.getSelection()
var index = selection[0].row;
var name = countrySelect[index];
$('#country').val(name).trigger('change');
});
chart.draw(data, options);
$('#loading').css('visibility', 'collapse');
$("body").css("cursor", "default");
}