document.addEventListener('DOMContentLoaded', function() {
  var video = document.getElementById('videoPlayer');
  var playPauseBtn = document.getElementById('playPauseBtn');
  var muteUnmuteBtn = document.getElementById('muteUnmuteBtn');
  var subtitleBtn = document.getElementById('subtitleBtn');
  var restartBtn = document.getElementById('restartBtn');
  var languageSelect = document.getElementById('languageSelect');
  var fullscreenBtn = document.getElementById('fullscreenBtn');
  var videoContainer = document.getElementById('videoContainer');
  let selectedLanguage = 'default';
  var subtitlesEnabled = false;
  var currentSubtitleURL = null;
  const videoDirectory = {
    default: {
      videoName:'./assets/videos/Daikins Commitment English.mp4',
      subtitlesEncoded:'WEBVTT%0A%0A00%3A00%3A02.000%20--%3E%2000%3A00%3A06.404%0AToday%2C%20the%20energy%20used%20by%20heating%20and%20cooling%0Asystems%20and%20water%20heating%0A%0A00%3A00%3A06.404%20--%3E%2000%3A00%3A10.741%0Aaccounts%20for%20up%20to%2060%25%20of%20the%20energy%20consumption%0Aof%20homes%20and%20buildings%2C%0A%0A00%3A00%3A10.800%20--%3E%2000%3A00%3A15.538%0Aand%20by%202050%2C%20the%20number%20of%20these%20systems%20is%0Aexpected%20to%20triple%20as%20countries%20develop%20%E2%80%93%0A%0A00%3A00%3A15.538%20--%3E%2000%3A00%3A18.195%0Afurther%20increasing%20the%20demand%0Afor%20energy.%0A%0A00%3A00%3A18.536%20--%3E%2000%3A00%3A23.130%0AThis%20means%20an%20exponential%20increase%20in%20the%0Aamount%20of%20greenhouse%20gasses%20released%0A%0A00%3A00%3A23.135%20--%3E%2000%3A00%3A25.589%0Aas%20cooling%20and%20heating%20equipment%20operates%20%E2%80%94%0A%0A00%3A00%3A25.589%20--%3E%2000%3A00%3A31.711%0Aand%20operates%20more%20frequently%20as%0Athe%20seasonal%20effects%20of%20climate%20change%20evolve.%0A%0A00%3A00%3A32.195%20--%3E%2000%3A00%3A37.972%0AAs%20an%20air%20conditioner%20manufacturer%20that%20operates%0Ain%20more%20than%20170%20countries%20around%20the%20world%2C%0A%0A00%3A00%3A37.972%20--%3E%2000%3A00%3A42.977%0ADaikin%20understands%20the%20impact%20that%20HVAC%20systems%0Acan%20have%20on%20climate%20change.%0A%0A00%3A00%3A43.350%20--%3E%2000%3A00%3A46.775%0AThat%27s%20why%20we%27re%20committed%20to%0Areducing%20emissions%20and%20ownership%20costs%0A%0A00%3A00%3A47.775%20--%3E%2000%3A00%3A49.501%0Athroughout%20the%20air%20conditioner%27s%20life%20cycle%E2%80%94%0A%0A00%3A00%3A49.501%20--%3E%2000%3A00%3A51.351%0Aand%20showing%20others%20how%20to%20do%20so%2C%20too.%0A%0A00%3A00%3A51.751%20--%3E%2000%3A00%3A57.231%0ADaikin%20has%20released%20more%20than%20400%20R-32%20related%0Atech%20patents%E2%80%94without%20obligation%E2%80%94%0A%0A00%3A00%3A57.325%20--%3E%2000%3A01%3A02.877%0Afor%20other%20air%20conditioner%20manufacturers%20to%20use%0Ato%20make%20their%20systems%20more%20efficient.%0A%0A00%3A01%3A02.877%20--%3E%2000%3A01%3A07.915%0AIn%20doing%20so%2C%20we%20hope%20to%20encourage%20the%20widespread%0Aadoption%20of%20R-32%20refrigerant%2C%0A%0A00%3A01%3A07.915%20--%3E%2000%3A01%3A11.686%0Awhich%20has%20a%20lower%20global%20warming%20potential%0Athan%20conventional%20refrigerants%0A%0A00%3A01%3A11.686%20--%3E%2000%3A01%3A16.858%0Aand%20can%20quickly%20make%20a%20major%20difference%20when%20it%20comes%0Ato%20limiting%20the%20effects%20of%20climate%20change.%0A%0A00%3A01%3A17.159%20--%3E%2000%3A01%3A21.971%0AWithin%20our%20own%20company%2C%20our%20latest%20strategic%0Amanagement%20plan%2C%20Fusion25%2C%0A%0A00%3A01%3A21.971%20--%3E%2000%3A01%3A26.267%0Aprioritizes%20investment%20in%20sustainable%20business%20expansion.%0A%0A00%3A01%3A26.267%20--%3E%2000%3A01%3A33.189%0AThrough%20this%2C%20we%E2%80%99re%20aiming%20for%20a%20reduction%0Aof%2030%25%20or%20more%20in%20net%20emissions%20in%202025.%0A%0A00%3A01%3A33.689%20--%3E%2000%3A01%3A38.971%0AFusion25%20is%20just%20one%20of%20many%20initiatives%20we%E2%80%99ve%0Aput%20in%20place%20to%20measure%20our%20progress%0A%0A00%3A01%3A38.971%20--%3E%2000%3A01%3A40.739%0Atoward%20our%20ultimate%20goal%3A%0A%0A00%3A01%3A40.739%20--%3E%2000%3A01%3A45.184%0Areducing%20our%20greenhouse%20gas%20emissions%0Ato%20net%20zero%20by%202050.%0A%0A00%3A01%3A45.535%20--%3E%2000%3A01%3A48.847%0AIn%20taking%20the%20lead%20in%20our%20industry%E2%80%99s%20decarbonization%2C%0A%0A00%3A01%3A48.847%20--%3E%2000%3A01%3A51.973%0Awe%20can%20both%20contribute%20to%20a%20cleaner%20atmosphere%2C%0A%0A00%3A01%3A51.973%20--%3E%2000%3A01%3A56.000%0Aand%20build%20partnerships%20that%20yield%20sustainable%0Aopportunities%20for%20global%20growth.',
      srclang:'en'
    },
    japanese: {
      videoName:'./assets/videos/Daikins Commitment Japanese.mp4',
      subtitlesEncoded:'WEBVTT%0A%0A00%3A00%3A02.000%20--%3E%2000%3A00%3A06.404%0AToday%2C%20the%20energy%20used%20by%20heating%20and%20cooling%0Asystems%20and%20water%20heating%0A%0A00%3A00%3A06.404%20--%3E%2000%3A00%3A10.741%0Aaccounts%20for%20up%20to%2060%25%20of%20the%20energy%20consumption%0Aof%20homes%20and%20buildings%2C%0A%0A00%3A00%3A10.800%20--%3E%2000%3A00%3A15.538%0Aand%20by%202050%2C%20the%20number%20of%20these%20systems%20is%0Aexpected%20to%20triple%20as%20countries%20develop%20%E2%80%93%0A%0A00%3A00%3A15.538%20--%3E%2000%3A00%3A18.195%0Afurther%20increasing%20the%20demand%0Afor%20energy.%0A%0A00%3A00%3A18.536%20--%3E%2000%3A00%3A23.130%0AThis%20means%20an%20exponential%20increase%20in%20the%0Aamount%20of%20greenhouse%20gasses%20released%0A%0A00%3A00%3A23.135%20--%3E%2000%3A00%3A25.589%0Aas%20cooling%20and%20heating%20equipment%20operates%20%E2%80%94%0A%0A00%3A00%3A25.589%20--%3E%2000%3A00%3A31.711%0Aand%20operates%20more%20frequently%20as%0Athe%20seasonal%20effects%20of%20climate%20change%20evolve.%0A%0A00%3A00%3A32.195%20--%3E%2000%3A00%3A37.972%0AAs%20an%20air%20conditioner%20manufacturer%20that%20operates%0Ain%20more%20than%20170%20countries%20around%20the%20world%2C%0A%0A00%3A00%3A37.972%20--%3E%2000%3A00%3A42.977%0ADaikin%20understands%20the%20impact%20that%20HVAC%20systems%0Acan%20have%20on%20climate%20change.%0A%0A00%3A00%3A43.350%20--%3E%2000%3A00%3A46.775%0AThat%27s%20why%20we%27re%20committed%20to%0Areducing%20emissions%20and%20ownership%20costs%0A%0A00%3A00%3A47.775%20--%3E%2000%3A00%3A49.501%0Athroughout%20the%20air%20conditioner%27s%20life%20cycle%E2%80%94%0A%0A00%3A00%3A49.501%20--%3E%2000%3A00%3A51.351%0Aand%20showing%20others%20how%20to%20do%20so%2C%20too.%0A%0A00%3A00%3A51.751%20--%3E%2000%3A00%3A57.231%0ADaikin%20has%20released%20more%20than%20400%20R-32%20related%0Atech%20patents%E2%80%94without%20obligation%E2%80%94%0A%0A00%3A00%3A57.325%20--%3E%2000%3A01%3A02.877%0Afor%20other%20air%20conditioner%20manufacturers%20to%20use%0Ato%20make%20their%20systems%20more%20efficient.%0A%0A00%3A01%3A02.877%20--%3E%2000%3A01%3A07.915%0AIn%20doing%20so%2C%20we%20hope%20to%20encourage%20the%20widespread%0Aadoption%20of%20R-32%20refrigerant%2C%0A%0A00%3A01%3A07.915%20--%3E%2000%3A01%3A11.686%0Awhich%20has%20a%20lower%20global%20warming%20potential%0Athan%20conventional%20refrigerants%0A%0A00%3A01%3A11.686%20--%3E%2000%3A01%3A16.858%0Aand%20can%20quickly%20make%20a%20major%20difference%20when%20it%20comes%0Ato%20limiting%20the%20effects%20of%20climate%20change.%0A%0A00%3A01%3A17.159%20--%3E%2000%3A01%3A21.971%0AWithin%20our%20own%20company%2C%20our%20latest%20strategic%0Amanagement%20plan%2C%20Fusion25%2C%0A%0A00%3A01%3A21.971%20--%3E%2000%3A01%3A26.267%0Aprioritizes%20investment%20in%20sustainable%20business%20expansion.%0A%0A00%3A01%3A26.267%20--%3E%2000%3A01%3A33.189%0AThrough%20this%2C%20we%E2%80%99re%20aiming%20for%20a%20reduction%0Aof%2030%25%20or%20more%20in%20net%20emissions%20in%202025.%0A%0A00%3A01%3A33.689%20--%3E%2000%3A01%3A38.971%0AFusion25%20is%20just%20one%20of%20many%20initiatives%20we%E2%80%99ve%0Aput%20in%20place%20to%20measure%20our%20progress%0A%0A00%3A01%3A38.971%20--%3E%2000%3A01%3A40.739%0Atoward%20our%20ultimate%20goal%3A%0A%0A00%3A01%3A40.739%20--%3E%2000%3A01%3A45.184%0Areducing%20our%20greenhouse%20gas%20emissions%0Ato%20net%20zero%20by%202050.%0A%0A00%3A01%3A45.535%20--%3E%2000%3A01%3A48.847%0AIn%20taking%20the%20lead%20in%20our%20industry%E2%80%99s%20decarbonization%2C%0A%0A00%3A01%3A48.847%20--%3E%2000%3A01%3A51.973%0Awe%20can%20both%20contribute%20to%20a%20cleaner%20atmosphere%2C%0A%0A00%3A01%3A51.973%20--%3E%2000%3A01%3A56.000%0Aand%20build%20partnerships%20that%20yield%20sustainable%0Aopportunities%20for%20global%20growth.',
      srclang:'ja'
    },
    spanish: {
      videoName:'./assets/videos/Daikins Commitment Spanish.mp4',
      subtitlesEncoded:'WEBVTT%0A%0A00%3A00%3A02.000%20--%3E%2000%3A00%3A06.404%0AToday%2C%20the%20energy%20used%20by%20heating%20and%20cooling%0Asystems%20and%20water%20heating%0A%0A00%3A00%3A06.404%20--%3E%2000%3A00%3A10.741%0Aaccounts%20for%20up%20to%2060%25%20of%20the%20energy%20consumption%0Aof%20homes%20and%20buildings%2C%0A%0A00%3A00%3A10.800%20--%3E%2000%3A00%3A15.538%0Aand%20by%202050%2C%20the%20number%20of%20these%20systems%20is%0Aexpected%20to%20triple%20as%20countries%20develop%20%E2%80%93%0A%0A00%3A00%3A15.538%20--%3E%2000%3A00%3A18.195%0Afurther%20increasing%20the%20demand%0Afor%20energy.%0A%0A00%3A00%3A18.536%20--%3E%2000%3A00%3A23.130%0AThis%20means%20an%20exponential%20increase%20in%20the%0Aamount%20of%20greenhouse%20gasses%20released%0A%0A00%3A00%3A23.135%20--%3E%2000%3A00%3A25.589%0Aas%20cooling%20and%20heating%20equipment%20operates%20%E2%80%94%0A%0A00%3A00%3A25.589%20--%3E%2000%3A00%3A31.711%0Aand%20operates%20more%20frequently%20as%0Athe%20seasonal%20effects%20of%20climate%20change%20evolve.%0A%0A00%3A00%3A32.195%20--%3E%2000%3A00%3A37.972%0AAs%20an%20air%20conditioner%20manufacturer%20that%20operates%0Ain%20more%20than%20170%20countries%20around%20the%20world%2C%0A%0A00%3A00%3A37.972%20--%3E%2000%3A00%3A42.977%0ADaikin%20understands%20the%20impact%20that%20HVAC%20systems%0Acan%20have%20on%20climate%20change.%0A%0A00%3A00%3A43.350%20--%3E%2000%3A00%3A46.775%0AThat%27s%20why%20we%27re%20committed%20to%0Areducing%20emissions%20and%20ownership%20costs%0A%0A00%3A00%3A47.775%20--%3E%2000%3A00%3A49.501%0Athroughout%20the%20air%20conditioner%27s%20life%20cycle%E2%80%94%0A%0A00%3A00%3A49.501%20--%3E%2000%3A00%3A51.351%0Aand%20showing%20others%20how%20to%20do%20so%2C%20too.%0A%0A00%3A00%3A51.751%20--%3E%2000%3A00%3A57.231%0ADaikin%20has%20released%20more%20than%20400%20R-32%20related%0Atech%20patents%E2%80%94without%20obligation%E2%80%94%0A%0A00%3A00%3A57.325%20--%3E%2000%3A01%3A02.877%0Afor%20other%20air%20conditioner%20manufacturers%20to%20use%0Ato%20make%20their%20systems%20more%20efficient.%0A%0A00%3A01%3A02.877%20--%3E%2000%3A01%3A07.915%0AIn%20doing%20so%2C%20we%20hope%20to%20encourage%20the%20widespread%0Aadoption%20of%20R-32%20refrigerant%2C%0A%0A00%3A01%3A07.915%20--%3E%2000%3A01%3A11.686%0Awhich%20has%20a%20lower%20global%20warming%20potential%0Athan%20conventional%20refrigerants%0A%0A00%3A01%3A11.686%20--%3E%2000%3A01%3A16.858%0Aand%20can%20quickly%20make%20a%20major%20difference%20when%20it%20comes%0Ato%20limiting%20the%20effects%20of%20climate%20change.%0A%0A00%3A01%3A17.159%20--%3E%2000%3A01%3A21.971%0AWithin%20our%20own%20company%2C%20our%20latest%20strategic%0Amanagement%20plan%2C%20Fusion25%2C%0A%0A00%3A01%3A21.971%20--%3E%2000%3A01%3A26.267%0Aprioritizes%20investment%20in%20sustainable%20business%20expansion.%0A%0A00%3A01%3A26.267%20--%3E%2000%3A01%3A33.189%0AThrough%20this%2C%20we%E2%80%99re%20aiming%20for%20a%20reduction%0Aof%2030%25%20or%20more%20in%20net%20emissions%20in%202025.%0A%0A00%3A01%3A33.689%20--%3E%2000%3A01%3A38.971%0AFusion25%20is%20just%20one%20of%20many%20initiatives%20we%E2%80%99ve%0Aput%20in%20place%20to%20measure%20our%20progress%0A%0A00%3A01%3A38.971%20--%3E%2000%3A01%3A40.739%0Atoward%20our%20ultimate%20goal%3A%0A%0A00%3A01%3A40.739%20--%3E%2000%3A01%3A45.184%0Areducing%20our%20greenhouse%20gas%20emissions%0Ato%20net%20zero%20by%202050.%0A%0A00%3A01%3A45.535%20--%3E%2000%3A01%3A48.847%0AIn%20taking%20the%20lead%20in%20our%20industry%E2%80%99s%20decarbonization%2C%0A%0A00%3A01%3A48.847%20--%3E%2000%3A01%3A51.973%0Awe%20can%20both%20contribute%20to%20a%20cleaner%20atmosphere%2C%0A%0A00%3A01%3A51.973%20--%3E%2000%3A01%3A56.000%0Aand%20build%20partnerships%20that%20yield%20sustainable%0Aopportunities%20for%20global%20growth.',
      srclang:'es'
    },
    french: {
      videoName:'./assets/videos/Daikins Commitment French.mp4',
      subtitlesEncoded:'WEBVTT%0A%0A00%3A00%3A02.000%20--%3E%2000%3A00%3A06.404%0AToday%2C%20the%20energy%20used%20by%20heating%20and%20cooling%0Asystems%20and%20water%20heating%0A%0A00%3A00%3A06.404%20--%3E%2000%3A00%3A10.741%0Aaccounts%20for%20up%20to%2060%25%20of%20the%20energy%20consumption%0Aof%20homes%20and%20buildings%2C%0A%0A00%3A00%3A10.800%20--%3E%2000%3A00%3A15.538%0Aand%20by%202050%2C%20the%20number%20of%20these%20systems%20is%0Aexpected%20to%20triple%20as%20countries%20develop%20%E2%80%93%0A%0A00%3A00%3A15.538%20--%3E%2000%3A00%3A18.195%0Afurther%20increasing%20the%20demand%0Afor%20energy.%0A%0A00%3A00%3A18.536%20--%3E%2000%3A00%3A23.130%0AThis%20means%20an%20exponential%20increase%20in%20the%0Aamount%20of%20greenhouse%20gasses%20released%0A%0A00%3A00%3A23.135%20--%3E%2000%3A00%3A25.589%0Aas%20cooling%20and%20heating%20equipment%20operates%20%E2%80%94%0A%0A00%3A00%3A25.589%20--%3E%2000%3A00%3A31.711%0Aand%20operates%20more%20frequently%20as%0Athe%20seasonal%20effects%20of%20climate%20change%20evolve.%0A%0A00%3A00%3A32.195%20--%3E%2000%3A00%3A37.972%0AAs%20an%20air%20conditioner%20manufacturer%20that%20operates%0Ain%20more%20than%20170%20countries%20around%20the%20world%2C%0A%0A00%3A00%3A37.972%20--%3E%2000%3A00%3A42.977%0ADaikin%20understands%20the%20impact%20that%20HVAC%20systems%0Acan%20have%20on%20climate%20change.%0A%0A00%3A00%3A43.350%20--%3E%2000%3A00%3A46.775%0AThat%27s%20why%20we%27re%20committed%20to%0Areducing%20emissions%20and%20ownership%20costs%0A%0A00%3A00%3A47.775%20--%3E%2000%3A00%3A49.501%0Athroughout%20the%20air%20conditioner%27s%20life%20cycle%E2%80%94%0A%0A00%3A00%3A49.501%20--%3E%2000%3A00%3A51.351%0Aand%20showing%20others%20how%20to%20do%20so%2C%20too.%0A%0A00%3A00%3A51.751%20--%3E%2000%3A00%3A57.231%0ADaikin%20has%20released%20more%20than%20400%20R-32%20related%0Atech%20patents%E2%80%94without%20obligation%E2%80%94%0A%0A00%3A00%3A57.325%20--%3E%2000%3A01%3A02.877%0Afor%20other%20air%20conditioner%20manufacturers%20to%20use%0Ato%20make%20their%20systems%20more%20efficient.%0A%0A00%3A01%3A02.877%20--%3E%2000%3A01%3A07.915%0AIn%20doing%20so%2C%20we%20hope%20to%20encourage%20the%20widespread%0Aadoption%20of%20R-32%20refrigerant%2C%0A%0A00%3A01%3A07.915%20--%3E%2000%3A01%3A11.686%0Awhich%20has%20a%20lower%20global%20warming%20potential%0Athan%20conventional%20refrigerants%0A%0A00%3A01%3A11.686%20--%3E%2000%3A01%3A16.858%0Aand%20can%20quickly%20make%20a%20major%20difference%20when%20it%20comes%0Ato%20limiting%20the%20effects%20of%20climate%20change.%0A%0A00%3A01%3A17.159%20--%3E%2000%3A01%3A21.971%0AWithin%20our%20own%20company%2C%20our%20latest%20strategic%0Amanagement%20plan%2C%20Fusion25%2C%0A%0A00%3A01%3A21.971%20--%3E%2000%3A01%3A26.267%0Aprioritizes%20investment%20in%20sustainable%20business%20expansion.%0A%0A00%3A01%3A26.267%20--%3E%2000%3A01%3A33.189%0AThrough%20this%2C%20we%E2%80%99re%20aiming%20for%20a%20reduction%0Aof%2030%25%20or%20more%20in%20net%20emissions%20in%202025.%0A%0A00%3A01%3A33.689%20--%3E%2000%3A01%3A38.971%0AFusion25%20is%20just%20one%20of%20many%20initiatives%20we%E2%80%99ve%0Aput%20in%20place%20to%20measure%20our%20progress%0A%0A00%3A01%3A38.971%20--%3E%2000%3A01%3A40.739%0Atoward%20our%20ultimate%20goal%3A%0A%0A00%3A01%3A40.739%20--%3E%2000%3A01%3A45.184%0Areducing%20our%20greenhouse%20gas%20emissions%0Ato%20net%20zero%20by%202050.%0A%0A00%3A01%3A45.535%20--%3E%2000%3A01%3A48.847%0AIn%20taking%20the%20lead%20in%20our%20industry%E2%80%99s%20decarbonization%2C%0A%0A00%3A01%3A48.847%20--%3E%2000%3A01%3A51.973%0Awe%20can%20both%20contribute%20to%20a%20cleaner%20atmosphere%2C%0A%0A00%3A01%3A51.973%20--%3E%2000%3A01%3A56.000%0Aand%20build%20partnerships%20that%20yield%20sustainable%0Aopportunities%20for%20global%20growth.',
      srclang:'fr'
    },
  };

  video.muted = false;
  selectedLanguage = languageSelect.value; // Set initial language based on select value
  updateSubtitles();

  video.play().catch(function(err) {
      console.log('Autoplay was prevented:', err);
  });

  // Play/Pause Button
  playPauseBtn.addEventListener('click', function() {
      playPauseBtn.src = video.paused ? "./assets/icons/pause.png" : "./assets/icons/play.png";
      if (video.paused) {
          video.play();
      } else {
          video.pause();
      }
  });

  // Mute/Unmute Button
  muteUnmuteBtn.addEventListener('click', function() {
      video.muted = !video.muted;
      muteUnmuteBtn.src = video.muted ? "./assets/icons/mute.png" : "./assets/icons/unmute.png";
  });

  // Restart Video
  restartBtn.addEventListener('click', function() {
      video.currentTime = 0;
      video.play();
  });

  // Subtitle Button
  subtitleBtn.addEventListener('click', function() {
      subtitlesEnabled = !subtitlesEnabled;
      updateSubtitleButtonIcon(); // Optional: Update the button icon or text
      // Toggle subtitles
      var tracks = video.textTracks;
      for (var i = 0; i < tracks.length; i++) {
          tracks[i].mode = subtitlesEnabled ? 'showing' : 'hidden';
      }
  });

  // Language Selection
  languageSelect.addEventListener('change', function() {
      selectedLanguage = languageSelect.value;
      console.log("Selected Language is", selectedLanguage);
      video.src = videoDirectory[selectedLanguage].videoName;
      updateSubtitles();
      video.load();
      video.play();
  });

  // Update Subtitles Function
  function updateSubtitles() {
      // Remove existing tracks
      var existingTracks = video.querySelectorAll('track');
      existingTracks.forEach(function(track) {
          if (track.src) {
              URL.revokeObjectURL(track.src);
          }
          track.remove();
      });

      // Create a new track element
      let newTrack = document.createElement('track');
      newTrack.kind = 'subtitles';
      newTrack.src = `data:text/vtt;charset=utf-8,${videoDirectory[selectedLanguage].subtitlesEncoded}`;
      newTrack.label = selectedLanguage;
      newTrack.srclang = videoDirectory[selectedLanguage].srclang;

      // Add the track to the video
      video.appendChild(newTrack);

      // Set the track's mode based on whether subtitles are enabled
      newTrack.track.mode = subtitlesEnabled ? 'showing' : 'hidden';
  }

  // Update Subtitle Button Icon (Optional)
  function updateSubtitleButtonIcon() {
      // Change the icon or text based on subtitlesEnabled
      subtitleBtn.src = subtitlesEnabled ? "./assets/icons/subtitle_on.png" : "./assets/icons/subtitle_off.png";
  }

  // Fullscreen functionality
  fullscreenBtn.addEventListener('click', function() {
      if (!document.fullscreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
          enterFullscreen(videoContainer);
      } else {
          exitFullscreen();
      }
  });

  // Enter fullscreen
  function enterFullscreen(element) {
      if (element.requestFullscreen) {
          element.requestFullscreen();
      } else if (element.webkitRequestFullscreen) { /* Safari */
          element.webkitRequestFullscreen();
      } else if (element.msRequestFullscreen) { /* IE11 */
          element.msRequestFullscreen();
      }
      fullscreenBtn.textContent = 'Exit Fullscreen';
  }

  // Exit fullscreen
  function exitFullscreen() {
      if (document.exitFullscreen) {
          document.exitFullscreen();
      } else if (document.webkitExitFullscreen) { /* Safari */
          document.webkitExitFullscreen();
      } else if (document.msExitFullscreen) { /* IE11 */
          document.msExitFullscreen();
      }
      fullscreenBtn.textContent = 'Fullscreen';
  }

  // Update fullscreen button text on fullscreen change
  function onFullscreenChange() {
      if (!document.fullscreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
          fullscreenBtn.textContent = 'Fullscreen';
      } else {
          fullscreenBtn.textContent = 'Exit Fullscreen';
      }
  }

  // Add event listeners for fullscreen change
  document.addEventListener('fullscreenchange', onFullscreenChange);
  document.addEventListener('webkitfullscreenchange', onFullscreenChange);
  document.addEventListener('msfullscreenchange', onFullscreenChange);

}); this is my js code and this <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Climate Change</title>
<link rel="stylesheet" href="style.css">

</head>
<body>
  <!-- Video Container -->
  <div id="videoContainer">
    <!-- Video Player -->
    <video id="videoPlayer" loop playsinline webkit-playsinline>
      <source src="assets/videos/Daikins Commitment English.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
    <div id="controls">
      <img src="assets/icons/pause.png" id="playPauseBtn" alt="playpause">
      <img src="assets/icons/unmute.png" id="muteUnmuteBtn" alt="muteunmute">
      <img src="assets/icons/restart.png" id="restartBtn" alt="restart">
      <img src="assets/icons/subtitle.png" id="subtitleBtn" alt="subtitle">
      <select id="languageSelect">
        <option value="default"> 🇬🇧 English</option>
        <option value="japanese"> 🇯🇵 Japanese</option>
        <option value="spanish"> 🇪🇸 Spanish</option>
        <option value="french"> 🇫🇷 French</option>
      </select>
      <button id="fullscreenBtn">Fullscreen</button>
    </div>
  </div>
<script src="script.js"></script>
</body>
</html> is my html code everything works good but when I change the language and the subtitle button is enabled or showing it adds another subtitle on top of it and if i click again subtitle button it works how can i fix this