window.is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('header').outerHeight();
$(window).scroll(function(event){
    didScroll = true;
    if (didScroll) {
        hasScrolled();
        didScroll = false;
    }
});

function hasScrolled() {
    var st = $(this).scrollTop();
    
    // Make sure they scroll more than delta
    if(Math.abs(lastScrollTop - st) <= delta)
        return;
    
    // If they scrolled down and are past the navbar, add class .nav-up.
    // This is necessary so you never see what is "behind" the navbar.
    if (st > lastScrollTop && st > 50 ){
        // Scroll Down
        $('body').removeClass('nav-down').addClass('nav-up');
    } else if ( st > 100 ) {
        // Scroll Up
        if(st + $(window).height() < $(document).height()  ) {
            $('body').removeClass('nav-up').addClass('nav-down');
        }
    } else {
      $('body').removeClass('nav-up').removeClass('nav-down');
    }
    
    lastScrollTop = st;
}


$(document).ready(function () {

$('.screenheight').height($(window).height());
$(window).scroll(function() {
  if ( $(window).scrollTop() > 100 ){
    $('body').addClass('scroll');
  } else {
    $('body').removeClass('scroll');
  }
  parallax(); 
});
parallax(); 



$('.section, section').waypoint(function(direction) {
    $(this.element).addClass('in');
    if ( $(this.element).data('function') ) {
      console.log($(this.element).data('function'));
      window[ $(this.element).data('function') ]();
    }
    this.destroy()
}, { offset: '50%'  });




$('.mobilemenu').click(function(event) {
  $('body').toggleClass('menuopen');
  event.preventDefault();
});

window.addEventListener('popstate', function(e) {
  location.reload();
});


window.pagedata = {}; 

$('body').on('mouseenter', '[data-template]', function(e) {
  
  if ( !window.pagedata[$(this).data('url')] && !$(this).data('loadingpagedata') ){ //prelaod ajax load
    $(this).data('loadingpagedata',1);
    preloadpage( $(this).data('url'), $(this).data('template') );
  }
});

$('body').on('click', '[data-template]', function(e) {
  e.preventDefault();

  $(window).unbind('load');

  if ( Modernizr.touch ) {
    if ( $(this).parent().hasClass('hasdd') ) return false;
  }

  $('body').removeClass('submenuopen');
  $('.simplenav .open').removeClass('open');
  $('body').removeClass('menuin').addClass('loading');
  $('body').removeClass('menuopen');
  $('.dd').removeClass('open');
  var t = this;
  $('.preloader').addClass('loading').css({
    'top' : '0',
    'height': '0%',
    'bottom': 'auto',
    'opacity': 0
  }).animate({ 
      'height': '100%',
      'opacity': 1
    }, 500, function () {
      
     
      if ( window.pagedata[$(t).data('url')] ){
        aftertemplate(t, window.pagedata[ $(t).data('url') ] );   
        console.log('from cache');
      } else {
        
       console.log('from post');
        $.post( '../includes/actions.php?url='+$(t).data('url'), 
            { 
              action: "loadtemplate",
              template: $(t).data('template'),
              url: $(t).data('url'),
              language : language
            },
            function(data){

              aftertemplate(t,data);              
            }
        );
     }
      

  });
});



//fix for unsupported webp images
if ( !Modernizr.webp ) {
  $(' [data-bgimage] ').each(function () {
    console.log( $(this) );
    $(this).css('background-image', 'url("'+$(this).data('bgimage')+'");' );
  });
}

}); //document ready



function parallax(st) {
  if ($(window).width() > 1000) {
        if (!st) st = $(window).scrollTop();

        $('.parallax2').each(function () {
            if ( isScrolledIntoView( $(this) ) ) {

                if ($(this).attr('data-initialy')) {
                  $(this).data('initialy',$(this).attr('data-initialy'));
                } else {
                  $(this).data('initialy',$(this).offset().top);
                }

                stt = st -  $(this).data('initialy');
                var top = (1-$(this).data('speed')) * stt;
                $(this).css('transform', 'translate3d( 0, '+ top +'px, 0)');
            }
        });
  }
}

