// All contents are copyright of NextBus Inc 2005. 
// May not be reproduced or reengineered in any manner.

// These functions are for providing the zoom box feature.
// Inspired by http://www40.brinkster.com/rajbk/map.html

//<![CDATA[


var ZOOMAREA_KEYCODE = 90; // 'z' key zooms an area when the mouse is moved (not dragged).
var ESCAPE_KEYCODE = 27;   // the 'Esc' key for aborting zoom
var MIN_TIME_BETWEEN_REDRAWS = 300; // msec. To prevent too many calls to drawing rectangle.

var mouseX, mouseY;
var keyPressX, keyPressY;
var currentlyAdjustingRectangle = false;
var zoomBoxPolyline;
var lastTimeDrewRectangle;
var redrawTimeout;

function getObjectTop(refobj) {
  var y = refobj.offsetTop;
  var obj = refobj.offsetParent;
  while (obj != null) {
    y += obj.offsetTop;
    obj = obj.offsetParent; 
  }
  return y;
}

function getObjectLeft(refobj) {
  var x = refobj.offsetLeft;
  var obj = refobj.offsetParent;
  while (obj != null) {
    x += obj.offsetLeft;
    obj = obj.offsetParent;
  }
  return x;
}

function getLatLonFromPixel(x, y) {
  return map.fromContainerPixelToLatLng(new GPoint(x, y));
}

function drawRectangle() {
    redrawTimeout = null;

    var oldZoomBoxPolyline = zoomBoxPolyline;
	
    // Add the new polyline
    if (currentlyAdjustingRectangle) {
      var pointsForPolyline = [];
      pointsForPolyline.push(getLatLonFromPixel(keyPressX, keyPressY));
      pointsForPolyline.push(getLatLonFromPixel(mouseX, keyPressY));
      pointsForPolyline.push(getLatLonFromPixel(mouseX, mouseY));
      pointsForPolyline.push(getLatLonFromPixel(keyPressX, mouseY));
      pointsForPolyline.push(getLatLonFromPixel(keyPressX, keyPressY));
      zoomBoxPolyline = new GPolyline(pointsForPolyline, 
	                              '#FF0000',     // color
                                      1,             // weight
				      1);            // opacity);
      map.addOverlay(zoomBoxPolyline);
    }

    // Erase the old zoom box polyline
    map.removeOverlay(oldZoomBoxPolyline);
}

function mouseMoveHandler(e){
  if (!e) e = window.event;

  var mapElement = document.getElementById("map");
  var mapLeftUpperCornerX = getObjectLeft(mapElement);
  var mapLeftUpperCornerY = getObjectTop(mapElement);

  mouseX = e.clientX - mapLeftUpperCornerX;
  mouseY = e.clientY - mapLeftUpperCornerY; 

  if (currentlyAdjustingRectangle) {
    // If less than MIN_TIME_BETWEEN_REDRAWS has elaspsed, return so
    // that don't get bogged down trying to draw old rectangles. This
    // should significantly improve the UI.
    var currentTime = (new Date()).getTime();
    if (currentTime < lastTimeDrewRectangle + MIN_TIME_BETWEEN_REDRAWS) {
      // If there is not already a timeout set, set one up now
      // so that the rectangle will be redrawn properly even if
      // the user stops moving the mouse.
      if (!redrawTimeout) {
        redrawTimeout = setTimeout("drawRectangle()", lastTimeDrewRectangle + MIN_TIME_BETWEEN_REDRAWS - currentTime);
      }
	  
      return;
    }
    lastTimeDrewRectangle = currentTime;
  
    // Draw the new rectangle (also erases old rectangle).
    drawRectangle();
  }
}

function keyDownHandler(e){
  if (!e) var e = window.event; 

  // Abort if user hits Esc key
  if ((currentlyAdjustingRectangle == true) && (e.keyCode == ESCAPE_KEYCODE)) {
    currentlyAdjustingRectangle = false;
 
    // Erase the old zoom box polyline
    map.removeOverlay(zoomBoxPolyline);
	
    return;
  }
  
  if ((currentlyAdjustingRectangle == false) && (e.keyCode == ZOOMAREA_KEYCODE)){
    currentlyAdjustingRectangle = true;
    keyPressX = mouseX;
    keyPressY = mouseY;
    var pointsForPolyline = [];
    pointsForPolyline.push(getLatLonFromPixel(keyPressX, keyPressY));
    pointsForPolyline.push(getLatLonFromPixel(keyPressX+1, keyPressY+1));
    zoomBoxPolyline = new GPolyline(pointsForPolyline, 
                                    '#FF0000',     // color
				                    1,             // weight
				                    1);         // opacity);
    map.addOverlay(zoomBoxPolyline);
  }
}


function keyUpHandler(e){
  if (!e) e = window.event;
  
  // If another key besides the z key, don't need to do anything here
  if (e.keyCode != ZOOMAREA_KEYCODE)
    return;
	
  // If wasn't tracking, don't need to do anything here
  if (currentlyAdjustingRectangle == false)
    return;

  currentlyAdjustingRectangle = false;

  // Erase the old zoom box polyline
  map.removeOverlay(zoomBoxPolyline);

  // Zoom to the new box
  var keyReleaseX = mouseX;
  var keyReleaseY = mouseY;
  var startLatLon = getLatLonFromPixel(keyPressX, keyPressY);
  var endLatLon = getLatLonFromPixel(keyReleaseX, keyReleaseY);
  var newCenterLatLng = new GLatLng((startLatLon.lat() + endLatLon.lat())/2.0, 
                                    (startLatLon.lng() + endLatLon.lng())/2.0);

  var minLat = Math.min(startLatLon.lat(), endLatLon.lat());
  var maxLat = Math.max(startLatLon.lat(), endLatLon.lat());
  var minLng = Math.min(startLatLon.lng(), endLatLon.lng());
  var maxLng = Math.max(startLatLon.lng(), endLatLon.lng());
  var sw = new GLatLng(minLat, minLng);
  var ne = new GLatLng(maxLat, maxLng);
  var bounds = new GLatLngBounds(sw, ne);
  var newZoomLevel = map.getBoundsZoomLevel(bounds);
  map.setCenter(newCenterLatLng, newZoomLevel);
}

function initZoomByRectangleFeature(mapObject) {
  document.onmousemove = mouseMoveHandler;
  document.onkeydown = keyDownHandler;
  document.onkeyup = keyUpHandler;
}


//]]>
// End of handling zoom by rectangle
