function showAddress(address) {
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, 13);
		}
	  }
	);
  }
}

function zoomToSweden()
{
	//alert('mapStartX' + mZ);
	if (mX != null && mY != null && mZ != null)
	{
		map.setCenter(new GLatLng(mY, mX), mZ);
		updateZoomControl(mZ);
		mX = null;
		mY = null;
		mZ = null;
	}
	else
	{
		map.setCenter(new GLatLng(61.75233128, 17.2265625), 5);
	}
}

function initialize() 
{
	if (GBrowserIsCompatible()) 
	{
		mapDiv = document.getElementById("map_canvas");
		mapDiv.style.left = mapLeft+"px";
		mapDiv.style.top = mapTop+"px";
		
		map = new GMap2(mapDiv, {draggableCursor: "pointer", draggingCursor: "move"});
		//map.enableDoubleClickZoom();
		map.continuousZoomEnabled();
		zoomToSweden();
		map.addControl(new GScaleControl());
//		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
	     map.addControl(new GLargeMapControl());
             map.addControl(new GOverviewMapControl());
		map.disableGoogleBar();
		
/*
GEvent.addListener(map, 'click', function(overlay, latlng) {
  var lat = latlng.lat();
    var lon = latlng.lng();
      var latOffset = 0.01;
        var lonOffset = 0.01;
          var polygon = new GPolygon([
              new GLatLng(lat, lon - lonOffset),
                  new GLatLng(lat + latOffset, lon),
                      new GLatLng(lat, lon + lonOffset),
                          new GLatLng(lat - latOffset, lon),
                              new GLatLng(lat, lon - lonOffset)
                                ], "#f33f00", 5, 1, "#ff0000", 0.2);
                                  map.addOverlay(polygon);
                                  });
*/

		geocoder = new GClientGeocoder();

//		manager = new MarkerManager(map);
		mapLoaded();
	}
}

function spawnInfo(s)
{
	var box = document.getElementById("infoBox");
	box.style.display = 'block';
	box.innerHTML = s;
}

function removeInfo()
{
	document.getElementById("infoBox").style.display = 'none';
}



function addAddressToMap(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
    place.Point.coordinates[0]);
    map.setCenter(point,15);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(place.address + '<br>' +
    '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode +'<br> lat:'+
    place.Point.coordinates[1]+'<br>long:'+place.Point.coordinates[0] );
  }
}

function addAddressToMapCity(response) {
//  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
    place.Point.coordinates[0]);
//    map.setCenter(point,7);
	var newIcon = new GIcon();
        newIcon.image = "images/ok.gif";
        newIcon.shadow = "";
        newIcon.iconSize= new GSize(14, 14);
        newIcon.shadowSize= new GSize(0, 0);
        newIcon.iconAnchor = new GPoint(7, 7);
        newIcon.infoWindowAnchor = new GPoint(7, 7);

    marker = new GMarker(point,newIcon);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(place.address + '<br>' +
    ' lat:'+place.Point.coordinates[1]+'<br>long:'+place.Point.coordinates[0] );
  }
}

function zoomToAddress(address)
{
//	alert('mapStartX' + mZ);
//	map.setCenter(new GLatLng(61.75233128, 17.2265625), 14);
	geocoder.getLocations(address+" sweden",addAddressToMap);
}

function zoomToCity(address)
{
//	alert('mapStartX' + mZ);
//	map.setCenter(new GLatLng(61.75233128, 17.2265625), 14);
	geocoder.getLocations(address,addAddressToMapCity);
}

function placeMarker(lat,long)
{
  point = new GLatLng(lat,long)
  map.setCenter(point,7);
  marker = new GMarker(point);
  map.addOverlay(marker);
}


function zoomToAddressUpdateAP(address)
{
	geocoder.getLocations(address+" sweden",function(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
    place.Point.coordinates[0]);
    map.setCenter(point,17);
    marker = new GMarker(point,{draggable:true});
    GEvent.addListener(marker, "dragstart", function() {
     map.closeInfoWindow();
    });
    GEvent.addListener(marker, "dragend", function() {
      var newPoint=marker.getLatLng();
      var lat=newPoint.lat();
      var lng=newPoint.lng();
      marker.openInfoWindowHtml("Latitude: "+lat+"<br>Longitude: "+lng);
      document.getElementById('latitude').value=lat.toFixed(6);
      document.getElementById('longitude').value=lng.toFixed(6);
    });
    map.addOverlay(marker);
    marker.openInfoWindowHtml(place.address + '<br>' +
    '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode +'<br> lat:'+
    place.Point.coordinates[1]+'<br>long:'+place.Point.coordinates[0] );
    document.getElementById('latitude').value=place.Point.coordinates[1];
    document.getElementById('longitude').value=place.Point.coordinates[0];
  } 
  } )
}
