
(function () {
  // yo yo
  function getCookie(cname) {
    let name = cname + "=";
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(';');
    for (let i = 0; i < ca.length; i++) {
      let c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }

  const cook_name = 'bsmart-embed-player'

  const BsmartWidget = Object.create({
    create() {
      const isSmall = window.matchMedia("(max-width: 980px)")

      const wdg = document.createElement("div");
      wdg.id = "bsmart-embed-player-playlist-x92k70"
      wdg.style.boxShadow = "0px 8px 10px -5px rgba(0,0,0,0.2),0px 16px 24px 2px rgba(0,0,0,0.14),0px 6px 30px 5px rgba(0,0,0,0.12)";
      wdg.style.border = "5px solid #fff";
      wdg.style.position = "fixed";
      wdg.style.right = "16px";
      wdg.style.height = "auto";
      wdg.style.bottom = "8px";
      wdg.style.display = "block";
      wdg.style.zIndex = 1000;

      const divheader = document.createElement("div");
      divheader.style.backgroundColor = "#fff";
      divheader.style.display = "flex";
      divheader.style.padding = "5px 0";
      divheader.style.justifyContent = "flex-end";

      const divTitle = document.createElement("div");
      //divTitle.style.backgroundColor = "green";
      //divTitle.innerHTML = "bla";
      //divTitle.style.backgroundImage = "url(https://www.bsmart.fr/statics/images/logo_bsmart_4change_noir.png)";
      divTitle.style.backgroundImage = "url(https://beta.bsmart.fr/statics/images/logo_bsmart_noir.png)";
      divTitle.style.backgroundSize = "contain";
      divTitle.style.backgroundRepeat = "no-repeat";
      divTitle.style.flexGrow = 1;
      divTitle.style.alignContent = "center";
      divTitle.style.cursor = "pointer";
      divTitle.addEventListener("click", (e) => {
        window.open('https://www.bsmart.fr', '_blank');
        e.stopPropagation();
      })
      divheader.append(divTitle);

      const divClose = document.createElement("div");
      divClose.innerHTML = "X";
      divClose.style.fontFamily = 'arial';
      divClose.style.alignContent = "center";
      divClose.style.fontWeight = 600;
      //divClose.style.backgroundColor = "#e4e4e4";
      divClose.style.padding = "0 5px";
      divClose.style.cursor = "pointer";
      divClose.addEventListener("click", (e) => {
        wdg.remove();
        e.stopPropagation();
      });

      divheader.append(divClose);


      wdg.append(divheader);


      const divpaper = document.createElement("div");
      divpaper.style.width = isSmall.matches ? "210px" : "340px";
      divpaper.style.aspectRatio = "16/9";
      wdg.append(divpaper);




      const iframe = document.createElement("iframe");
      iframe.frameBorder = 0;
      iframe.classList.add("bsmart-embed-player");
      iframe.src = "https://embed.bsmart.fr/video.html?playlist=x92k70"
      iframe.style.width = "100%";
      iframe.style.height = "100%";
      divpaper.append(iframe);
      return wdg;
    }
  });
  const wasDisplayed = getCookie(cook_name) === 'displayed';
  if (!wasDisplayed) {
    const bsmartVideoWidget = BsmartWidget.create();
    document.body.appendChild(bsmartVideoWidget);
    document.cookie = `${cook_name}=displayed; max-age=3600; path=/`
  }
})();
