// source --> https://medicarama.com/wp-content/plugins/edd-add-to-cart-popup/assets/js/jquery.bpopup.min.js?ver=99.1.3 
/*================================================================================
 * @name: bPopup - if you can't get it up, use bPopup
 * @author: (c)Bjoern Klinggaard (twitter@bklinggaard)
 * @demo: http://dinbror.dk/bpopup
 * @version: 0.11.0.min
 ================================================================================*/
 (function(c){c.fn.bPopup=function(A,E){function L(){a.contentContainer=c(a.contentContainer||b);switch(a.content){case "iframe":var d=c('<iframe class="b-iframe" '+a.iframeAttr+"></iframe>");d.appendTo(a.contentContainer);t=b.outerHeight(!0);u=b.outerWidth(!0);B();d.attr("src",a.loadUrl);l(a.loadCallback);break;case "image":B();c("<img />").load(function(){l(a.loadCallback);F(c(this))}).attr("src",a.loadUrl).hide().appendTo(a.contentContainer);break;default:B(),c('<div class="b-ajax-wrapper"></div>').load(a.loadUrl,a.loadData,function(d,b,e){l(a.loadCallback,b);F(c(this))}).hide().appendTo(a.contentContainer)}}function B(){a.modal&&c('<div class="b-modal '+e+'"></div>').css({backgroundColor:a.modalColor,position:"fixed",top:0,right:0,bottom:0,left:0,opacity:0,zIndex:a.zIndex+v}).appendTo(a.appendTo).fadeTo(a.speed,a.opacity);C();b.data("bPopup",a).data("id",e).css({left:"slideIn"==a.transition||"slideBack"==a.transition?"slideBack"==a.transition?f.scrollLeft()+w:-1*(x+u):m(!(!a.follow[0]&&n||g)),position:a.positionStyle||"absolute",top:"slideDown"==a.transition||"slideUp"==a.transition?"slideUp"==a.transition?f.scrollTop()+y:z+-1*t:p(!(!a.follow[1]&&q||g)),"z-index":a.zIndex+v+1}).each(function(){a.appending&&c(this).appendTo(a.appendTo)});G(!0)}function r(){a.modal&&c(".b-modal."+b.data("id")).fadeTo(a.speed,0,function(){c(this).remove()});a.scrollBar||c("html").css("overflow","auto");c(".b-modal."+e).unbind("click");f.unbind("keydown."+e);k.unbind("."+e).data("bPopup",0<k.data("bPopup")-1?k.data("bPopup")-1:null);b.undelegate(".bClose, ."+a.closeClass,"click."+e,r).data("bPopup",null);clearTimeout(H);G();return!1}function I(d){y=k.height();w=k.width();h=D();if(h.x||h.y)clearTimeout(J),J=setTimeout(function(){C();d=d||a.followSpeed;var e={};h.x&&(e.left=a.follow[0]?m(!0):"auto");h.y&&(e.top=a.follow[1]?p(!0):"auto");b.dequeue().each(function(){g?c(this).css({left:x,top:z}):c(this).animate(e,d,a.followEasing)})},50)}function F(d){var c=d.width(),e=d.height(),f={};a.contentContainer.css({height:e,width:c});e>=b.height()&&(f.height=b.height());c>=b.width()&&(f.width=b.width());t=b.outerHeight(!0);u=b.outerWidth(!0);C();a.contentContainer.css({height:"auto",width:"auto"});f.left=m(!(!a.follow[0]&&n||g));f.top=p(!(!a.follow[1]&&q||g));b.animate(f,250,function(){d.show();h=D()})}function M(){k.data("bPopup",v);b.delegate(".bClose, ."+a.closeClass,"click."+e,r);a.modalClose&&c(".b-modal."+e).css("cursor","pointer").bind("click",r);N||!a.follow[0]&&!a.follow[1]||k.bind("scroll."+e,function(){if(h.x||h.y){var d={};h.x&&(d.left=a.follow[0]?m(!g):"auto");h.y&&(d.top=a.follow[1]?p(!g):"auto");b.dequeue().animate(d,a.followSpeed,a.followEasing)}}).bind("resize."+e,function(){I()});a.escClose&&f.bind("keydown."+e,function(a){27==a.which&&r()})}function G(d){function c(e){b.css({display:"block",opacity:1}).animate(e,a.speed,a.easing,function(){K(d)})}switch(d?a.transition:a.transitionClose||a.transition){case "slideIn":c({left:d?m(!(!a.follow[0]&&n||g)):f.scrollLeft()-(u||b.outerWidth(!0))-200});break;case "slideBack":c({left:d?m(!(!a.follow[0]&&n||g)):f.scrollLeft()+w+200});break;case "slideDown":c({top:d?p(!(!a.follow[1]&&q||g)):f.scrollTop()-(t||b.outerHeight(!0))-200});break;case "slideUp":c({top:d?p(!(!a.follow[1]&&q||g)):f.scrollTop()+y+200});break;default:b.stop().fadeTo(a.speed,d?1:0,function(){K(d)})}}function K(d){d?(M(),l(E),a.autoClose&&(H=setTimeout(r,a.autoClose))):(b.hide(),l(a.onClose),a.loadUrl&&(a.contentContainer.empty(),b.css({height:"auto",width:"auto"})))}function m(a){return a?x+f.scrollLeft():x}function p(a){return a?z+f.scrollTop():z}function l(a,e){c.isFunction(a)&&a.call(b,e)}function C(){z=q?a.position[1]:Math.max(0,(y-b.outerHeight(!0))/2-a.amsl);x=n?a.position[0]:(w-b.outerWidth(!0))/2;h=D()}function D(){return{x:w>b.outerWidth(!0),y:y>b.outerHeight(!0)}}c.isFunction(A)&&(E=A,A=null);var a=c.extend({},c.fn.bPopup.defaults,A);a.scrollBar||c("html").css("overflow","hidden");var b=this,f=c(document),k=c(window),y=k.height(),w=k.width(),N=/OS 6(_\d)+/i.test(navigator.userAgent),v=0,e,h,q,n,g,z,x,t,u,J,H;b.close=function(){r()};b.reposition=function(a){I(a)};return b.each(function(){c(this).data("bPopup")||(l(a.onOpen),v=(k.data("bPopup")||0)+1,e="__b-popup"+v+"__",q="auto"!==a.position[1],n="auto"!==a.position[0],g="fixed"===a.positionStyle,t=b.outerHeight(!0),u=b.outerWidth(!0),a.loadUrl?L():B())})};c.fn.bPopup.defaults={amsl:50,appending:!0,appendTo:"body",autoClose:!1,closeClass:"b-close",content:"ajax",contentContainer:!1,easing:"swing",escClose:!0,follow:[!0,!0],followEasing:"swing",followSpeed:500,iframeAttr:'scrolling="no" frameborder="0"',loadCallback:!1,loadData:!1,loadUrl:!1,modal:!0,modalClose:!0,modalColor:"#000",onClose:!1,onOpen:!1,opacity:.7,position:["auto","auto"],positionStyle:"absolute",scrollBar:!0,speed:250,transition:"fadeIn",transitionClose:!1,zIndex:9997}})(jQuery);
