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 new_polygon = true;

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 COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];
}

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() {
    getCoordinates(polygon);
  }, color);
}

function startDrawing(poly, name, onUpdate, color) {
  map.addOverlay(poly);
  poly.enableDrawing(options);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
  GEvent.addListener(poly, "endline", function() {
	gbl_gmap_checked=true;
    select("hand_b");
    //var cells = addFeatureEntry(name, color);
    GEvent.bind(poly, "lineupdated", 'intentionally_blank', 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});
      }
    });
  });
}


function getCoordinates(polygon) {

	var vertexes = polygon.getVertexCount();
	var coordinates = [];
	
	var i = 0;
	while(i < vertexes){
		coordinates.push(polygon.getVertex(i));
		i++;
	}
	new_polygon = false;
	
	// send coordinates array to hidden input
	//alert(coordinates);
	$('#project_polygon_coordinates').val(coordinates);
	//return coordinates;
	
}

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(10,-90), 2);
    map.setMapType(G_NORMAL_MAP);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.clearOverlays();
	//featureTable_ = document.getElementById("featuretbody");
    select("hand_b");
  }
}

function initialize_edit(poly) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(10,-90), 2);
    map.setMapType(G_NORMAL_MAP);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.clearOverlays();
	if(poly)
	{
		map.addOverlay(poly);
		var color = getColor(false);
		startDrawing_edit(poly, "Shape " + (++shapeCounter_), function() {
    getCoordinates(poly);
  }, color);
	}
	
	//featureTable_ = document.getElementById("featuretbody");
    select("shape_b");
  }
}

function startDrawing_edit(poly, name, onUpdate, color) {
  map.addOverlay(poly);
  //poly.enableDrawing(options);
  getCoordinates(poly);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
  GEvent.addListener(poly, "endline", function() {
	gbl_gmap_checked=true;
    select("hand_b");
    //var cells = addFeatureEntry(name, color);
    GEvent.bind(poly, "lineupdated", 'intentionally_blank', 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});
      }
    });
  });
}
