{% include "api/CallPowerForm.js" with context %}

var main = function() {
  callPowerForm = new CallPowerForm('{{campaign.embed.get("form_sel","#call_form")}}', jQuery);
  {% if campaign.embed.get('script_display') == 'overlay' %}
    jQuery.getScript("{{ url_for('static', filename='embed/overlay.js', _external=True) }}");
    jQuery('head').append('<link rel="stylesheet" href="{{ url_for("static", filename="embed/overlay.css", _external=True) }}" />');
  {% endif %}
}

// from substack/semver-compare
// license MIT
function versionCmp (a, b) {
    var pa = a.split('.');
    var pb = b.split('.');
    for (var i = 0; i < 3; i++) {
        var na = Number(pa[i]);
        var nb = Number(pb[i]);
        if (na > nb) return 1;
        if (nb > na) return -1;
        if (!isNaN(na) && isNaN(nb)) return 1;
        if (isNaN(na) && !isNaN(nb)) return -1;
    }
    return 0;
};

if ((typeof jQuery == 'undefined') || (versionCmp(jQuery.fn.jquery, '1.7.0') < 0)) {
  // load jQuery from cloudflare
  var scriptElement = document.createElement("script");
  scriptElement.src = '//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js';
  scriptElement.type = "text/javascript";
  var head = document.getElementsByTagName("head")[0] || document.documentElement;
  head.appendChild(scriptElement);
  scriptElement.onload = main;
} else {
  // use in-page jQuery
  jQuery(document).ready(main);
}

{% include "api/CallPowerForm.js" with context %}

var main = function($) {
  callPowerForm = new CallPowerForm('{{campaign.embed.get("form_sel","#call_form")}}', $);
  {% if campaign.embed.get('script_display') == 'overlay' %}
    $.getScript("{{ url_for('static', filename='embed/overlay.js', _external=True) }}");
    $('head').append('<link rel="stylesheet" href="{{ url_for("static", filename="embed/overlay.css", _external=True) }}" />');
  {% endif %}
}

// from substack/semver-compare
// license MIT
function versionCmp (a, b) {
    var pa = a.split('.');
    var pb = b.split('.');
    for (var i = 0; i < 3; i++) {
        var na = Number(pa[i]);
        var nb = Number(pb[i]);
        if (na > nb) return 1;
        if (nb > na) return -1;
        if (!isNaN(na) && isNaN(nb)) return 1;
        if (isNaN(na) && !isNaN(nb)) return -1;
    }
    return 0;
};

// from https://css-tricks.com/snippets/jquery/load-jquery-only-if-not-present/
function getScript(url, success) {
    var script = document.createElement('script');
    script.src = url;
    
    var head = document.getElementsByTagName('head')[0],
    done = false;
    
    // Attach handlers for all browsers
    script.onload = script.onreadystatechange = function() {
      if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
      
      done = true;
      // callback function provided as param
      success();
      script.onload = script.onreadystatechange = null;
      head.removeChild(script);
    };
  };  
  head.appendChild(script);
};

if (typeof window.jQuery === 'undefined') {
  // load jQuery from cloudflare
  getScript('//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.js', function() {
    return main(jQuery);
  });
} else if (versionCmp(window.jQuery.fn.jquery, '1.7.0') < 0) {
  console.log('jQuery is really old', jQuery.fn.jquery);
  getScript('//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js', function() {
    // make sure new version of jQuery plays nice with existing one
    jQuery.noConflict();
    return main(jQuery);
  });
} else {
  // in-page jQuery is sufficient, carry on
  jQuery(document).ready(main);
}

rework jQuery loading to better handle existing older versions on the page