// source --> https://medicarama.com/wp-content/plugins/edd-add-to-cart-popup/assets/js/edd-acp.js?ver=99.1.3 
var EddAcp = (function EddAcpClass() {

    EddAcp.prototype = Object.create(Object.prototype);

    var $ = jQuery;

    function EddAcp(element, nobind) {
        this.element = element;
        this.testing = false;
        this.initElems();
        nobind = (typeof nobind === 'undefined') ? false : true;
        if (!nobind) {
            this.initEvents();
        }
    }

    EddAcp.prototype.initElems = function () {
        // Get purchase elements
        this.eddPurchaseWrapper = this.element.find('> .edd_purchase_submit_wrapper');
        this.eddPurchaseButton = this.eddPurchaseWrapper.find('> .edd-add-to-cart');
        // Get the popup element
        this.popup = this.element.parent().find('.edd-acp-popup');
        // Get the item name
        this.itemName = this.popup.find('.edd-acp-item-name').val();
        // Get the variable price option element, if available
        this.priceOptions = this.element.find('div.edd_price_options');
        // Set the url of the "continue to checkout button" to the checkout page
        this.popup.find('a.edd-acp-goto-checkout').attr('href', window.edd_scripts ? edd_scripts.checkout_page : '#');

        return this;
    };

    EddAcp.prototype.initEvents = function () {
        this.eddPurchaseButton.click(this.onPurchaseClick.bind(this));
        $(document.body).on('edd_cart_item_added', this.onCartAdded.bind(this));
        return this;
    };

    EddAcp.prototype.onPurchaseClick = function (event) {
        if (this.eddPurchaseButton.is('.edd-free-download')) {
            return;
        }
        if (this.element.find('.edd_action_input').val() !== 'add_to_cart') {
            return;
        }
        // Item name to show on popup
        var name = this.itemName;
        // Get the default quantity field
        var eddQtyField = this.element.find('.edd-item-quantity');
        var singleQty = eddQtyField.length > 0 ? parseInt(eddQtyField.val()) : 0;
        // Get selected price options
        var priceOptions = this.getSelectedPriceOption();
        var numItemsSelected = priceOptions.length > 0 ? priceOptions.length : 1;
        // If no selection, or variable pricing disabled, skip the below block
        if (priceOptions.length > 0) {
            // Use hyphen between item name and selected options
            name += ' - ';
            // Put selections in array, to join by comma later
            var optionStrings = [];
            for (var i in priceOptions) {
                // If the quantity is 1 and only one option is selected, do not show quantity
                var qtyStr = '';
                if (priceOptions[i].qty > 1 || priceOptions.length > 1) {
                    qtyStr = ' x' + priceOptions[i].qty;
                } else if (eddQtyField.length > 0 && singleQty > 1) {
                    qtyStr = ' x' + singleQty;
                }
                // Generate string for selected price option and add to array
                var optionString = ' ' + priceOptions[i].name + qtyStr;
                optionStrings.push(optionString);
            }
            // Join options by comma and add to item name
            name += optionStrings.join(', ');
        } else if (singleQty > 1) {
            name += ' - x' + singleQty;
        }
        // Set the item name on popup element
        this.popup.find('strong.item-name').text(name);
        // Hide both singular and plural texts
        this.popup.find('div.edd-acp-popup-singular, div.edd-acp-popup-plural').hide();

        if (numItemsSelected > 1 || singleQty > 1) {
            this.popup.find('div.edd-acp-popup-plural').show();
        } else {
            this.popup.find('div.edd-acp-popup-singular').show();
        }

        // Set a flag to show the popup once the AJAX request succeeds
        this.waitingForCart = true;

        // Show the popup immediately but in loading state
        this.showPopup();
        this.popup.addClass('edd-acp-is-loading');
        this.wantsToCheckout = false; // Reset flag
        
        var $checkoutBtn = this.popup.find('.edd-acp-checkout-btn');
        if (!$checkoutBtn.data('original-text')) {
            $checkoutBtn.data('original-text', $checkoutBtn.html());
        }
        
        // Capture click even during loading
        var self = this;
        this.popup.find('a.edd-acp-goto-checkout').off('click.acp-wait').on('click.acp-wait', function(e) {
            if (self.waitingForCart) {
                e.preventDefault();
                self.wantsToCheckout = true;
            }
        });

        if (this.testing) {
            event.stopPropagation();
            event.preventDefault();
        }
    };

    EddAcp.prototype.onCartAdded = function (event, response, post_data) {
        if (this.waitingForCart) {
            this.popup.removeClass('edd-acp-is-loading');
            this.popup.find('a.edd-acp-goto-checkout').off('click.acp-wait');
            this.waitingForCart = false;

            // If user already clicked checkout, redirect now
            if (this.wantsToCheckout) {
                window.location.href = window.edd_scripts ? edd_scripts.checkout_page : '#';
            }
        }
    };

    EddAcp.prototype.showPopup = function() {
        // Hide popup
        this.popup.css({ visibility: 'hidden' });
        // Initialize the popup
        this.popup.bPopup({
            positionStyle: 'fixed',
            speed: 100,
            followSpeed: 1,
            closeClass: 'edd-acp-close-popup',
            position: ['auto', 'auto']
        });
        // Show popup
        this.popup.css({ visibility: 'visible' });
    };

    EddAcp.prototype.getSelectedPriceOption = function () {
        if (this.priceOptions.length === 0) {
            return [];
        }
        var selected = [];
        // For each price option
        this.priceOptions.find('> ul > li').each(function (i, l) {
            // Get the label and quantity wrapper
            var label = $(l).find('> label');
            var qtyWrapper = $(l).find('> div.edd_download_quantity_wrapper');
            // If the option is selected
            if (label.find('input').is('[type="checkbox"]:checked, [type="radio"]:checked')) {
                // Get the option name and entered quantity
                var name = label.find('.edd_price_option_name').text();
                var qty = qtyWrapper.length === 0
                    ? 1
                    : parseInt(qtyWrapper.find('input.edd-item-quantity').val());
                // Add selection
                selected.push({
                    name: name,
                    qty: qty
                });
            }
        });
        console.log(selected);
        return selected;
    };

    return EddAcp;
}());

jQuery(document).ready(function () {
    window.edd_acp = {};
    // Instances array
    window.edd_acp.instances = [];
    // Go through each download and init instance
    jQuery('form.edd_download_purchase_form').each(function () {
        window.edd_acp.instances.push(new EddAcp(jQuery(this)));
    });
});