// Base configuration 
document.cookiesjsr = {
    apiUrl: '',
    configQuery: 'https://cloud.info-uvic.cat/cookiesjsr-config.json'
}

var dispatcher = {
    marketing: {
        activate: function () {
            // Do stuff to enable Matomo. See best practices below.
        },
        fallback: function () {
            // Do stuff to fallback. E.g. display layer where the benefits are explained,
            // when Matomo is enabled.
        }
    },
    analytics: {
        activate: function () {
            // Do stuff to enable Google Analytics. See best practices below.
        },
        fallback: function () {
            // Do stuff to fallback. E.g. display layer where the benefits are explained,
            // when Google Analytics is enabled.
        }
    }
}

/**
 * Entry to your custom code:
 * Catch the Event 'cookiesjsrUserConsent' that comes with an object services inside the
 * event object called with event.detail.services. It contains the current user decisions.
 *
 * This event is fired when DOM is loaded or user updates his settings.
 */
document.addEventListener('cookiesjsrUserConsent', function (event) {
    var services = (typeof event.detail.services === 'object') ? event.detail.services : {};
    for (var sid in services) {
        if (typeof dispatcher[sid] === 'object') {
            if (services[sid] === true && typeof dispatcher[sid].activate === 'function') {
                dispatcher[sid].activate();
            } else if (typeof dispatcher[sid].fallback === 'function') {
                dispatcher[sid].fallback();
            }
        }
    }
});
