var def_box="2.0";		// 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.dimension = new Dimension(width-(left<0?left:0),height-(top<0?top:0));
  this.top = (top<0?0:top);
  this.left = (left<0?0:left);
  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();
};
Boundry.prototype.pointInBounds =  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,10))?50:offsetX;
  offsetY = isNaN(parseInt(offsetY,10))?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.moveTo =  function( dimension, offsetX, offsetY )
{
  offsetX = isNaN(parseInt(offsetX,10))?50:offsetX;
  offsetY = isNaN(parseInt(offsetY,10))?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;
  return this;
};

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;
  return this;
};

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;
  return this;
};

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;
  return this;
};

Boundry.fromElem = function( sel )
{
  var elem = $(sel), elemOffset = elem.offset();
  return new Boundry( elemOffset.top,
                      elemOffset.left,
                      elem.outerWidth(),
                      elem.outerHeight()
                    );
};

Boundry.prototype.setLayer = function( sel )
{
  $(sel).width(this.dimension.width).height(this.dimension.height);
  $(sel).css({"left":this.left, "top":this.top});
  return this;
};

Boundry.fromElemClip = function( sel )
{
  var clip = $(sel).clip();
  return new Boundry( clip.top, clip.left, (clip.right-clip.left), (clip.bottom-clip.top) );
};

Boundry.prototype.setElemClip = function( sel )
{
  $(sel).clip({top:this.top, left:this.left, bottom:this.bottom, right:this.right} );
  return this;
};

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;
  return this;
};

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;
  return this;
};



