var map;
var polyLine1;
var polyLine2;
var options = {};
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var colorIndex_ = 0;

function select(buttonId) {
  document.getElementById("hand_b").className="unselected";
  document.getElementById("line_b1").className="unselected";
  document.getElementById("line_b2").className="unselected";
  document.getElementById(buttonId).className="selected";
}

function stopEditing() {
  select("hand_b");
  if (polyLine1 != null) {
  	polyLine1.disableEditing();
  }
  if (polyLine2 != null) {
  	polyLine2.disableEditing();
  }
}

function clearLines() {
    select("hand_b");
    if (polyLine1 != null) {
  	    map.removeOverlay(polyLine1);
  	    polyLine1 = null;
    }
    if (polyLine2 != null) {
  	     map.removeOverlay(polyLine2);
  	     polyLine2 = null;
    }
}

function setUpPolyLine(polyline) {    
    polyline.enableEditing({onEvent: "mouseover"});
	polyline.disableEditing({onEvent: "mouseout"});
	GEvent.addListener(polyline, "endline", function() {
    	  select("hand_b");
    	  GEvent.addListener(polyline, "click", function(latlng, index) {
		  if (typeof index == "number") {
		    //alert(index);
			polyline.deleteVertex(index);
		  }
		});
	  });
}

function startLine1(){
  stopEditing();
  select("line_b1");  
  if (polyLine1 == null) {
  		polyLine1 =  new GPolyline([], "#ff0000");
	    map.addOverlay(polyLine1);	
	    setUpPolyLine(polyLine1);
  } 
  polyLine1.enableDrawing(options);
}

function startLine2() {

  stopEditing();
  select("line_b2");    
  if (polyLine2 == null) {
        polyLine2 =  new GPolyline([], "#0000ff");
	    map.addOverlay(polyLine2);	
	    setUpPolyLine(polyLine2);
  } 
  polyLine2.enableDrawing(options);
}

function saveMapData() {
   var textLine = ""; 
   var lat = "";
   var lng = "";
   if (polyLine1) {
	   for (var i = 0; i < polyLine1.getVertexCount(); i++) {
		lat = polyLine1.getVertex(i).lat() + "";
		lng = polyLine1.getVertex(i).lng() + "";
		lat = lat.substring(0, 13);
		lng = lng.substring(0, 14);
		

		textLine += lat + " " + lng;
		if (i < polyLine1.getVertexCount() - 1) {
			 textLine += ", ";
		}
	   }
	   document.getElementById('mapdata1').value = textLine;	   
   }
   textLine = ""; 
   if (polyLine2) {
	   for (var i = 0; i < polyLine2.getVertexCount(); i++) {
		lat = polyLine2.getVertex(i).lat() + "";
		lng = polyLine2.getVertex(i).lng() + "";
		lat = lat.substring(0, 13);
		lng = lng.substring(0, 14);
		

		textLine += lat + " " + lng;
		if (i < polyLine2.getVertexCount() - 1) {
			 textLine += ", ";
		}
	   }
	   document.getElementById('mapdata2').value = textLine;	   
   }
}

function searchPlace() {
    address = document.getElementById('searchName').value;
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(
            address + ' distrito federal mexico',
            function(point) {
              if (point) {                              
                  map.setCenter(point, 15);              
              }              
            }
          );
}