currentModal = null;

jQuery(document).ready(function($) {
    "use strict";

    //Check to see if the window is top if not then display button
	$(window).scroll(function(){
		if ($(this).scrollTop() > 100) {
			$('.scrollToTop').fadeIn();
		} else {
			$('.scrollToTop').fadeOut();
		}
	});

    // Sidemenu One Page Smooth Scrolling
    $(function() {
        $('a[href*=#]:not([href=#])').click(function() {
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
                var target = $(this.hash);
                target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
                if (target.length) {
                    $('html,body').animate({
                        scrollTop: target.offset().top
                    }, 1000);
                }
            }
        });
    });
    var lastId,
    topMenu = $(".sidemenu-inner ul"),
    topMenuHeight = topMenu.outerHeight() + 15,
    // All list items
    menuItems = topMenu.find("a"),
    scrollItems = menuItems.map(function() {
        var item = $($(this).attr("href"));
        if (item.length) {
            return item;
        }
    });
    $(window).scroll(function() {
        // Get container scroll position
        var fromTop = $(this).scrollTop() + topMenuHeight;

        // Get id of current scroll item
        var cur = 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 : "";

        if (lastId !== id) {
            lastId = id;
            // Set/remove active class
            menuItems
                .parent().removeClass("selected")
                .end().filter("[href=#" + id + "]").parent().addClass("selected");
        }
    });

    // Sidemenu Toggle Button Animation
    var toggles = document.querySelectorAll(".c-hamburger");
    for (var i = toggles.length - 1; i >= 0; i--) {
      var toggle = toggles[i];
      toggleHandler(toggle);
    };
    function toggleHandler(toggle) {
      toggle.addEventListener( "click", function(e) {
        e.preventDefault();
        (this.classList.contains("is-active") === true) ? this.classList.remove("is-active") : this.classList.add("is-active");
      });
    }

    // Menu Opening and Body Blur
    $(".c-hamburger").on("click",function(){
        $(this).next(".sidemenu").toggleClass("slidein");
        $("body").toggleClass("menu-opened");
    });

    $("html").on("click",function(){
        $(".sidemenu").removeClass("slidein");
        $("body").removeClass("menu-opened");
        $(".c-hamburger").removeClass("is-active");
    });
    $(".c-hamburger, .sidemenu").on("click",function(e){
        e.stopPropagation();
    });
    $(".sidemenu li a").on("click",function(){
        setTimeout(function(){
            $(".sidemenu").removeClass("slidein");
            $("body").removeClass("menu-opened");
            $(".c-hamburger").removeClass("is-active");
        },1000);
    });

    // Categories Selectors
    $(".categories a").on("click",function(){
        $(this).addClass("selected");
        $(this).siblings().removeClass("selected");
    });

    // Materialize
    $('select').material_select();
    $('.materialboxed').materialbox();
    $('.parallax').parallax();
    //$(".portfolio-img > a").on("click",function(){
    //    $(this).parent().find(".materialboxed").trigger("click");
    //    return false;
    //});


    function loadModalCarousel(x) {
        var tmpModal = $(x).attr('data-modal');
        var modalCarousel = $('#' + tmpModal).find("div[class^='modal-carousel-']");
        var itemsAmount = $(modalCarousel).find('a').length;

        setTimeout(function() {
            $(modalCarousel).owlCarousel({
                loop:itemsAmount > 3 ? true : false,
                dots:false,
                nav: itemsAmount > 3 ? true : false,
                margin:30,
                mouseDrag:true,
                items: itemsAmount > 3 ? 3 : itemsAmount,
                navText: ['<i class="fa fa-long-arrow-left"></i>', '<i class="fa fa-long-arrow-right"></i>'],
                responsive:{
                    1200:{items:itemsAmount > 3 ? 3 : itemsAmount},
                    992:{items:itemsAmount > 3 ? 3 : itemsAmount},
                    600:{items:itemsAmount > 3 ? 2 : itemsAmount},
                    0:{items:1}
                }
            });
        }, 200);
    }

    function openModalOnNextPrev(currentModal) {
        var itemsAmount = $(currentModal).find('a').length;
        setTimeout(function() {
            $(currentModal).find("div[class^='modal-carousel-']").owlCarousel({
                loop:itemsAmount > 3 ? true : false,
                dots:false,
                nav: itemsAmount > 3 ? true : false,
                margin:30,
                mouseDrag:true,
                items: itemsAmount > 3 ? 3 : itemsAmount,
                navText: ['<i class="fa fa-long-arrow-left"></i>', '<i class="fa fa-long-arrow-right"></i>'],
                responsive:{
                    1200:{items:itemsAmount > 3 ? 3 : itemsAmount},
                    992:{items:itemsAmount > 3 ? 3 : itemsAmount},
                    600:{items:itemsAmount > 3 ? 2 : itemsAmount},
                    0:{items:1}
                }
            });
        }, 200);
    }

    //service modal
    $(document).on('click', '.innovative-service > h4', function() {
        var attr = $(this).attr('data-modal');
        if (typeof attr !== typeof undefined && attr !== false) {
            var popupNumber = $(this).attr('data-modal');
            currentModal = $('#' + popupNumber);
            currentModal.openModal({
                'ready': loadModalCarousel($(this))
            });
        }
    });

    $(document).on('click', '.close-modal-btn', function() {
        $(this).parent().closeModal();
    });

    $(document).on('click', '.modal .next', function() {
       var nextAll = $(currentModal).closest("div[id^='service']").nextAll();

        if (nextAll.length == 0) {
            nextAll = $(currentModal).parent().find("div[id^='service']").first();
        }

        $(currentModal).closeModal();
        currentModal = $(nextAll).first();
        currentModal.openModal({
            'ready': openModalOnNextPrev(currentModal)
        });
    });

    $(document).on('click', '.modal .prev', function() {
        var prevAll = $(currentModal).closest("div[id^='service']").prevAll();

        if (prevAll.length == 0) {
            prevAll = $(currentModal).parent().find("div[id^='service']").last();
        }

        $(currentModal).closeModal();
        currentModal = $(prevAll).first();
        currentModal.openModal({
            'ready': openModalOnNextPrev(currentModal)
        });
    });

    //change contact from 7 subit color
    if ($('#formButtonColor').length) {
        var lc = $("#formButtonColor").data('color');
        $(".wpcf7-submit").css("background", lc);
    }


}); // Document.Ready Ends Here

