// Left
jQuery(document).ready(function () {
  jQuery('.toggle-left').on('click', function () {
    jQuery('.left-inner-sidebar').toggleClass('show');
  });
  jQuery('.left-inner-sidebar .closebtn').on('click', function () {
    jQuery('.left-inner-sidebar').removeClass('show');
  });
});

// Right
jQuery(document).ready(function () {
  jQuery('.toggle-right').on('click', function () {
    jQuery('.right-inner-sidebar').toggleClass('show');
  });
  jQuery('.right-inner-sidebar .closebtn').on('click', function () {
    jQuery('.right-inner-sidebar').removeClass('show');
  });
});

( function( window, document ) {
  function anime_stream_keepFocusInMenu() {
    document.addEventListener( 'keydown', function( e ) {
      const anime_stream_nav = document.querySelector( '.sidenav' );

      if ( ! anime_stream_nav || ! anime_stream_nav.classList.contains( 'show' ) ) {
        return;
      }
      const elements = [...anime_stream_nav.querySelectorAll( 'input, a, button' )],
        anime_stream_lastEl = elements[ elements.length - 1 ],
        anime_stream_firstEl = elements[0],
        anime_stream_activeEl = document.activeElement,
        tabKey = e.keyCode === 9,
        shiftKey = e.shiftKey;

      if ( ! shiftKey && tabKey && anime_stream_lastEl === anime_stream_activeEl ) {
        e.preventDefault();
        anime_stream_firstEl.focus();
      }

      if ( shiftKey && tabKey && anime_stream_firstEl === anime_stream_activeEl ) {
        e.preventDefault();
        anime_stream_lastEl.focus();
      }
    } );
  }
  anime_stream_keepFocusInMenu();
} )( window, document );

jQuery('document').ready(function($){
	// preloader
  setTimeout(function () {
		jQuery("#preloader").fadeOut("slow");
  },1000);

  // Sticky Header
  $(window).scroll(function(){
		var sticky = $('.header-sticky'),
			scroll = $(window).scrollTop();

		if (scroll >= 100) sticky.addClass('header-fixed');
		else sticky.removeClass('header-fixed');
	});
  // Sticky Copyright
  $(window).scroll(function(){
    var sticky = $('.copyright-sticky'),
      scroll = $(window).scrollTop();

    if (scroll >= 100) sticky.addClass('copyright-fixed');
    else sticky.removeClass('copyright-fixed');
  });
});

// Scroller
jQuery(document).ready(function () {
	jQuery(window).scroll(function () {
    if (jQuery(this).scrollTop() > 100) {
      jQuery('.scrollup i').fadeIn();
    } else {
      jQuery('.scrollup i').fadeOut();
    }
	});
	jQuery('.scrollup i').click(function () {
    jQuery("html, body").animate({
      scrollTop: 0
    }, 600);
    return false;
	});
});

// Project SLider
jQuery(function ($) {
  let anime_stream_isRotating = false;
  let anime_stream_sliderInterval = null;

  function anime_stream_rotateCards() {
    if (anime_stream_isRotating) return;
    anime_stream_isRotating = true;

    const $anime_stream_first = $('#project-slider .project-box:first-child');
    $anime_stream_first.fadeOut(400, function () {
      $(this).remove();
      $('#project-slider')
        .append($anime_stream_first.clone(true).hide().fadeIn(400, function () {
          anime_stream_isRotating = false;
        }));
    });
  }

  function anime_stream_startSliderInterval() {
    clearInterval(anime_stream_sliderInterval);
    anime_stream_sliderInterval = setInterval(anime_stream_rotateCards, 2000);
  }

  function anime_stream_stopSliderInterval() {
    clearInterval(anime_stream_sliderInterval);
    anime_stream_sliderInterval = null;
  }

  anime_stream_startSliderInterval();

  $('#project-slider').hover(
    function () {
      anime_stream_stopSliderInterval();
    },
    function () {
      if (!$('body').hasClass('popup-active')) {
        anime_stream_startSliderInterval();
      }
    }
  );

  $('#project-slider').on('click', '.project-box, .project-box .close-button', function (e) {
    if ($(e.target).closest('.slider-video-btn').length > 0) return;

    if (!$('body').hasClass('popup-active')) {
      anime_stream_rotateCards();
    }
  });

  $(document).on('click', '.slider-video-btn', function (e) {
    e.stopPropagation();
    anime_stream_openPopup($(this).data('video'));
  });

  function anime_stream_openPopup(id) {
    $('#' + id).fadeIn();
    $('body').addClass('popup-active');
    anime_stream_stopSliderInterval();
  }

  function anime_stream_closePopup($anime_stream_popup) {
    $anime_stream_popup.find('iframe').each(function () {
      const src = $(this).attr('src');
      $(this).attr('src', '').attr('src', src);
    });

    $anime_stream_popup.find('video').each(function () {
      this.pause();
      this.currentTime = 0;
    });

    $anime_stream_popup.fadeOut();
    $('body').removeClass('popup-active');

    // Only restart if no popups are visible
    if ($('.slider-video-popup:visible, .chart-video-popup:visible').length === 0) {
      anime_stream_startSliderInterval();
    }
  }

  $(document).on('click', '.slider-close-popup', function () {
    anime_stream_closePopup($(this).closest('.slider-video-popup'));
  });

  $(document).on('click', '.chart-video-btn', function (e) {
    e.stopPropagation();
    anime_stream_openPopup($(this).data('video'));
  });

  $(document).on('click', '.chart-close-popup', function () {
    anime_stream_closePopup($(this).closest('.chart-video-popup'));
  });
});

