$(document).ready(function(){


    /* Load Map
     ========================= */
    (function () {
    
        // Set Maps location and theme
        var address = "New York";
        loadMap(address,"ultra-white"); // themes: 1.normal 2.ultra-white
    
    })();


    /* Browser capability
     ========================= */

    if ( (bowser.firefox && bowser.version < 52) || (bowser.msie && bowser.version <= 11)) {
        $("<link/>", {
           rel: "stylesheet",
           type: "text/css",
           href: "css/browser-fix.css"
        }).appendTo("head");
    }

    if( !bowser.msie && !bowser.msedge ) {
        imagesLoaded(document.querySelector('main'), function() {
            $('div.tilter').each(function(i, el) { 
                new TiltFx(el, [{}]);
            });
        });
    }


    /* Scoll Navigation
    ========================= */

    var NvScr = {
      
        offset : -10,

        init : function() {

            NvScr.topMenu = $(".menu");
            NvScr.topMenuHeight = NvScr.topMenu.outerHeight() + NvScr.offset;
            NvScr.menuItems =  NvScr.topMenu.find('a[href*="#"]'),

            NvScr.scrollItems = NvScr.menuItems.map(function(){
                var href = $(this).attr("href"),
                id = href.substring(href.indexOf('#')),
                item = $(id);
                if (item.length) { return item; }
            });

             // so we can get a fancy scroll animation
            NvScr.menuItems.on('click', NvScr.onClick);

            NvScr.shrinkMenu();

            $(window).on("scroll", NvScr.onScroll)

        },

        onClick : function(e) {

            // close menu on click for mobile phones
            var screen_width = $(window).innerWidth()
            if(screen_width < 769)
                $('.navbar-toggle').trigger('click');

            var href = $(this).attr("href"),
            id = href.substring(href.indexOf('#'));
            offsetTop = href === "#" ? 0 : $(id).offset().top - NvScr.topMenuHeight+1;
            $('html, body').stop().animate({ 
                scrollTop: offsetTop
            }, 1000);
            return false;
        },

        onScroll: function() {
            // Get container scroll position
           var fromTop = $(this).scrollTop() + NvScr.topMenuHeight;

           // Get id of current scroll item
           var cur = NvScr.scrollItems.map(function(){
             if ($(this).offset().top < fromTop)
               return this;
           });

           // Get the id of the current element
           cur = cur[cur.length-1];
           var id = cur && cur.length ? cur[0].id : "";               
           
           NvScr.menuItems.parent().removeClass("menu-item--current");
           if(id){
                NvScr.menuItems.parent().end().filter("[href*='#"+id+"']").parent().addClass("menu-item--current");
           }

           NvScr.shrinkMenu();
        },

        shrinkMenu: function() {
            var scrollTop = $(window).scrollTop();
            if ( scrollTop > 150 ) { 
                $("#menu-container").addClass("menu-scrolled");
            } else {
                $("#menu-container").removeClass("menu-scrolled");
            }
        }

    }

    NvScr.init();


    /* Owl Slider
    ========================= */
    $('#owl-carousel').owlCarousel({
        items: 1,
        singleItem: true,
        loop: true,
        autoplay:true
    });


    /* Owl Slider Client Testimonials
    =================================== */
    $('#owl-carousel-testimonials').owlCarousel({
        items: 1,
        singleItem: true,
        loop: true,
        autoplay:true
    });


    /* Owl Slider Our Latest Projects
    =================================== */
    $('#owl-carousel-projects').owlCarousel({
        items: 4,
        margin: 20,
        loop: false,
        navigation: false,
        dots: false,
        autoplay:false,

        responsive:{
            0:{
                items:1,
            },
            601:{
                items:2
            },
            992:{
                items:3
            },
            1200:{
                items:4
            }
        },
        responsiveRefreshRate : 50,
    });

    /* Owl Slider Our Team
    =================================== */
    $('#owl-carousel-team').owlCarousel({
        items: 4,
        margin: 10,
        loop: false,
        navigation: false,
        dots: false,
        autoplay:false,

        responsive:{
            0:{
                items:1,
            },
            601:{
                items:2,
            },
            992:{
                items:3,
            },
            1200:{
                items:4
            }
        },
        responsiveRefreshRate : 50,
    });

    // Preloader
    imagesLoaded(document.querySelector('body'), function() {
        $("#preloader").css('display','none');
    })

    /* Contact Form
     ========================= */
    $(function () { 

        var contact_form = $("#contact-form");
        var contact_response = $(".mail-response");

        contact_form.on('submit', function(e) {

            contact_response.html("&nbsp;");
            $.ajax({
                url: 'contact.php',
                type: 'POST',
                data: contact_form.serialize(),
                success : function(res) {
                    console.log(res);
                    console.log(res.status);
                    if(res.status === 401 || res.status === 400 )
                        contact_response.addClass("error-msg");
                    else if(res.status === "200") {
                        console.log("this is called")
                        contact_response.addClass("success-msg");
                    }

                    contact_response.html(res.message);
                }
            });

            e.preventDefault();
        });

    });

});