;(function ($) {

    "use strict";

    /* ===================
     Page reload
     ===================== */
    var scroll_top;
    var window_height;
    var window_width;
    var scroll_status = '';
    var lastScrollTop = 0;
    $(window).on('load', function () {
        $(".cms-loader").fadeOut("slow");
        window_width = $(window).width();
        raworganic_col_offset();
        raworganic_header_sticky();
        raworganic_menu_mobile();
        raworganic_rtl();
        raworganic_scroll_to_top();
    });
    $(window).on('resize', function () {
        window_width = $(window).width();
        raworganic_col_offset();
        raworganic_header_sticky();
        raworganic_menu_mobile();
    });

    $(window).on('scroll', function () {
        scroll_top = $(window).scrollTop();
        window_height = $(window).height();
        window_width = $(window).width();
        if (scroll_top < lastScrollTop) {
            scroll_status = 'up';
        } else {
            scroll_status = 'down';
        }
        lastScrollTop = scroll_top;
        raworganic_header_sticky();
        raworganic_scroll_to_top();
    });

    $(document).ready(function () {

        /* =================
         Menu Dropdown
         =================== */
        var $menu = $('.main-navigation');
        $menu.find('ul.sub-menu > li').each(function () {
            var $submenu = $(this).find('>ul');
            if ($submenu.length == 1) {
                $(this).hover(function () {
                    if ($submenu.offset().left + $submenu.width() > $(window).width()) {
                        $submenu.addClass('back');
                    } else if ($submenu.offset().left < 0) {
                        $submenu.addClass('back');
                    }
                }, function () {
                    $submenu.removeClass('back');
                });
            }
        });

        $('.sub-menu .current-menu-item').parents('.menu-item-has-children').addClass('current-menu-ancestor');
        /* =================
         Menu Mobile
         =================== */
        $("#main-menu-mobile .open-menu").on('click', function () {
            $(this).toggleClass('opened');
            $('#site-navigation').toggleClass('navigation-open');
        })

        /* ===================
         Search Toggle
         ===================== */
        $('.h-btn-form').click(function (e) {
            e.preventDefault();
            $('.cms-modal-contact-form').removeClass('remove').toggleClass('open');
        });
        $('.cms-close').click(function (e) {
            e.preventDefault();
            $(this).parent().addClass('remove').removeClass('open');
            $(this).parents('.cms-modal').addClass('remove').removeClass('open');
            $(this).parents('#page').find('.site-overlay').removeClass('open');
        });
        $(document).on('click', function (e) {
            if (e.target.className == 'cms-modal cms-modal-contact-form open')
                $('.cms-modal-contact-form').removeClass('open').addClass('remove');
        });

        /* Video 16:9 */
        $('.entry-video iframe').each(function () {
            var v_width = $(this).width();

            v_width = v_width / (16 / 9);
            $(this).attr('height', v_width + 35);
        });
        /* Images Light Box - Gallery:True */
        $('.images-light-box').each(function () {
            $(this).magnificPopup({
                delegate: 'a.light-box',
                type: 'image',
                gallery: {
                    enabled: true
                },
                mainClass: 'mfp-fade',
            });
        });

        $('.image-light-box').each(function () {
            $(this).magnificPopup({
                delegate: 'a.light-box',
                type: 'image',
                gallery: {
                    enabled: false
                },
                mainClass: 'mfp-fade',
            });
        });

        /* Video Light Box */
        $('.cms-video-button, .btn-video').magnificPopup({
            type: 'iframe',
            mainClass: 'mfp-fade',
            removalDelay: 160,
            preloader: false,

            fixedContentPos: false
        });

        /* ===================
        Theia Sticky Sidebar
        ===================== */
        var sidebarOffset = $('#headroom').outerHeight();
        $(".sidebar-fixed-").theiaStickySidebar({
            "containerSelector": "",
            "additionalMarginTop": sidebarOffset,
            "additionalMarginBottom": "0",
            "updateSidebarHeight": false,
            "minWidth": "768",
            "sidebarBehavior": "modern"
        });
        
        /* ====================
         Scroll To Top
         ====================== */
        $('.scroll-top').click(function () {
            $('html, body').animate({scrollTop: 0}, 800);
            return false;
        });

        /* =================
        Add Class
        =================== */
        $('.wpcf7-select').parent().addClass('wpcf7-menu');
        /* =================
         Row & VC Column Animation
         =================== */
        $('.vc_row.wpb_row.vc_row-fluid').each(function () {
            var vctime = 100;
            var vc_inner = $(this).children().length;
            var _vci = vc_inner - 1;
            $(this).find('> .wpb_animate_when_almost_visible').each(function (index, obj) {
                $(this).css('animation-delay', vctime + 'ms');
                if (_vci === index) {
                    vctime = 100;
                    _vci = _vci + vc_inner;
                } else {
                    vctime = vctime + 100;
                }
            })
        });
        $('.animation-time').each(function () {
            var vctime = 100;
            var vc_inner = $(this).children().length;
            var _vci = vc_inner - 1;
            $(this).find('> .grid-item > .wpb_animate_when_almost_visible').each(function (index, obj) {
                $(this).css('animation-delay', vctime + 'ms');
                if (_vci === index) {
                    vctime = 100;
                    _vci = _vci + vc_inner;
                } else {
                    vctime = vctime + 40;
                }
            });
        });
        $(document).on('click', '.card-header', function () {
            var _parent = $(this).parents('.grid-item');
            _parent.css('z-index', 9999);
            setTimeout(function () {
                _parent.css('z-index', 1);
            }, 800);
            setTimeout(function () {
                $('.filter-item.active').trigger('click');
            }, 400);
        });

        $(document).on('change', '.raw-archive-sort', function () {
            $(this).parents('form.archive-ordering').submit();
        });

        /* =================
         The clicked item should be in center in owl carousel
         =================== */
        var $owl_item = $('.owl-active-click');
        $owl_item.children().each(function (index) {
            $(this).attr('data-position', index);
        });
        $(document).on('click', '.owl-active-click .owl-item > div', function () {
            $owl_item.trigger('to.owl.carousel', $(this).data('position'));
        });

        /* =================
         Multi Select
         =================== */
        $('select:not(.country_select):not(.state_select):not(#billing_invoice_type)').each(function () {
            $(this).niceSelect();
        });

        /* Newsletter */
        $('.tnp-field-email').each(function () {
            var email_text = $(this).find('label').text();
            $(this).find('label').remove();
            $(this).find(".tnp-email").each(function (ev) {
                if (!$(this).val()) {
                    $(this).attr("placeholder", email_text);
                }
            });
        });

        $('.tnp-field-firstname').each(function () {
            var firstname_text = $(this).find('label').text();
            $(this).find('label').remove();
            $(this).find(".tnp-firstname").each(function (ev) {
                if (!$(this).val()) {
                    $(this).attr("placeholder", firstname_text);
                }
            });
        });

        $('.tnp-field-lastname').each(function () {
            var lastname_text = $(this).find('label').text();
            $(this).find('label').remove();
            $(this).find(".tnp-lastname").each(function (ev) {
                if (!$(this).val()) {
                    $(this).attr("placeholder", lastname_text);
                }
            });
        });

        $('.layout-list').on('click', function () {
            $(this).addClass('active').parents('.archive-topbar').find('.layout-grid').removeClass('active');
            $(this).parents('.site-main').find('ul.products').addClass('products-list');
            $(this).parents('.site-main').find('ul.products .woocommerce-product-inner').addClass('product-layout-list');
            $(this).parents('.site-main').find('.recipe-layout-grid').removeClass('recipe-layout-grid').addClass('recipe-layout-list');
        });
        $('.product-layout-list').parents('.site-main').find('.archive-topbar .layout-list').addClass('active');
        $('.product-layout-list').parents('.site-main').find('.archive-topbar .layout-grid').removeClass('active');
        $('.layout-grid').on('click', function () {
            $(this).addClass('active').parents('.archive-topbar').find('.layout-list').removeClass('active');
            $(this).parents('.site-main').find('ul.products').removeClass('products-list');
            $(this).parents('.site-main').find('ul.products .woocommerce-product-inner').addClass('product-layout-list');
            $(this).parents('.site-main').find('.recipe-layout-list').removeClass('recipe-layout-list').addClass('recipe-layout-grid');
        });
        $('.vc_row-o-equal-height .col-equal-height').matchHeight();
        $('.vc_row-o-equal-height .cms-fancybox-layout7').matchHeight();
        $('.recipe-body .recipe-item-inner').matchHeight();
        $('.recipe-body .recipe-item-inner .recipe-title').matchHeight();
        $('.cms-recipe-carousel .recipe-title').matchHeight();
        $('.vc_row .cms-fancybox-layout1').matchHeight();
        $('.vc_row .cms-fancybox-layout1').matchHeight();
        $('.vc_row .cms-fancybox-list').matchHeight();
        $('.vc_row .cms-fancybox-layout8').matchHeight();
        $('.vc_row .cms-fancybox-layout9').matchHeight();
        /**
         * Reviews
         */

        $('.raw-star').hover(function () {
                var _this = $(this);
                var _index = $(this).index();
                $('.raw-rating-hover').html(review_hover[_index]);
                for (var i = 1; i <= 5; i++) {
                    if (i <= _index) {
                        var _star = _this.parent().find('span').eq(i);
                        _star.removeClass("zmdi-star-outline").addClass("zmdi-star");
                    } else {
                        var __star = _this.parent().find('span').eq(i);
                        __star.removeClass("zmdi-star").addClass("zmdi-star-outline");
                    }
                }
            },
            function () {
            });
        $('.raw-rating-select').hover(
            function () {
            },
            function () {
                $('.raw-rating-hover').html($('.raw-rating-value').attr('data-hover'));
                var _this = $(this);
                var _vote_val = $('.raw-rating-value').val();
                if (_vote_val === '') {
                    _vote_val = 0;
                }
                for (var i = 0; i < 5; i++) {
                    if (i < _vote_val) {
                        var _star = _this.parent().find('span').eq(i);
                        _star.removeClass("zmdi-star-outline").addClass("zmdi-star");
                    } else {
                        var __star = _this.parent().find('span').eq(i);
                        __star.removeClass("zmdi-star").addClass("zmdi-star-outline");
                    }
                }
            }
        );
        $(document).on('click', '.raw-star', function (e) {
            e.preventDefault();
            var _vote_val = $(this).index();
            $('.raw-rating-value').attr('data-hover', review_hover[_vote_val]);
            $('.raw-rating-hover').html(review_hover[_vote_val]);
            $('.raw-rating-value').val(parseInt(_vote_val) + 1);
        });
        $(document).on('click', '.raw-rv-submit', function (e) {
            e.preventDefault();
            var _check = true,
                _this = $(this),
                _rv_content = $('#raw-rate-content'),
                _rv_name = $('#raw-rate-name'),
                _rv_email = $('#raw-rate-email');
            _rv_content.removeClass('raw-error');
            _rv_name.removeClass('raw-error');
            _rv_email.removeClass('raw-error');
            if (_rv_content.val().length === 0) {
                _rv_content.addClass('raw-error');
                _check = false;
            }
            if (_rv_name.length === 1 && _rv_name.val().length === 0) {
                _rv_name.addClass('raw-error');
                _check = false;
            }
            if (_rv_email.length === 1 && _rv_email.val().length === 0) {
                _rv_email.addClass('raw-error');
                _check = false;
            }
            if (_check) {
                _this.parents('form').submit();
            }
        });
        $(document).on('click', '.raw-alert-close', function (e) {
            e.preventDefault();
            $(this).parents('.raw-alert').remove();
        });
        setTimeout(function () {
            if(!$(document).find('.raw-alert').hasClass('secret')){
                $('.raw-alert').remove();
            }
        }, 5000);

    });

    /* =================
     Column Absolute
     =================== */
    function raworganic_col_offset() {
        var w_vc_row_lg = ($('#content').width() - 1230) / 2;
        if (window_width > 1200) {
            $('.col-offset-right > .vc_column-inner').css('padding-right', w_vc_row_lg + 'px');
            $('.col-offset-left > .vc_column-inner').css('padding-left', w_vc_row_lg + 'px');
            $('.col-offset-right > .col-offset-inner').css('padding-right', w_vc_row_lg + 'px');
            $('.col-offset-left > .col-offset-inner').css('padding-left', w_vc_row_lg + 'px');
        }
    }

    function raworganic_header_sticky() {
        var offsetTop = $('#site-header-wrap').outerHeight();
        var h_header = $('.fixed-height').outerHeight();
        var offsetTopAnimation = offsetTop + 200;
        if (scroll_status == 'down' && scroll_top > offsetTopAnimation) {
            $('#headroom').addClass('headroom--down').removeClass('headroom--up');
            $('body.header-sticky').addClass('headroom--down').removeClass('headroom--up');
        }
        if (scroll_status == 'up' && scroll_top > offsetTopAnimation) {
            $('#headroom').addClass('headroom--up').removeClass('headroom--down');
            $('body.header-sticky').addClass('headroom--up').removeClass('headroom--down');
        } else if (scroll_status == 'up' && scroll_top < offsetTopAnimation) {
            $('#headroom').removeClass('headroom--up');
            $('body.header-sticky').removeClass('headroom--up');
        }
        if (window_width > 992) {
            $('.fixed-height').css({
                'height': h_header
            });
        }
    }

    function raworganic_menu_mobile() {
        if ($(window).width() < 992) {
            $('.main-navigation li.menu-item-has-children').append('<span class="main-menu-toggle"></span>');
            $('.main-menu-toggle').on('click', function () {
                $(this).parent().find('> .sub-menu').toggleClass('submenu-open');
                $(this).parent().find('> .sub-menu').slideToggle();
            });
        }
    }

    function raworganic_rtl() {
        /* =================
        RTL
        =================== */
        if ($('html').attr('dir') == 'rtl') {
            $('[data-vc-full-width="true"]').each(function (i, v) {
                $(this).css('right', $(this).css('left')).css('left', 'auto');
            });
        }
    }

    /* ====================
     Scroll To Top
     ====================== */
    function raworganic_scroll_to_top() {
        if (scroll_top < window_height) {
            $('.scroll-top').addClass('off').removeClass('on');
        }
        if (scroll_top > window_height) {
            $('.scroll-top').addClass('on').removeClass('off');
        }
    }

})(jQuery);
