// map.js: create Google map of poster countries
// International Poster Collection
// Colorado State University Libraries
// Greg Vogl 2007-06-28

var map = null;
var geocoder = null;
var markers = [];
var mgr;
	
function load() {
	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, nposters) {
    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 = nposters > 1 ? "s" : "";
				var markerTitle = country + ": " + nposters + " poster" + plural;
				var marker = new GMarker(point, {icon: myicon, title: markerTitle});
				map.addOverlay(marker);
				GEvent.addListener(marker, "click", function() {
					var cdm3CountryURL = "http://digital.library.colostate.edu/cgi-bin/pquery.exe?"
				 	+ "CISOROOT1=/poster&CISOROOT2=/poster9193&CISOOP=all&CISOROWS=2&CISOCOLS=5&" 
					+ "CISORESTMP=/poster/items.html&CISOVIEWTMP=/poster/item.html&"
					+ "CISOFIELD1=temp1&CISOBOX1=" + country;
					window.location.href = cdm3CountryURL;
				});
				markers.push(marker);
				return marker;
			}
		  }
		);
	}
}

function showPosters() {
	for (var i=0; i<myarray.length-1; i++) {
		var country = myarray[i][0];
		var nposters = myarray[i][1];
		addCountry(country, nposters);
	}
}
