// Copyright 2017-2020, University of Colorado Boulder

/**
 * For preventing Safari from going to sleep - added to the self.display.domElement instead of the body to prevent a VoiceOver bug
 * where the virtual cursor would spontaneously move when the div content changed, see https://github.com/phetsims/joist/issues/140
 *
 * @author Jonathan Olson (PhET Interactive Simulations)
 * @author Sam Reid (PhET Interactive Simulations)
 */

import joist from './joist.js';

// variables
let started = false;

const Heartbeat = {

  /**
   * Initializes the heartbeat div to begin ticking to prevent Safari from going to sleep.
   * @param {Sim} sim
   * @public
   */
  start: function( sim ) {
    assert && assert( !started, 'Heartbeat can only be started once' );
    started = true;

    const heartbeatDiv = document.createElement( 'div' );
    heartbeatDiv.style.opacity = 0;

    // Extra style (also used for accessibility) that makes it take up no visual layout space.
    // Without this, it could cause some layout issues. See https://github.com/phetsims/gravity-force-lab/issues/39
    heartbeatDiv.style.position = 'absolute';
    heartbeatDiv.style.left = '0';
    heartbeatDiv.style.top = '0';
    heartbeatDiv.style.width = '0';
    heartbeatDiv.style.height = '0';
    heartbeatDiv.style.clip = 'rect(0,0,0,0)';
    heartbeatDiv.setAttribute( 'aria-hidden', true ); // hide div from screen readers (a11y)
    sim.display.domElement.appendChild( heartbeatDiv );

    // prevent Safari from going to sleep, see https://github.com/phetsims/joist/issues/140
    sim.frameStartedEmitter.addListener( function() {
      if ( sim.frameCounter % 1000 === 0 ) {
        heartbeatDiv.innerHTML = phet.joist.random.nextDouble();
      }
    } );
  }
};

joist.register( 'Heartbeat', Heartbeat );

export default Heartbeat;
@@ -500,16 +500,122 @@
 false;%0A%0A
+// a boolean to flip back and forth to make sure safari doesn't get sleepy, see usage.%0Alet value = true;%0A%0A
 const He
@@ -1763,24 +1763,48 @@
 0 === 0 ) %7B%0A
+        value = !value;%0A
         hear
@@ -1828,38 +1828,13 @@
 L = 
-phet.joist.random.nextDouble()
+value
 ;%0A  

Use boolean flip for heartbeat instead of random, see https://github.com/phetsims/states-of-matter/issues/325