(function ($) {
    "use strict";

    //Selectbox
    $(document).ready(function () {
        if (jQuery.type(jQuery.fn.selectBox) !== "undefined") {
            $(".orderby, #size, #inch, #color").selectbox({
                effect: "fade"
            });
            $(".search-recipe-content .col-sm-4 select").selectbox({
                effect: "fade"
            });
        }
    });

    /* Product Grid, List Switch */
    var listSwitcher = function () {
        var activeClass = 'switcher-active';
        var gridClass = 'product-grid';
        var listClass = 'product-list';
        jQuery('.switchToList').on('click', function () {
            if (!jQuery.cookie('products_page') || jQuery.cookie('products_page') == 'grid') {
                switchToList();
            }
        });
        jQuery('.switchToGrid').on('click', function () {
            if (!jQuery.cookie('products_page') || jQuery.cookie('products_page') == 'list') {
                switchToGrid();
            }
        });

        function switchToList() {
            jQuery('.switchToList').addClass(activeClass);
            jQuery('.switchToGrid').removeClass(activeClass);
            jQuery('.archive_switch').fadeOut(300, function () {
                jQuery(this).removeClass(gridClass).addClass(listClass).fadeIn(300);
                jQuery.cookie('products_page', 'list', {expires: 3, path: '/'});
            });
        }

        function switchToGrid() {
            jQuery('.switchToGrid').addClass(activeClass);
            jQuery('.switchToList').removeClass(activeClass);
            jQuery('.archive_switch').fadeOut(300, function () {
                jQuery(this).removeClass(listClass).addClass(gridClass).fadeIn(300);
                jQuery.cookie('products_page', 'grid', {expires: 3, path: '/'});
            });
        }
    }

    var check_view_mod = function () {
        var activeClass = 'switcher-active';
        if (jQuery.cookie('products_page') == 'grid') {
            jQuery('.archive_switch').removeClass('product-list').addClass('product-grid');
            jQuery('.switchToGrid').addClass(activeClass);
        } else if (jQuery.cookie('products_page') == 'list') {
            jQuery('.archive_switch').removeClass('product-grid').addClass('product-list');
            jQuery('.switchToList').addClass(activeClass);
        }
        else {
            jQuery('.switchToGrid').addClass(activeClass);
            jQuery('.archive_switch').removeClass('product-list').addClass('product-grid');
        }
    }

    var miniCartHover = function () {
        jQuery("body").on("mouseenter mouseleave", ".menu-right .widget_shopping_cart", function (e) {
            var cart = jQuery('.menu-right .widget_shopping_cart');
            var cart_content = cart.find('.widget_shopping_cart_content');
            if (e.type == "mouseenter") {
                cart_content.stop().slideDown(200);
            } else {
                cart_content.stop().slideUp(200);
            }
        });
    }

    if (jQuery("body.woocommerce").length) {
        check_view_mod();
        listSwitcher();
    }
    miniCartHover();

    /* ****** PRODUCT QUICK VIEW  ******/
    var thim_quick_view = function () {
        $('.quick-view').on('click', function (e) {
            /* add loader  */
            $('.quick-view a').css('display', 'none');
            $(this).append('<a href="javascript:;" class="loading dark"></a>');
            var product_id = $(this).attr('data-prod');
            var data = {action: 'jck_quickview', product: product_id};
            $.post(ajaxurl, data, function (response) {
                $.magnificPopup.open({
                    mainClass: 'my-mfp-zoom-in',
                    items: {
                        src: '<div class="mfp-iframe-scaler">' + response + '</div>',
                        type: 'inline'
                    }
                });
                $('.quick-view a').css('display', 'inline-block');
                $('.loading').remove();
                $('.product-card .wrapper').removeClass('animate');
                setTimeout(function () {
                    $('.product-lightbox form').wc_variation_form();
                }, 600);
            });
            e.preventDefault();
        });
    }
    thim_quick_view();

    jQuery('.recipe-instructions p').on('click', function () {
        jQuery(this).next().css({'text-decoration': 'line-through', 'color': 'gray'});
    });

    jQuery('.list-ingredients p .checkbox').on('click', function () {
        jQuery(this).next().css({'text-decoration': 'line-through'});
    });

    var holder = jQuery('#ourHolder');
    var item = holder.children('div.item');

    jQuery('#filterOptions li a').on('click', function () {
        // fetch the class of the clicked item
        var ourClass = $(this).attr('class');

        // reset the active class on all the buttons
        jQuery('#filterOptions li').removeClass('active');
        // update the active state on our clicked button
        jQuery(this).parent().addClass('active');

        item.show('fast');

        if (ourClass == 'all') {
            // show all our items
            holder.prepend(item);
        }
        else {
            if (item.hasClass(ourClass)) {
                var show = holder.children('div.' + ourClass);
                holder.prepend(show);
            }
            else {
                var hide = holder.children('div:not(.' + ourClass + ')');
                holder.append(hide);
            }
            holder.children('div:not(.' + ourClass + ')').hide();
        }
        return false;
    });

    /*
     * Remove attribute frameBorder when embed video
     */
    jQuery('iframe').removeAttr('frameBorder').css('border', '0');

    /**
     * Gallery for product
     */
    $(document).ready(function () {
        if (jQuery.type(jQuery.fn.flexslider) !== "undefined" && jQuery('.thumbnail-product>.flexslider').length) {
            jQuery('.thumbnail-product>.flexslider').flexslider({
                animation: "slide",
                controlNav: "thumbnails"
            });
        }
    });

    var new_products = jQuery(".slide-new-products");
    if (jQuery(".slide-new-products").length) {
        new_products.owlCarousel({
            items: 4, //10 items above 1000px browser width
            itemsDesktop: [1000, 3], //5 items between 1000px and 901px
            itemsDesktopSmall: [900, 2], // betweem 900px and 601px
            itemsTablet: [600, 1], //2 items between 600 and 0
            itemsMobile: false, // itemsMobile disabled - inherit from itemsTablet option,
            mouseDrag: false
        });

        // Custom Navigation Events
        jQuery(".next").on('click', function () {
            new_products.trigger('owl.next');
        });
        jQuery(".prev").on('click', function () {
            new_products.trigger('owl.prev');
        });
    }

    $(".thim-owl-gallery").each(function () {
        var $this = $(this);
        if ($this.length) {
            $this.owlCarousel({
                items: 4, //10 items above 1000px browser width
                itemsDesktop: [1000, 3], //5 items between 1000px and 901px
                itemsDesktopSmall: [900, 2], // betweem 900px and 601px
                itemsTablet: [600, 1], //2 items between 600 and 0
                itemsMobile: false, // itemsMobile disabled - inherit from itemsTablet option,
                mouseDrag: true
            });

            $this.parent().find(".slider-next").on('click', function () {
                $this.trigger('owl.next');
            });
            $this.parent().find(".slider-prev").on('click', function () {
                $this.trigger('owl.prev');
            });
        }
    });

    jQuery(function ($) {
        if ($(window).width() > 768) {
            $('#masthead .navigation li.menu-item-has-children').on({
                mouseenter: function () {
                    $(this).children('.sub-menu').stop(true, false).slideDown(250);
                },
                mouseleave: function () {
                    $(this).children('.sub-menu').stop(true, false).slideUp(250);
                }
            });
        }
    });

    /* mobile menu */
    jQuery('.navbar-nav>li.menu-item-has-children >a,.navbar-nav>li.menu-item-has-children >span').after('<span class="icon-toggle"><i class="fa fa-angle-down"></i></span>');
    jQuery('.navbar-nav>li.menu-item-has-children .icon-toggle').on('click', function () {
        //alert('test');
        if (jQuery(this).next('ul.sub-menu').is(':hidden')) {
            jQuery(this).next('ul.sub-menu').slideDown(500, 'linear');
            jQuery(this).html('<i class="fa fa-angle-up"></i>');
        }
        else {
            jQuery(this).next('ul.sub-menu').slideUp(500, 'linear');
            jQuery(this).html('<i class="fa fa-angle-down"></i>');
        }
    });

    //Back To top
    var back_to_top = function () {
        jQuery(window).scroll(function () {
            if (jQuery(this).scrollTop() > 100) {
                jQuery('#back-to-top').css({bottom: "15px"});
            } else {
                jQuery('#back-to-top').css({bottom: "-100px"});
            }
        });
        jQuery('#back-to-top').on('click', function () {
            jQuery('html, body').animate({scrollTop: '0px'}, 800);
            return false;
        });
    }
    back_to_top();

    $(window).scroll(function () {
        var $header = $('#masthead.sticky-header');
        if ($(this).scrollTop() > 2) {
            $header.addClass('affix');
            $header.removeClass('affix-top');
        } else {
            $header.removeClass('affix');
            $header.addClass('affix-top');
        }
    });

    var post_audio = function () {
        $('.jp-jplayer').each(function () {
            var $this = $(this),
                url = $this.data('audio'),
                type = url.substr(url.lastIndexOf('.') + 1),
                player = '#' + $this.data('player'),
                audio = {};
            audio[type] = url;
            $this.jPlayer({
                ready: function () {
                    $this.jPlayer('setMedia', audio);
                },
                swfPath: 'jplayer/',
                cssSelectorAncestor: player
            });
        });
    }

    var post_gallery = function () {
        $('article.format-gallery .flexslider').imagesLoaded(function () {
            $('.flexslider').flexslider({
                slideshow: true,
                animation: 'fade',
                pauseOnHover: true,
                animationSpeed: 400,
                smoothHeight: true,
                directionNav: true,
                controlNav: false
            });
        });
    }
    $(document).ready(function () {
        post_gallery();
        post_audio();
    });

    /*Ajax search*/
    jQuery(document).on('click', function () {
        jQuery('.main-nav-search-form').hide();
        jQuery('.main-header-search-form-input').hide();
        jQuery('.header .row').removeClass('relative');
        jQuery('.navigation .table-cell').css({'opacity': 1});
    });
    jQuery('.header-search-close').on('click', function () {
        jQuery('.main-header-search-form-input').hide();
        jQuery('.navigation .table-cell').css({'opacity': 1});
    });
    jQuery('.main-nav-search-form').on('click', function (e) {
        e.stopPropagation();
    });
    jQuery('.main-header-search-form-input').on('click', function (e) {
        e.stopPropagation();
    });

    jQuery('.main-nav-search .search-link').on('click', function (e) {
        e.stopPropagation();
        if (jQuery('.main-nav-search-form').css('display') == 'block') {
            jQuery('.main-nav-search-form').hide();
        } else {
            jQuery('.main-nav-search-form').stop(true, true).slideDown(250);
            jQuery('.navbar-nav #s').focus();
        }
    });

    jQuery('.widget-smartsearch').on('click', function (e) {
        e.preventDefault();
        e.stopPropagation();
        if (jQuery('.main-header-search-form-input').css('display') == 'block') {
            jQuery('.main-header-search-form-input').hide();
        } else {
            jQuery('.main-header-search-form-input').stop(true, true).fadeIn(400);
            jQuery('#header-search-form-input #s').focus();
            jQuery('.navigation .table-cell').css({'opacity': 0});
        }
    });

    jQuery(document).ready(function () {
        jQuery('.ob-search-input').on('keyup', function (event) {

            clearTimeout(jQuery.data(this, 'timer'));
            if (event.which == 13) {
                event.preventDefault();
                jQuery(this).stop();
            } else if (event.which == 38) {
                if (navigator.userAgent.indexOf('Chrome') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Chrome') + 7).split(' ')[0]) >= 15) {
                    var selected = jQuery(".ob_selected");
                    jQuery(".ob_list_search li").removeClass("ob_selected");

                    // if there is no element before the selected one, we select the last one
                    if (selected.prev().length == 0) {
                        selected.siblings().last().addClass("ob_selected");
                    } else { // otherwise we just select the next one
                        selected.prev().addClass("ob_selected");
                    }
                }
            } else if (event.which == 40) {
                if (navigator.userAgent.indexOf('Chrome') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Chrome') + 7).split(' ')[0]) >= 15) {
                    var selected = jQuery(".ob_selected");
                    jQuery(".ob_list_search li").removeClass("ob_selected");

                    // if there is no element before the selected one, we select the last one
                    if (selected.next().length == 0) {
                        selected.siblings().first().addClass("ob_selected");
                    } else { // otherwise we just select the next one
                        selected.next().addClass("ob_selected");
                    }
                }
            } else if (event.which == 27) {
                jQuery('.main-header-search-form-input').hide();
                jQuery('.navigation .table-cell').css({'opacity': 1});
                jQuery('.header-search-close').html('<i class="fa fa-times"></i>');
                jQuery('.ob_list_search').html('');
                jQuery(this).val('');
                jQuery(this).stop();
            } else {
                jQuery(this).data('timer', setTimeout(search, 1000));
            }
        });
        jQuery('.ob-search-input').on('keypress', function (event) {

            if (event.keyCode == 13) {
                var selected = jQuery(".ob_selected");
                if (selected.length > 0) {
                    var ob_href = selected.find('a').first().attr('href');
                    window.location.href = ob_href;
                } else {
                    jQuery(this).parent().submit();
                }
                event.preventDefault();
            }
            if (event.keyCode == 27) {
                jQuery('.main-header-search-form-input').hide();
                jQuery('.navigation .table-cell').css({'opacity': 1});
            }
            if (event.keyCode == 38) {
                var selected = jQuery(".ob_selected");
                jQuery(".ob_list_search li").removeClass("ob_selected");

                // if there is no element before the selected one, we select the last one
                if (selected.prev().length == 0) {
                    selected.siblings().last().addClass("ob_selected");
                } else { // otherwise we just select the next one
                    selected.prev().addClass("ob_selected");
                }
            }
            if (event.keyCode == 40) {
                var selected = jQuery(".ob_selected");
                jQuery(".ob_list_search li").removeClass("ob_selected");

                // if there is no element before the selected one, we select the last one
                if (selected.next().length == 0) {
                    selected.siblings().first().addClass("ob_selected");
                } else { // otherwise we just select the next one
                    selected.next().addClass("ob_selected");
                }
            }
        });
    });
    function search(waitKey) {
        var keyword = jQuery('.ob-search-input').val();

        if (keyword) {
            if (!waitKey && keyword.length < 3) {
                return;
            }
            jQuery('.header-search-close').html('<i class="fa fa-spinner fa-spin"></i>');
            jQuery.ajax({
                type: 'POST',
                data: 'action=result_search&keyword=' + keyword,
                url: thim_ob_ajax_url,
                success: function (html) {
                    var data_li = '';
                    var items = jQuery.parseJSON(html);
                    jQuery.each(items, function (index) {
                        if (index == 0) {
                            data_li += '<li class="ui-menu-item' + this['id'] + ' ob_selected"><a id="ui-id-' + this['id'] + '" class="ui-corner-all" href="' + this['guid'] + '"><i class="icon-page"></i><span class="search-title">' + this['title'] + '</span><span class="search-date">' + this['date'] + '</span></a></li>';
                        } else {
                            data_li += '<li class="ui-menu-item' + this['id'] + '"><a id="ui-id-' + this['id'] + '" class="ui-corner-all" href="' + this['guid'] + '"><i class="icon-page"></i><span class="search-title">' + this['title'] + '</span><span class="search-date">' + this['date'] + '</span></a></li>';
                        }
                    });
                    jQuery('.ob_list_search').html('').append(data_li);
                    jQuery('.header-search-close').html('<i class="fa fa-times"></i>');
                    jQuery('.ob_list_search').addClass('active');
                },
                error: function (html) {
                }
            });
        }
    }

    $("body").on('click', function (event) {
        var $target = $(event.target);
        if (!$target.parents().is("#header-search-form-input") && !$target.is("#header-search-form-input")) {
            $("body").find("#header-search-form-input .ob_list_search").html('');
            $("body").find("#header-search-form-input .ob_list_search").removeClass('active');
            $('#header-search-form-input input[type="text"]').val('');
        }
    });
    jQuery(document).ready(function () {
        if (navigator.userAgent.search('iPhone OS 8') > 0) {
            jQuery(window).resize(function () {
                jQuery('.content-pusher').height(jQuery(window).height() - jQuery('#wpadminbar').height());
            });
            jQuery('.content-pusher').height(jQuery(window).height() - jQuery('#wpadminbar').height()).css('overflow', 'scroll');
        }
    });
    jQuery(document).ready(function () {
        var $height = jQuery('.setHeight').height();
        jQuery('.separator .line').css("height", $height);
        jQuery(window).resize(function () {
            jQuery('.separator .line').css("height", '');
            $height = jQuery('.setHeight').height();
            jQuery('.separator .line').css("height", $height);
        });
    });
    jQuery(document).ready(function () {
        setTimeout(function () {
            jQuery(window).trigger('resize');
        }, 450);
        jQuery('.flexslider img').load(function () {
            jQuery(window).trigger('resize');
            console.log('resize window');
        });

        $(".fancybox").fancybox();
    });

    // Check to see if the 'Delete File' link exists on the page...
    if ($('a#thim_custom_attachment_delete').length === 1) {

        // Since the link exists, we need to handle the case when the user clicks on it...
        $('#thim_custom_attachment_delete').on('click', function (evt) {

            // We don't want the link to remove us from the current page
            // so we're going to stop it's normal behavior.
            evt.preventDefault();

            // Find the text input element that stores the path to the file
            // and clear it's value.
            $('#thim_custom_attachment_url').val('');

            // Hide this link so users can't click on it multiple times
            $(this).hide();

        });

    } // end if

	jQuery(document).ready(function () {
		function albert_fix_vc_full_width_row() {
			var $elements = jQuery('.rtl [data-vc-full-width="true"]');
			jQuery.each($elements, function () {
				var $el = jQuery(this);
				$el.css('right', $el.css('left')).css('right', 'auto');
			});
		}
		// Fixes rows in RTL
		jQuery(document).on('.rtl vc-full-width-row', function () {
			albert_fix_vc_full_width_row();
		});
		// Run one time because it was not firing in Mac/Firefox and Windows/Edge some times
		albert_fix_vc_full_width_row();
	});

})(jQuery);

jQuery(document).ready(function () {
    setTimeout(function () {
        jQuery(window).trigger('resize');
    }, 150);
    jQuery(window).resize(function () {
        var get_padding1 = jQuery('.rtl .content-area #main .vc_row-has-fill').css('left');
        var get_padding2 = jQuery('.rtl .content-area #main .vc_row-no-padding').css('left');
        if (get_padding1 != null) {
            var right = 0 - parseInt(get_padding1.replace('px','')) + 15;
            jQuery('.rtl .content-area #main .vc_row-has-fill').css('right', get_padding1);
            jQuery('.tp-parallax-wrap').css('right',right);
        }
        if (get_padding2 != null) {
            jQuery('.rtl .content-area #main .vc_row-no-padding').css('right', get_padding2);
        }
    });
});