$(document).ready(function() {
    var headerHeight;
    $('.search span.search-top').on('click', function() {
        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').on('click', function() {
        var nextDiv = $(this).parent().next();
        $('html,body').animate({
            scrollTop: $(nextDiv).offset().top - headerHeight
        }, 500)
    });

    $('.navigation ').css({
        marginTop: headerHeight,
        transition: 'all 0.5s ease'
    });
    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,
    });

    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(parent) {
        $(parent).each(function() {
            var child = $(this).find('.smooth-scroll');
            var secId = child.attr('href');
            child.on('click', function() {
                $('html,body').animate({
                    scrollTop: $(secId).offset().top - $('header').outerHeight()
                }, 1500);
            });
        });
    };
    scrollTo('.overview .cell');
    scrollTo('.menu.vertical > li');

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