
// Based on http://alexmarandon.com/articles/web_widget_jquery/
// TODO can be removed soon, as the user widget is deprecated

(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.11.3') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "https://zonnepanelendelen.nl/static/jquery/dist/jquery.min.f03e5a3bf534.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();
}

/******** Our main function ********/
function main() {
    console.warn("--------------------DEPRECATED--------------------\nWe will remove support for this widget soon. Stay tuned for a new and better version!")
    jQuery(document).ready(function($) {
        window.odometerOptions = {
            format: '(d),dd',
            duration: 10000,
            auto: 'false',
            theme: 'zonnepanelendelen'
        };

        /******* Load CSS and JS *******/
        var css_link = $("<link>", {
            rel: "stylesheet",
            type: "text/css",
            href: "https://zonnepanelendelen.nl/static/css/odometer-mijnstroom.237de0226772.css"
        });
        css_link.appendTo('head');
        var js_link = $("<script>", {
            type: "text/javascript",
            src: "https://zonnepanelendelen.nl/static/js/odometer.b6857db7af69.js"
        });
        js_link.appendTo('head');


        /******* Load HTML *******/
        var container = $('#mijnstroom-production-widget');
        var user = container.data('user');
        if (!user) user = '';
        var jsonp_url = "https://zonnepanelendelen.nl/widgets/v1/widget/production/client/" + user + "?callback=?";
        $.getJSON(jsonp_url, function(data) {
            container.html(data.html);
        });
    });
}

})(); // We call our anonymous function immediately
