// ---------------------------------------------------------------------------
// (c) 2003 Geotalk, Hans Martin Mohn
// ---------------------------------------------------------------------------
//
// Tool to facilitate panning of the map.
//
// Dependencies:
//
//    gtTool.js
//

gtToolPan.prototype = new gtTool();  // Sets gtToolPan to be a subclass of gtTool!
gtToolPan.prototype.constructor = gtTool;
gtToolPan.superclass = gtTool.prototype;

var g_click_limit = 5; // How many pixels you can move your mouse with button pressed and still be considered a click, not a drag.

//
// Tool to enable panning the map
//
// Dependencies:
//
// gtTool.js
//
function gtToolPan()
{
  // Member variables
  this.m_map          = null;   // The context (document, canvas, selection, etc) to operate upon.
  this.m_start_x      = 0;
  this.m_start_y      = 0;
  this.m_button_down  = false;
} // end gtToolPan() constructor



// Methods
gtToolPan.prototype.select = function(map)
{
  this.m_map = map;
  this.m_map.setCursor("move");
}; // end select()



gtToolPan.prototype.buttonDown = function(evt)
{
  this.m_button_down = true;
  this.m_start_x = evt.gtX;
  this.m_start_y = evt.gtY;
  gtPreventDefault(evt);
}; // end buttonDown()



gtToolPan.prototype.mouseMove = function(evt)
{
//  document.title = "element=" + evt.gtElement.tagName + " (" + evt.gtX + ", " + evt.gtY + ")";
  if (this.m_button_down)
  {
    // Move map
    var dx = evt.gtX - this.m_start_x;
    var dy = evt.gtY - this.m_start_y;
    gtSetLeft(this.m_map.m_img, dx);
    gtSetTop (this.m_map.m_img, dy);
  }
  gtCancelBubble(evt);
  return gtPreventDefault(evt);
}; // end mouseMove()



gtToolPan.prototype.buttonUp = function(evt)
{
  if (!this.m_button_down)
    return;

  this.m_button_down = false;

  var dx = evt.gtX - this.m_start_x;
  var dy = evt.gtY - this.m_start_y;
  if (Math.abs(dx) > g_click_limit || Math.abs(dy) > g_click_limit)
  {
    // Treat as drag
    this.m_map.m_url.setParam("map.x", this.m_map.m_img.width  / 2 - dx);
    this.m_map.m_url.setParam("map.y", this.m_map.m_img.height / 2 - dy);
  }
  else
  {
    // Treat as set new center
    this.m_map.m_url.setParam("map.x", evt.gtX);
    this.m_map.m_url.setParam("map.y", evt.gtY);
  }

  this.m_map.m_url.setParam("cmd", "center");
  this.m_map.showMap(this.m_map.m_url);
  gtCancelBubble(evt);
  return gtPreventDefault(evt);
}; // end buttonUp()



// This tool does not depend on the current selection!
this.canHandleSelection = function (selection) { return true; }

