var _maplib_options = {};
var _maplib_poly=null;
var _maplib_marker=null;
var _maplib_color="#FF0000";
var cp2;
var cp1;

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 clearPoly()
{
	if (_maplib_poly!=null)
	{
		_maplib_poly.disableEditing();
		map.removeOverlay(_maplib_poly);
		_maplib_poly=null;
	}

	if (_maplib_marker!=null)
	{
		_maplib_marker.hide();
	}
}

function startShape() {
  select("shape_b");
  clearPoly();
  _maplib_poly = new GPolygon([], _maplib_color, 2, 0.7, _maplib_color, 0.2);
  startDrawing(_maplib_poly, "Shape", function() {}, _maplib_color,2);
}

function startLine() {
  select("line_b");
  clearPoly();
  _maplib_poly = new GPolyline([], _maplib_color);
  startDrawing(_maplib_poly, "Line", function() {}, _maplib_color,1);
}

function postPoly()
{
	clearPoly();
	updateTextArea("_maplib_desc");
	ajaxRequest("action.php?","frm_post_poly");
	_maplib_overlay(false);

	getMarkers(_maplib_id,'',1);
}

function editPoly()
{
	clearPoly();
	updateTextArea("_maplib_desc");
	ajaxRequest("action.php?","frm_post_poly");
	_maplib_overlay(false);
	refresh_markers(_maplib_id,1,0,1,'');
	initiate_pointlist(_maplib_id,1,1);
}

function startDrawing(poly, name, onUpdate, color,stype) {

  map.addOverlay(poly);
  poly.enableDrawing(_maplib_options);

  GEvent.addListener(poly, "endline", function() {
    select("hand_b");

	if (cp1==null)
	{
	cp1 = new ColorPicker('window');
	cp2 = new ColorPicker('window');
	}

	_maplib_overlay(true);
	var _maplib_pts=[];
	for (var i=0;i<poly.getVertexCount();i++ )
	{
		_maplib_pts.push(poly.getVertex(i));
	}
	
	if (stype==1)
	{
		ajaxUpdate("full_overlay2","templates/_form_line.htm?");
	} else
	if (stype==2)
	{
		ajaxUpdate("full_overlay2","templates/_form_polygon.htm?");
	}

	WYSIWYG.attach('_maplib_desc', small);
	//generate_wysiwyg('_maplib_desc',400,150);

	document.getElementById('_maplib_data').value=_maplib_pts.toJSONString();
	document.getElementById('_maplib_lat').value=map.getCenter().lat();
	document.getElementById('_maplib_lng').value=map.getCenter().lng();
	document.getElementById('_maplib_z').value=map.getZoom();
	document.getElementById('_maplib_id').value=_maplib_id;

    GEvent.addListener(poly, "click", function(latlng, index) {
      if (typeof index == "number") {
        poly.deleteVertex(index);
      } else {
      }
    });
  });
}

function placeMarker() {
  select("placemark_b");

  GEvent.addListener(map, "mousemove", function(latlng) {
		
		if (_maplib_marker==null)
		{
			_maplib_marker = new GMarker(latlng);
			map.addOverlay(_maplib_marker);
		} else
		{
			_maplib_marker.setPoint(latlng);
		}
  });

  var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
    if (latlng) {
      select("hand_b");
	  GEvent.clearListeners(map,'mousemove');
      GEvent.removeListener(listener);

		if (_maplib_marker==null)
		{
			_maplib_marker = new GMarker(latlng);
			map.addOverlay(_maplib_marker);
		} else
		{
			_maplib_marker.show();
			_maplib_marker.setPoint(latlng);
		}

	  _maplib_overlay(true);

	  ajaxUpdate("full_overlay2","_form_marker.php?");
	  //generate_wysiwyg('_maplib_desc',400,150);
	   WYSIWYG.attachAll(small);
	  //WYSIWYG._generate('_maplib_desc', small);
	  ajaxUpdate("panel_icons","icons.php?");
	  document.getElementById('_maplib_lat').value=latlng.lat();
	  document.getElementById('_maplib_lng').value=latlng.lng();
	  document.getElementById('_maplib_z').value=map.getZoom();
	  document.getElementById('_maplib_id').value=_maplib_id;
    }
  });
}

function _maplib_initialize(mid) {
	
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(70,5));
	pos.apply(document.getElementById("_maplib_toolbar"));
	map.getContainer().appendChild(document.getElementById("_maplib_toolbar"));
    select("hand_b");
}


function _maplib_overlay(flag) {
	el = document.getElementById("full_overlay");
	el2 = document.getElementById("full_overlay2");
	el3=document.getElementById("wysiwyg_maplib_desc");

	if (flag)
	{
		el.style.visibility = "visible";
		el2.style.visibility = "visible";
		el2.style.left=(window.screen.width-300)/2+"px";
		el2.style.top="50px";
	} else
	{
		el.style.visibility = "hidden";
		el2.style.visibility = "hidden";
		if (el3)
		{
			el3.style.display = "none";
		}
	}
}