/** 
 * Resize control PResizeControl for Google's map
 * (c) 2010 by xemle@phtagr.org for phtagr - social photo gallery for your community
 * --
 * Usage, copy, modifications are permitted in any case, but link www.phtagr.org in 
 * your source and/or page.
 * --
 * based on http://www.wolfpil.de/map-in-a-box.html 
 * by Wolfgang Pichler, wolfpil-at-gmail-com 
 */
function PResizeControl() {};
PResizeControl.prototype = new GControl();
PResizeControl.RESIZE_BOTH = 0;
PResizeControl.RESIZE_WIDTH = 1;
PResizeControl.RESIZE_HEIGHT = 2;
PResizeControl.prototype.initialize = function(gMap) {
  var that = this;
  this._map = gMap;
  this.resizable = false;

  /** modes: 0 both, 1 only width, 2 only height */
  this.mode = PResizeControl.RESIZE_BOTH;

  this.minWidth = 295;
  this.minHeight = 295;
  this.maxWidth = 650;
  this.maxHeight = 400;

  this.diffX = 0;
  this.diffY = 0;

  var container = document.createElement("div");
  container.style.width = "20px";
  container.style.height = "20px";
  // embedded image does not work with IE < 8
  container.style.backgroundImage = "url(images/drag.png)";

  gMap.getContainer().appendChild(container);

  GEvent.addDomListener(container, 'mousedown', function() { that.resizable = true; });
  GEvent.addDomListener(document, 'mouseup', function() { that.resizable = false; });
  GEvent.addDomListener(document, 'mousemove', function(e) { that.onmouseover(e); });

  /* Move the 'Terms of Use' 25px to the left to make sure that it's fully
   * readable */
  var terms = gMap.getContainer().childNodes[2];
  terms.style.marginRight = "25px";

  return container;
}

PResizeControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT,new GSize(0,0));
}

PResizeControl.prototype.onmouseover = function(e) {
  // Include possible scroll values
  var sx = window.scrollX || document.documentElement.scrollLeft || 0;
  var sy = window.scrollY || document.documentElement.scrollTop || 0;

  // IEs event definition
  if(!e) { 
    e = window.event;
  }

  mouseX = e.clientX + sx;
  mouseY = e.clientY + sy;

  /* Direction of mouse movement
  *  deltaX: -1 for left, 1 for right
  *  deltaY: -1 for up, 1 for down
  */
  var deltaX = this.diffX - mouseX;
  var deltaY = mouseY - this.diffY;
  // Store difference in object's variables
  this.diffX = mouseX;
  this.diffY = mouseY;

  // resize button is being held
  if (this.resizable) {
    this.changeMapSize(deltaX, deltaY);
  }

  return false;
}

// Resizes the map's width and height by the given increment
PResizeControl.prototype.changeMapSize = function(dx, dy) {
  var container = this._map.getContainer();
  var width = parseInt(container.style.width);
  var height = parseInt(container.style.height);

  width += dx;
  height += dy;

  if (this.minWidth) { width = Math.max(this.minWidth, width); }
  if (this.maxWidth) { width = Math.min(this.maxWidth, width); }
  if (this.minHeight) { height = Math.max(this.minHeight, height); }
  if (this.maxHeight) { height = Math.min(this.maxHeight, height); }

  if (this.mode != PResizeControl.RESIZE_HEIGHT) {
   container.style.width = width + "px";
  }
  if (this.mode != PResizeControl.RESIZE_WIDTH) {
    container.style.height = height + "px";
  }
  this._map.checkResize();
}
