var COLORS = [["red", "#ff0000"], ["orange", "#ff8800"], ["green","#008000"],
              ["blue", "#000080"], ["purple", "#800080"]];
var options = {};
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var colorIndex_ = 0;
var featureTable_;
var map;
var polygonMode;
var MarkersArray = [];  // global 
var ArrayOnMap = false;
var PolyArray = [];  // global 
var PolyOnMap = false;

function select(buttonId) {
  document.getElementById("hand_b").className="unselected";
  document.getElementById("shape_b").className="unselected";
  //document.getElementById("line_b").className="unselected";
  document.getElementById("placemark_b").className="unselected";
  document.getElementById(buttonId).className="selected";
}

function stopEditing() {
  select("hand_b");
}

function getColor(named) {
  return document.getElementById("POIColor").value;
}

function getIcon(color) {
  var icon = new GIcon();
  icon.image = "http://google.com/mapfiles/ms/micons/" + color + ".png";
  icon.iconSize = new GSize(32, 32);
  icon.iconAnchor = new GPoint(15, 32);
  return icon;
}

function startShape() {
  select("shape_b");
  var color = getColor(false);
  var polygon = new GPolygon([], color, 2, 0.7, color, 0.2);
  startDrawing(polygon, "Shape " + (++shapeCounter_), function() {
    polygonMode = true;
				var cell = this;
    var area = polygon.getArea();
    cell.innerHTML = (Math.round(area * 0.621 / 10000) / 100) + "mi<sup>2</sup>";
				logCoordinates (polygon)
  }, color);
}

function startLine() {
  select("line_b");
  var color = getColor(false);
  var line = new GPolyline([], color);
  startDrawing(line, "Line " + (++lineCounter_), function() {
    var cell = this;
    var len = line.getLength();
    cell.innerHTML = (Math.round(len / 10) / 100) + "km";
  }, color);
}

function addFeatureEntry(name, color) {
  currentRow_ = document.createElement("tr");
  var colorCell = document.createElement("td");
  currentRow_.appendChild(colorCell);
  colorCell.style.backgroundColor = color;
  colorCell.style.width = "1em";
  var nameCell = document.createElement("td");
  currentRow_.appendChild(nameCell);
  nameCell.innerHTML = name;
  var descriptionCell = document.createElement("td");
  currentRow_.appendChild(descriptionCell);
  featureTable_.appendChild(currentRow_);
  return {desc: descriptionCell, color: colorCell};
}

function startDrawing(poly, name, onUpdate, color) {
		if(PolyOnMap) {
			map.removeOverlay(PolyArray[1]); 
		}
  map.addOverlay(poly);
		PolyArray[1] = poly; 
		PolyOnMap = true;
  poly.enableDrawing(options);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
  GEvent.addListener(poly, "endline", function() {
    select("hand_b");
    var cells = addFeatureEntry(name, color);
    GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);
				
    GEvent.addListener(poly, "click", function(latlng, index) {
      if (typeof index == "number") {
        poly.deleteVertex(index);
      } else {
        var newColor = getColor(false);
        cells.color.style.backgroundColor = newColor
        poly.setStrokeStyle({color: newColor, weight: 4});
      }
    });
			logCoordinates(poly);
  });
		

}

function placeMarker() {
		if(ArrayOnMap) {
			map.removeOverlay(MarkersArray[1]); 
		}
  select("placemark_b");
  var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
		  document.getElementById("POIMarkerCoordinates").value = latlng;
    if (latlng) {
      select("hand_b");
      GEvent.removeListener(listener);
      var color = getColor(true);
      var marker = new GMarker(latlng, {icon: getIcon('red'), draggable: true});
      map.addOverlay(marker);
						MarkersArray[1] = marker; 
						ArrayOnMap = true;
      var cells = addFeatureEntry("Placemark " + (++markerCounter_), color);
      updateMarker(marker, cells);
      GEvent.addListener(marker, "dragend", function() {
        updateMarker(marker, cells);
      });
      GEvent.addListener(marker, "click", function() {
        updateMarker(marker, cells, true);
      });
    }
  });
}

function updateMarker(marker, cells, opt_changeColor) {
  if (opt_changeColor) {
    var color = getColor(true);
    marker.setImage(getIcon('red').image);
    cells.color.style.backgroundColor = color;
  }
  var latlng = marker.getPoint();
		document.getElementById("POIMarkerCoordinates").value = latlng;
  cells.desc.innerHTML = "(" + Math.round(latlng.y * 100) / 100 + ", " +
  Math.round(latlng.x * 100) / 100 + ")";
}
// Clear current Map
function clearMap(){
		map.clearOverlays();
		polyPoints = [];
		document.getElementById('POICoordinates').value = "";
document.getElementById("POIMarkerCoordinates").value = "";
}

		// logCoordinates - prints out coordinates of global polyPoints array
function logCoordinates( poly ) {
			var n = poly.getVertexCount();
			var lat = 0, lng = 0;
			var UserCoords= "";
			for( var i = 0;  i < n;  i++ ) {
						var vert = poly.getVertex( i );
						UserCoords += vert.lat() + ", " + vert.lng() + "^"; 
			}
			document.getElementById('POICoordinates').value = UserCoords;
}