// Creare's 'Implied Consent' EU Cookie Law Banner v:2.4
// Conceived by Robert Kent, James Bavington & Tom Foyster
// Modified by Simon Freytag for syntax, namespace, jQuery and Bootstrap

C = {
    // Number of days before the cookie expires, and the banner reappears
    cookieDuration : 14,

    // Name of our cookie
    cookieName: 'complianceCookie',

    // Value of cookie
    cookieValue: 'on',

    // Message banner title
    bannerTitle: "<p class="+'"cookie-js-title"'+">Cookie Policy:</p>",

    // Message banner message
    bannerMessage: "<p class="+'"cookie-js-content"'+">"+('In our recent Privacy Policy you will find out how we take care of your privacy data. Please note that our website uses cookies to ensure the best user experience. These files provides us with information about users page habits, but do not store personal information. Click on continue botton without changing your settings, we thank you for helping us and contributing to the use of cookies. You can change your decision any time with change your cookie settings in your browser.').replace(/"/g, '\\"')+"</p>",

    // Message banner dismiss button
    bannerButton: "<p class="+'"cookie-js-button"'+">Allow</p>",

    // Link to your cookie policy.
    bannerLinkURL: "/privacy-policy/",

    // Link text
    bannerLinkText: "More details.",

    // Text alignment
    alertAlign: "center",
    
    // Link text
    buttonClass: "btn-success btn-xs",    

    createDiv: function () {
        var banner = $(
            '<div class="TCCookieConsetDiv alert alert-success alert-dismissible text-'+
             this.alertAlign +' fade in" ' +
            'role="alert" style="position: fixed; bottom: 0; width: 100%; ' +
            'margin-bottom: 0"><strong>' + this.bannerTitle + '</strong> ' +
            this.bannerMessage + '<p class="cookie-js-utltext"> <a href="' + this.bannerLinkURL + '">' +
            this.bannerLinkText + '</a></p> <button type="button" class="btn ' +
             this.buttonClass + '" onclick="C.createCookie(C.cookieName, C.cookieValue' +
            ', C.cookieDuration)" data-dismiss="alert" aria-label="Close">' +
            this.bannerButton + '</button></div>'
        )
        $("body").append(banner)
    },

    createCookie: function(name, value, days) {
        //console.log("Create cookie")
        var expires = ""
        if (days) {
            var date = new Date()
            date.setTime(date.getTime() + (days*24*60*60*1000))
            expires = "; expires=" + date.toGMTString()
        }
        document.cookie = name + "=" + value + expires + "; path=/";

        console.log('setcookie');

        gtag('consent', 'update', {
          'ad_user_data': 'granted',
          'ad_personalization': 'granted'
        });
    },

    checkCookie: function(name) {
        var nameEQ = name + "="
        var ca = document.cookie.split(';')
        for(var i = 0; i < ca.length; i++) {
            var c = ca[i]
            while (c.charAt(0)==' ')
                c = c.substring(1, c.length)
            if (c.indexOf(nameEQ) == 0) 
                return c.substring(nameEQ.length, c.length)
        }
        return null
    },

    init: function() {
        if (this.checkCookie(this.cookieName) != this.cookieValue)
            this.createDiv()
    }
}

$(document).ready(function() {
    C.init()
})