function SurfaceList( surface, debugLog )
{
	this.construct = function( surface, debugLog )
	{

		this._list = new Array();
		this._surface = surface;

		if( debugLog ) {
			this._debugLog = debugLog;
		}

	}

	this._debugLog = null;
	this._surface = null;
	this._locked = false;
	this._list = null;
	this._ensured = false;

	this.__class = 'SurfaceList';

	this.__exception = function( fname, message )
	{
		var m = '';

		if( this._surface.getId() != null ) {
			m += '[' + this._surface.getId() + '] ';
		}

		m += this.__class + '.' + fname + '(): ' + message + '.';

		throw m;
	}

	this.__unlocked = function( fname, fn )
	{
		if( !this._locked ) {
			//this._updated = false;
			this._ensured = false;
			fn( this );
		}
		else this.__exception( fname, this.__class + ' must NOT be locked to perform this operation' );
	}

	this.__locked = function( fname, fn )
	{
		if( this._locked ) {
			fn( this );
		}
		else this.__exception( fname, this.__class + ' MUST be locked to perform this operation' );
	}

	this.__ensured = function( fname, fn )
	{
		if( this._ensured ) {
			fn( this );
		}
		else this.__exception( fname, this.__class + ' for ' + this._surface + ' MUST be ensured to perform this operation' );
	}

	this.kill = function()
	{

		if( this._list.length > 0 ) {
			for( var child in this._list ) {
				this._list[ child ].kill();
			}
		}
	}

	this.count = function()
	{
		return this._list.length;
	}

	this.get = function( index )
	{
		//this.__locked( 'get', function( self ) {
			return this._list[ index ];
		//});
	}

	this.add = function( surface, id )
	{
		this.__unlocked( 'add', function( self ) {

                	var sf = null;
                	var tmpid;

			if( id ) tmpid = id; tmpid = self._surface.getId() + '_child_' + ( self.count() + 1 );

			if( surface ) {

				if( surface.__isSurface == true ) {
                                	sf = surface;
		  		}
		  		else {
					sf = new Surface( tmpid, self._surface, self._debugLog );
				}
	  		}
	  		else {

				sf = new Surface( tmp, self._surface, self._debugLog );
			}

			self._list.push( sf );

			return sf;
		});
	}

	this.lock = function()
	{
		this.__unlocked( 'lock', function( self ) {
			self._locked = true;
		});

	}

	this.unlock = function()
	{
		this.__locked( 'unlock', function( self ) {
			self._locked = false;
		});

	}

	this.ensure = function()
	{
		this.__unlocked( 'ensure', function( self ) {

                	for( i in self._list ) {

				if( !self._list[ i ].isLocked() ) {
					self._list[ i ].lock();
				}

				self._list[ i ].getChildren().ensure();

				if( !self._list[ i ].isUpdated() ) {
					self._list[ i ].update();
				}
			}
			
			self._ensured = true;

		});
	}


	/*
	this.maxWidth = function()
	{
		this.__locked( 'maxWidth', function( self ) {
			self._ensured( 'maxWidth', function( self ) {
                        	var tmp = 0;;
                        	var max = 0;

                        	for( i in self._list ) {




                                	tmp = self._list[ i ].__getMinDim().width;
					if( tmp > max ) max = tmp;
				}
				return max;

			});
		});
	}


	this.maxHeight = function()
	{
		this.__locked( 'maxHeight', function( self ) {
			self._ensured( 'maxHeight', function( self ) {
                        	var tmp = 0;
                        	var max = 0;

                        	for( i in self._list ) {
                                	tmp = self._list[ i ].__getMinDim().height;
					if( tmp > max ) max = tmp;
				}
				return max;

			});
		});
	}

	this.minX = function()
	{
		this.__locked( 'minX', function( self ) {
			self._ensured( 'minX', function( self ) {
                        	var tmp = 0;
                        	var min = 0;

                        	for( i in self._list ) {
                                	tmp = self._list[ i ].getX();
					if( tmp < min ) min = tmp;
				}
				return min;

			});
		});
	}


	this.minY = function()
	{
		this.__locked( 'minY', function( self ) {
			self._ensured( 'minY', function( self ) {
                        	var tmp = 0;
                        	var min = 0;

                        	for( i in self._list ) {
                                	tmp = self._list[ i ].getY();
					if( tmp < min ) min = tmp;
				}
				return min;

			});
		});
	}


	this.resetOrigin = function( width, height )
	{
		this.__locked( 'resetOrigin', function( self ) {
			self._ensured( 'resetOrigin', function( self ) {

                        	if( width ) {
					var minX = self.minX();

					for( i in self._list ) {
						self._list.setX( self._list.getX() - minX );
					}

				}


				if( height ) {
					var minY = self.minY();
					for( i in self._list ) {
						self._list.setY( self._list.getY() - minY );
					}
				}


			});
		});
	}
	*/
	this.construct( surface, debugLog );
}
