// Builds an array of geocode responses for the 5 cities.
var city = [
  {
	name: "Киев, Киевская",
	Status: {
	  code: 200,
	  request: "geocode"
	},
	Placemark: [
	  {
		address: "Киев, Киевская, UA",
		population: "0.563M",
		Point: {
		  coordinates: [30.5214786529541, 50.44553544031367, 10 ]
		},
		AddressDetails: {
		  Country: {
			CountryNameCode: "UA",
			AdministrativeArea: {
			AdministrativeAreaName: "Киевская",
			  Locality: {LocalityName: "Киев"}
			}
		  }
		}
	  }
	]
  },
  {
	name: "Харьков, Украина",
	Status: {
	  code: 200,
	  request: "geocode"
	},
	Placemark: [
	  {
		address: "Харьков, Украина",
		population: "12.527M",
		Point: {
		  coordinates: [36.239776611328125, 49.99096683459368, 12]
		},
		  AddressDetails: {
		  Country: {
			CountryNameCode: "UA",
			AdministrativeArea: {
			  AdministrativeAreaName: "UA",
			  Locality: {LocalityName: "Харьков"}
			}
		  }
		}
	  }
	]
  }
];


var map;
var geocoder;

// CapitalCitiesCache is a custom cache that extends the standard GeocodeCache.
// We call apply(this) to invoke the parent's class constructor.
function CapitalCitiesCache() {
  GGeocodeCache.apply(this);
}

// Assigns an instance of the parent class as a prototype of the
// child class, to make sure that all methods defined on the parent
// class can be directly invoked on the child class.
CapitalCitiesCache.prototype = new GGeocodeCache();


// Override the reset method to populate the empty cache with
// information from our array of geocode responses for capitals.
CapitalCitiesCache.prototype.reset = function() {
GGeocodeCache.prototype.reset.call(this);
for (var i in city) {
	this.put(city[i].name, city[i]);
}
}

function initialize() {
  map = new GMap2(document.getElementById("map_canvas"));
  map.setCenter(new GLatLng(50.44553544031367, 30.5214786529541), 12);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  geocoder = new GClientGeocoder();
	  }
	  


function addAddressToMap(response) {
  map.clearOverlays();
  


  if (response && response.Status.code != 200) {
	alert("Unable to locate " + decodeURIComponent(response.name));
  } else {
	var place = response.Placemark[0];
	var point = new GLatLng(place.Point.coordinates[1],
							place.Point.coordinates[0]);
	map.setCenter(point, 6);
	map.openInfoWindowHtml(point, "<b>Город:</b> " + place.address
	 + "<br><b>Population:</b> " + place.population);
  }
}

function findCity(which) {
  if (which != 0) {
	geocoder.getLocations(city[which - 1].name, addAddressToMap);
  }
}

	var map;
var geoXml;
var toggleState = 1;

function initialize() {
  if (GBrowserIsCompatible()) {
	geoXml = new GGeoXml("http://willy.kiev.ua/map2.xml");
	map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(50.44553544031367, 30.5214786529541), 11); 
	map.addControl(new GLargeMapControl());
	map.addControl(new GLargeMapControl());
	map.addOverlay(geoXml);
  }
} 

function toggleMyKml() {
  if (toggleState == 1) {
	map.removeOverlay(geoXml);
	toggleState = 0;
  } else {
	map.addOverlay(geoXml);
	toggleState = 1;
  }
}
