var Wick = (window.Wick = window.Wick || {});

Wick.BigWinsProto = (function () {
    var bwVar = {
        $body: $('body'),
        $bigWins: $('.bw'),
        $bwActions: $('.bw_act'),
        $bwChosen: $('.bw__chosen'),
        $pdpPrice: $('.pdp-price'),
        $defStoreWrap: $('.my-account_default_store'),
        $tiTotal: $('.ti__total'),
        $stock: $('.stock'),
        $stockTabs: $('.stock__tabs'),
        $stockTab: $('.stock__tab'),
        $defStockTab: $('.def-stock__tab'),
        $stockInfo: $('.stock__info'),
        $stockAccordion: $('.stock__accordion'),
        $stockContent: $('.stock__content'),
        $stockMore: $('.stock-more'),
        $stockResult: $('.stock__result'),
        $stockFind: $('.stock__find'),
        $stockFindBtn: $('.stock__find_btn'),
        $stockEditBtn: $('.stock__edit_btn'),
        $stockQuestions: $('.stock__quest'),
        $stockRadio: $('.stock__radio label'),
        $stockQuantity: $('.tbx_quantity'),
        $stockPostcode: $('.stock__postcode input'),
        $stockPostcodeEl: $('.stock__postcode'),
        $stockAmount: $('.stock__amount-val'),
        $stockDescr: $('.stock__descr'),
        $stockPost: $('.stock__descr_post'),
        $stockClose: $('.stock__close '),
        $stockCancel: $('.stock__cancel-text'),
        $storeMoreLink: $('.store__more a'),
        $sDetails: $('.s-details'),
        $sDetailsClose: $('.s-details__close'),
        $sDetailsToggleMap: $('.s-details__tgl-map'),
        $sDetailsToggleMapText: $('.s-details__tgl-map_text'),
        $sDetailsMap: $('.s-details__map'),
        $sDetailsCopy: $('.s-details__copy'),
        $sDetailsAddress: $('.s-details__address'),
        $chosen: $('.chosen'),
        $protoOnlyPopulated: $('.proto-only-populated'),
        $storeState: $('.store-state'),
        $makeDefault: $('.default-store__make'),
        $nameDefault: $('.default-store__name'),
        $defaultCurrent: $('.default-store__default'),
        $defaultInfo: $('.default-store__det'),
        $store: $('.store'),
        $chosenItem: $('.chosen__item'),
        $storeCount: $('.store__count'),
        $stockList: $('.stock__list'),
        $stockTarget: $('.stock__target'),
        $storeSearch: $('.store-search'),
        $storeChange: $('.store-change'),
        $addStore: $('.my-account_add-store'),
        $addForDelivery: $('.btn-add-to-basket'),
        $addClickAndCollect: $('.nearest-store-purchasable'),
        $cancelStore: $('.my-account_cancel-store'),
        $toDefaultIcon: $('.store__tick'),
        $isDefaultIcon: $('.store__marker'),
        $currentDefault: $('.default-store__det__current'),
        $findStore: $('.my-account_find-store'),
        $postCodeErrorMsg: $('.stock__postcode_invalid-msg'),
        $ccOrder: $('.cc-order'),
        storeState: 'store__state',
        storeCount: '.store__count',
        ccBtn: '.bw__cc-btn',
        pdpPriceBW: 'pdp-price_bw',
        tiTotalBW: 'ti__total_bw',
        stockTab: '.stock__tab',
        active: 'active',
        stockMore: 'stock-more',
        stockItem: '.stock__item',
        stockContentNearest: 'stock__content_nearest',
        stockContentMore: 'stock__content_more',
        stockResultHide: 'stock__result_hide',
        stockResultSuccess: 'stock__result_success',
        stockTabsHide: 'stock__tabs_hide',
        stockFindHide: 'stock__find_hide',
        stockFindBtn: '.stock__find_btn',
        stockQuestionInvalid: 'stock__quest_invalid',
        stockDescrC: 'stock__descr_c',
        stockDescrS: 'stock__descr_s',
        stockPostcodeInvalid: 'stock__postcode_invalid',
        stockDetailsOpen: 'stock__det-open',
        stockBot: 'stock__bot',
        pdpPriceDetailsOpen: 'pdp-price__det-open',
        sDetailsHide: 's-details_hide',
        sDetails: '.s-details',
        sDetailsStoreName: '.s-details__s-name',
        sDetailsInfo: '.s-details__info',
        sDetailsMapHide: 's-details__map_hide',
        sDetailsAddress: '.s-details__address',
        chosenHide: 'chosen_hide',
        chosenNearest: 'chosen__nearest',
        chosenSuccess: 'chosen__success',
        chosenError: 'chosen__error',
        store: '.store',
        storeName: '.store__name-text',
        defaultStoreName: '.default-store__name-txt',
        collapsed: 'collapsed',
        show: 'show',
        scrollDisable: 'scroll-disable',
        bwMinHeight: 'bw_min-h',
        copyLinkHovered: 'copy-link_hovered',
        protoOnlyPopulated: 'proto-only-populated',
        moreStoresPresent: 'more-stores-present',
        defStoresPresent: 'def-stores-present',
        CLICK_AND_COLLECT: 'FREE Click & Collect within 30 minutes',
        CLICK_AND_COLLECT_NOT_AVAILABLE: 'Click & Collect not available',
    };

    function init() {
        Wick.SnackBar.init();
        bwVar.$stockTabs.on('click', bwVar.stockTab, function () {
            if (
                $(bwVar.ccBtn).attr('disabled') &&
                $(bwVar.ccBtn).hasClass('nearest-store-purchasable')
            ) {
                $(bwVar.ccBtn).removeAttr('disabled');
                $(bwVar.ccBtn).find('.btn__text').text(bwVar.CLICK_AND_COLLECT);
            }

            if (!$(this).hasClass(bwVar.active)) {
                //toggle tabs
                $(this).closest(bwVar.$bigWins).find(bwVar.$stockTab).removeClass(bwVar.active);
                $(this).addClass(bwVar.active);

                $(this).closest(bwVar.$bigWins).find(bwVar.$stockFindBtn).unbind();

                //show/close modal with more stores
                if ($(this).attr('data-stores') === 'more') {
                    //open block
                    $(this).closest(bwVar.$bigWins).find(bwVar.$stock).addClass(bwVar.stockBot);
                    $(this).closest(bwVar.$bigWins).addClass(bwVar.bwMinHeight);

                    let self;
                    if ($(window).width() < 767) {
                        $(this)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stock)
                            .addClass(bwVar.stockMore);
                    } else {
                        self = this;
                        setTimeout(function () {
                            $(self)
                                .closest(bwVar.$bigWins)
                                .find(bwVar.$stock)
                                .addClass(bwVar.stockMore);
                        }, 1);
                    }

                    // remove ability collapse block
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockAccordion)
                        .removeAttr('data-toggle');

                    //event to show results
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockFindBtn)
                        .on('click', function (e) {
                            showSearchResultMore(e, this);
                            showSearchResultMoreProto(this);
                        });

                    //delete error validation
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockPostcodeEl)
                        .removeClass(bwVar.stockPostcodeInvalid);

                    if ($(window).width() < 767) {
                        //disable scroll for modal
                        bwVar.$body.addClass(bwVar.scrollDisable);
                    }

                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.stockItem)
                        .each(function () {
                            var attr = $(this).find('.stock__radio input').attr('checked');
                            if (typeof attr !== 'undefined' && attr !== false) {
                                var value = $(this)
                                    .closest(bwVar.stockItem)
                                    .find('.store__stock_val')
                                    .text();
                                if (value.match(/^out$/)) {
                                    $(this)
                                        .closest(bwVar.stockItem)
                                        .find('.stock__valid')
                                        .addClass('stock__valid_show');
                                    $(bwVar.ccBtn).attr('disabled', 'disabled');
                                    $(bwVar.ccBtn)
                                        .find('.btn__text')
                                        .text(bwVar.CLICK_AND_COLLECT_NOT_AVAILABLE);
                                }
                            }
                        });
                } else if ($(this).attr('data-stores') === 'nearest') {
                    //close more block
                    $(this).closest(bwVar.$bigWins).find(bwVar.$stock).removeClass(bwVar.stockMore);
                    let self;
                    if ($(window).width() < 767) {
                        $(this)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stock)
                            .removeClass(bwVar.stockBot);
                        $(this).closest(bwVar.$bigWins).removeClass(bwVar.bwMinHeight);
                    } else {
                        self = this;
                        setTimeout(function () {
                            $(self)
                                .closest(bwVar.$bigWins)
                                .find(bwVar.$stock)
                                .removeClass(bwVar.stockBot);
                            $(self).closest(bwVar.$bigWins).removeClass(bwVar.bwMinHeight);
                        }, 600);
                    }

                    // add ability collapse block
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockAccordion)
                        .attr('data-toggle', 'collapse');
                    //event to show results
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockFindBtn)
                        .on('click', showSearchResultNearest);
                    //unlock scroll for modal
                    bwVar.$body.removeClass(bwVar.scrollDisable);
                    //delete error validation
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockPostcodeEl)
                        .removeClass(bwVar.stockPostcodeInvalid);

                    scrollToBlockMobile(this);
                }
            }
            closeStoreDetails(this);
        });

        bwVar.$stockFindBtn.on('click', function () {
            showSearchResultNearest(this);
        });

        bwVar.$defStockTab.on('click', function (e) {
            e.preventDefault();

            bwVar.$stockResult.removeClass('stock__result_hide');
            $(this).closest(bwVar.$bigWins).find(bwVar.$stock).addClass(bwVar.stockBot);
            $(this).closest(bwVar.$bigWins).addClass(bwVar.bwMinHeight);
            let self;
            if ($(window).width() < 767) {
                $(this).closest(bwVar.$bigWins).find(bwVar.$stock).addClass(bwVar.stockMore);
            } else {
                self = this;
                setTimeout(function () {
                    $(self).closest(bwVar.$bigWins).find(bwVar.$stock).addClass(bwVar.stockMore);
                }, 1);
            }
        });

        //choose default store in more stores section
        bwVar.$storeState.on('click', function (e) {
            let defVal = $(this).text().trim(),
                toDefault = 'Make this your default store',
                isDefault = 'Default store',
                selectedStore = $(this).closest(bwVar.$store).html();

            e.preventDefault();

            if (defVal === toDefault) {
                bwVar.$storeState.text(toDefault);
                $(this).text(isDefault);
                bwVar.$bwChosen.find(bwVar.$store).html(selectedStore);
                bwVar.$storeState.parent().find(bwVar.$toDefaultIcon).hide();
                bwVar.$storeState.parent().find(bwVar.$isDefaultIcon).show();

                $(this).parent().find(bwVar.$isDefaultIcon).hide();
                $(this).parent().find(bwVar.$toDefaultIcon).show();
            } else {
                $(this).click(false);
            }
        });

        //make store default from main page
        bwVar.$nameDefault.on('click', function (e) {
            e.preventDefault();
            let makeDefValue = $(this).find(bwVar.defaultStoreName).text();

            bwVar.$stockList.find(bwVar.storeName).each(function () {
                if ($(this).text() === makeDefValue) {
                    $(this).closest(bwVar.$store).find('.store-state').click();
                }
            });

            bwVar.$makeDefault.hide();
            bwVar.$defaultInfo.hide();
            bwVar.$defaultCurrent.show();
        });

        bwVar.$currentDefault.on('click', function (e) {
            e.preventDefault();
            let makeDefValue = $(this).find(bwVar.defaultStoreName).text();

            bwVar.$stockList.find(bwVar.storeName).each(function () {
                if ($(this).text() === makeDefValue) {
                    $(this).closest(bwVar.$store).find('.store-state').click();
                }
            });

            bwVar.$makeDefault.hide();
            bwVar.$defaultInfo.hide();
            bwVar.$defaultCurrent.show();
        });

        //my-acc default store component logic
        bwVar.$storeSearch.on('click', function (e) {
            e.preventDefault();
            bwVar.$storeSearch.hide();
            bwVar.$stockPostcodeEl.show();
        });

        //my-acc scroll to default store component logic
        $('.scrollToDefault').on('click', function (e) {
            e.preventDefault();
            var target = $(this).attr('href');
            $('html, body').animate(
                {
                    scrollTop: $(target).offset().top,
                },
                'slow'
            );
        });

        bwVar.$findStore.on('click', function (e) {
            e.preventDefault();

            if ($('.stock__input').val().length !== 0) {
                bwVar.$stockPostcodeEl.removeClass(bwVar.stockPostcodeInvalid);
                bwVar.$postCodeErrorMsg.hide();
                bwVar.$bigWins.show();

                if (bwVar.$bigWins.css('display') === 'block') {
                    bwVar.$bwActions.css('display', 'flex');
                } else {
                    bwVar.$bwActions.hide();
                }
            } else {
                bwVar.$stockPostcodeEl.addClass(bwVar.stockPostcodeInvalid);
                bwVar.$postCodeErrorMsg.css('display', 'inherit');
            }
        });

        bwVar.$addStore.on('click', function (e) {
            e.preventDefault();
            bwVar.$bigWins.hide();
            bwVar.$stockPostcodeEl.hide();
            bwVar.$storeChange.css('display', 'block');
            bwVar.$bwActions.hide();
            bwVar.$addStore.text('Update Default Store');
        });

        bwVar.$cancelStore.on('click', function (e) {
            e.preventDefault();
            bwVar.$bigWins.hide();
            bwVar.$bwActions.hide();
            bwVar.$stockPostcodeEl.hide();
            bwVar.$storeSearch.show();
        });

        bwVar.$storeChange.on('click', function (e) {
            e.preventDefault();
            bwVar.$bigWins.show();
            bwVar.$stockPostcodeEl.show();
            bwVar.$bwActions.show();
            bwVar.$storeChange.hide();
        });

        bwVar.$defStoreWrap.find(bwVar.$addStore).on('click', function () {
            if ($("input:radio[name='store']").is(':checked')) {
                let currentInp = $("input:radio[name='store'][checked='checked']"),
                    selectedVal = currentInp
                        .closest(bwVar.stockItem)
                        .find(bwVar.sDetailsAddress)
                        .text();

                bwVar.$defStoreWrap.find('.table__content-address').text(selectedVal);
            }
        });

        //show store details
        bwVar.$storeMoreLink.on('click', function (e) {
            e.preventDefault();

            console.log($(this).closest('.basket').length);
            if (!$(this).closest('.basket').length) {
                var storeName = $(this)
                    .closest('.store')
                    .find(bwVar.storeName)
                    .text()
                    .trim()
                    .toLowerCase();

                if ($(this).text().trim() === 'Store Info') {
                    closeStoreDetails(this);
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockRadio)
                        .find('input')
                        .removeAttr('checked');
                    $(this).closest(bwVar.stockItem).find(bwVar.$stockRadio).trigger('click');
                    $(this)
                        .closest(bwVar.stockItem)
                        .find(bwVar.$stockRadio)
                        .attr('checked', 'checked');

                    $(this).text('Hide Info');

                    //show pseudoclass
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stock)
                        .addClass(bwVar.stockDetailsOpen);

                    //classes only for Price
                    console.log($(this).closest(bwVar.$pdpPrice).length);

                    if ($(this).closest(bwVar.$pdpPrice).length) {
                        $(this).closest(bwVar.$pdpPrice).addClass(bwVar.stockDetailsOpen);
                        $(this).closest(bwVar.$pdpPrice).addClass(bwVar.pdpPriceBW);
                    } else if ($(this).closest(bwVar.$tiTotal).length) {
                        $(this).closest(bwVar.$tiTotal).addClass(bwVar.stockDetailsOpen);
                        $(this).closest(bwVar.$tiTotal).addClass(bwVar.tiTotalBW);
                    }

                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$sDetails)
                        .addClass(bwVar.sDetailsHide);
                    //open tab with current store
                    $(this)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$sDetails)
                        .each(function () {
                            if (
                                $(this)
                                    .find(bwVar.sDetailsStoreName)
                                    .text()
                                    .trim()
                                    .toLowerCase() === storeName
                            ) {
                                $(this).removeClass(bwVar.sDetailsHide);
                            }
                        });

                    if (!bwVar.$ccOrder.length) {
                        if ($(window).width() < 767) {
                            //disable scroll for modal
                            bwVar.$body.addClass(bwVar.scrollDisable);
                        }
                    }
                } else if ($(this).text().trim() === 'Hide Info') {
                    closeStoreDetails(this);
                }
            }
        });

        //hide store details
        bwVar.$sDetailsClose.on('click', function (e) {
            e.preventDefault();
            closeStoreDetails(this);
        });

        // show store via radio buttons
        bwVar.$stockRadio.on('change', function () {
            $(this).closest(bwVar.$bigWins).find('.stock__valid').removeClass('stock__valid_show');
            if ($(this).closest(bwVar.$bigWins).find(bwVar.ccBtn).attr('disabled')) {
                $(this).closest(bwVar.$bigWins).find(bwVar.ccBtn).removeAttr('disabled');
                $(this)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.ccBtn)
                    .find('.btn__text')
                    .text(bwVar.CLICK_AND_COLLECT);
            }

            var openedInfo = false;
            $(this)
                .closest(bwVar.$bigWins)
                .find('.s-details')
                .each(function () {
                    if (!$(this).hasClass('s-details_hide')) {
                        openedInfo = true;
                    }
                });

            if (openedInfo) {
                var storeName = $(this)
                    .closest('.stock__item')
                    .find(bwVar.storeName)
                    .text()
                    .trim()
                    .toLowerCase();
                $(this).closest(bwVar.$bigWins).find(bwVar.$storeMoreLink).text('Store Info');
                $(this).closest('.stock__item').find(bwVar.$storeMoreLink).text('Hide Info');

                //show pseudoclass
                $(this).closest(bwVar.$bigWins).find(bwVar.$stock).addClass(bwVar.stockDetailsOpen);
                $(this)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$pdpPrice)
                    .addClass(bwVar.stockDetailsOpen);

                $(this).closest(bwVar.$pdpPrice).addClass(bwVar.pdpPriceBW);

                $(this).closest(bwVar.$bigWins).find(bwVar.$sDetails).addClass(bwVar.sDetailsHide);

                //open tab with current store
                $(this)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$sDetails)
                    .each(function () {
                        if (
                            $(this).find(bwVar.sDetailsStoreName).text().trim().toLowerCase() ===
                            storeName
                        ) {
                            $(this).removeClass(bwVar.sDetailsHide);
                        }
                    });

                if ($(window).width() < 767) {
                    //disable scroll for modal
                    bwVar.$body.addClass(bwVar.scrollDisable);
                }
            } else {
                closeStoreDetails(this);
            }

            $(this).trigger('click');
            $(this)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockRadio)
                .find('input')
                .removeAttr('checked');
            $(this).find('input').attr('checked', 'checked');

            var attr = $(this).find('input').attr('checked');
            if (typeof attr !== 'undefined' && attr !== false) {
                var value = $(this).closest('.stock__item').find('.store__stock_val').text();
                if (!value.match(/^-{0,1}\d+$/)) {
                    $(this)
                        .closest('.stock__item')
                        .find('.stock__valid')
                        .addClass('stock__valid_show');
                    $(bwVar.ccBtn).attr('disabled', 'disabled');
                    $(bwVar.ccBtn).find('.btn__text').text(bwVar.CLICK_AND_COLLECT_NOT_AVAILABLE);
                }
            }
        });

        //toggle map
        bwVar.$sDetailsToggleMap.on('click', function (e) {
            e.preventDefault();
            $(this).closest(bwVar.$bigWins).find(bwVar.$pdpPrice).removeClass(bwVar.pdpPriceBW);

            if (
                $(this)
                    .closest(bwVar.sDetailsInfo)
                    .find(bwVar.$sDetailsToggleMapText)
                    .text()
                    .trim() === 'Hide Map'
            ) {
                $(this)
                    .closest(bwVar.sDetailsInfo)
                    .find(bwVar.$sDetailsToggleMapText)
                    .text('Show Map');
                $(this)
                    .closest(bwVar.sDetailsInfo)
                    .find(bwVar.$sDetailsMap)
                    .addClass(bwVar.sDetailsMapHide);
            } else if (
                $(this)
                    .closest(bwVar.sDetailsInfo)
                    .find(bwVar.$sDetailsToggleMapText)
                    .text()
                    .trim() === 'Show Map'
            ) {
                $(this)
                    .closest(bwVar.sDetailsInfo)
                    .find(bwVar.$sDetailsToggleMapText)
                    .text('Hide Map');
                $(this)
                    .closest(bwVar.sDetailsInfo)
                    .find(bwVar.$sDetailsMap)
                    .removeClass(bwVar.sDetailsMapHide);
            }
        });

        bwVar.$stockQuestions.find('input').on('click', function () {
            if ($(this).attr('checked') === 'checked') {
                $(this).removeAttr('checked');
            } else {
                $(this).attr('checked', 'checked');
            }
        });

        bwVar.$stockClose.on('click', function (e) {
            closeMoreStorestModal(e, this);
            closeStoreDetails(this);
        });

        //come back to search tab
        bwVar.$stockEditBtn.on('click', function () {
            closeStoreDetails(this);
            $(this)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockResult)
                .addClass(bwVar.stockResultHide);
            $(this).closest(bwVar.$bigWins).find(bwVar.$stockTabs).removeClass(bwVar.stockTabsHide);
            $(this).closest(bwVar.$bigWins).find(bwVar.$stockFind).removeClass(bwVar.stockFindHide);
        });

        bwVar.$stockCancel.on('click', function (e) {
            closeMoreStorestModal(e, this);
        });

        //C&C btn
        bwVar.$bigWins.on('click', bwVar.ccBtn, function (e) {
            closeMoreStorestModal(e, this);

            //hide store info
            $(this).closest(bwVar.$bigWins).find(bwVar.$sDetails).addClass(bwVar.sDetailsHide);
            $(this).closest(bwVar.$pdpPrice).removeClass(bwVar.pdpPriceBW);

            //show store info
            $(this).closest(bwVar.$bigWins).find(bwVar.$chosen).removeClass(bwVar.chosenHide);

            if ($(this).closest(bwVar.$bigWins).hasClass('bw_unhappy')) {
                $(this).closest(bwVar.$bigWins).find(bwVar.$chosen).addClass(bwVar.chosenError);
            } else {
                $(this).closest(bwVar.$bigWins).find(bwVar.$chosen).addClass(bwVar.chosenSuccess);
            }

            //hide pseudoclass
            $(this).closest(bwVar.$bigWins).find(bwVar.$stock).removeClass(bwVar.stockDetailsOpen);

            //unlock scroll for modal
            bwVar.$body.removeClass(bwVar.scrollDisable);

            scrollToBlockMobile(this);
        });

        bwVar.$sDetailsCopy.on('click', function (e) {
            e.preventDefault();
            var $temp = $('<input>');
            $(this.parentElement).append($temp); //workaround for iPad scrolling
            var text = $(this).closest(bwVar.sDetails).find(bwVar.$sDetailsAddress).text().trim();
            var str = text.replace(/(^\w|\s\w)/g, function (m) {
                return m.toUpperCase();
            });
            var textFiltered = str.split(' ').filter(function (value) {
                return value !== '';
            });
            var textTrimmed = textFiltered.join(' ');
            $temp.val(textTrimmed);
            if (navigator.userAgent.match(/TradeProMobile_iOS|DIYMobile_iOS/i)) {
                iosCopyToClipboard($temp);
            } else if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
                $temp.select();
                $temp.setSelectionRange(0, 999999);
            } else {
                $temp.select();
            }

            document.execCommand('copy');
            $(this).addClass(bwVar.copyLinkHovered);
            $temp.remove();
        });

        // bwVar.$addForDelivery.on('click', function () {
        //   Wick.SnackBar.show(
        //       {
        //         text: 'Notification success message for Delivery with Close Btn',
        //         type: Wick.SnackBar.types.SUCCESS
        //       }
        //   );
        //   Wick.SnackBar.show(
        //       {
        //         text: 'Notification warning message for Delivery with Close Btn',
        //         type: Wick.SnackBar.types.WARNING
        //       }
        //   );
        //   Wick.SnackBar.show(
        //       {
        //         text: 'Notification error message for Delivery with Close Btn',
        //         type: Wick.SnackBar.types.ERROR
        //       }
        //   );
        //   Wick.SnackBar.show(
        //       {
        //         text: 'Notification success message for Delivery with Autoclose',
        //         type: Wick.SnackBar.types.SUCCESS,
        //         withCloseButton: false,
        //         autoClose: true,
        //         delayBeforeClose: 3000
        //       }
        //   );
        // });

        bwVar.$addClickAndCollect.on('click', function (e) {
            e.stopPropagation();
            Wick.SnackBar.show({
                text: 'Notification success message for Click & Collect with Close Btn',
                type: Wick.SnackBar.types.SUCCESS,
                withCloseButton: true,
            });
            Wick.SnackBar.show({
                text: 'Notification warning message for Click & Collect with Close Btn',
                type: Wick.SnackBar.types.WARNING,
                withCloseButton: true,
            });
            Wick.SnackBar.show({
                text: 'Notification error message for Click & Collect with Close Btn',
                type: Wick.SnackBar.types.ERROR,
                withCloseButton: true,
            });
            Wick.SnackBar.show({
                text: 'Notification success message for Click & Collect with Autoclose',
                type: Wick.SnackBar.types.SUCCESS,
                withCloseButton: false,
                autoClose: true,
                delayBeforeClose: 3000,
            });
        });

        //func to show results
        function showSearchResultMore(e, context) {
            e.preventDefault();
            var postcodeLength = $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockPostcode)
                .val().length;

            $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockPostcodeEl)
                .removeClass(bwVar.stockPostcodeInvalid);

            if (postcodeLength >= 5) {
                if ($(window).width() < 767) {
                    bwVar.$body.addClass(bwVar.scrollDisable);
                }
                //open new section
                if (
                    !$(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockTabs)
                        .hasClass('more-stores-present')
                ) {
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockResult)
                        .removeClass(bwVar.stockResultHide);
                } else {
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockTabs)
                        .addClass(bwVar.stockTabsHide);
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockFind)
                        .addClass(bwVar.stockFindHide);
                }

                //clean classes for descr
                $(context)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$stockDescr)
                    .removeClass(bwVar.stockDescrS);
                $(context)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$stockDescr)
                    .removeClass(bwVar.stockDescrC);

                var quantity = $(context).closest(bwVar.$bigWins).find(bwVar.$stockQuantity).val(),
                    postcode = $(context).closest(bwVar.$bigWins).find(bwVar.$stockPostcode).val(),
                    onlyStock =
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stockQuestions)
                            .find('input[name="stock-only"]')
                            .attr('checked') === 'checked',
                    onlyCC =
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stockQuestions)
                            .find('input[name="cc-only"]')
                            .attr('checked') === 'checked';

                //amend text in info block
                $(context).closest(bwVar.$bigWins).find(bwVar.$stockAmount).text(quantity);
                $(context).closest(bwVar.$bigWins).find(bwVar.$stockPost).text(postcode);

                if (onlyCC !== onlyStock) {
                    if (onlyStock) {
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stockDescr)
                            .addClass(bwVar.stockDescrS);
                    } else if (onlyCC) {
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stockDescr)
                            .addClass(bwVar.stockDescrC);
                    }
                } else if (onlyStock === false && onlyCC === false) {
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockDescr)
                        .addClass(bwVar.stockDescrS);
                }
            } else {
                $(context)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$stockPostcodeEl)
                    .addClass(bwVar.stockPostcodeInvalid);
            }
        }

        function showSearchResultNearest(context) {
            var postcodeLength = $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockPostcode)
                .val().length;

            if (postcodeLength >= 5) {
                closeStoreDetails(context);
                $(context)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$stockPostcodeEl)
                    .removeClass(bwVar.stockPostcodeInvalid);
                $(context)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$chosen)
                    .removeClass(bwVar.chosenHide);
                $(context)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$chosen)
                    .addClass(bwVar.chosenNearest);

                scrollToBlockMobile(context);

                var value = $(context)
                    .closest(bwVar.$bigWins)
                    .find('.chosen__nearest')
                    .find('.store__stock_val')
                    .text();
                if (value.match(/^out$/)) {
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find('.chosen__nearest')
                        .find('.stock__valid')
                        .addClass('stock__valid_show');
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.ccBtn)
                        .attr('disabled', 'disabled');
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.ccBtn)
                        .find('.btn__text')
                        .text(bwVar.CLICK_AND_COLLECT_NOT_AVAILABLE);
                } else {
                    if (
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find('#isCnCForProductAndStoreDisabled')
                            .val() === 'true'
                    ) {
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find('.chosen__nearest')
                            .find('.stock__valid')
                            .addClass('stock__valid_show');
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.ccBtn)
                            .attr('disabled', 'disabled');
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.ccBtn)
                            .find('.btn__text')
                            .text(bwVar.CLICK_AND_COLLECT_NOT_AVAILABLE);
                    } else {
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find('.chosen__nearest')
                            .find('.stock__valid')
                            .removeClass('stock__valid_show');
                        if ($(context).closest(bwVar.$bigWins).find(bwVar.ccBtn).attr('disabled')) {
                            $(context)
                                .closest(bwVar.$bigWins)
                                .find(bwVar.ccBtn)
                                .removeAttr('disabled');
                            $(context)
                                .closest(bwVar.$bigWins)
                                .find(bwVar.ccBtn)
                                .find('.btn__text')
                                .text(bwVar.CLICK_AND_COLLECT);
                        }
                    }
                }
            } else {
                $(context)
                    .closest(bwVar.$bigWins)
                    .find(bwVar.$stockPostcodeEl)
                    .addClass(bwVar.stockPostcodeInvalid);
            }
        }

        function closeMoreStorestModal(e, context) {
            e.preventDefault();
            //close more block
            $(context).closest(bwVar.$bigWins).find(bwVar.$stock).removeClass(bwVar.stockMore);

            if ($(window).width() < 767) {
                $(context).closest(bwVar.$bigWins).find(bwVar.$stock).removeClass(bwVar.stockBot);
                $(context).closest(bwVar.$bigWins).removeClass(bwVar.bwMinHeight);
            } else {
                setTimeout(function () {
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stock)
                        .removeClass(bwVar.stockBot);
                    $(context).closest(bwVar.$bigWins).removeClass(bwVar.bwMinHeight);
                }, 600);
            }

            //bind nearestStore event
            $(context).closest(bwVar.$bigWins).find(bwVar.$stockFindBtn).unbind();
            $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockFindBtn)
                .on('click', showSearchResultNearest);

            //toggle tabs to nearest
            $(context).closest(bwVar.$bigWins).find(bwVar.$stock).removeClass(bwVar.stockMore);
            $(context).closest(bwVar.$bigWins).find(bwVar.$stockTab).removeClass(bwVar.active);
            $(context).closest(bwVar.$bigWins).find(bwVar.$stockTab).first().addClass(bwVar.active);

            $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockResult)
                .addClass(bwVar.stockResultHide);
            $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockTabs)
                .removeClass(bwVar.stockTabsHide);
            $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stockFind)
                .removeClass(bwVar.stockFindHide);

            bwVar.$body.removeClass(bwVar.scrollDisable);

            scrollToBlockMobile(context);
        }

        function closeStoreDetails(context) {
            bwVar.$storeMoreLink.text('Store Info');
            //hide pseudoclass
            $(context)
                .closest(bwVar.$bigWins)
                .find(bwVar.$stock)
                .removeClass(bwVar.stockDetailsOpen);
            $(context).closest(bwVar.$bigWins).find(bwVar.$sDetails).addClass(bwVar.sDetailsHide);
            bwVar.$pdpPrice.removeClass(bwVar.pdpPriceBW);
            bwVar.$tiTotal.removeClass(bwVar.tiTotalBW);
            bwVar.$body.removeClass(bwVar.scrollDisable);

            scrollToBlockMobile(context);
        }

        function showSearchResultMoreProto(context) {
            if (
                $(context)
                    .closest(bwVar.$bigWins)
                    .find('.stock__tab.active')
                    .attr('data-stores') === 'more'
            ) {
                var postcodeLength;

                if (
                    $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$bwChosen)
                        .hasClass(bwVar.protoOnlyPopulated)
                ) {
                    postcodeLength = $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$protoOnlyPopulated)
                        .find(bwVar.storeName)
                        .text().length;
                } else {
                    postcodeLength = $(context)
                        .closest(bwVar.$bigWins)
                        .find(bwVar.$stockPostcode)
                        .val().length;
                }

                if (postcodeLength >= 5) {
                    if (
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stockTabs)
                            .hasClass(bwVar.moreStoresPresent || bwVar.defStoresPresent)
                    ) {
                        $(context)
                            .closest(bwVar.$bigWins)
                            .find(bwVar.$stockResult)
                            .removeClass(bwVar.stockResultHide);
                    }
                }
            }
        }

        function iosCopyToClipboard($input) {
            var el = $input.get(0);
            var editable = el.contentEditable;
            var readOnly = el.readOnly;

            el.contentEditable = true;
            el.readOnly = false;
            var range = document.createRange();
            range.selectNodeContents(el);
            var sel = window.getSelection();
            sel.removeAllRanges();
            sel.addRange(range);
            el.setSelectionRange(0, 999999);
            el.contentEditable = editable;
            el.readOnly = readOnly;
        }

        function scrollToBlockMobile(context) {
            if ($(window).width() < 767) {
                if ($(context).closest(bwVar.$tiTotal).length) {
                    window.scroll(0, bwVar.$tiTotal.offset().top - 60);
                } else if ($(context).closest(bwVar.$pdpPrice).length) {
                    window.scroll(0, bwVar.$bigWins.offset().top - 60);
                }
            }
        }

        (function checkStoreProductsNearest() {
            var value = $('.chosen__item').find('.store__stock_val').text();
            if (value.match(/^out$/)) {
                $('.chosen').find('.stock__valid').addClass('stock__valid_show');
                $(bwVar.ccBtn).attr('disabled', 'disabled');
                $(bwVar.ccBtn).find('.btn__text').text(bwVar.CLICK_AND_COLLECT_NOT_AVAILABLE);
            } else {
                if ($('#isCnCForProductAndStoreDisabled').val() === 'true') {
                    $('.chosen__nearest').find('.stock__valid').addClass('stock__valid_show');
                    $(bwVar.ccBtn).attr('disabled', 'disabled');
                    $(bwVar.ccBtn).find('.btn__text').text(bwVar.CLICK_AND_COLLECT_NOT_AVAILABLE);
                } else {
                    $('.chosen__nearest').find('.stock__valid').removeClass('stock__valid_show');
                    if ($(bwVar.ccBtn).attr('disabled')) {
                        $(bwVar.ccBtn).removeAttr('disabled');
                        $(bwVar.ccBtn).find('.btn__text').text(bwVar.CLICK_AND_COLLECT);
                    }
                }
            }
        })();

        (function getCorrectIdToCheckbox() {
            bwVar.$stockQuestions.each(function (index) {
                $(this)
                    .find("label[for='stock-only']")
                    .attr('for', 'stock-only' + index);
                $(this)
                    .find("label[for='cc-only']")
                    .attr('for', 'cc-only' + index);

                $(this)
                    .find('#stock-only')
                    .attr('id', 'stock-only' + index);
                $(this)
                    .find('#cc-only')
                    .attr('id', 'cc-only' + index);
            });

            bwVar.$stockRadio.each(function (index) {
                var currentName = $(this).attr('for');
                $(this).attr('for', currentName + index);
                $(this)
                    .find('input')
                    .attr('id', currentName + index);
            });
        })();
    }

    if (!bwVar.$bigWins.length) {
        return;
    }

    init();
})();
