/* Optional script description */

$().ready(function() {
    appFront.init();
});

// Popup youtube video
$(document).ready(function () {
 $('.popup-youtube, .popup-vimeo, .popup-gmaps').magnificPopup({
  disableOn: 700,
  type: 'iframe',
  mainClass: 'mfp-fade',
  removalDelay: 160,
  preloader: false,

  fixedContentPos: false
 });
});

/**
 * Define JS object namespace
 */
var appFront = (function() {

 return {

     /**
     *
     */
     init: function() {
         this.bindScrollTo();
         this.navScroll();
         this.modalInit();
         this.mobileMenu();
         this.mobileMenuClick();
     },

     /**
     *
     */
     bindScrollTo: function() {

         // Scroll to
         $(".scroll-to").click(function(evn){
             evn.preventDefault();
             var ww = window.innerWidth;

             var offset = 0;

             if ($(this).data("offset")) {
                 offset = $(this).data("offset");
             } else {
                 offset = 0;
             }

    if (ww > 1024) {
              offset += $(".nav").height() - 20;
             }

             if ($(this).data("href")) {
                 var href = $(this).data("href");
                 $('html,body').scrollTo(href, href, offset);
             } else {
                 $('html,body').scrollTo(this.hash, this.hash, offset);
             }

         });
     },

     /**
      *
      */
     navScroll: function() {
   var prevScrollpos = window.pageYOffset;
   var ww = window.innerWidth;

   if (ww > 1024) {
    checkNavPosition();

    window.onscroll = function() {
      checkNavPosition();
    }
   }

   function checkNavPosition() {
    var currentScrollPos = window.pageYOffset;
    var nav = $(".nav");
    var headerH = $(".header").height();

    if (prevScrollpos > (headerH - 120)) {
     nav.addClass("nav--fixed");
     nav.removeClass("nav--hide");
    } else if (prevScrollpos > 200) {
     nav.addClass("nav--hide");
     nav.removeClass("nav--fixed");
    } else {
     nav.removeClass("nav--hide");
     nav.removeClass("nav--fixed");
    }

    //console.log(prevScrollpos);
    //console.log("headerH: " + headerH);

    // DeBounce MAC
    if (prevScrollpos <= 200) {
     nav.removeClass("nav--hide");
     nav.removeClass("nav--fixed");
    }

    prevScrollpos = currentScrollPos;
   }


  },

  /**
      *
      */
  mobileMenu: function() {
   $('.nav__mobile').on("click tap", function() {
    $('.nav').toggleClass('nav--show');
   })
  },

  /**
      *
      */
  mobileMenuClick: function() {
   var ww = window.innerWidth;

   if (ww <= 1024) {
    $('.nav__link').on("click tap", function() {
     $('.nav').toggleClass('nav--show');
    })
   }
  },

  /**
      *
      */
  modalInit: function() {

   $('.button').click(function () {
    var $btn = $(this);
    var id = $btn.data('id');

    $(id).addClass('active');
    $(id).find('.modal').addClass('active');

   });

   $('.close-modal').click(function () {
    var $this = $(this);

    $this.parent().removeClass('active');
    $this.parent().parent().removeClass('active');
   });

   $('.modal-overlay').click(function (e) {    
    //console.log(e.target);
    var modal = $(this);

    if (e.target === this) {     
     modal.removeClass('active');
     modal.children().removeClass('active');
    }    
   });

  }
 }

})();