var map = null;
var geocoder = null;
var gLat  = 59.939278155961205;
var gLng  = 30.315570831298828;

function ShowMap()
{
	var saunaIcon = new GIcon(G_DEFAULT_ICON);
	saunaIcon.shadow = "/img/poineter_sh.png";
	saunaIcon.image = "/img/poineter.png";
	var markerOptions = { icon:saunaIcon };


	if (document.getElementById("link").innerHTML == 'Скрыть карту'){
		document.getElementById("oMap").style.display = "none";
		document.getElementById("link").innerHTML = 'Показать на карте';
	}
	else
	{
		document.getElementById("oMap").style.display = "block";
		document.getElementById("link").innerHTML = 'Скрыть карту';
		
		var lat = document.getElementById("lat").value;
		var lng = document.getElementById("lng").value;
		var address = document.getElementById("address").value;

		mapLoad();
		if(lat != "notset" && lng != "notset")
		{
			gLat = lat;
			gLng = lng;
			map.setCenter(new GLatLng(gLat, gLng), 16);
			//map.addOverlay(marker);
			map.addOverlay(new GMarker(new GLatLng(gLat, gLng), markerOptions));
			//marker.openInfoWindowHtml(address);
		}
		else
		{
			showAddress(address, markerOptions);
		}
	}
}

// A TextualZoomControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).
function TextualZoomControl() {
}
TextualZoomControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
TextualZoomControl.prototype.initialize = function(map) {
	  var container = document.getElementById("zoomControl");
	  container.innerHTML = "";
	  var zoomInDiv = document.createElement("img");
	  this.setButtonStyle_(zoomInDiv);
	  zoomInDiv.src = "/img/button-plus.gif";
	  container.appendChild(zoomInDiv);
	  //zoomInDiv.appendChild(document.createTextNode("крупнее"));
	  GEvent.addDomListener(zoomInDiv, "click", function() {
	    map.zoomIn();
	  });

	  var zoomOutDiv = document.createElement("img");
	  this.setButtonStyle_(zoomOutDiv);
	  zoomOutDiv.src = "/img/button-minus.gif";
	  container.appendChild(zoomOutDiv);
	  //zoomOutDiv.appendChild(document.createTextNode("мельче"));
	  GEvent.addDomListener(zoomOutDiv, "click", function() {
	    map.zoomOut();
	  });
 // map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
/*TextualZoomControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}*/

// Sets the proper CSS for the given button element.
TextualZoomControl.prototype.setButtonStyle_ = function(button) {
 /* //button.style.textDecoration = "underline";
  //button.style.color = "#0000cc";
  button.style.color = "#fff";
  //button.style.backgroundColor = "white";
  button.style.background ="url('/img/bg-header.gif') repeat-x";
  //button.style.font = "small Arial";
  //button.style.border = "1px solid black";
  button.style.border = "none";
  //button.style.padding = "2px";
  button.style.marginBottom = "2px";
  //button.style.textAlign = "center";
  //button.style.width = "6em";
  button.style.cursor = "pointer";*/
}

function mapLoad() {
  if(document.getElementById("map") != null)
  {
	  if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		//map.addControl(new GSmallMapControl());
		map.addControl(new TextualZoomControl());
		map.setCenter(new GLatLng(gLat, gLng), 16);
		geocoder = new GClientGeocoder();
	  }
  } 	  
}

function showAddress(address, markerOptions) {
  if(document.getElementById("map") != null)
  {
	  if (geocoder) {
		geocoder.getLatLng(
		  address,
		  function(point) {
			if (!point) {
			  alert(address + " not found");
			} else {
			  map.setCenter(point, 16);
			  //var marker = new GMarker(point);
			  //map.addOverlay(marker);
			  map.addOverlay(new GMarker(point, markerOptions));
			  //marker.openInfoWindowHtml(address);
			}
		  }
		);
	  }
  }
}