(function() {
var jQuery;
var widgetContainer;
var jsonp_url = "http://app.plant-for-the-planet.org/treecounter-widget/widget?callback=?";
var minRefresh = '30';
var refresh = '0';
var theme = 'default';
var mode = 'default';
var apiKey = '';
var timeOut = 0;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src", "https://code.jquery.com/jquery-1.11.3.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main();
}
/******** Update tree values ******/
function updateData() {
var data = widgetContainer.data();
data.op = 'update';
data.apiKey = apiKey;
jQuery.getJSON(jsonp_url, data, function(data) {
jQuery('.contribution .target', widgetContainer).html(data.target);
jQuery('.contribution .planted', widgetContainer).html(data.planted);
jQuery('.contribution .pledged', widgetContainer).html(data.pledged);
jQuery('.contribution .community', widgetContainer).html(data.community);
if(timeOut > 0) {
setTimeout(updateData, timeOut);
}
});
}
/******** Our main function ********/
function main() {
jQuery(document).ready(function($) {
widgetContainer = $('#pftp-treecounter-widget');
if(widgetContainer.length == 0) {
warning("widget container '#pftp-treecounter-widget' could be found on this page.");
return;
}
if(widgetContainer.data('api-key') == '') {
warning("no api-key data provided in widget container");
return;
}
if(widgetContainer.data('api-key')) { apiKey = widgetContainer.data('api-key'); }
if(widgetContainer.data('refresh')) { refresh = widgetContainer.data('refresh'); }
if(widgetContainer.data('theme')) { theme = widgetContainer.data('theme'); }
if(widgetContainer.data('mode')) { mode = widgetContainer.data('mode'); }
///******* Load CSS *******/
var css_link = $("", {
rel: "stylesheet",
type: "text/css",
href: "https://app.plant-for-the-planet.org/css/widget/themes/" + theme + ".css"
});
css_link.appendTo('head');
if(refresh > 0) {
timeOut = Math.max(minRefresh * 1000, refresh * 1000);
}
var data = widgetContainer.data();
data.op = 'render';
data.apiKey = apiKey;
data.theme = theme;
data.mode = mode;
///******* Load HTML *******/
$.getJSON(jsonp_url, data, function(data) {
widgetContainer.html(data.html);
setTimeout(updateData, timeOut);
});
function warning(text)
{
if(console) console.log(text);
}
});
}
})(); // We call our anonymous function immediately