function isScrolledIntoView(ele) {
    //console.log(ele);
    var lBound = $(window).scrollTop(),
        uBound = lBound + $(window).height(),
        top = ele.offset().top,
        bottom = top + ele.outerHeight(true);

    return (top > lBound && top < uBound)
        || (bottom > lBound && bottom < uBound)
        || (lBound >= top && lBound <= bottom)
        || (uBound >= top && uBound <= bottom);
}

(function( $ ) {
 
$.fn.loopClasses = function(attr) {
  defaultattr = {'interval': 5000, 'class': 'currrent'};
  if (!attr) attr = {};
  t = this;
  $.extend(true, this, defaultattr, attr);
  
  $(':first-child',this).addClass(attr['class']);
  var action = function() {
    var next = $('.'+attr['class'], t).next();
    $('.'+attr['class'], t).removeClass(attr['class']);      
    if (next.length > 0)
      $(next).addClass(attr['class'])
    else
      $(':first-child',t).addClass(attr['class']);
  };
  setInterval(action, attr['interval']);
 
};
 
}( jQuery ));




function afterload() {

  $('.screenheight').height($(window).height());
  $(window).scroll(function() {
    if ( $(window).scrollTop() > 100 ){
      $('body').addClass('scroll');
    } else {
      $('body').removeClass('scroll');
    }
    parallax(); 
  });
  parallax(); 

  setTimeout(function () {
    $('.parallax2').css('transform','translate3d(0px, 0px, 0px)');  
    $('body').addClass('bodyin');      
    $('body').removeClass('loading');               
  },250);

  $(document).foundation(); 


  if ( !window.is_safari ) {
  var offset = 300;
  if ( $(window).width() < 1000 ) var offset = 100;
  Parachute.page({
    scrollContainer: '#scrollContainer',
    heightContainer: '#fakeScrollContainer'
  });

  Parachute.sequence({
    element: '.section',
    offset: offset,
    callback: function(active) {
      if (active) {
        $(this.$element).addClass('in');
      } else {
        $(this.$element).removeClass('in');
      }
    }
  });
  Parachute.init();
  } else {
      console.log( 'safari' );
      $('html').addClass('safari');
      $(window).scroll(function() {
        parallax(); 
      });
      $('section, .section').addClass('in');
  }
  
  window.loadfunc = 1;
  $(window).load();

  $('video')[0].play();

  $(window).resize();
  setInterval(function () {
        $(window).resize();
      },500);

  $('.section, section').waypoint(function(direction) {
    $(this.element).addClass('in');
    if ( $(this.element).data('function') ) {
      console.log($(this.element).data('function'));
      window[ $(this.element).data('function') ]();
    }
    this.destroy()
}, { offset: '50%'  });

} //afterload

if(navigator.userAgent.match(/Trident\/7\./)) { // if IE
  $('html').addClass('ie');
}

function aftertemplate(t,data) {
 // console.log(data);
  $('body').removeClass(); //.addClass('inner').addClass('loading').addClass('firstloadin');
  if ( $(t).data('bodyclass') ) $('body').addClass(  $(t).data('bodyclass') );

  $('body').removeClass('bodyin');

  $(window).scrollTop(0);
  $('.container').html(data);
  $('.screenheight').outerHeight($(window).height());         

  url = $(t).attr('href');
  data.url = $(t).data('url');
  data.template = $(t).data('template');
    history.pushState(data, null, url );
              
  
    $('.preloader').css({
      'bottom': '0',
      'top': 'auto',
      'opacity': 1
    }).animate({
      'height': 0,
      'opacity': 0
    }, 500, function () {
      $('.preloader').removeClass('loading');
      afterload(); 
    });
    $('body').addClass('loading');

  if ( typeof ga !== 'undefined'){
    ga('send', 'pageview');
  }
  
}

$(document).on('open.fndtn.reveal', '[data-reveal]', function () {
    $('body').addClass('stop-scrolling');
});
$(document).on('close.fndtn.reveal', '[data-reveal]', function () {
    $('body').removeClass('stop-scrolling');
});

function preloadpage(url, template){
//  console.log( 'preloading '+url );
  var t = this;
  
  $.post( '../includes/actions.php?url='+$(this).data('url'), 
  { 
    action: "loadtemplate",
    template: template,
    url: url,
    language : language
  },function (data){
    window.pagedata[ url ] = data;
  })  
}