// Gallery slider
jQuery(document).ready(function($) {
  var anime_stream_rotateCards = function () {
    var $anime_stream_slider = $('.home-right-sidebar .right-img-slider');
    var $anime_stream_first = $anime_stream_slider.find('.right-img-box:first-child');
    var $anime_stream_cloned = $anime_stream_first.clone(true);

    $anime_stream_first.fadeOut(400, function () {
      $(this).remove();
      $anime_stream_slider.append($anime_stream_cloned.hide().fadeIn(400)); 
    });
  };
  var anime_stream_sliderInterval = setInterval(anime_stream_rotateCards, 3000);
  $('.home-right-sidebar').hover(
    function () {
      clearInterval(anime_stream_sliderInterval);
    },
    function () {
      anime_stream_sliderInterval = setInterval(anime_stream_rotateCards, 3000);
    }
  );
  $('.home-right-sidebar .right-img-slider .right-img-box').on('click', function () {
    anime_stream_rotateCards();
  });
});

// Top Charts
jQuery(document).ready(function () {
  var swiper = new Swiper("#top-chart-section .cards-mySwiper", {
    breakpoints: {
      0: {
        slidesPerView: 1,
      },
      600: {
        slidesPerView: 2,
      },
      992: {
        slidesPerView: 3,
      }
    },
    speed: 1200,
    spaceBetween: 30,
    navigation: {
      nextEl: ".cards-swiper-button-next",
      prevEl: ".cards-swiper-button-prev",
    },
  });
});

// Right Sidebar Slider
jQuery(document).ready(function () {
  var swiper = new Swiper(".mySwiper", {
    slidesPerView: 2,
    grid: {
      rows: 2,
      fill: 'row',
    },
    spaceBetween: 10,
    navigation: {
      nextEl: ".swiper-button-next",
      prevEl: ".swiper-button-prev",
    },
  });
});

/*sticky sidebar*/
window.addEventListener('scroll', function() {
  var sticky = document.querySelector('.sidebar-sticky');
  if (!sticky) return;

  var scrollTop = window.scrollY || document.documentElement.scrollTop;
  var windowHeight = window.innerHeight;
  var documentHeight = document.documentElement.scrollHeight;

  var isBottom = scrollTop + windowHeight >= documentHeight-100;

  if (scrollTop >= 100 && !isBottom) {
    sticky.classList.add('sidebar-fixed');
  } else {
    sticky.classList.remove('sidebar-fixed');
  }
});
/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const anime_stream_progressBar =
        document.getElementById("anime_stream_elemento_progress_bar");
    if (!anime_stream_progressBar) return;
    window.addEventListener("scroll", function () {
        const anime_stream_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const anime_stream_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const anime_stream_scrolled =
            (anime_stream_scrollTop / anime_stream_height) * 100;
        anime_stream_progressBar.style.width =
            anime_stream_scrolled + "%";
    });
});