var graphic = null;
var text = null;

var mapPoints = null;
var imagePoints = null;

var isLine = false;
var isPolygon = false;


function mapDown() {
  
   if (mapMain.getClickMode() != MapClickMode.ClientClick) {
    return;
  }
  
  if (graphic == null) {
    isLine = optMeasureLine.isSelected();
    isPolygon = !isLine;
    
    graphic = document.createElement("v:shape");
    graphic.style.position = "absolute";
    graphic.style.left = "0px";
    graphic.style.top = "0px";
    graphic.style.width = "1000";
    graphic.style.height = "1000";
    graphic.coordorigin = "0 0";
    graphic.coordsize = "1000 1000";
    graphic.strokecolor = "#336699";
    graphic.strokeweight = "2px";

    var fill = document.createElement("v:fill");
    fill.color = "#ffffcc";
    fill.opacity = optMeasureLine.isSelected() ? 0 : .5;
    graphic.appendChild(fill);

    mapMain.content.appendChild(graphic); 
    mapPoints = new Array();
    imagePoints = new Array();

    createText();

    if (isLine) {
      text.style.left = (mapMain.imagePoint.x - 50) + "px";
    }
  }
  
  var i = mapPoints.length;
  mapPoints[i] = mapMain.mapPoint;
  imagePoints[i] = mapMain.imagePoint;
  
  ++i;
  mapPoints[i] = mapMain.mapPoint;
  imagePoints[i] = mapMain.imagePoint;
  
  redrawShape();

  if (isPolygon) {
    labelPolygon();
  }
}

function mapDoubleClick() {
  if (graphic != null) {
    mapReset();
  }
}

function mapMove() {
  if (graphic != null) {
    var p = mapMain.imagePoint;

    var i = mapPoints.length - 1;
    mapPoints[i] = mapMain.mapPoint;
    imagePoints[i] = p;
    
    redrawShape();

    if (isLine) {
      if (imagePoints.length == 2) {
        text.style.top = (imagePoints[0].y + (p.y < imagePoints[0].y ? 7 : -18)) + "px";
        text.style.visibility = "visible";
      }

      labelLine();
    }
      
    if (isPolygon) {
      labelPolygon();
    }
  }
}

function mapOut() {
  mapReset();
}

function mapReset() {
  if (text != null) {
    mapMain.content.removeChild(text);
    text = null;
  }
  if (graphic != null) {
    mapMain.content.removeChild(graphic);
    graphic = null;
  }
  
  mapPoints = null;
  imagePoints = null;

  isLine = false;
  isPolygon = false;
}

function createText() {
  text = document.createElement("div");
  text.style.position = "absolute";
  text.style.textAlign = "center";
  text.style.fontFamily = "Verdana";
  text.style.fontSize = "11px";
  text.style.color = "#800000";
  text.style.width = "100px";
  text.style.height = "27px";
  text.style.visibility = "hidden";
  mapMain.content.appendChild(text);
}

function area(points) {
  var a = 0;
  
  for (var i = 1; i <= points.length; ++i) {
    var j = i % points.length;
    a += (points[i - 1].x - points[j].x) * (points[i - 1].y + points[j].y) / 2;
  }
  
  return Math.abs(a);
}

function centroid(points) {
  var a = 0;
  var c = new Point(0, 0);
  
  for (var i = 1; i <= points.length; ++i) {
    var j = i % points.length;
    var n = (points[i - 1].x * points[j].y) - (points[j].x * points[i - 1].y);
    a += n;
    c.x += (points[i - 1].x + points[j].x) * n;
    c.y += (points[i - 1].y + points[j].y) * n;
  }
  
  if (a == 0) {
    return null;
  }
  
  a *= 3;
  c.x /= a;
  c.y /= a;
  
  return c;
}

function labelLine() {
  var d = 0;

  for (var i = 1; i < mapPoints.length; ++i) {
    d += mapPoints[i - 1].distanceTo(mapPoints[i]);
  }

  if (d < 5280) {
    text.innerHTML = Math.round(d) + "'";
  }
  else {
    text.innerHTML = realFormat(d / 5280, 1) + " mi";
  }
}

function labelPolygon() {
  if (mapPoints.length < 3) {
    return;
  }
  
  var c = centroid(imagePoints);
  
  if (c == null) {
    text.style.visibility = "hidden";
  }
  else {
    text.style.visibility = "visible";
    text.style.left = c.x - 50;
    text.style.top = c.y - 7;
    
    var a = area(mapPoints);

    if (a <= 2787840) {
      text.innerHTML = Math.round(a) + " sq ft";
    }
    else {
      text.innerHTML = realFormat(a / 27878400, 2) + " sq mi";
    }
  }
}

function redrawShape() {
  var path = "m " + imagePoints[0].x + "," + imagePoints[0].y + " l ";

  for (i = 1; i < mapPoints.length; ++i) {
    path += imagePoints[i].x + "," + imagePoints[i].y;
    if (i < mapPoints.length - 1) {
      path += ",";
    }
  }
  
  if (isPolygon) {
    path += " x";
  }
  graphic.path = path + " e";
}

function realFormat(n, d) {
  var x = n > 0 ? Math.floor(n) : Math.ceil(n);
  var r = Math.floor(Math.abs(n - x) * Math.pow(10, d));
  return x + "." + (r + "000000").substr(0, d);
}
