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 mapc;
var polygon;
 
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);
  polygon = new GPolygon([], color, 2, 0.7, color, 0.2);
  document.getElementById("t_polyset").value = color +","+ 2 +","+ 0.7 +","+ color +","+ 0.2;
  startDrawing(polygon, "Shape " + (++shapeCounter_), function() {
    var cell = this;
    var area = polygon.getArea();
    cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
//	updatePolygonContainer(poly);
  }, color);
  document.getElementById("0_poly_save").style.display = "block";
  document.getElementById("0_poly_create").style.display = "none";
  document.getElementById("poly_title").focus();
}
 
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 updatePolygonContainer(poly) {
  //document.getElementById("t_polygon").value 
  var iCnt = poly.getVertexCount();
  var iTempString = "";
  // get vertex count, cycle all vertices, saving one by one...
  // SUBSTITUTE "IZVEIDOT ..." with "SAVE 
  for (i=0;i<iCnt;i++) {
	iTempString = iTempString + poly.getVertex(i) + ";";
  }
  //cell.innerHTML = document.getElementById("t_polygon").value;
  
  document.getElementById("t_polygon").value = iTempString;
    
  //alert(document.getElementById("t_polygon").value);
}
 
function startDrawing(poly, name, onUpdate, color) {
  mapc.addOverlay(poly);
  poly.enableDrawing(options);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
  GEvent.addListener(poly, "endline", function() {
    //select("hand_b");
    var cells = addFeatureEntry(name, color);
	//updatePolygonContainer(poly); //update!
    GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);
    GEvent.addListener(poly, "click", function(latlng, index) {
      if (typeof index == "number") {
        poly.deleteVertex(index);
		//updatePolygonContainer(poly); //update!
      } else {
        var newColor = getColor(false);
        cells.color.style.backgroundColor = newColor
        poly.setStrokeStyle({color: newColor, weight: 4});
		//updatePolygonContainer(poly); //update!
      }
    });
  });
}
 
function placeMarker() {
  //select("placemark_b");
  var listener = GEvent.addListener(mapc, "click", function(overlay, latlng) {
    if (latlng) {
		document.getElementById("t_clat").value = latlng.lat();
		document.getElementById("t_clon").value = latlng.lng();
		document.getElementById("t_czoom").value = mapc.getZoom();
      //select("hand_b");
      GEvent.removeListener(listener);
      var color = getColor(true);
      var marker = new GMarker(latlng, {icon: getIcon(color), draggable: true});
      mapc.addOverlay(marker);
      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(color).image);
    cells.color.style.backgroundColor = color;
  }
  var latlng = marker.getPoint();
  cells.desc.innerHTML = "(" + Math.round(latlng.y * 100) / 100 + ", " +
  Math.round(latlng.x * 100) / 100 + ")";
}
 
 
function initialize() {
  if (GBrowserIsCompatible()) {
/*	mapc = new Gmapc2(document.getElementById("mapc"));
    mapc.setCenter(new GLatLng(37.4419, -122.1419), 13);
    mapc.addControl(new GSmallmapcControl());
    mapc.addControl(new GmapcTypeControl());
    mapc.clearOverlays();
*/
    featureTable_ = document.getElementById("featuretbody");
    //select("hand_b");
  }
}

function testPoly() {
	var polygon_street_1 = new GPolygon([
		new GLatLng(24.7631835938,57.2196084625),
		new GLatLng(25.4772949219,57.3561641479),
		new GLatLng(25.7409667969,57.3857831496),
		new GLatLng(25.9387207031,57.3443098500),
		new GLatLng(26.4880371094,57.4449494358),
		new GLatLng(26.8725585938,57.5335197435),
		new GLatLng(27.0593261719,57.3146573557),
		new GLatLng(26.5539550781,57.2017594768),
		new GLatLng(25.9606933594,57.1600782674),
		new GLatLng(25.5871582031,57.1958078966),
		new GLatLng(25.1367187500,57.1064190290),
		new GLatLng(24.8181152344,57.0287738515),
		new GLatLng(24.7631835938,57.2196084625)
	], "#ff0000", 2.00, 0.70, "#ff0000", 0.20);
	mapc.addOverlay(polygon_street_1);
}

