//ZoomBox.js
//used by the map image object to handle drawing a box.  returns size of drawn box.

ZoomBox = function(newID, newParent, newXOffset, newYOffset, newMapImage)
{
  if (arguments.length > 0)
    this.init(newID, newParent, newXOffset, newYOffset, newMapImage);
};

ZoomBox.prototype = new GuiWidget();
ZoomBox.prototype.constructor = ZoomBox;
ZoomBox.superclass = GuiWidget.prototype;

/*******************************************/

ZoomBox.prototype.init = function(newID, newParent, newXOffset, newYOffset, newMapImage)
{
  ZoomBox.superclass.init.call(this, newID, newParent, 1, 1, 2, 1, 1, false, "zoomBox");
  this.xStart = 0;
  this.yStart = 0;
  this.xOffset = newXOffset;
  this.yOffset = newYOffset;
  this.mapImage = newMapImage;
  this.isActive = false;
};

/*******************************************/

ZoomBox.prototype.setActive = function (newXStart, newYStart)
{
  xMoveTo(this.element,0,0);
  xResizeTo(this.element,1,1);
  
  this.xStart = newXStart;
  this.yStart = newYStart;
  
  this.isActive = true;
  xMoveTo(this.element,this.xStart, this.yStart);
  xResizeTo(this.element,1,1);
  xShow(this.element);
};

/*******************************************/

ZoomBox.prototype.update = function (newXPosition, newYPosition)
{
  this.xEnd = newXPosition+this.xOffset;
  this.yEnd = newYPosition+this.yOffset;
  if ((this.isActive) && (this.xEnd > (this.mapImage.zoomBoxMargin)) && (this.xEnd < (this.mapImage.width() - this.mapImage.zoomBoxMargin)) && (this.yEnd > (this.mapImage.zoomBoxMargin)) && (this.yEnd < (this.mapImage.height() - this.mapImage.zoomBoxMargin)))
  {
    xMoveTo(this.element,(this.xStart<this.xEnd)?this.xStart:this.xEnd ,(this.yStart<this.yEnd)?this.yStart:this.yEnd);
    xResizeTo(this.element,Math.abs(this.xStart - this.xEnd), Math.abs(this.yStart - this.yEnd));
  }
  else
    this.setInactive(newXPosition, newYPosition);
};

/*******************************************/

ZoomBox.prototype.setInactive = function(newXPosition, newYPosition)
{
  this.isActive = false;
  this.xStart = 0;
  this.yStart = 0;
  this.xEnd = 0;
  this.yEnd = 0;
  xResizeTo(this.element,2,2);
  xMoveTo(this.element,1,1);
  xHide(this.element);
};

