﻿(function () {
    // Detect if the script should be loaded over HTTPS.
    var href = /^https/.exec(document.location.protocol)
        ? 'https://subscriber.e-mark.nl/conversion/' : 'http://subscriber.e-mark.nl/';
    // Add the conversion script.
    href += 'conversion.js';

    // Create the script element to be added to your website.
    var script = document.createElement('script');
    script.type = 'text/javascript'; script.async = true; script.src = href;

    // When done, it should not run again. This is for IE7.
    var done = false;

    // The load method, which auto-converses when _mark_converse
    // object is present in the window, eg:
    // <script type="text/javascript">
    //  window._emark_converse = {action: 'Black T-Shirt', value: 49.99};
    // </script>
    // <script type="text/javascript" src="emarkconversion.js"></script>
    var load = function () {
        if (done) return;
        done = true;
        // Always create the EmarkConversion class.
        window._emark_conversion = new EmarkConversion();
        // When present, auto-converse.
        if (window._emark_converse) {
            var converse = window._emark_converse;
            window._emark_conversion.registerConversion(converse.action, converse.value);
        }
    };

    // Loading IE
    script.onreadystatechange = function () {
        if (this.readyState == 'loaded' || this.readyState == 'complete') load();
    }
    // All others
    script.onload = load;

    // Inject the script into the browser.
    document.getElementsByTagName('script')[0].parentNode.appendChild(script);
})();
