$(document).ready(function() {
    $(document).foundation();
    var headerHeight;
    $('.search span.search-top').on('click', function(event) {
        event.stopPropagation();
        $('#search-block').animate({
            width: "toggle"
        });
    });

    $('#search-block').submit(function() {
        if ($.trim($("#search-block #search").val()) === "") {
            $(this).animate({
                width: "toggle"
            });
            return false;
        }
    });

    function navHeight(hdHeight) {
        $('nav ul.vertical.menu').css({
            'height': 'calc(100% - ' + hdHeight + 'px)'
        });
    };

    headerHeight = $('header').outerHeight();

    navHeight(headerHeight);
    $('body').css({
        'paddingTop': headerHeight
    });

    $('.to-next i.icon-Banner-mouse').on('click', function() {
        var nextDiv = $(this).parents('.carousel').next();
        $('html,body').animate({
            scrollTop: $(nextDiv).offset().top - $('header').outerHeight()
        }, 500)
    });
    setTimeout(function() {
        $('.navigation ').css({
            marginTop: $('header').outerHeight(),
            transition: 'all 0.5s ease'
        });
    }, 500);

    window.addEventListener("orientationchange", function(e) {
        window.setTimeout(function() {
            headerHeight = $('header').outerHeight();
            $('body').css({
                'paddingTop': headerHeight
            });
            $('.navigation ').css({
                marginTop: headerHeight
            });
            navHeight(headerHeight);
        }, 600);
    });

    $('.overview-column ').hoverdir({
        speed: 500,
    });
    $('header .login>a.button').click(function(e) {
        e.preventDefault();
        $(this).toggleClass('active');
        $(this).siblings('.header-form').toggleClass('active');
        $('header .form-wrap').slideToggle();
    });

    function mobileMenuAdj() {
        $(window).scroll(function() {
            var scroll = $(window).scrollTop();
            if (scroll >= 300) {
                $("header").addClass("small");
                $('.navigation ').css({
                    marginTop: $('header').outerHeight()
                });

            } else {
                $("header").removeClass("small");
                $('.navigation ').css({
                    marginTop: headerHeight
                });

            }
        });
    }

    $(window).on('changed.zf.mediaquery', function(event, newSize, oldSize) {
        if (Foundation.MediaQuery.atLeast('medium')) {
            mobileMenuAdj();
            navHeight(headerHeight);
        }
    });

    if (Foundation.MediaQuery.atLeast('medium')) {
        mobileMenuAdj();
        navHeight(headerHeight);
    }
    $('.count').countUp({
        delay: 10,
        time: 2000
    });

    function scrollTo() {
        var child = $('.smooth-scroll');
        child.on('click', function() {
            var secId = $(this).attr('href').split('#')[1];
            $('html,body').animate({
                scrollTop: $('#' + secId).offset().top - $('header').outerHeight()
            }, 1500);
        });
    };

    scrollTo();

    $('.client-list').owlCarousel({
        loop: true,
        margin: 60,
        nav: false,
        autoplay: true,
        responsive: {
            0: {
                items: 1,
                margin: 0
            },
            420: {
                items: 2,
                margin: 10
            },
            600: {
                items: 3,
                margin: 20
            },
            1000: {
                items: 4,
                margin: 40
            },
            1200: {
                items: 5
            }
        }
    });

    $('.forgot-password').click(function(e) {
        e.preventDefault();
        var parentItem = $(this).parents('.item');
        parentItem.addClass('hide-to-top');
        parentItem.siblings('.item').addClass('show-from-bottom');
    });
    $('.select-plan ul li').click(function() {
        $(this).parents('.select-plan').toggleClass('active');
        var curTab = $(this).attr('data-plan');
        $(this).siblings().slideToggle();
        $(this).parent().prepend(this);
        $('.plan-detail').hide();
        $('#' + curTab).show();
    });
    if ($(window).width() < 641) {
        $('.hamburger-menu').on('click', function() {
            $('.navigation').toggleClass('active');
        });
    };
    $('.table-scroll .vx-table').cardtable();

    $('.search-result .item-block').each(function() {
        if ($(this).children().length === 0) {
            $(this).remove();
        }
    });

    $('input[type="radio"]').click(function() {
        if ($(this).attr('id') == 'card-pay') {
            $('.card-details').addClass('active');
        } else {
            $('.card-details').removeClass('active');
        }
    });
    if ($('#card-pay').prop('checked')) {
        $('.card-details').addClass('active');
    }

    var inputTypes = {
        text: document.querySelectorAll('input[type="text"]'),
        password: document.querySelectorAll('input[type="password"]'),
        textarea: document.querySelectorAll('textarea')
    }

    $.each(inputTypes, function(key, value) {
        var valueArr = $.makeArray(value);
        $.each(valueArr, function(index, value) {
            if ($(value).val()) {
                $(this).attr('data-focus', 'true');
            } else {
                $(this).attr('data-focus', 'false')
            }
            $(value).on('change keydown paste input', function() {
                if ($(this).val()) {
                    $(this).attr('data-focus', 'true')
                } else {
                    $(this).attr('data-focus', 'false')
                }
            });

        });
    });

});