jQuery(window).load(function(){
    "use strict";

    // Map Height
    if ($('.map').length) {
        $(".map > div").each(function () {
            var map_height = $(this).parent().parent().siblings().find(".get-in-touch").innerHeight();
            $(this).css({
                "height": map_height
            });
        });
    }

    // Column Title Height
    $(".column-title").each(function(){
        var column_title_height = $(this).parent().siblings().innerHeight();
        $(this).css({
            "height":column_title_height
        });
    });

    // Related Carousel
    $('.related-carousel').owlCarousel({
        autoplay:true,
        autoplayTimeout:2500,
        smartSpeed:2000,
        autoplayHoverPause:true,
        loop:true,
        dots:false,
        nav:true,
        margin:30,
        mouseDrag:true,
        items:3,
        responsive:{
            1200:{items:3},
            992:{items:3},
            600:{items:2},
            0:{items:1}
        }
    });

    // Testimonials Carousel
    var total_testimonail = $(".testimonials-carousel .owl-stage").find(".owl-item").length;
    $(".total").html(total_testimonail);
    $(".current").html('1');
    $(".testimonials-carousel").on('change.owl.carousel',function(){
        setTimeout(function(){
            var current_testimonail = $(".testimonials-carousel .owl-stage").find(".owl-item.active").index()+1;
            $(".current").html(current_testimonail);
        },300);
    });

    $('.modal-trigger').leanModal();


    //hack do menu w podstronach
    $.each($('.menu-item >'), function(k, link) {
        $(link).attr('href', '/' + $(link).attr('href'));
    });


    if (window.location.hash) {
        $('html,body').animate({
            scrollTop: $(window.location.hash).offset().top
        }, 1000);
    }

    if ($('.post-pagi .disabled').length) {
        $('.post-pagi .disabled').removeAttr('href');
    }


    if ($('.magnific-gallery').length) {
        $('.magnific-gallery').magnificPopup({
            type: 'image',
            delegate: 'a',
            gallery: {
                enabled: true
            }
        });
    }

});

$('.masonary').masonry().on( 'layoutComplete', function() {

    $(".masonary > div").removeClass("red");


    var temp = [];
    var ptop = '0px';
    var filter2 = ":odd";
    $(".masonary>div:visible").each(function() {
        if ($(this).css('top') != ptop) {
            $(temp).filter(filter2).addClass('red');
            filter2 = (filter2 == ":odd") ? ":even" : ":odd";
            temp = [];
            ptop = $(this).css('top');
        }
        temp.push(this);
        if ($(this).is(":last-child")) {
            $(temp).filter(filter2).addClass('red');
        }
    });
});