{#
/**
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2020 Xibo Signage Ltd
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}
<!doctype html>
<html lang="en">
<head>
    <title>Xibo Open Source Digital Signage</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width={{ viewPortWidth }}" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- Copyright 2006-2021 Xibo Signage Ltd. Part of the Xibo Open Source Digital Signage Solution. Released under the AGPLv3 or later. -->
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            font-family: sans-serif, Arial, Verdana, "Trebuchet MS";
        }
        h1, h2, h3, h4, p {
            margin-top: 0;
        }
        #iframe {
            border: 0;
        }
        .cycle-slide p, p.cycle-slide {
            margin-bottom:0;
        }
    </style>
    {{ styleSheet|raw }}
    {{ head|raw }}
</head>
<!--[if lt IE 7 ]><body class="ie6"><![endif]-->
<!--[if IE 7 ]><body class="ie7"><![endif]-->
<!--[if IE 8 ]><body class="ie8"><![endif]-->
<!--[if IE 9 ]><body class="ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><body><!--<![endif]-->
<div id="content" style="text-align: center">
    <canvas id="the-canvas"></canvas>
</div>
{{ javaScript|raw }}
<script type="text/javascript">
  var url = "{{ file }}";
  pdfjsLib.GlobalWorkerOptions.workerSrc = "{{ pdfWorkerSrc }}";

  var pdfDoc = null,
      pageNum = 1,
      pageRendering = false,
      pageNumPending = null,
      scale = 1,
      canvas = document.getElementById('the-canvas'),
      ctx = canvas.getContext('2d'),
      width,
      height,
      interval;

  if (options.previewWidth === 0 || options.previewHeight === 0) {
    width = $(window).width();
    height = $(window).height();
  }
  else {
    width = options.previewWidth;
    height = options.previewHeight;
  }

  canvas.width = width;
  canvas.height = height;

  /**
   * Get page info from document, resize canvas accordingly, and render page.
   * @param num Page number.
   */
  function renderPage(num) {
    pageRendering = true;
    // Using promise to fetch the page
    pdfDoc.getPage(num).then(function(page) {
      var unscaledViewport = page.getViewport({ scale: 1 });
      var scale = Math.min((height / unscaledViewport.height), (width / unscaledViewport.width));
      var viewport = page.getViewport({ scale: scale });

      canvas.height = viewport.height;
      canvas.width = viewport.width;

      // Render PDF page into canvas context
      var renderContext = {
        canvasContext: ctx,
        transform: null,
        viewport: viewport
      };
      var renderTask = page.render(renderContext);

      // Wait for rendering to finish
      renderTask.promise.then(function () {
        pageRendering = false;
        if (pageNumPending !== null) {
          // New page rendering is pending
          renderPage(pageNumPending);
          pageNumPending = null;
        }
      });
    });
  }

  /**
   * If another page rendering in progress, waits until the rendering is
   * finised. Otherwise, executes rendering immediately.
   */
  function queueRenderPage(num) {
    if (pageRendering) {
      pageNumPending = num;
    } else {
      renderPage(num);
    }
  }

  /**
   * Displays previous page.
   */
  function onPrevPage() {
    if (pageNum <= 1) {
      return;
    }
    pageNum--;
    queueRenderPage(pageNum);
  }

  /**
   * Displays next page.
   */
  function onNextPage() {
    if (pageNum >= pdfDoc.numPages) {
      pageNum = 0;
    }
    pageNum++;
    queueRenderPage(pageNum);
  }

  /**
   * Asynchronously downloads PDF.
   */
  pdfjsLib.getDocument(url).promise.then(function (pdfDoc_) {
    pdfDoc = pdfDoc_;

    var startInterval = function(interval) {
      // Set a timer
      setInterval(function () {
        onNextPage();
      }, interval * 1000);
    };

    // Initial/first page rendering
    renderPage(pageNum);

    if(options.durationIsPerItem) {
      // Set new widget duration by number of pages
      xiboIC.setWidgetDuration(
          (options.duration * pdfDoc.numPages),
          {
            done: function() { // Callback after the request
              // Start interval ( the defined duration )
              startInterval(options.duration);
            },
            error: function() {
              // If the call fails, keep the defalt behaviour
              startInterval(options.duration / pdfDoc.numPages);
            }
          }
      );
    } else {
      // Start interval ( total duration divided by the number of pages )
      startInterval(options.duration / pdfDoc.numPages);
    }
  });
</script>
</body>
</html>
{{ controlMeta|raw }}
