// map.js: create Google map of artist countries
// International Poster Collection
// Colorado State University Libraries
// Greg Vogl 2007-06-28

var map = null;
var geocoder = null;
var markers = [];
var mgr;
	
function loadPosters() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		geocoder = new GClientGeocoder();
		map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0)));
		map.addControl(new GMapTypeControl());
		var centerPoint = new GLatLng(25, 11);
		map.setCenter(centerPoint, 2);
		mgr = new GMarkerManager(map);
		addPosters();
	}
}
	
function addPosters() {
	showPosters();
	mgr.addMarkers(markers, 1);
	mgr.refresh();
}

function createIcon(country) {
	country = country.replace(' ', '').toLowerCase();
	var icon = new GIcon();
	icon.image = "images/flags/" + country + "_small.png";
	icon.shadow = "images/flags/flag-shadow.png";
	icon.iconSize = new GSize(32, 20);
	icon.shadowSize = new GSize(48, 36);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

function addCountry(country, nartists) {
    if (geocoder) {
        geocoder.getLatLng(
          country,
          function(point) {
			if (country == "China") point = new GLatLng(32, 100);
			if (country == "Hong Kong") point = new GLatLng(22, 114);
			if (country == "England") point = new GLatLng(51.5, -2);
			if (country == "Yugoslavia") point = new GLatLng(44.8, 20.5);
			if (country == "United States") point = new GLatLng(35, -90);
            if (point) {
				var myicon = createIcon(country);
				var plural = nartists > 1 ? "s" : "";
				var markerTitle = country + ": " + nartists + " artist" + plural;
				var marker = new GMarker(point, {icon: myicon, title: markerTitle});
				map.addOverlay(marker);
				GEvent.addListener(marker, "click", function() {
					var countryURL = "results-artists.php?sel_country=" + country;
					window.location.href = countryURL;
				});
				markers.push(marker);
				return marker;
			}
		  }
		);
	}
}

function addCountry2(country, nartists) {
	if (latlong[country]) {
		point = new GLatLng(latlong[country][0], latlong[country][1]);
		var myicon = createIcon(country);
		var plural = nartists > 1 ? "s" : "";
		var markerTitle = country + ": " + nartists + " artist" + plural;
		var marker = new GMarker(point, {icon: myicon, title: markerTitle});
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() {
			var countryURL = "results-artists.php?sel_country=" + country;
			window.location.href = countryURL;
		});
		markers.push(marker);
	} else {
//		alert('no latlong for country ' + country);
	}
}

function showPosters() {
	for (var i=0; i<myarray.length-1; i++) {
		var country = myarray[i][0];
		var nartists = myarray[i][1];
		addCountry2(country, nartists);
	}
}
