//ZoomBox.js

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;
  this.hasFocus = false;
  this.element.cbe.addEventListener('mousemove',EVENT_LISTENER, false);
  this.element.cbe.addEventListener('mouseover',EVENT_LISTENER, false);
  this.element.cbe.addEventListener('mouseout',EVENT_LISTENER, false);
};

/*******************************************/

ZoomBox.prototype.setActive = function (newXStart, newYStart)
{
  this.xStart = newXStart+this.xOffset;
  this.yStart = newYStart+this.yOffset;
  
  this.isActive = true;
  this.hasFocus = false;
  this.element.cbe.moveTo(this.xStart, this.yStart);
  this.element.cbe.resizeTo(1,1);
  this.element.cbe.show();
};

/*******************************************/

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.xOffset)) && (this.xEnd < (this.mapImage.width() + this.xOffset - this.mapImage.zoomBoxMargin)) && (this.yEnd > (this.mapImage.zoomBoxMargin + this.yOffset)) && (this.yEnd < (this.mapImage.height() - this.mapImage.zoomBoxMargin + this.yOffset)))
  {
    this.element.cbe.moveTo((this.xStart<this.xEnd)?this.xStart:this.xEnd ,(this.yStart<this.yEnd)?this.yStart:this.yEnd);
    this.element.cbe.resizeTo(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.hasFocus = false;
  if (arguments.length > 0)
  {
    this.xEnd = newXPosition + this.xOffset;
    this.yEnd = newYPosition + this.yOffset;
  }  
  var results = new Array(this.xStart-this.xOffset, this.yStart-this.yOffset, this.xStart + this.width() -this.xOffset, this.yStart + this.height() - this.yOffset);
  this.xStart = 0;
  this.yStart = 0;
  this.xEnd = 0;
  this.yEnd = 0;

  this.element.cbe.resizeTo(2,2);
  this.element.cbe.moveTo(1,1);
  this.element.cbe.hide();
  return results;
};

ZoomBox.prototype.repositionUsingLocalCoordinates = function(newX, newY)
{
  if (this.xStart < this.xEnd){                          //box is on positive x axis, so the origin is on the left
    var newXPosition = this.xStart + newX - this.xOffset;
  }
  else{                                                  //box is lying on the negative x axis, so the origin is on the right
    var newXPosition = this.xStart - this.width() + newX - this.xOffset;
  }
  if (this.yStart < this.yEnd){                          //box is on the positive y axis, so the origin is at the top
    var newYPosition = this.yStart + newY - this.yOffset;
  }
  else{                                                  //box is on the negative y axis, so the origin is at the bottom
    var newYPosition = this.yStart - this.height() + newY - this.yOffset;
  }
  this.update(newXPosition, newYPosition);
};
/*******************************************/
ZoomBox.prototype.mouseMoveCallback = function(e)
{
  this.repositionUsingLocalCoordinates(e.offsetX, e.offsetY);
  this.hasFocus = true;
};

ZoomBox.prototype.mouseOverCallback = function(e)
{
  this.repositionUsingLocalCoordinates(e.offsetX, e.offsetY);
  this.hasFocus = true;
};

/*******************************************/
ZoomBox.prototype.mouseUpCallback = function(e)
{
  this.hasFocus = false;
};


ZoomBox.prototype.mouseOutCallback = function(e)
{
  this.hasFocus = false;
};

