// // import external dependencies
// import 'jquery';

// // Import everything from autoload
// import './autoload/**/*'

// // import local dependencies
// import Router from './util/Router';
// import common from './routes/common';
// import home from './routes/home';
// import aboutUs from './routes/about';

// /** Populate Router instance with DOM routes */
// const routes = new Router({
//   // All pages
//   common,
//   // Home page
//   home,
//   // About Us page, note the change from about-us to aboutUs.
//   aboutUs,
// });

// // Load Events
// jQuery(document).ready(() => routes.loadEvents());

if(!window.$) { $ = jQuery;}

  onDomLoad(()=>{

    console.log('ready doc');

    if($('.hero.bonus-400').length > 0) {
      console.log('hero-bonus-400 present');

      var last_scrollTop = window.scrollY;
      var el_form_container = $('.hero.bonus-400 .container .flex .sub-container .right .form-container');

      console.log('hey');
      function form_position(el_form_container, mode){
        var hero_content_height = parseFloat($('.hero.bonus-400 .container').css('height'));
        var hero_padding_bottom = parseFloat($('.hero.bonus-400').css('padding-bottom'));
        var hero_padding_top = parseFloat($('.hero.bonus-400').css('padding-top'));
        var two_col_panel_bonus_400_padding_top = parseFloat($('.two_col_panel.bonus-400').css('padding-top'));
        var two_col_panel_bonus_400_content_height = parseFloat($('.two_col_panel.bonus-400 .container').css('height'));
    
        var form_position = 0;
        if(mode == "max"){
          var max_distance = (hero_content_height) + hero_padding_bottom + two_col_panel_bonus_400_padding_top + two_col_panel_bonus_400_content_height;
          form_position = max_distance - parseFloat(el_form_container.css("height"));
        }
        else if (mode == "min") {
          var min_distance = (hero_content_height*.75) + hero_padding_top;
          form_position = min_distance;
        }
        return form_position;
      }

      function get_transform_Y_amount(element){
        var this_matrix = element.css("transform").split('(');
        this_matrix = this_matrix[this_matrix.length - 1].split(')');
        this_matrix = this_matrix[0].split(',');
        var transform_y = this_matrix[this_matrix.length -1].trim();
        return transform_y;
      }

      console.log('hey2');
      $(window).on('scroll',function(e){
        var form_current_position = get_transform_Y_amount(el_form_container);
        var min_pos = form_position(el_form_container,"min");
        var max_pos = form_position(el_form_container,"max");

        if(window.scrollY > last_scrollTop){
          var movedBy = window.scrollY - last_scrollTop;
          console.log('scrolling down ' + 'movedBy '+movedBy);
          if(parseFloat(window.scrollY) + window.innerHeight > parseFloat(el_form_container.offset().top) + parseFloat(el_form_container.css('height')) + 50) {
            var new_position = parseFloat(form_current_position) + parseFloat(movedBy);
            if(new_position > max_pos) {
              new_position = max_pos;
            }
            console.log(`   old: ${form_current_position} new: ${new_position}`);
            el_form_container.css("transform",`translateY(${new_position}px)`)
          }

        }
        else if(window.scrollY < last_scrollTop){
          console.log('scrolling up');
          if(parseFloat(window.scrollY) + parseFloat($('header').css('height')) + 50 < parseFloat($('.two_col_panel.bonus-400').offset().top)) {
            var movedBy = window.scrollY - last_scrollTop;
            console.log('scrolling up ' + 'movedBy '+movedBy);
    
            var new_position = parseFloat(form_current_position) + parseFloat(movedBy);
            if(new_position < min_pos) {
              new_position = min_pos;
            }
            el_form_container.css("transform",`translateY(${new_position}px)`);
          }
        }
        last_scrollTop = window.scrollY;
      })
      
      $('.video-button').on('click',function(){
        $('.video-lightbox-container').addClass('active');
      })

      $('.close-container').on('click',function(){
        var video_id = $(this).closest('.video-lightbox-container').find('.video-container').data('video-id');
        console.log('clicked close-container');
        console.log("video_id "+video_id);
        $('.video-lightbox-container').removeClass('active');
        try {
          var this_video = Wistia.api(video_id);
          this_video.pause();
        }catch{

        }
      });
    }
  });