// Owl slider
odoo.define('kingfisher.custom_js', function (require) {
"use strict";

    var ajax = require('web.ajax');
    $(document).ready(function() {
        $('.oe_website_sale').each(function() {
            var oe_website_sale = this;

            $(oe_website_sale).on("change", ".oe_cart input.js_quantity", function() {
                setTimeout("location.reload();", 1000);
            });
            $(oe_website_sale).on('click', 'a.js_add_cart_json_new', function(ev) {
                ev.preventDefault();
                var $link = $(ev.currentTarget);
                var $input = $link.parent().parent().find("input");
                var value = parseInt(0, 10);
                var line_id = parseInt($input.data('line-id'), 10);
                if (isNaN(value)) value = 0;
                ajax.jsonRpc("/shop/cart/update_json", 'call', {
                        'line_id': line_id,
                        'product_id': parseInt($input.data('product-id'), 10),
                        'set_qty': value
                    })
                    .then(function(data) {
                        if (!data.quantity) {
                            location.reload();
                            return;
                        }
                    });
            });
        });


        $('.owl-carousel').owlCarousel({
            loop: true,
            margin: 30,
            responsiveClass: true,
            responsive: {
                0: {
                    items: 1,
                    nav: false,
                    dots: true
                },
                480: {
                    items: 2,
                    nav: false,
                    dots: true
                },
                768: {
                    items: 3,
                    nav: false,
                    dots: true
                },
                1000: {
                    items: 4,
                    nav: true,
                    loop: false,
                    dots: false
                }
            }
        })

    });

    // header fix
    $(window).scroll(function() {
        if ($(window).scrollTop() >= 110) {
            $('.nav-top').addClass('navbar-fixed-top');
        } else {
            $('.nav-top').removeClass('navbar-fixed-top');
        }
        // if ($('#oe_main_menu_navbar')[0]) {
        //     $('.nav-top').attr('style', 'top: 34px;');
        // } else {
        //     $('.nav-top').attr('style', 'top: 0px;');
        // }
    });


    /*function equalHeight(group) {
    	var tallest = 0;
    	group.each(function() {
    		var thisHeight = $(this).height();
    		if(thisHeight > tallest) {
    			tallest = thisHeight;
    		}
    	});
    	group.height(tallest);
    }

    $(document).ready(function() {
    	equalHeight($(".product .title"));
    	equalHeight($(".product-img"));
    });

    $(window).resize(function() {
        equalHeight($(".product .title"));
        equalHeight($(".product-img"));
    });*/


    $(document).ready(function() {
        var images = Array();

        jQuery(".product-img").each(function(index, ele) {
            images.push(ele.children[0].src);
        });


        jQuery.imgpreload(images, function() {
            function resetHeight() {
                var maxHeight = 0;
                jQuery(".product .title").height("auto").each(function() {
                    maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
                }).height(maxHeight);
            }
            /*resetHeight();*/
            /* jQuery(window).resize(function () {
                 resetHeight();
             });*/

            function resetHeight1() {
                var maxHeight = 0;
                jQuery(".product-img").height("auto").each(function() {
                    maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
                }).height(maxHeight);
            }
            resetHeight1();

            function resetHeight2() {
                var maxHeight = 0;
                jQuery(".product .price-box .oe_subdescription").height("auto").each(function() {
                    maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
                }).height(maxHeight);
            }
            resetHeight2();

            jQuery(window).resize(function() {
                resetHeight();
                resetHeight1();
            });

            resetHeight();
            resetHeight1();
            resetHeight2();

        });
    });



    /**
     * jquery.imgpreload 1.6.2 <https://github.com/farinspace/jquery.imgpreload>
     * Copyright 2009-2014 Dimas Begunoff <http://farinspace.com>
     * License MIT <http://opensource.org/licenses/MIT>
     */
    if ('undefined' != typeof jQuery) {
        (function($) {
            'use strict';

            // extend jquery (because i love jQuery)
            $.imgpreload = function(imgs, settings) {
                settings = $.extend({}, $.fn.imgpreload.defaults, (settings instanceof Function) ? {
                    all: settings
                } : settings);

                // use of typeof required
                // https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Instanceof_Operator#Description
                if ('string' == typeof imgs) {
                    imgs = [imgs];
                }

                var loaded = [];

                $.each(imgs, function(i, elem) {
                    var img = new Image();

                    var url = elem;

                    var img_obj = img;

                    if ('string' != typeof elem && typeof elem != 'undefined') {
                        url = $(elem).attr('src') || $(elem).css('background-image').replace(/^url\((?:"|')?(.*)(?:'|")?\)$/mg, "$1");

                        img_obj = elem;
                    }

                    $(img).bind('load error', function(e) {
                        loaded.push(img_obj);

                        $.data(img_obj, 'loaded', ('error' == e.type) ? false : true);

                        // http://msdn.microsoft.com/en-us/library/ie/tkcsy6fe(v=vs.94).aspx
                        if (settings.each instanceof Function) {
                            settings.each.call(img_obj, loaded.slice(0));
                        }

                        // http://jsperf.com/length-in-a-variable
                        if (loaded.length >= imgs.length && settings.all instanceof Function) {
                            settings.all.call(loaded);
                        }

                        $(this).unbind('load error');
                    });

                    img.src = url;
                });
            };

            $.fn.imgpreload = function(settings) {
                $.imgpreload(this, settings);

                return this;
            };

            $.fn.imgpreload.defaults = {
                each: null, // callback invoked when each image is loaded
                all: null // callback invoked when all images have loaded
            };

        })(jQuery);
    }
});