jQuery(document).ready(function () {
    jQuery('#check-menu').click(function () {
        jQuery("body, html").toggleClass('hidden');
    });
    OpenBox({
        wrap: '.open-wrap',
        link: '.drop-btn, .flag-icon, .country-head > span',
        box: '.drop-box',
        openClass: 'open',
        single: true
    });
    OpenBox({
        wrap: '.menu > li',
        link: '.drop-btn',
        box: '.drop-box',
        openClass: 'open',
        openBoxMenu: true
    });
    if (jQuery("#typer-text").length) {
        jQuery("#typer-text").typer({
            strings: jQuery("#typer-text").data('texts').split('|')
        });
    };
    jQuery('.simplebox').simplebox({
        overlay: {
            color: '#02061D',
            opacity: 0.6
        },
        linkClose: '.close-modal'
    });
    if(jQuery('#select-region-popup').length > 0){
        jQuery.simplebox('#select-region-popup', {
            linkClose: '.close-modal',
            duration: 0
        });
    };
    jQuery('.simplebox-lang').simplebox({
        overlay: {
            color: '#02061D',
            closeClick: false,
            opacity: 0.6
        },
        onOpen: function() {
            jQuery('html, body').css('overflow', 'hidden');
        },
        onClose: function() {
            jQuery('html, body').css('overflow', '');
        }
    });
    jQuery('.reviews-photos').slick({
        centerMode: true,
        centerPadding: '0',
        slidesToShow: 5,
        focusOnSelect: true,
        asNavFor: '.reviews-text',
        responsive: [
            {
                breakpoint: 719,
                settings: {
                    slidesToShow: 3,
                }
            }
        ]
    });
    jQuery('.reviews-text').slick({
        arrows: false,
        adaptiveHeight: true,
        asNavFor: '.reviews-photos'
    });
    jQuery('.customers-row-top').slick({
        variableWidth: true,
        arrows: false,
        speed: 10000,
        autoplay: true,
        autoplaySpeed: 0,
        cssEase: 'linear'
    });
    jQuery('.customers-row-bottom').slick({
        variableWidth: true,
        arrows: false,
        speed: 10000,
        autoplay: true,
        autoplaySpeed: 0,
        cssEase: 'linear',
        rtl: true
    });
    jQuery('.tabs').tabs();

    jQuery(".country-drop ul li .item-hold").click(function (e) {
        jQuery(".country-col").removeClass('open');
    });
    openClose();

    searchCountry();

    jQuery(window).scroll(function () {
        var height = jQuery(window).scrollTop();

        if (height > 75) {
            jQuery('#header').addClass('fixed-header');
        } else {
            jQuery('#header').removeClass('fixed-header');
        }
    });
    selectCountry();

    showVideo();

    submitSbForm();

    var hash = window.top.location.hash.substr(1);
    if (hash.length && hash == 'res') {
        jQuery('#modal-form-res-link').trigger('click');

        history.pushState('', document.title, window.location.pathname + window.location.search);
    }
    if (hash.length && hash == 'language') {
        jQuery('#modal-language-link').trigger('click');
    }

    Fancybox.bind("[data-fancybox]");
});

function OpenBox(obj) {
    jQuery(obj.wrap).each(function () {
        var hold = jQuery(this);
        var link = hold.find(obj.link);
        var box = hold.find(obj.box);
        var w = obj.w;
        var close = hold.find(obj.close);

        link.click(function () {
            //jQuery(obj.wrap).not(hold).removeClass(obj.openClass);
            if (!hold.hasClass(obj.openClass)) {
                hold.addClass(obj.openClass);
            } else {
                hold.removeClass(obj.openClass);
            }
            return false;
        });
        close.click(function () {
            hold.removeClass(obj.openClass);

            return false;
        });

        if (obj.single) {
            link.click(function () {
                jQuery(obj.wrap).not(hold).removeClass(obj.openClass);
            });
            hold.hover(function () {
                jQuery(this).addClass('hovering');
            }, function () {
                jQuery(this).removeClass('hovering');
            });
            jQuery(document).bind('touchstart mousedown', function (e) {
                if (!(jQuery(e.target).parents().index(hold) != -1 || jQuery(e.target).index(hold) != -1)) {
                    hold.removeClass(obj.openClass);
                }
            });
        } else if (obj.openBoxMenu) {
            link.click(function () {
                jQuery(obj.wrap).not(hold).removeClass(obj.openClass);
            });
        }
    });
};

