<!DOCTYPE html>
<html>
<title>Clicking on YouTube Embedded Iframe - RJM Programming - June, 2016 - via Google YouTube API https://developers.google.com/youtube/iframe_api_reference</title>
 <script type='text/javascript'>
var cnt=0;
var toggle=1;
var secscnt=0;
var suffix="#t=00m00s";
var presuffix="";
var wo=null;
var yturl='';
var playtime=4000;
var delay=1300;
var vlen=0;
var eventtarget=null;
var duration=-1;
var previd="7o9wvYXR3n0";
var vid="7o9wvYXR3n0";
function updatesc() {
    cnt++;
    if (toggle == 0) secscnt++;
    var ours="00" + eval(secscnt % 60);
    var ourm="00" + eval((secscnt - ours) / 60);
    suffix="#t=" + ourm.substring(eval(ourm.length - 2)) + "m" + ours.substring(eval(ours.length - 2)) + "s";
    if (presuffix != "") presuffix=suffix;
}
</script>
</head>
  <body onload='setInterval(updatesc,1000);'>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>
    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      if (document.URL.indexOf('vid=') != -1) {
      vid=location.search.split('vid=')[1] ? location.search.split('vid=')[1].split('&')[0] : prompt('Please enter YouTube video id', previd);
      var splaytime=location.search.split('playtime=')[1] ? location.search.split('playtime=')[1].split('&')[0] : prompt('Please enter playtime snippets in seconds (-1 to play whole thing in one session)', playtime / 1000);
      if (document.URL.indexOf('playtime=') != -1) {
      playtime=eval(splaytime * 1);
      } else {
      playtime=eval(splaytime * 1000);
      }
      } else {
      vid=location.search.split('vid=')[1] ? location.search.split('vid=')[1].split('&')[0] : prompt('Please enter YouTube video id', previd);
      var ssplaytime=location.search.split('playtime=')[1] ? location.search.split('playtime=')[1].split('&')[0] : prompt('Please enter playtime snippets in seconds (-1 to play whole thing in one session)', playtime / 1000);
      if (document.URL.indexOf('playtime=') != -1) {
      playtime=eval(ssplaytime * 1);
      } else {
      playtime=eval(ssplaytime * 1000);
      }
      if (vid != previd) location.href='./stop_start_youtube.html?vid=' + vid + '&playtime=' + playtime;
      }
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: vid,
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }
      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
        toggle=eval(1 - toggle);
      }
      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING && !done) {
          duration=player.getDuration();
          if (playtime < 0) playtime=duration * 1000;
          document.title='Playing YouTube ' + vid + ' for ' + playtime / 1000 + ' seconds ... is ' + duration + ' seconds altogether.'; 
          document.getElementById('ptitle').innerHTML='Playing YouTube ' + vid + ' for ' + playtime / 1000 + ' seconds ... is ' + duration + ' seconds altogether.'; 
          setTimeout(stopVideo, playtime + delay);
          done = true;
          eventtarget=event.target;
        } 
      }
      function stopVideo() {
        toggle=1;
        secscnt=player.getCurrentTime();
        duration=player.getDuration();
        if (playtime < 0) playtime=duration * 1000;
        player.stopVideo();
        //alert(secscnt);
        if (secscnt >= duration) {
        alert("Stumps has been called.  End of play.");
        } else {
        setTimeout(startVideo, playtime + delay);
        }
      }
      function startVideo() {
        if (secscnt >= duration) {
        alert("Stumps is called.  End of play.");
        } else {
        document.title='Playing YouTube ' + vid + ' starting at ' + secscnt + ' seconds to ' + Math.min(duration, eval(secscnt + (playtime / 1000))) + ' seconds for up to ' + playtime / 1000 + ' seconds ... is ' + duration + ' seconds altogether.'; 
        document.getElementById('ptitle').innerHTML='Playing YouTube ' + vid + ' starting at ' + secscnt + ' seconds to ' + Math.min(duration, eval(secscnt + (playtime / 1000))) + ' seconds for up to ' + playtime / 1000 + ' seconds ... is ' + duration + ' seconds altogether.'; 
        eventtarget.cueVideoById(vid, secscnt, secscnt + playtime / 1000, "medium"); 
        toggle=0;
        player.playVideo();
        setTimeout(stopVideo, playtime + delay);
        }
      }
    </script>
  <p id='ptitle'></p>
  </body>
</html>