(function () {
  var embedRootUrl = 'https://www.teachingchannel.org/';
  var ooReadyQueue = ooReadyQueue || [];
  var player;
  var onOOReady = onOOReady || function(unique_id, player_id, embed_code, on_create) {
      player = jwplayer(player_id);
      options = {
          "file": "//content.jwplatform.com/manifests/" + embed_code + ".m3u8",
          "mediaid": embed_code,
          "image": "//content.jwplatform.com/thumbs/" + embed_code + ".jpg"
      }
      player.setup(options);
      player.on('ready', on_create);
  };

  window.OOReady = window.OOReady || function() {
    ooReadyQueue.push(arguments);
  };

  function addEvent(element, event, fn) {
    if (element.addEventListener) {
      element.addEventListener(event, fn, false);
    } else if (element.attachEvent) {
      element.attachEvent('on' + event, fn);
    }
  }

  if(typeof window.TCH_LOADED_SCRIPTS === "undefined"){
    (function() {
      var i;
      var oo = document.createElement('script');
      oo.src = '//content.jwplatform.com/libraries/DwwE9scS.js';

      oo.type = 'text/javascript';
      oo.onload = oo.onreadystatechange = function() {
        if (window.OOReady === onOOReady) {
                return;
        }

        var ooHelpers = document.createElement('script');
        ooHelpers.type = 'text/javascript';
        ooHelpers.src = 'https://www.teachingchannel.org/assets/video_helpers-8474a93080425fc889f5b5b4fd9aa72e.js';

        ooHelpers.onload = function() {
          for(i = 0; i < ooReadyQueue.length; ++i) {
            onOOReady.apply(window, ooReadyQueue[i]);
          }

          window.OOReady = onOOReady;
        };

        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ooHelpers, s);
      };

      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(oo, s);
    })();

    window.TCH_LOADED_SCRIPTS = true;
    window.TCH_VIDEO_COUNT = 0;

    window.tchPlayers = {};
  } else {
    window.TCH_VIDEO_COUNT++;
  }

  var videoNum = window.TCH_VIDEO_COUNT;

  if (videoNum == 0) {
    var tempDiv = document.createElement('div');
    var tempRef = document.getElementsByTagName('base')[0] ||
      document.getElementsByTagName('script')[0];

    if (tempRef) {
      tempDiv.innerHTML = '&shy;<style>.tch-embed-box { visibility: hidden; }</style>';
      tempRef.parentNode.insertBefore(tempDiv, tempRef);

      // Attach onload function
      addEvent(window, 'load', function () {
        tempDiv.parentNode.removeChild(tempDiv);
      });
    }
  }

  var embedBody = "<style>.tch-embed-box--related-video{zoom:1}.tch-embed-box--related-video:before,.tch-embed-box--related-video:after{content:\"\";display:table}.tch-embed-box--related-video:after{clear:both}.tch-embed-box{box-sizing:content-box;overflow:hidden;position:relative;font-family:Arial;padding:0 0 40px;background-color:transparent}.tch-embed-box--header{box-sizing:border-box;height:60px;padding-left:16px;position:relative;background-color:#34312c}.tch-embed-box--logo{width:200px;height:100%}.tch-embed-box--info{padding:10px 0 0 12px;color:#494641;background-color:#d9d7d0;font-size:12px;height:42px;box-sizing:content-box}.tch-embed-box--title{color:#f55243;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}.tch-embed-box--category{color:#494641;text-transform:uppercase;font-weight:bold}.tch-embed-box--video-link{color:#f55243;text-decoration:none;font-weight:bold}.tch-embed-box--tags{font-size:11px;margin:2px 0 0 0;font-weight:normal}.tch-embed-box--overlay{display:none;position:absolute;top:60px;left:0;z-index:100000}.tch-embed-box--related-container{position:relative;padding-bottom:56.25%;padding-top:0;height:0;padding-top:52px;background-color:#2C2C2C}.tch-embed-box--related{position:absolute;top:0;left:0;width:100%;height:100%}.tch-embed-box--related-video{background-color:#f1eee9;height:33%;border-bottom:1px solid #fff;color:#494641;position:relative;font-size:11px;-webkit-transition:background-color 0.3s;transition:background-color 0.3s}.tch-embed-box--related-video:last-child{border-bottom:0}.tch-embed-box--related-video:hover{background-color:#d9d7d0}.tch-embed-box--related-video img{float:left;margin-right:14px;height:100%}.tch-embed-box--related-text{padding:2% 2% 0 0;box-sizing:border-box;overflow:hidden;text-overflow:ellipsis;position:relative;top:0px;bottom:0px;height:100%}.tch-embed-box--related-category{text-transform:uppercase;margin:0 0 4px 0;font-weight:bold}.tch-embed-box--related-link{display:block;position:absolute;z-index:4;background:transparent url(https://www.teachingchannel.org/assets/spacer-d32ab56a76a0cdf84600628e756f61cd.gif) repeat;top:0;left:0;height:100%;width:100%}.tch-embed-box--related-name{color:#f55243;text-decoration:none;font-size:12px;margin:0}.tch-embed-box--related-tags{color:#f55243;margin:2px 0 0 0;padding:0;font-weight:normal}.tch-embed-box--video-container{position:relative;padding-bottom:56.25%;padding-top:0;height:0}.tch-embed-box--video-box{position:absolute;top:0;left:0;width:100%;height:100%}.tch-embed-box .innerWrapper{z-index:1}.tch-embed-box .oo-canvas-ctrls{z-index:0}.tch-embed-box--small .oo-scrubber{display:none}.tch-embed-box--small .tch-embed-box--share{display:none}.tch-embed-box--small .tch-embed-box--related-category,.tch-embed-box--small .tch-embed-box--related-tags,.tch-embed-box--small .tch-embed-box--category{display:none}.tch-embed-box .ooyala-player .oo-canvas-ctrls{display:none}.tch-embed-box .js-player .oo-canvas-ctrls{display:none}.tch-embed-box--show-related .tch-embed-box--overlay{display:block}\n<\/style>\n<div class=\'tch-embed-box\' id=\'tch-video{{TCH_VIDEO_COUNT}}\' style=\'width: 480px;\'>\n<div class=\'tch-embed-box--header\'>\n<div class=\'tch-embed-box--logo\'>\n<img alt=\"Teaching Channel\" src=\"https://www.teachingchannel.org/assets/embed-logo-0c3808bdc6ce81b6308fd8652bfdbfeb.png\" />\n<\/div>\n<\/div>\n<div class=\'tch-embed-box--info\'>\n<div class=\'tch-embed-box--title\'>\n<span class=\'tch-embed-box--category\'>\nLesson Idea (Common Core)\n<\/span>\n<a class=\"tch-embed-box--video-link\" href=\"https://www.teachingchannel.org/videos/common-core-teaching-division\">Reasoning About Division (7 min)<\/a>\n<\/div>\n<div class=\'tch-embed-box--tags\'>\nGrades 3-5 / Math / Strategies\n<\/div>\n<\/div>\n<div class=\'tch-embed-box--overlay\' style=\'width: 480px;\'>\n<div class=\'tch-embed-box--related-container\'>\n<div class=\'tch-embed-box--related\'>\n<div class=\'tch-embed-box--related-video\'>\n<a class=\"tch-embed-box--related-link\" href=\"https://www.teachingchannel.org/videos/3rd-grade-number-talks\"><\/a>\n<img alt=\"Multiplication Number Talk: Third Grade\" src=\"https://ds0vt0n1s74d2.cloudfront.net/video_photos/14/869/tiny_1488410854.jpg\" />\n<div class=\'tch-embed-box--related-text\'>\n<div class=\'tch-embed-box--related-category\'>\nTch Special\n<\/div>\n<div class=\'tch-embed-box--related-name\'>\nMultiplication Number Talk: Third Grade\n(15 min)\n<\/div>\n<div class=\'tch-embed-box--related-tags\'>\nGrade 3 / Math / Tch Diy\n<\/div>\n<\/div>\n<\/div>\n<div class=\'tch-embed-box--related-video\'>\n<a class=\"tch-embed-box--related-link\" href=\"https://www.teachingchannel.org/videos/true-false-equation-routine\"><\/a>\n<img alt=\"True or False Equation Routine: Third Grade\" src=\"https://dqam6mam97sh3.cloudfront.net/video_photos/14/867/tiny_1488410854.jpg\" />\n<div class=\'tch-embed-box--related-text\'>\n<div class=\'tch-embed-box--related-category\'>\nTch Special\n<\/div>\n<div class=\'tch-embed-box--related-name\'>\nTrue or False Equation Routine: Third Grade\n(20 min)\n<\/div>\n<div class=\'tch-embed-box--related-tags\'>\nGrade 3 / Math / Tch Diy\n<\/div>\n<\/div>\n<\/div>\n<div class=\'tch-embed-box--related-video\'>\n<a class=\"tch-embed-box--related-link\" href=\"https://www.teachingchannel.org/videos/4th-5th-grade-number-talks\"><\/a>\n<img alt=\"Multiplication Number Talk: Fourth &amp; Fifth Grade\" src=\"https://ds0vt0n1s74d2.cloudfront.net/video_photos/14/915/tiny_1490387590.jpg\" />\n<div class=\'tch-embed-box--related-text\'>\n<div class=\'tch-embed-box--related-category\'>\nTch Special\n<\/div>\n<div class=\'tch-embed-box--related-name\'>\nMultiplication Number Talk: Fourth &amp; Fifth Grade\n(18 min)\n<\/div>\n<div class=\'tch-embed-box--related-tags\'>\nGrades 4-5 / Math / Tch Diy\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\'tch-embed-box--video-container\'>\n<div class=\'tch-embed-box--video-box\'>\n<div class=\'video-player\' id=\'video-player-5274-n_hDZqky8qukjbX9VPix\'><\/div>\n<\/div>\n<\/div>\n<noscript>\nPlease enable Javascript to watch this video\n<\/noscript>\n<\/div>\n";

  embedBody = embedBody
    .replace(/{{TCH_VIDEO_COUNT}}/g, videoNum)
    .replace(/{{embedRootUrl}}/g, embedRootUrl);

  document.write(embedBody);

  var videoPlayer;

  var toggleClassIfMaxWidth = function (element, maxWidth, className) {
    if (element.offsetWidth < maxWidth) {
      if (element.className.indexOf(className) == -1) {
        element.className = element.className + ' ' + className;
      }
    } else {
      element.className = element.className.replace(className, '');
    }
  };

  var playerResize = function () {
    videoPlayer = document.getElementById('tch-video' + videoNum);

    if (!videoPlayer) {
      // sometimes the player isn't in the DOM yet
      setTimeout(playerResize, 250);
      return;
    }

    toggleClassIfMaxWidth(videoPlayer, 325, 'tch-embed-box--small');
  };

  setTimeout(playerResize, 250);

  OOReady(
    "n_hDZqky8qukjbX9VPix",
    "video-player-5274-n_hDZqky8qukjbX9VPix",
    "DL5txUdk",
    function (ready) {
        player.on('play', window.TchPlayer_n_hDZqky8qukjbX9VPix.onPlay);
        player.on('complete', window.TchPlayer_n_hDZqky8qukjbX9VPix.onComplete);

        window.tchPlayers[player.elementId] = player;
    }
  );

  var closest = function (element, className) {
    if (!element) {
      return;
    } else {
      var classes = element.className.split(" ");

      for (var i = 0; i < classes.length; i++) {
        if (classes[i] === className) {
          return element;
        }
      }

      return closest(element.parentElement, className);
    }
  };

  window.TchPlayer_n_hDZqky8qukjbX9VPix = {
    replay: function(el){
      window.tchPlayers['video-player-5274-n_hDZqky8qukjbX9VPix'].setPlayheadTime(0);

      el.parentElement.style.display = 'none';

      $('#video-player-5274-n_hDZqky8qukjbX9VPix .oo-ctrls').show();

      window.tchPlayers['video-player-5274-n_hDZqky8qukjbX9VPix'].play();

      el.parentElement.previousSibling.style.display = 'block';

      return false;
    },

    onPlay: function(){
      var videoPlayer = window.tchPlayers['video-player-5274-n_hDZqky8qukjbX9VPix'];
      var player = document.getElementById('video-player-5274-n_hDZqky8qukjbX9VPix');

      var container = closest(player, 'tch-embed-box');

      if (container) {
        container.className = container
          .className
          .replace('tch-embed-box--show-related', '');
      }
    },

    onComplete: function(evt) {
      var videoPlayer = window.tchPlayers['video-player-5274-n_hDZqky8qukjbX9VPix'];
      var player = document.getElementById('video-player-5274-n_hDZqky8qukjbX9VPix');
      var container = closest(player, 'tch-embed-box');

      if (container) {
        container.className = container.className + ' ' +
          'tch-embed-box--show-related';
      }
    }
  };
})();
