{% styles default %}
{% styles colors %}
{% styles custom %}

<script type="text/javascript" async src="https://play.vidyard.com/embed/v4.js"></script>

<!-- fontawesome inclusion -->
<script src="https://kit.fontawesome.com/41a3aee3ad.js" crossorigin="anonymous"></script>

<!-- clamping text to given number of lines -->
<script src="https://s3.amazonaws.com/static.northpass.com/JS+scripts/clamp.js" type="module"></script>

<!-- JavaScript Bundle with Popper -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

{% include "cookie_banner" %}

<!-- language scripts -->
<script>
  // changing language
  function changeLanguage(newLanguage, sendRequest=true) {
    document.querySelector(".languages-dropdown").classList.remove('show');
    if (!window.current_language || window.current_language !== newLanguage) {
      window.current_language = newLanguage
    } else {
      console.log('language already selected')
      return null
    }

    document.querySelectorAll('.current-lang').forEach(activeElement => {
      activeElement.classList.remove('current-lang');
    });
    document.querySelectorAll(`[data-lang="${newLanguage}"]`).forEach(newLanguageElement => {
      newLanguageElement.classList.add('current-lang');
    });
    {% if current_person.signed_in? and current_school.properties.sandbox == false %}
      if (sendRequest) {
        let xhr = new XMLHttpRequest();
        {% if current_school.sso_active? %}
          var url = "https://www.workato.com/webhooks/rest/bd1a1eb7-7e79-4208-a1db-8e9c7440bcc9/pipedrive-sso-change-user-language";
        {% else %}
          var url = "https://www.workato.com/webhooks/rest/bd1a1eb7-7e79-4208-a1db-8e9c7440bcc9/pipedrive-open-change-user-language";;
        {% endif %}
        xhr.open("POST", url, true);
        xhr.send(JSON.stringify({
          user_id: '{{ current_person.id }}',
          language: newLanguage
        }));
      }
    {% endif %}

    window.localStorage.setItem('academy-language', newLanguage);
    document.querySelector('body').className = `lang-${newLanguage}`;

  }

  // setup body class based on property or cookies
  function setupInitialLanguage() {
    let userStorageLanguage = window.localStorage.getItem('academy-language');

    var selectedLanguage = userStorageLanguage || "en";

    if (['en', 'de', 'es', 'fr', 'br'].includes(selectedLanguage)) {
      changeLanguage(selectedLanguage, false);
    } else {
      if (
        window.location.pathname.includes('/de-homepage') ||
        window.location.pathname.includes('/de-video-tutorials') ||
        window.location.pathname.includes('/de-catalog')
      ) {
        selectedLanguage = 'de';
      } else if (
        window.location.pathname.includes('/es-homepage') ||
        window.location.pathname.includes('/es-video-tutorials') ||
        window.location.pathname.includes('/es-catalog')
      ) {
        selectedLanguage = 'es';
      } else if (
        window.location.pathname.includes('/fr-homepage') ||
        window.location.pathname.includes('/fr-video-tutorials') ||
        window.location.pathname.includes('/fr-catalog')
      ) {
        selectedLanguage = 'fr';
      } else if (
        window.location.pathname.includes('/br-homepage') ||
        window.location.pathname.includes('/br-video-tutorials') ||
        window.location.pathname.includes('/br-catalog')
      ) {
        selectedLanguage = 'br';
      } else {
        selectedLanguage =  'en';
      }
  
      changeLanguage(selectedLanguage, false);
    }
  }
  
  addEventListener('DOMContentLoaded', () => {
    setupInitialLanguage();
  });

  function redirectToLanguageHomepage(language) {
    const languageMappings = {
      de: {
        homepage: '/app/de-homepage',
        catalog: '/app/de-catalog',
        videoTutorials: '/app/de-video-tutorials'
      },
      es: {
        homepage: '/app/es-homepage',
        catalog: '/app/es-catalog',
        videoTutorials: '/app/es-video-tutorials'
      },
      fr: {
        homepage: '/app/fr-homepage',
        catalog: '/app/fr-catalog',
        videoTutorials: '/app/fr-video-tutorials'
      },
      br: {
        homepage: '/app/br-homepage',
        catalog: '/app/br-catalog',
        videoTutorials: '/app/br-video-tutorials'
      },
      en: {
        homepage: '/app',
        catalog: '/app/catalog',
        videoTutorials: '/app/video-tutorials'
      }
    };
  
    const currentUrl = window.location.pathname;
    const currentLang = window.current_language || 'en';
  
    let category = 'homepage';
    if (currentUrl.includes('catalog')) {
      category = 'catalog';
    } else if (currentUrl.includes('video-tutorials')) {
      category = 'videoTutorials';
    }
  
    const newUrl = languageMappings[currentLang][category] || '/app';
  
    window.location.href = newUrl;
  }
  

  const academyLanguage = window.localStorage.getItem('academy-language');
  console.log(academyLanguage);
</script>

<!--Function to display proper courses based on language value-->
<script>
  function filterCoursesByLanguage() {
    var selectedLanguage = localStorage.getItem('academy-language');
    var courses = document.querySelectorAll('.homepage-card-wrapper, .course-card');
  
    if (!courses.length) {
      return; // Nie ma żadnych kursów na stronie
    }
  
    for (var i = 0; i < courses.length; i++) {
      var course = courses[i];
      var courseLanguage = course.getAttribute('language');
  
      if (selectedLanguage && courseLanguage !== selectedLanguage) {
        course.style.display = 'none'; // Ukryj kursy o nieodpowiednim języku
      } else {
        course.style.display = 'block'; // Pokaż kursy o wybranym języku
      }
    }
  }
  
 // Wywołanie funkcji po załadowaniu strony
window.addEventListener('DOMContentLoaded', filterCoursesByLanguage);

// Wywołanie funkcji po zmianie wartości w local storage
window.addEventListener('storage', filterCoursesByLanguage);

  
</script>

<!-- on SSO school redirect new users to setup their name -->
{% if current_person.signed_in? and current_school.sso_active? %}
  {% unless current_person.first_name %}
    <script>
      if ("{{ current_person.email }}".includes('+preview-') === false &&
        window.location.pathname !== "/app/profile-settings" &&
        !window.sessionStorage.getItem('nameSetup'))
      {
        window.location.replace('/app/profile-settings')
      }
      
    </script>
  {% endunless %}
{% endif %}

<!-- clear last video filter when leaving video tutorials pages -->
<script>
  if (window.location.pathname !== '/app/video-tutorials' &&
    !window.location.pathname.includes('/app/courses/'))
  {
    window.sessionStorage.removeItem('last-selected-video-filter')
  }
</script>

{% include 'seo_logo' %}

{% if current_school.sso_active? and current_person.signed_in? == false %}
  {% include 'sso_login_widget' %}
{% endif %}

{% if current_school.sso_active? %}
  {% if current_person.signed_in? %}
    <script>
      if (window.location.pathname === '/app' && window.sessionStorage.sso_last_page) {
        let new_url = window.sessionStorage.sso_last_page;
        window.sessionStorage.sso_last_page = ''
        window.location.replace(new_url);
      }
    </script>
  {% else %}
    <script>
      window.sessionStorage.sso_last_page = window.location.pathname;
    </script>
  {% endif %}
{% endif %}