function openClose() {
    var wrap = jQuery('.question-hold').each(function () {
        var boxed = jQuery(this);
        var link = boxed.find('.question-title');
        var boxOpen = boxed.find('.question-drop');

        boxOpen.css('display', 'none');
        boxed.removeClass('open')
        link.on('click', function (e) {
            if (!boxed.hasClass('open')) {
                boxed.addClass('open');
                boxOpen.slideUp(0).slideDown(400);
            } else {
                boxOpen.slideUp(400, function () {
                    boxed.removeClass('open');
                });
            }
            return false;
        });
    });
};

function searchCountry() {
    jQuery('.search-input').on('keyup', function (e) {
        var sq = jQuery(this).val().trim().toUpperCase();
        var items = jQuery(this).parents('.country-drop').find('li');
        items.each(function (index) {
            if (jQuery(this).text().trim().toUpperCase().indexOf(sq) > -1) {
                jQuery(this).show();
            } else {
                jQuery(this).hide();
            }
        });

        items = jQuery(this).parents('.country-drop').find('h3');
        items.each(function (index) {
            if (jQuery(this).text().trim().toUpperCase().indexOf(sq) > -1) {
                jQuery(this).show();
            } else {
                jQuery(this).hide();
            }
        });
    });
};

function selectCountry() {
    jQuery('.change-country ul .item-hold').click(function (e) {
        e.preventDefault();
        var url = window.location.href;
        var pos = url.indexOf('loc=');

        if(pos == -1){
            window.location.href += '?loc=' + encodeURIComponent(jQuery(this).text().trim().toLowerCase());
        } else {
            window.location.href = url.substr(0, (url.indexOf('loc=') + 4)) + encodeURIComponent(jQuery(this).text().trim().toLowerCase());
        }
        return false;
    });
    jQuery('.country-col ul .item-hold').click(function (e) {
        e.preventDefault();

        var li_element = jQuery(this).parent('li');
        var title = li_element.text().trim();
        var flag = li_element.find('.flag-icon').attr('src');
        var phone = li_element.find('a.phone').attr('href');
        var email = li_element.find('a.email').attr('href');
        var website = li_element.find('a.website').attr('href');

        var head = li_element.parents('.country-col').find('.country-head');

        head.find('span').text(title);
        head.find('.flag-icon').attr('src', flag);
        head.find('a.phone').attr('href', phone);
        head.find('a.email').attr('href', email);
        head.find('a.website').attr('href', website);

        if (jQuery('#map').length) {
            launchInfoWindow(li_element.attr('counter'));
        }
        return false;
    });
};

function showVideo() {
    jQuery(".videos-hold .simplebox, .video-link").click(function () {
        jQuery('#modal-video02 .video-hold').append(
            jQuery('<iframe>', {
                'id': 'player',
                'src': 'https://www.youtube-nocookie.com/embed/' + jQuery(this).attr('data-video') + '?autoplay=1&rel=0&modestbranding=1&showinfo=0',
                'frameborder': '0',
                'allow': 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture',
                'allowfullscreen': 'true'
            })
        );
    });
    jQuery(".modal-video .close-modal, .simplebox-overlay").click(function () {
        jQuery("#modal-video02 .video-hold").html(null);
    });
};

function submitSbForm() {
    jQuery('#footersbmt').submit(function (e) {
        e.preventDefault();
        var form = jQuery(this);
        jQuery.ajax({
            type: "POST",
            url: "",
            data: form.serialize(),
            complete: function () {
                form.find('input').val('');
                form.find('button').remove();
                form.append('Your email was successfully subscribed');
            }
        });
        return false;
    });
}
