// Copyright 2017, University of Colorado Boulder

/**
 *
 * @author - Michael Kauzmann (PhET Interactive Simulations)
 */
define( function( require ) {
  'use strict';

  var inherit = require( 'PHET_CORE/inherit' );
  var joist = require( 'JOIST/joist' );

  var Emitter = require( 'AXON/Emitter' );
  var Rectangle = require( 'SCENERY/nodes/Rectangle' );
  var ButtonListener = require( 'SCENERY/input/ButtonListener' );


  // phet-io modules
  var TBarrierRectangle = require( 'ifphetio!PHET_IO/types/scenery/nodes/TBarrierRectangle' );


  /**
   * @public (joist-internal)
   *
   * Semi-transparent black barrier used to block input events when a dialog
   * (or other popup) is present, and fade out the background.
   *
   * @param x
   * @param y
   * @param width
   * @param height
   * @param cornerXRadius
   * @param cornerYRadius
   * @param modalNodeStack
   * @param options
   * @constructor
   */
  function BarrierRectangle( x, y, width, height, cornerXRadius, cornerYRadius, modalNodeStack, options ) {
    var self = this;


    var tandem = options.tandem;

    // This will be passed up to parents
    options.tandem = tandem.createSupertypeTandem();
    Rectangle.call( this, x, y, width, height, cornerXRadius, cornerYRadius, options );

    this.startedCallbacksForFiredEmitter = new Emitter();
    this.endedCallbacksForFiredEmitter = new Emitter();

    modalNodeStack.lengthProperty.link( function( numBarriers ) {
      self.visible = numBarriers > 0;
    } );

    this.addInputListener( new ButtonListener( {
      fire: function( event ) {
        self.startedCallbacksForFiredEmitter.emit();
        assert && assert( modalNodeStack.length > 0 );
        modalNodeStack.get( modalNodeStack.length - 1 ).hide();
        self.endedCallbacksForFiredEmitter.emit();
      }
    } ) );


    this.mutate( { tandem: tandem, phetioType: TBarrierRectangle } );
  }

  joist.register( 'BarrierRectangle', BarrierRectangle );

  return inherit( Rectangle, BarrierRectangle );
} );
@@ -48,16 +48,152 @@
 er%0A%0A/**%0A
+ * Semi-transparent black barrier used to block input events when a dialog%0A * (or other popup) is present, and fade out the background.%0A
  *%0A * @a
@@ -554,17 +554,16 @@
 er' );%0A%0A
-%0A
   // phe
@@ -675,195 +675,14 @@
 );%0A%0A
-%0A
   /**%0A
-   * @public (joist-internal)%0A   *%0A   * Semi-transparent black barrier used to block input events when a dialog%0A   * (or other popup) is present, and fade out the background.%0A   *%0A
    *
@@ -969,24 +969,25 @@
 options ) %7B%0A
+%0A
     var self
@@ -996,17 +996,16 @@
  this;%0A%0A
-%0A
     var 
@@ -1120,24 +1120,25 @@
 peTandem();%0A
+%0A
     Rectangl
@@ -1767,17 +1767,16 @@
 %7D ) );%0A%0A
-%0A
     this
@@ -1785,16 +1785,22 @@
 utate( %7B
+%0A     
  tandem:
@@ -1807,16 +1807,22 @@
  tandem,
+%0A     
  phetioT
@@ -1843,16 +1843,20 @@
 ectangle
+%0A   
  %7D );%0A  

fix doc, fix options formatting, remove extra blank lines, #394