$(window).scroll(function() {
    if ($(window).scrollTop() >= 41) {
        $('body').addClass('header-fixed');
    } else {
        $('body').removeClass('header-fixed');
    }
});

//mobile touch
$(document).ready(function($) {
    $(".carousel").carousel();
});

$(document).ready(function($) {
    // browser window scroll (in pixels) after which the "back to top" link is shown
    var offset = 300,
        //browser window scroll (in pixels) after which the "back to top" link opacity is reduced
        offset_opacity = 1200,
        //duration of the top scrolling animation (in ms)
        scroll_top_duration = 700,
        //grab the "back to top" link
        $back_to_top = $('.cd-top');

    //hide or show the "back to top" link
    $(window).scroll(function() {
        ($(this).scrollTop() > offset) ? $back_to_top.addClass('cd-is-visible'): $back_to_top.removeClass('cd-is-visible cd-fade-out');
        if ($(this).scrollTop() > offset_opacity) {
            $back_to_top.addClass('cd-fade-out');
        }
    });

    //smooth scroll to top
    $back_to_top.on('click', function(event) {
        event.preventDefault();
        $('body,html').animate({
            scrollTop: 0,
        }, scroll_top_duration);
    });

    // Multi image gallery
    openerp.jsonRpc('/kingfisher_pro/multi_image_effect_config', 'call', {})
        .done(function(res) {
            var dynamic_data = {}
            dynamic_data['gallery_images_preload_type'] = 'all'
            dynamic_data['slider_enable_text_panel'] = false
            dynamic_data['gallery_skin'] = "alexis"
            dynamic_data['gallery_height'] = 800

            if (res.theme_panel_position != false) {
                dynamic_data['theme_panel_position'] = res.theme_panel_position
            }

            if (res.interval_play != false) {
                dynamic_data['gallery_play_interval'] = res.interval_play
            }

            if (res.color_opt_thumbnail != false && res.color_opt_thumbnail != 'default') {
                dynamic_data['thumb_image_overlay_effect'] = true
                if (res.color_opt_thumbnail == 'b_n_w') {}
                if (res.color_opt_thumbnail == 'blur') {
                    dynamic_data['thumb_image_overlay_type'] = "blur"
                }
                if (res.color_opt_thumbnail == 'sepia') {
                    dynamic_data['thumb_image_overlay_type'] = "sepia"
                }
            }

            if (res.enable_disable_text == true) {
                dynamic_data['slider_enable_text_panel'] = true
            }

            if (res.change_thumbnail_size == true) {
                dynamic_data['thumb_height'] = res.thumb_height
                dynamic_data['thumb_width'] = res.thumb_width
            }

            if (res.no_extra_options == false) {
                dynamic_data['slider_enable_arrows'] = false
                dynamic_data['slider_enable_progress_indicator'] = false
                dynamic_data['slider_enable_play_button'] = false
                dynamic_data['slider_enable_fullscreen_button'] = false
                dynamic_data['slider_enable_zoom_panel'] = false
                dynamic_data['slider_enable_text_panel'] = false
                dynamic_data['strippanel_enable_handle'] = false
                dynamic_data['gridpanel_enable_handle'] = false
                dynamic_data['theme_panel_position'] = 'bottom'
                dynamic_data['thumb_image_overlay_effect'] = false
            }

            $('#gallery').unitegallery(
                dynamic_data
            );
        });

    $('div#recommended_products_slider').owlCarousel({
        margin: 10,
        responsiveClass: true,
        items: 4,
        loop: true,
        autoPlay: 7000,
        stopOnHover: true,
        navigation: true,
        responsive: {
            0: {
                items: 1,
                nav: false
            },
            500: {
                items: 2,
                nav: false
            },
            700: {
                items: 3,
                margin: 10,
                nav: false
            },
            1000: {
                items: 4,
                nav: false,
                loop: false
            },
            1500: {
                items: 4,
                nav: false,
                loop: false
            }
        }
    });

    // Grid/List switching code
    $(".oe_website_sale .shift_list_view").click(function(e) {
        $(".oe_website_sale .shift_grid_view").removeClass('active')
        $(this).addClass('active')
        $('#products_grid').addClass("list-view-box");
        localStorage.setItem("product_view", "list");
    });

    $(".oe_website_sale .shift_grid_view").click(function(e) {
        $(".oe_website_sale .shift_list_view").removeClass('active')
        $(this).addClass('active')
        $('#products_grid').removeClass("list-view-box");
        localStorage.setItem("product_view", "grid");
    });

    if (localStorage.getItem("product_view") == 'list') {
        $(".oe_website_sale .shift_grid_view").removeClass('active')
        $(".oe_website_sale .shift_list_view").addClass('active')
        $('#products_grid').addClass("list-view-box");
    }

    if (localStorage.getItem("product_view") == 'grid') {
        $(".oe_website_sale .shift_list_view").removeClass('active')
        $(".oe_website_sale .shift_grid_view").addClass('active')
        $('#products_grid').removeClass("list-view-box");
    }
    // Grid/List switching code ends

    // Price slider code start
    var minval = $("input#m1").attr('value'),
        maxval = $('input#m2').attr('value'),
        minrange = $('input#ra1').attr('value'),
        maxrange = $('input#ra2').attr('value'),
        website_currency = $('input#king_pro_website_currency').attr('value');

    if (!minval) {
        minval = 0;
    }
    if (!maxval) {
        maxval = maxrange;
    }
    if (!minrange) {
        minrange = 0;

    }
    if (!maxrange) {
        maxrange = 2000;
    }


    $("div#priceslider").ionRangeSlider({
        keyboard: true,
        min: parseInt(minrange),
        max: parseInt(maxrange),
        type: 'double',
        from: minval,
        to: maxval,
        step: 1,
        prefix: website_currency,
        grid: true,
        onFinish: function(data) {
            $("input[name='min1']").attr('value', parseInt(data.from));
            $("input[name='max1']").attr('value', parseInt(data.to));
            $("div#priceslider").closest("form").submit();
        },
    });
    // Price slider code ends

    //attribute remove code
    $("a#clear").on('click', function() {
        var url = window.location.href.split("?");
        var lival;
        lival = $(this).closest("label").attr('id');
        openerp.jsonRpc("/kingfisher_pro/removeattribute", 'call', {
            'attr_remove': lival
        }).then(function(data) {
            if (data = true) {
                window.location.href = url[0];
            }
        })
    });

    // Category Collapse
    $('#o_shop_collapse_category').on('click', '.fa-chevron-right', function() {
        $(this).parent().siblings().find('.fa-chevron-down:first').click();
        $(this).parents('li').find('ul:first').show('normal');
        $(this).toggleClass('fa-chevron-down fa-chevron-right');
    });

    $('#o_shop_collapse_category').on('click', '.fa-chevron-down', function() {
        $(this).parent().find('ul:first').hide('normal');
        $(this).toggleClass('fa-chevron-down fa-chevron-right');
    });

    // Dynamic cart update
    $('.oe_website_sale').each(function() {
        var oe_website_sale = this;
        $(oe_website_sale).find(".oe_cart input.js_quantity").on("change", function() {
            var $input = $(this);
            if ($input.data('update_change')) {
                return;
            }
            var value = parseInt($input.val(), 10);
            var $dom = $(this).closest('tr');
            // var default_price = parseFloat($dom.find('.text-danger > span.oe_currency_value').text());
            var $dom_optional = $dom.nextUntil(':not(.optional_product.info)');
            var line_id = parseInt($input.data('line-id'), 10);
            var product_id = parseInt($input.data('product-id'), 10);
            var product_ids = [product_id];
            $dom_optional.each(function() {
                product_ids.push($(this).find('span[data-product-id]').data('product-id'));
            });
            if (isNaN(value)) value = 0;
            $input.data('update_change', true);
            openerp.jsonRpc("/shop/get_unit_price", 'call', {
                'product_ids': product_ids,
                'add_qty': value,
                'use_order_pricelist': true
            }).then(function(res) {
                //optional case
                openerp.jsonRpc("/shop/cart/update_json", 'call', {
                    'line_id': line_id,
                    'product_id': parseInt($input.data('product-id'), 10),
                    'set_qty': value
                }).then(function(data) {
                    $input.data('update_change', false);
                    if (value !== parseInt($input.val(), 10)) {
                        $input.trigger('change');
                        return;
                    }
                    if (!data.quantity) {
                        location.reload(true);
                        return;
                    }
                    var $q = $(".king_pro_cart_quantity");
                    $q.parent().parent().removeClass("hidden", !data.quantity);
                    $q.html(data.cart_quantity).hide().fadeIn(600);

                    $input.val(data.quantity);
                    $('.js_quantity[data-line-id=' + line_id + ']').val(data.quantity).html(data.quantity);
                    $("table#cart_total").replaceWith(data['website_sale.total']);
                    $("#king_hover_total").replaceWith(data['kingfisher_pro.hover_total']);
                });
            });
        });
    });

    // Switched to review section
    $('p.review').click(function() {
        $('body').animate({
            scrollTop: $(this).offset().top
        }, 1800);
        $('ul#description_reviews_tabs > li').removeClass('active')
        $('div#description_reviews_tabs_contents > div').removeClass('active')
        $('ul#description_reviews_tabs > li:nth-child(2)').addClass('active')
        $('div#description_reviews_tabs_contents > div:nth-child(2)').addClass('active')
    });

    // Toggle global search
    $('.st-toggle').on('click', function(e) {
        $('.search-toggle').toggleClass("search-toggle-open");
    });

});

// For Megamenu
$(document).on('click', '.mega-dropdown-menu', function(e) {
    e.stopPropagation()
});

//Equal height
$(window).load(function() {
    equal_height_all();
});

function equal_height_all() {
    function resetHeight() {
    
        var maxHeight = 0;
        jQuery(".cs-product .pwp-info .pwpi-title").height("auto").each(function() {
            maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
        }).height(maxHeight);
    }
    resetHeight();
    jQuery(window).resize(function() {
        resetHeight();
    });
}

$(document).ready(function() {
    $(".li-mega-menu > a .fa").click(function() {
        $(".li-mega-menu").toggleClass("open");
    });
});

