/**
 * 2007-2023 ETS-Soft
 *
 * NOTICE OF LICENSE
 *
 * This file is not open source! Each license that you purchased is only available for 1 website only.
 * If you want to use this file on more websites (or projects), you need to purchase additional licenses. 
 * You are not allowed to redistribute, resell, lease, license, sub-license or offer our resources to any third party.
 * 
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please contact us for extra customization service at an affordable price
 *
 *  @author ETS-Soft <etssoft.jsc@gmail.com>
 *  @copyright  2007-2023 ETS-Soft
 *  @license    Valid for 1 website (or project) for each purchase of license
 *  International Registered Trademark & Property of ETS-Soft
 */
$(document).ready(function(){
    if($('.ets_pr_display_popup').length)
    {
        $('.ets_pr_display_popup').each(function(){
           if($(this).data('delay') >0)
           {
                var time_delay = $(this).data('delay')*1000;
                var popup = $(this);
                setTimeout(function(){
                    popup.addClass('show');
                },time_delay);
           }
           else
              $(this).addClass('show');  
        });
    }
    $(document).on('click','.close_highlight_bar',function(){
       $(this).parents('.ets_pr_display_highlight_bar').removeClass('show'); 
       var id_promote = $(this).data('id');
       var link_ajax = $(this).data('href');
        $.ajax({
            url: link_ajax,
            data: 'submitEtsPrCloseHighlightBar='+id_promote,
            type: 'post',
            dataType: 'json',                
            success: function(json){ 
            
            }
        });
    });
    $(document).on('click','.ets_pr_close_popup',function(){
        $(this).parents('.ets_pr_display_popup').removeClass('show');
        var id_promote = $(this).data('id');
        var link_ajax = $(this).data('href');
        $.ajax({
            url: link_ajax,
            data: 'submitEtsPrClosePopup='+id_promote,
            type: 'post',
            dataType: 'json',                
            success: function(json){ 
            
            }
        });
    });
    $(document).on('click','.add-to-cart-gift-product',function(e){
        e.preventDefault();
        if($(".js-cart-payment-step-refresh").length)
            $(".js-cart-payment-step-refresh").addClass('js-cart-payment-step-refresh2').removeClass('js-cart-payment-step-refresh');
        if(!$(this).hasClass('loading'))
        {
            $(this).addClass('loading');
            var $this = $(this);
            var url_link = $(this).data('link');
            $.ajax({
                url: url_link,
                data: 'action=update&ajax=1',
                type: 'post',
                dataType: 'json',                
                success: function(json){ 

                    if(json.hasError && json.errors)
                    {
                        alert(json.errors[0]);
                        $this.removeClass('loading');
                    }
                    else
                    {
                        if($('#form_ets_onepagecheckout').length)
                        {
                            if($(".js-cart").data("refresh-url"))
                            {
                                $.ajax({
                                    url: $(".js-cart").data("refresh-url"),
                                    data: 'id_country='+$('#shipping_address_id_country').val()+($('#shipping_address_id_state').length ? '&id_state='+$('#shipping_address_id_state').val() :'')+($('#shipping_address_postal_code').length ? '&postal_code='+$('#shipping_address_postal_code').val():'')+($('.block-shop-license-info').length ? '&'+$('.block-shop-license-info input').serialize():''),
                                    type: 'post',
                                    dataType: 'json',                
                                    success: function(json){
                                        $(".cart-detailed-totals").replaceWith(json.cart_detailed_totals);
                                        $(".cart-summary-items-subtotal").replaceWith(json.cart_summary_items_subtotal);
                                        $(".cart-summary-subtotals-container").replaceWith(json.cart_summary_subtotals_container);
                                        $(".cart-summary-totals").replaceWith(json.cart_summary_totals);
                                        $(".cart-detailed-actions").replaceWith(json.cart_detailed_actions);
                                        $(".cart-voucher").replaceWith(json.cart_voucher);
                                        $(".cart-overview").replaceWith(json.cart_detailed);
                                        $(".js-cart-line-product-quantity").each(function(e, t) {
                                                var n = $(t);
                                                n.attr("value", n.val());
                                        });
                                        prestashop.emit("updatedCart", {
                                            eventType: "updateCart",
                                            resp: json
                                        });
                                    }
                                });
                            }
                        }
                        else
                        prestashop.emit("updateCart", {
                            reason: {
                                idProduct: json.id_product,
                                idProductAttribute: json.id_product_attribute,
                                idCustomization: 0,
                                linkAction: "",
                                cart: json.cart
                            },
                            resp: json
                        });
                    }
                    
                }
            });
        }
    });
    if($('.cart-grid-body .ets_pr_gift_products').length)
    {
        $(document ).ajaxComplete(function( event, xhr, settings ) {
            if( xhr.responseText && xhr.responseText.indexOf('cart_detailed')>=0)
            {
                setTimeout(function(){
                    if($('.li_ets_pr_gift_products').length)
                    {
                        $('.cart-grid-body .ets_pr_gift_products').replaceWith($('.li_ets_pr_gift_products').html());
                    }
                    else
                        $('.cart-grid-body .ets_pr_gift_products').html('');
                },1000);
                var data = JSON.parse(xhr.responseText);
                if(data.cart_sumary_products)
                {
                    $('.cart-grid-right .cart-summary-products').replaceWith(data.cart_sumary_products);
                }
            }
        });
    }
    //custom hook;
    if ($('#js-product-list-header').length > 0) {
        if ($('#etsPRhookDisplayProductListHeaderBefore').length > 0) {
            $('#js-product-list-header').prepend($('#etsPRhookDisplayProductListHeaderBefore').html());
        }
        if ($('#etsPRhookDisplayProductListHeaderAfter').length > 0) {
            $('#js-product-list-header').append($('#etsPRhookDisplayProductListHeaderAfter').html());
        }
    }
    if ($('.content_scene_cat').length > 0) {
        if ($('#etsPRhookDisplayProductListHeaderBefore').length > 0) {
            $('.content_scene_cat').before($('#etsPRhookDisplayProductListHeaderBefore').html());
        }
        if ($('#etsPRhookDisplayProductListHeaderAfter').length > 0) {
            $('.content_scene_cat').after($('#etsPRhookDisplayProductListHeaderAfter').html());
        }
    }
    if ($('#left-column').length > 0) {
        if ($('#etsPRhookDisplayLeftColumnBefore').length > 0) {
            $('#left-column').prepend($('#etsPRhookDisplayLeftColumnBefore').html());
        }
    }
    if ($('#left_column').length > 0) {
        if ($('#etsPRhookDisplayLeftColumnBefore').length > 0) {
            $('#left_column').prepend($('#etsPRhookDisplayLeftColumnBefore').html());
        }
    }
    if ($('#right-column').length > 0) {
        if ($('#etsPRhookDisplayRightColumnBefore').length > 0) {
            $('#right-column').prepend($('#etsPRhookDisplayRightColumnBefore').html());
        }
    }
    if ($('#right_column').length > 0) {
        if ($('#etsPRhookDisplayRightColumnBefore').length > 0) {
            $('#right_column').prepend($('#etsPRhookDisplayRightColumnBefore').html());
        }
    }
    if ($('.product-variants').length > 0) {
        if ($('#etsPRhookDisplayProductVariantsBefore').length > 0) {
            $('.product-variants').prepend($('#etsPRhookDisplayProductVariantsBefore').html());
        }
    }
    if ($('.product_attributes').length > 0) {
        if ($('#etsPRhookDisplayProductVariantsBefore').length > 0) {
            $('.product_attributes').prepend($('#etsPRhookDisplayProductVariantsBefore').html());
        }
    }
    if ($('.product-variants').length > 0) {
        if ($('#etsPRhookDisplayProductVariantsAfter').length > 0) {
            $('.product-variants').append($('#etsPRhookDisplayProductVariantsAfter').html());
        }
    }
    if ($('.product_attributes').length > 0) {
        if ($('#etsPRhookDisplayProductVariantsAfter').length > 0) {
            $('.product_attributes').append($('#etsPRhookDisplayProductVariantsAfter').html());
        }
    }
    if ($('#product-comments-list-header').length > 0) {
        if ($('#etsPRhookDisplayProductCommentsListHeaderBefore').length > 0) {
            $('#product-comments-list-header').before($('#etsPRhookDisplayProductCommentsListHeaderBefore').html());
        }
    }
    if ($('.cart-grid-body').length > 0) {
        if ($('#etsPRhookDisplayCartGridBodyBefore1').length > 0) {
            $('.cart-grid-body').prepend($('#etsPRhookDisplayCartGridBodyBefore1').html());
        }
        if ($('#etsPRhookDisplayCartGridBodyBefore2').length > 0) {
            $('.cart-grid-body').prepend($('#etsPRhookDisplayCartGridBodyBefore2').html());
        }
        if ($('#etsPRhookDisplayCartGridBodyAfter').length > 0) {
            $('.cart-grid-body').append($('#etsPRhookDisplayCartGridBodyAfter').html());
        }
    }
    if ($('#order_step').length > 0){
        if ($('#etsPRhookDisplayCartGridBodyBefore2').length > 0) {
            $('#order_step').after($('#etsPRhookDisplayCartGridBodyBefore2').html());
        }
    }
    if($('.cart_navigation').length > 0){
        if ($('#etsPRhookDisplayCartGridBodyAfter').length > 0) {
            $('.cart_navigation').after($('#etsPRhookDisplayCartGridBodyAfter').html());
        }
    }
    if($('body#product').length)
    {
         $(document ).ajaxComplete(function( event, xhr, settings ) {
            if(xhr.responseText && xhr.responseText.indexOf("product_prices")>=0)
            {
                 if ($('.product-variants').length > 0) {
                if ($('#etsPRhookDisplayProductVariantsBefore').length > 0) {
                    $('.product-variants').prepend($('#etsPRhookDisplayProductVariantsBefore').html());
                }
            }
            if ($('.product_attributes').length > 0) {
                if ($('#etsPRhookDisplayProductVariantsBefore').length > 0) {
                    $('.product_attributes').prepend($('#etsPRhookDisplayProductVariantsBefore').html());
                }
            }
            if ($('.product-variants').length > 0) {
                if ($('#etsPRhookDisplayProductVariantsAfter').length > 0) {
                    $('.product-variants').append($('#etsPRhookDisplayProductVariantsAfter').html());
                }
            }
            if ($('.product_attributes').length > 0) {
                if ($('#etsPRhookDisplayProductVariantsAfter').length > 0) {
                    $('.product_attributes').append($('#etsPRhookDisplayProductVariantsAfter').html());
                }
            }
            if ($('#product-comments-list-header').length > 0) {
                if ($('#etsPRhookDisplayProductCommentsListHeaderBefore').length > 0) {
                    $('#product-comments-list-header').before($('#etsPRhookDisplayProductCommentsListHeaderBefore').html());
                }
            }
            }
        });
    }
});