var def_box="1.1";		// box.js version

function Dimension(width,height)
{
  this.width = width;
  this.height = height;
}
Dimension.prototype.toString =  function()
{
  return this.width+"x"+this.height;
}


function Boundry(top,left,width,height)
{
  this.top = top;
  this.left = left;
  this.dimension = new Dimension(width,height);
  this.right = this.left+this.dimension.width;
  this.bottom = this.top+this.dimension.height;
}

Boundry.prototype.toString = function()
{
  return this.top+","+this.right+","+this.bottom+","+this.left+" - "+this.dimension.toString()
}

/* @DEPRECATED - use containsPoint */
Boundry.prototype.pointInBounds =  function( dimension )
{
  return (this.left <= dimension.width && dimension.width <= this.right) &&
         (this.top <= dimension.height && dimension.height <= this.bottom );
}

Boundry.prototype.containsPoint =  function( dimension )
{
  return (this.left <= dimension.width && dimension.width <= this.right) &&
         (this.top <= dimension.height && dimension.height <= this.bottom );
}

Boundry.prototype.center = function( offsetX, offsetY )
{
  offsetX = isNaN(parseInt(offsetX))?50:offsetX;
  offsetY = isNaN(parseInt(offsetY))?50:offsetY;
  return new Dimension( (this.left + Math.round(this.dimension.width*(offsetX/100)) ), (this.top + Math.round(this.dimension.height*(offsetY/100)) ) );
}

Boundry.prototype.topLeft =  function()
{
  return new Dimension( this.left, this.top );
}

Boundry.prototype.offsetTopLeft =  function( topLeft )
{
  this.top	+= topLeft.height;
  this.left	+= topLeft.width;
  this.bottom	+= topLeft.height;
  this.right	+= topLeft.width;
}
Boundry.prototype.containsBounds = function( bound )
{
  return (this.top <= bound.top && this.left <= bound.left && this.bottom >= bound.bottom && this.right >= bound.right)

}

Boundry.prototype.intersects = function( bound )
{
  var intersect = this.getIntersect( bound );

  return ( intersect.dimension.width > 0 && intersect.dimension.height > 0 );
}

Boundry.prototype.getIntersect = function( bound )
{
  var newTop    = Math.max(this.top,bound.top);
  var newLeft   = Math.max(this.left,bound.left);
  var newBottom = Math.min(this.bottom,bound.bottom);
  var newRight  = Math.min(this.right,bound.right);
  return new Boundry(newTop,newLeft,(newRight-newLeft),(newBottom-newTop));
}

Boundry.prototype.getExtendDelta = function( bound )
{
  var newTop    = Math.min(this.top,bound.top) - this.top;
  var newLeft   = Math.min(this.left,bound.left) - this.left;
  var newBottom = Math.max(this.bottom,bound.bottom) - this.bottom;
  var newRight  = Math.max(this.right,bound.right) - this.right;
  return new Boundry(newTop,newLeft,(newRight-newLeft),(newBottom-newTop));
}


Boundry.prototype.moveTo =  function( dimension, offsetX, offsetY )
{
  offsetX = isNaN(parseInt(offsetX))?50:offsetX;
  offsetY = isNaN(parseInt(offsetY))?50:offsetY;
  this.left = dimension.width - (this.dimension.width*(offsetX/100));
  this.right = this.left + this.dimension.width;
  this.top = dimension.height - (this.dimension.height*(offsetY/100));
  this.right = this.top + this.dimension.height;
}

Boundry.prototype.grow = function( pixelX, pixelY )
{
  this.left   = Math.max(0, this.left-(pixelX/2));
  this.dimension.width  = this.dimension.width+pixelX;
  this.top    = Math.max(0, this.top-(pixelY/2));
  this.dimension.height = this.dimension.height+pixelY;

  this.right  = this.left+this.dimension.width;
  this.bottom = this.top+this.dimension.height;
}

Boundry.prototype.growToFit = function( pixelStep, fitDim )
{
  var percentGrowth = 0;
  var center        = this.center();
  var newWidth      = 0;
  var newHeight     = 0;

  if( fitDim.width > fitDim.height )
  {
    percentGrowth = (pixelStep / (fitDim.width-this.dimension.width));
    newWidth      = Math.round( Math.min(fitDim.width, this.dimension.width + pixelStep) );
    newHeight     = Math.round( Math.min(fitDim.height, this.dimension.height + ((fitDim.height-this.dimension.height)*percentGrowth)) );
  }
  else
  {
    percentGrowth = (pixelStep / (fitDim.height-this.dimension.height));
    newHeight     = Math.round( Math.min(fitDim.height, this.dimension.height + pixelStep) );
    newWidth      = Math.round( Math.min(fitDim.width, this.dimension.width + ((fitDim.width-this.dimension.width)*percentGrowth)) );
  }

  this.left             = Math.max(0, center.width - Math.round(newWidth/2) );
  this.dimension.width  = newWidth;
  this.top              = Math.max(0, center.height - Math.round(newHeight/2) );
  this.dimension.height = newHeight;

  this.right  = this.left+this.dimension.width;
  this.bottom = this.top+this.dimension.height;
}

Boundry.prototype.shrink = function( pixelX, pixelY )
{
  this.left   = this.left+(pixelX/2);
  this.dimension.width  = Math.max(0, this.dimension.width-pixelX);
  this.top    = this.top+(pixelY/2);
  this.dimension.height = Math.max(0, this.dimension.height-pixelY);

  this.right  = this.left+this.dimension.width;
  this.bottom = this.top+this.dimension.height;
}

Boundry.fromLayer = function( layerId )
{
  return new Boundry( getlayertop(layerId),
                      getlayerleft(layerId),
                      getlayerwidth(layerId),
                      getlayerheight(layerId)
                    );
}

Boundry.prototype.setLayer = function( layerId )
{
  sizelayer(layerId, this.dimension.width, this.dimension.height);
  movelayer(layerId, this.left, this.top);
}

Boundry.fromLayerClip = function( layerId )
{
  var width = getlayerwidth(layerId);
  var height = getlayerheight(layerId);
  var clipT = getcliptop(layerId);
  var clipL = getclipleft(layerId);
  var clipB = getclipbottom(layerId);
  var clipR = getclipright(layerId);
  clipT = isNaN(clipT)?0:clipT;
  clipB = isNaN(clipB)?height:clipB;
  clipL = isNaN(clipL)?0:clipL;
  clipR = isNaN(clipR)?width:clipR;

  var clip = new Boundry( clipT, clipL, (clipR-clipL), (clipB-clipT) );
  return clip;
}

Boundry.prototype.setLayerClip = function( layerId )
{
  cliplayer( layerId, this.left+"px", this.right+"px", this.top+"px", this.bottom+"px" );
}

Boundry.prototype.setWidth = function( width )
{
  var diff = width-this.dimension.width;
  this.dimension.width = width;
  this.left  -= (diff/2);
  if(this.left < 0){ this.left = 0; }
  this.right = this.left + width;
}

Boundry.prototype.setHeight = function( height )
{
  var diff = height-this.dimension.height;
  this.dimension.height = height;
  this.top  -= (diff/2);
  if(this.top < 0){ this.top = 0; }
  this.bottom = this.top + height;
}