// Copyright 2013-2015, University of Colorado Boulder

/*
 * The front of a bucket (does not include the hole)
 */
define( function( require ) {
  'use strict';

  // Includes
  var Color = require( 'SCENERY/util/Color' );
  var inherit = require( 'PHET_CORE/inherit' );
  var LinearGradient = require( 'SCENERY/util/LinearGradient' );
  var Matrix3 = require( 'DOT/Matrix3' );
  var Node = require( 'SCENERY/nodes/Node' );
  var Path = require( 'SCENERY/nodes/Path' );
  var PhetFont = require( 'SCENERY_PHET/PhetFont' );
  var sceneryPhet = require( 'SCENERY_PHET/sceneryPhet' );
  var Tandem = require( 'TANDEM/Tandem' );
  var Text = require( 'SCENERY/nodes/Text' );

  /**
   * @param {Bucket} bucket - Model of a bucket, type definition found in phetcommon/model as of this writing.
   * @param {ModelViewTransform2} modelViewTransform
   * @param {Object} [options]
   * @constructor
   */
  function BucketFront( bucket, modelViewTransform, options ) {

    // Invoke super constructor.
    Node.call( this, { cursor: 'pointer' } );

    options = _.extend( {
      labelNode: new Text( bucket.captionText, {
        font: new PhetFont( 20 ),
        fill: bucket.captionColor,
        tandem: Tandem.tandemRequired().createTandem( 'label' )
      } )
    }, options );

    // @public (a11y)
    this.bucket = bucket;

    var scaleMatrix = Matrix3.scaling( modelViewTransform.getMatrix().m00(), modelViewTransform.getMatrix().m11() );
    this.transformedShape = bucket.containerShape.transformed( scaleMatrix );
    var baseColor = new Color( bucket.baseColor );
    var frontGradient = new LinearGradient( this.transformedShape.bounds.getMinX(),
      0,
      this.transformedShape.bounds.getMaxX(),
      0 );
    frontGradient.addColorStop( 0, baseColor.colorUtilsBrighter( 0.5 ).toCSS() );
    frontGradient.addColorStop( 1, baseColor.colorUtilsDarker( 0.5 ).toCSS() );
    this.addChild( new Path( this.transformedShape, {
      fill: frontGradient
    } ) );
    this.labelNode = options.labelNode;
    this.setLabel( this.labelNode );

    // Set initial position.
    this.translation = modelViewTransform.modelToViewPosition( bucket.position );

    this.mutate( options );
  }

  sceneryPhet.register( 'BucketFront', BucketFront );

  return inherit( Node, BucketFront, {
      /**
       * Set a scenery node to appear in front of the bucket.
       * @public
       * @param {Node} labelNode
       */
      setLabel: function( labelNode ) {
        if ( this.hasChild( this.labelNode ) ) {
          this.removeChild( this.labelNode );
          this.labelNode = labelNode;
        }
        labelNode.scale( Math.min( 1,
          Math.min( ( ( this.transformedShape.bounds.width * 0.75 ) / labelNode.width ),
            ( this.transformedShape.bounds.height * 0.8 ) / labelNode.height ) ) );
        labelNode.center = this.transformedShape.bounds.getCenter();
        this.addChild( labelNode );
      }
    }
  );
} );
@@ -1439,29 +1439,28 @@
 11() );%0A    
-this.
+var 
 transformedS
@@ -1611,21 +1611,16 @@
 adient( 
-this.
 transfor
@@ -1657,29 +1657,24 @@
    0,%0A      
-this.
 transformedS
@@ -1898,21 +1898,16 @@
 w Path( 
-this.
 transfor
@@ -1950,24 +1950,40 @@
 t%0A    %7D ) );
+%0A%0A    // @public
 %0A    this.la
@@ -2292,24 +2292,20 @@
 , %7B%0A    
-  
 /**%0A
-  
      * S
@@ -2357,26 +2357,24 @@
 ucket.%0A     
-  
 * @public%0A  
@@ -2371,18 +2371,16 @@
 @public%0A
-  
      * @
@@ -2407,23 +2407,19 @@
 de%0A     
- 
- 
 */%0A
-  
     setL
@@ -2444,26 +2444,25 @@
 belNode ) %7B%0A
-  
+%0A
       if ( t
@@ -2496,18 +2496,16 @@
 e ) ) %7B%0A
-  
         
@@ -2538,24 +2538,56 @@
 elNode );%0A  
+    %7D%0A%0A      if ( labelNode ) %7B%0A
         this
@@ -2622,219 +2622,98 @@
     
-%7D%0A        labelNode.scale( Math.min( 1,%0A          Math.min( ( ( this.transformedShape.bounds.width * 0.75 ) / labelNode.width ),%0A            ( this.transformedShape.bounds.height * 0.8 ) / labelNode
+this.labelNode.maxWidth = this.width * 0.8;%0A        this.labelNode.maxHeight = this
 .height
- ) ) )
 ;%0A  
@@ -2714,24 +2714,29 @@
 ht;%0A        
+this.
 labelNode.ce
@@ -2751,43 +2751,26 @@
 his.
-transformedShape.b
+localB
 ounds.
-getC
+c
 enter
-()
 ;%0A  
@@ -2790,16 +2790,21 @@
 dChild( 
+this.
 labelNod
@@ -2823,16 +2823,18 @@
 %0A    %7D%0A 
+ %7D
  );%0A%7D );

revise setLabel method (see #308)