(function () {
    var server_path = 'https://iframe.continuetogive.com/'; // will be replaced with HTTPS_PATH
    var is_mobile = false; // will be replaced with MobileCore::isMobile

    if (document.querySelectorAll('link[data-name="c2g-badge-styles"]').length < 1) document.head.insertAdjacentHTML('beforeend', '<link rel="stylesheet" href="' + server_path + 'application/assets/css/badge-popup.css" data-name="c2g-badge-styles">');

    window.c2gGetCookieContent = function (name) {
        const value = '; ' + document.cookie;
        const parts = value.split('; ' + name + '=');
        if (parts.length === 2) return parts.pop().split(';').shift();
    }

    var cookie_content = window.c2gGetCookieContent('ctg_cookie_name_iframe');

    var ajax_append = typeof (cookie_content) === 'undefined' ? '' : '?__s__=' + cookie_content;
    var ajax_path = server_path + 'badges/badge_session.php' + ajax_append;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200 && this.responseText != cookie_content) {
            var dateExpireCookie = new Date();
            var numberOfDaysToAdd = 21;
            dateExpireCookie.setDate(dateExpireCookie.getDate() + numberOfDaysToAdd);
            document.cookie = 'ctg_cookie_name_iframe' + "=" + this.responseText + "; expires=" + dateExpireCookie + ";";
            cookie_content = window.c2gGetCookieContent('ctg_cookie_name_iframe');
        }
    };
    xhttp.open("POST", ajax_path, true);
    xhttp.send();

    window.newC2gBadge = function () {
        return new c2gBadge();
    }

    window.c2gBadge = function () {
    }
    c2gBadge.prototype.serverUrl = server_path;
    c2gBadge.prototype.slideUp = function (target, duration) {
        duration = duration || 500;
        target.style.transitionProperty = 'height, margin, padding';
        target.style.transitionDuration = duration + 'ms';
        target.style.boxSizing = 'border-box';
        target.style.height = target.offsetHeight + 'px';
        target.offsetHeight;
        target.style.overflow = 'hidden';
        target.style.height = 0;
        target.style.paddingTop = 0;
        target.style.paddingBottom = 0;
        target.style.marginTop = 0;
        target.style.marginBottom = 0;
        window.setTimeout(function () {
            target.style.display = 'none';
            target.style.removeProperty('height');
            target.style.removeProperty('padding-top');
            target.style.removeProperty('padding-bottom');
            target.style.removeProperty('margin-top');
            target.style.removeProperty('margin-bottom');
            target.style.removeProperty('overflow');
            target.style.removeProperty('transition-duration');
            target.style.removeProperty('transition-property');
            //alert("!");
        }, duration);
    }
    c2gBadge.prototype.slideDown = function (target, duration) {
        duration = duration || 500;
        target.style.removeProperty('display');
        var display = window.getComputedStyle(target).display;
        if (display === 'none') display = 'block';
        target.style.display = display;
        var height = target.offsetHeight;
        target.style.overflow = 'hidden';
        target.style.height = 0;
        target.style.paddingTop = 0;
        target.style.paddingBottom = 0;
        target.style.marginTop = 0;
        target.style.marginBottom = 0;
        target.offsetHeight;
        target.style.boxSizing = 'border-box';
        target.style.transitionProperty = "height, margin, padding";
        target.style.transitionDuration = duration + 'ms';
        target.style.height = height + 'px';
        target.style.removeProperty('padding-top');
        target.style.removeProperty('padding-bottom');
        target.style.removeProperty('margin-top');
        target.style.removeProperty('margin-bottom');
        window.setTimeout(function () {
            target.style.removeProperty('height');
            target.style.removeProperty('overflow');
            target.style.removeProperty('transition-duration');
            target.style.removeProperty('transition-property');
        }, duration);
    }

    c2gBadge.prototype.toUrl = function (params) {
        var reqArray = [];
        for (var index in params) {
            reqArray.push(escape(index) + '=' + escape(params[index]))
        }
        return reqArray.join('&');
    }

    c2gBadge.prototype.hide_all_continue_button = function () {
        Array.prototype.forEach.call(document.querySelectorAll('.c2g-iframe-continue-donation-parent-div'), function (el) {
            el.style.display = 'none';
        })
    }

    c2gBadge.prototype.popIn = function () {
        this.hide_all_continue_button()
        this.slideDown(this.backdrop)
        this.slideDown(this.frameContainer)
        this.slideDown(this.frame)
    }

    c2gBadge.prototype.popOut = function () {
        this.slideUp(this.backdrop)
        this.slideUp(this.frameContainer)
        this.hide_all_continue_button()
        if (this.add_floating_button) this.slideDown(this.continue_button.parentElement)
        this.slideUp(this.frame)
    }

    c2gBadge.prototype.popup = function (page, params) {
        this.isStickyButton = params.hasOwnProperty('stickyButton') ? true : false;
        
        if (this.isStickyButton) {
            this.createStickyButton(page, params.stickyButton);
        }
        
        this.add_floating_button = params.hasOwnProperty('float_custom_button') ? false : params.add_floating_button
        params.__s__ = cookie_content;
        if (Object.hasOwnProperty('button_selector') && (params.button_selector.length == false || document.querySelectorAll(params.button_selector).length === 0)) {
            var message = 'Missing or invalid property: button_selector';
            alert(message);
            throw new Error(message);
        }

        var thisBadge = this;

        if (params.custom_button){
            this.custom_button = document.getElementById(params.custom_button);
            this.custom_button.addEventListener('click',function () {
                thisBadge.popIn()
            })
            if (params.hasOwnProperty('float_custom_button')) {
                this.custom_button.classList.add('c2g-float-'+params.float_custom_button);
            }
            document.currentScript.insertAdjacentElement('afterend',this.custom_button);
        }

        this.id = String.prototype.concat('_', (Math.random() + 1).toString(36).substring(2));

        if (!document.getElementById('c2g-iframe-badge-popup-backdrop')) parent.document.body.insertAdjacentHTML('beforeend', '<div id="c2g-iframe-badge-popup-backdrop" style="display: none"></div>');
        c2gBadge.prototype.backdrop = document.getElementById('c2g-iframe-badge-popup-backdrop');

        if (this.add_floating_button){
            parent.document.body.insertAdjacentHTML('beforeend', '<div class="' + this.id + ' c2g-iframe-continue-donation-parent-div" style="display: none"><div class="' + this.id + ' c2g-iframe-badge-popup-continue-donation" style="background: ' + params.headercolor + '"><img style="margin-right: .25rem; vertical-align: middle" src="' + this.serverUrl + 'application/assets/images/givingpage-icon.png" alt="" width="32">Give</div><button type="button" class="' + this.id + ' c2g_continue_close_btn">&times;</button></div>');

            this.continue_button = document.querySelector(String.prototype.concat('.', this.id, '.c2g-iframe-badge-popup-continue-donation'));
            this.continue_button.addEventListener('click', function () {
                thisBadge.popIn()
            });

            this.close_continue_button = document.querySelector(String.prototype.concat('.', this.id, '.c2g-iframe-continue-donation-parent-div button.c2g_continue_close_btn'))
            this.close_continue_button.addEventListener('click', function () {
                thisBadge.slideUp(thisBadge.continue_button.parentElement)
            });
        }
        this.frameCloseBtn = document.createElement('button');
        this.frameCloseBtn.classList.add('c2g_popup_closeBtn');
        this.frameCloseBtn.innerText = 'Close';
        this.frameCloseBtn.type = 'button';

        this.frame = document.createElement('iframe');
        this.frame.src = String.prototype.concat(this.serverUrl, page + '/donation_prompt?', this.toUrl(params));

        this.frameBase = document.createElement('div');
        this.frameBase.classList.add('c2g-iframe-base-container');
        this.frameBase.appendChild(this.frameCloseBtn);
        this.frameBase.appendChild(this.frame);

        this.frameContainer = document.createElement('div');
        this.frameContainer.classList.add('c2g-iframe-popup-badge-div', this.id);
        this.frameContainer.appendChild(this.frameBase);

        parent.document.body.appendChild(this.frameContainer)

        this.frameCloseBtn.addEventListener('click', function () {
            thisBadge.popOut()
        });

        Array.prototype.forEach.call(document.querySelectorAll(params.button_selector), function (el) {
            el.addEventListener('click', function () {
                thisBadge.popIn()
            })
        })
    }

    c2gBadge.prototype.embed = function (page, params) {


        if (params.container_selector && params.container_selector.length && document.querySelectorAll(params.container_selector).length === 0) {
            var message = 'Invalid property: container_selector';
            alert(message);
            throw new Error(message);
        }

        params.moduleType = 'Module_Badge';
        params.pageid = page;
        params.__s__ = cookie_content;
        params.makeresponsive = params.makeresponsive || true;

        if (is_mobile) {
            params.makeresponsive = true;
            params.task = "show_donation_badge_form";
            params.type = "show_donation_badge_form";
            params.scrolling = "yes";
        }

        var appendPromptUri = params.type === '' ? page + '/donation_prompt?' : '?';

        var iframe = document.createElement('iframe');
        iframe.classList.add('c2g-frame');
        iframe.src = String.prototype.concat(this.serverUrl, appendPromptUri, this.toUrl(params));
        iframe.setAttribute('allowtransparency', 'true');
        iframe.setAttribute('scrolling', params.scrolling || 'auto');

        var frameContainer = document.createElement('div');
        frameContainer.classList.add('c2g-badge-container')

        if (params.makeresponsive === true) {
            iframe.classList.add('make-responsive')
            frameContainer.classList.add('make-responsive')
            iframe.setAttribute('height', "100%");
            iframe.setAttribute('width', "100%");
        } else if (params.type === 'show_badge' && params.size === "small") {
            iframe.setAttribute('height', "430");
            iframe.setAttribute('width', "210");
        } else if (params.type === 'show_donation_badge') {
            iframe.setAttribute('height', "735");
            iframe.setAttribute('width', "360");
        } else if (params.type === 'show_donation_badge_with_comments') {
            iframe.setAttribute('height', "735");
            iframe.setAttribute('width', "700");
        } else if (params.type === 'show_donation_badge_with_description') {
            iframe.setAttribute('height', "735");
            iframe.setAttribute('width', "700");
        } else if (params.height && params.width) {
            iframe.setAttribute('height', params.height);
            iframe.setAttribute('width', params.width);
        }

        iframe.style.border = 'none'
        if (is_mobile) {
            if (params.forceiframeonmobile === false) frameContainer.innerHTML = '<div style="width:100%; text-align:center;"><h1 style="text-align: center;">Online Giving</h1><a href="' + this.serverUrl + params.pageid + '/donation_prompt?badgegiving=true&' + this.toUrl(params) + '" class="myButton" target="blank">Click To Give</a></div>';
            else frameContainer.appendChild(iframe)
        } else {
            frameContainer.appendChild(iframe)
        }
        if (params.container_selector && document.querySelectorAll(params.container_selector).length) document.querySelector(params.container_selector).appendChild(frameContainer);
        else document.currentScript.insertAdjacentElement('afterend', frameContainer);
    }
    
    c2gBadge.prototype.createStickyButton = function (page, params) {
        var customized_sticky_button = document.createElement("Button");
        var alignment = params.alignment;
        var position = params.position;
        var borderRadius = params.borderRadius;
        var hPadding = params.hPadding;
        var vPadding = params.vPadding;
        var topColor = params.backgroundTop;
        var btmColor = params.backgroundBottom;
        var buttonText = params.buttonText;
        var borderColor = params.borderColor;
        var borderSize = params.borderSize;
        var fontColor = params.fontColor;
        var fontFamily = params.fontFamily;
        var fontSize = params.fontSize;

        customized_sticky_button.innerHTML = '<span class="c2g-sticky-button-span" style="display: inline-block;"><svg class="c2g-transform-scale" viewBox="0 0 32 29.6"><path fill="currentColor" d="M23.6,0c-3.4,0-6.3,2.7-7.6,5.6C14.7,2.7,11.8,0,8.4,0C3.8,0,0,3.8,0,8.4c0,9.4,9.5,11.9,16,21.2 c6.1-9.3,16-12.1,16-21.2C32,3.8,28.2,0,23.6,0z"/></svg>&nbsp;&nbsp;'+buttonText+'</span><span class="c2g-ripple"></span>';
        customized_sticky_button.style.removeProperty('left');
        customized_sticky_button.style.removeProperty('top');
        customized_sticky_button.style.removeProperty('bottom');
        customized_sticky_button.style.removeProperty('right');
        customized_sticky_button.style.removeProperty('transform');
        customized_sticky_button.style.removeProperty('-ms-transform');
        customized_sticky_button.style.removeProperty('-moz-transform');
        customized_sticky_button.style.removeProperty('-webkit-transform');
        customized_sticky_button.style.removeProperty('-o-transform');
        customized_sticky_button.style.removeProperty('transform-origin');
        customized_sticky_button.getElementsByClassName('c2g-sticky-button-span')[0].style.removeProperty('transform');
        customized_sticky_button.style.removeProperty('border-top-right-radius');
        customized_sticky_button.style.removeProperty('border-top-left-radius');
        customized_sticky_button.style.removeProperty('border-bottom-right-radius');
        customized_sticky_button.style.removeProperty('border-bottom-left-radius');
        customized_sticky_button.style.removeProperty('padding-left');
        customized_sticky_button.style.removeProperty('padding-right');
        customized_sticky_button.style.removeProperty('padding-top');
        customized_sticky_button.style.removeProperty('padding-bottom');
        customized_sticky_button.style.setProperty('border-color', borderColor);
        customized_sticky_button.style.setProperty('border', borderSize+'px');
        customized_sticky_button.style.setProperty('color', fontColor);
        customized_sticky_button.style.setProperty('font-family', fontFamily);
        customized_sticky_button.style.setProperty('font-size', fontSize+'px');
        customized_sticky_button.style.setProperty('cursor', 'pointer');
        customized_sticky_button.style.setProperty('position', 'fixed');
        customized_sticky_button.style.setProperty('overflow', 'hidden');
        customized_sticky_button.style.setProperty('z-index', '2147483640');
        customized_sticky_button.getElementsByClassName('c2g-sticky-button-span')[0].getElementsByClassName('c2g-transform-scale')[0].style.width = fontSize+'px';

        switch(alignment) {
            case 'top':
                switch(position) {
                    case 'left':
                        customized_sticky_button.style.setProperty('left', '5em');
                        customized_sticky_button.style.setProperty('top', 0);
                        break;
                    case 'center':
                        customized_sticky_button.style.setProperty('left', 'calc(50% - 3em)');
                        customized_sticky_button.style.setProperty('top', 0);
                        break;
                    case 'right':
                        customized_sticky_button.style.setProperty('top', 0);
                        customized_sticky_button.style.setProperty('right', '5em');
                        break;
                }
                customized_sticky_button.style.setProperty('border-bottom-right-radius', borderRadius+'px');
                customized_sticky_button.style.setProperty('border-bottom-left-radius', borderRadius+'px');
                customized_sticky_button.style.setProperty('padding-left', hPadding+'px');
                customized_sticky_button.style.setProperty('padding-right', hPadding+'px');
                customized_sticky_button.style.setProperty('padding-top', vPadding+'px');
                customized_sticky_button.style.setProperty('padding-bottom', vPadding+'px');
                customized_sticky_button.style.setProperty('background', 'linear-gradient(' + btmColor + ', ' + topColor + ')');
                break;
            case 'left':
                switch(position) {
                    case 'top':
                        customized_sticky_button.style.setProperty('left', 0);
                        customized_sticky_button.style.setProperty('top', '10em');
                        customized_sticky_button.style.setProperty('transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-ms-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-moz-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-webkit-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-o-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('transform-origin', 'bottom left');
                        customized_sticky_button.getElementsByClassName('c2g-sticky-button-span')[0].style.setProperty('transform', 'rotate(180deg)');
                        customized_sticky_button.style.setProperty('border-top-right-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('border-top-left-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('background', 'linear-gradient(' + topColor + ', ' + btmColor + ')');
                        break;
                    case 'center':
                        customized_sticky_button.style.setProperty('left', 0);
                        customized_sticky_button.style.setProperty('top', 'calc(50% - 8em)');
                        customized_sticky_button.style.setProperty('transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-ms-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-moz-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-webkit-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-o-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('transform-origin', 'bottom left');
                        customized_sticky_button.getElementsByClassName('c2g-sticky-button-span')[0].style.setProperty('transform', 'rotate(180deg)');
                        customized_sticky_button.style.setProperty('border-top-right-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('border-top-left-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('background', 'linear-gradient(' + topColor + ', ' + btmColor + ')');
                        break;
                    case 'bottom':
                        customized_sticky_button.style.setProperty('left', 0);
                        customized_sticky_button.style.setProperty('bottom', '10em');
                        customized_sticky_button.style.setProperty('transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-ms-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-moz-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-webkit-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-o-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('transform-origin', 'top left');
                        customized_sticky_button.style.setProperty('border-bottom-right-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('border-bottom-left-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('background', 'linear-gradient(' + btmColor + ', ' + topColor + ')');
                        break;
                }
                customized_sticky_button.style.setProperty('padding-left', vPadding+'px');
                customized_sticky_button.style.setProperty('padding-right', vPadding+'px');
                customized_sticky_button.style.setProperty('padding-top', hPadding+'px');
                customized_sticky_button.style.setProperty('padding-bottom', hPadding+'px');
                break;
            case 'right':
                switch(position) {
                    case 'top':
                        customized_sticky_button.style.setProperty('right', 0);
                        customized_sticky_button.style.setProperty('top', '10em');
                        customized_sticky_button.style.setProperty('transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-ms-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-moz-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-webkit-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-o-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('transform-origin', 'bottom right');
                        customized_sticky_button.style.setProperty('border-top-right-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('border-top-left-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('background', 'linear-gradient(' + btmColor + ', ' + topColor + ')');
                        break;
                    case 'center':
                        customized_sticky_button.style.setProperty('right', 0);
                        customized_sticky_button.style.setProperty('top', 'calc(50% - 8em)');
                        customized_sticky_button.style.setProperty('transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-ms-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-moz-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-webkit-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('-o-transform', 'rotate(-90deg)');
                        customized_sticky_button.style.setProperty('transform-origin', 'bottom right');
                        customized_sticky_button.style.setProperty('border-top-right-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('border-top-left-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('background', 'linear-gradient(' + btmColor + ', ' + topColor + ')');
                        break;
                    case 'bottom':
                        customized_sticky_button.style.setProperty('right', 0);
                        customized_sticky_button.style.setProperty('bottom', '10em');
                        customized_sticky_button.style.setProperty('transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-ms-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-moz-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-webkit-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('-o-transform', 'rotate(90deg)');
                        customized_sticky_button.style.setProperty('transform-origin', 'top right');
                        customized_sticky_button.getElementsByClassName('c2g-sticky-button-span')[0].style.setProperty('transform', 'rotate(180deg)');
                        customized_sticky_button.style.setProperty('border-bottom-right-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('border-bottom-left-radius', borderRadius+'px');
                        customized_sticky_button.style.setProperty('background', 'linear-gradient(' + topColor + ', ' + btmColor + ')');
                        break;
                }
                customized_sticky_button.style.setProperty('padding-left', vPadding+'px');
                customized_sticky_button.style.setProperty('padding-right', vPadding+'px');
                customized_sticky_button.style.setProperty('padding-top', hPadding+'px');
                customized_sticky_button.style.setProperty('padding-bottom', hPadding+'px');
                break;
            case 'bottom':
                switch(position) {
                    case 'left':
                        customized_sticky_button.style.setProperty('left', '5em');
                        customized_sticky_button.style.setProperty('bottom', 0);
                        break;
                    case 'center':
                        customized_sticky_button.style.setProperty('left', 'calc(50% - 3em)');
                        customized_sticky_button.style.setProperty('bottom', 0);
                        break;
                    case 'right':
                        customized_sticky_button.style.setProperty('bottom', 0);
                        customized_sticky_button.style.setProperty('right', '5em');
                        break;
                }
                customized_sticky_button.style.setProperty('border-top-right-radius', borderRadius+'px');
                customized_sticky_button.style.setProperty('border-top-left-radius', borderRadius+'px');
                customized_sticky_button.style.setProperty('padding-left', hPadding+'px');
                customized_sticky_button.style.setProperty('padding-right', hPadding+'px');
                customized_sticky_button.style.setProperty('padding-top', vPadding+'px');
                customized_sticky_button.style.setProperty('padding-bottom', vPadding+'px');
                customized_sticky_button.style.setProperty('background', 'linear-gradient(' + btmColor + ', ' + topColor + ')');
                break;
        }
        
        var thisBadge = this;
        
        customized_sticky_button.addEventListener('click', function () {
            thisBadge.popIn();
        });
        
        document.body.appendChild(customized_sticky_button);
    }
})();