/* {{{ GPL

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

}}} */

var Frame = Class.extend({

  // {{{ init
  init: function() {
    
    this.figures = new Array();
    this.animation = null;

    this.cameraView = new CameraView();
  },
  // }}}

  // {{{ addedTo
  addedTo: function( animation ) {

    this.log.debug( "Added frame to", animation );
    this.animation = animation;
    this.cameraView.addedTo( animation );
    this.addFigure( this.cameraView );
  
  },
  // }}}

  // {{{ copy
  copy: function() {

    var copy = new Frame();

    copy.cameraView.translate( this.cameraView.dx, this.cameraView.dy );
    copy.cameraView.scale( this.cameraView.zoom );
    copy.cameraView.rotate( this.cameraView.rotation );

    for ( var i = 0; i < this.figures. length; i ++ ) {
      if ( ! this.figures[ i ].special ) {
        copy.figures[ copy.figures.length ] = this.figures[ i ].copy();
      }
    }
    
    return copy;
  },
  // }}}

  // {{{ draw
  draw: function( context, onionSkinning ) {

    for ( var i = 0; i < this.figures.length; i ++ ) {
      this.log.debug( "drawing" + this.figures[i] );
      this.figures[i].draw( context, onionSkinning );
    }
  },
  // }}}
  
  // {{{ addFigure
  addFigure: function( figure, index ) {
    
    if ( this.animation == null ) {
      throw( "Cannot add a figure to a frame until the frame is added to an animation" );
      // This is so that a unique id can be guarenteed.
    }
    
    this.log.debug( "Need to add figure", figure );
    if ( figure.id == null ) {
      
      this.log.debug( "id was null" );
      figure.id = this.animation.allocateNextFigureId();

    } else {

      this.log.debug( "id was NOT null" );
      var foundFigure = this.findFigure( figure.id );
      if ( foundFigure == figure ) {
        throw( "Attempted to add a figure which is already on the frame" );
      }
        
      while ( foundFigure != null ) {
        this.log.debug( "found figure, so figure id will be ", this.nextFigureId );
        figure.id = this.animation.allocateNextFigureId();
        foundFigure = this.findFigure( figure.id );
      }
      
    }

    this.animation.usedFigureId( figure.id );
    
    if ( index == null ) {
      this.figures[this.figures.length] = figure;
    } else {
      this.figures.splice( index, 0, figure );
    }

    this.log.debug( "Added figure to frame", figure );
  },
  // }}}
  
  // {{{ removeFigure
  removeFigure: function( id ) {
    for( var i = 0; i < this.figures.length; i ++ ) {
      var figure = this.figures[ i ];
      if ( figure.id == id ) {
        this.figures.splice(i, 1);
        return i;
      }
    }    
    
    return null;
  },
  // }}}

  // {{{ figureIndex
  figureIndex: function( figure ) {
    for( var i = 0; i < this.figures.length; i ++ ) {
      if ( figure == this.figures[ i ] ) {
        this.log.debug( "found it", figure ); 
        return i;
      }
    }
    this.log.debug( "didn't found it" ); 
    return null;
  },
  // }}}
  
  // {{{ findFigure
  findFigure: function( id ) {
    for( var i = 0; i < this.figures.length; i ++ ) {
      var figure = this.figures[ i ];
      this.log.debug( "Looking for figure id", id, this.figures[i] ); 
      if ( figure.id == id ) {
        this.log.debug( "found it", figure ); 
        return figure;
      }
    }
    this.log.debug( "didn't found it" ); 
    return null;
  },
  // }}}
  
  // {{{ findFigureAt
  findFigureAt: function( x, y, zoom ) {
    
    for ( var i = this.figures.length - 1; i >= 0; i -- ) {
      var figure = this.figures[i];
      if ( figure.findNodeAt( x, y ) != null ) {
        return figure;
      }
      if ( figure.findHandleAt( x, y, zoom ) != null ) {
        return figure;
      }
    }
    return null;
  },
  // }}}

  // {{{ replaceFigure
  replaceFigure: function( id, replacementFigure, reposition ) {

    this.log.debug( "Replacing new : ", replacementFigure, replacementFigure.root.x, replacementFigure.root.y );
    
    for( var i = 0; i < this.figures.length; i ++ ) {
      var figure = this.figures[ i ];
      this.log.debug( "comparing", figure.id, id );
      if ( figure.id == id ) {
        this.figures[ i ] = replacementFigure;
        
        this.log.debug( "Replacing old : ", figure, figure.root.x, figure.root.y );
    
        if ( reposition ) {
          replacementFigure.translate( figure.root.x - replacementFigure.root.x, figure.root.y - replacementFigure.root.y );
        }
        
        return figure;
        
      }
    }
    
    return null;
  },
  // }}}
  
  // {{{ getBackgroundColor
  getBackgroundColor: function() {
    return this.animation.backgroundColor;
  },
  // }}}

  // {{{ toString
  toString: function( ) {
    return "Frame";
  }
  // }}}

}); 

Frame.prototype.log = new Log( "Frame" );

