// "Nanafukujin meguri" map for Katsumaru Guide Site

var nanafuku;
function drawMap(elementID, latitude, longitude, zoomLevel, iconBasePath, dataBasePath) {
	nanafuku = new Nanafuku(elementID, latitude, longitude, zoomLevel, iconBasePath, dataBasePath);
}

function cleanupMap() {
	nanafuku.cleanup();
}

Nanafuku = function (elementID, latitude, longitude, zoomLevel, iconBasePath, dataBasePath) {
	this.main = new Nanafuku.main(elementID, latitude, longitude, zoomLevel, iconBasePath);

	// load data
	if (GBrowserIsCompatible()) {
		GDownloadUrl(dataBasePath + "/nanafuku.json", this.main.run);
		this.cleanup = function () { GUnload(); }
	}
	else 
		this.cleanup = function () {};
	return this;
}


Nanafuku.main = function (elementid, latitude, longitude, zoomLevel, iconbasepath) {

	var map;

	this.run = function(data, code) {

		if (code != 200) return;
		spots = eval("(" + data + ")");

		var map = new GMap2(document.getElementById(elementid));
		map.setCenter(new GLatLng(latitude, longitude), zoomLevel);
		map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());
		map.addControl(new GOverviewMapControl());
		GEvent.addListener(map, "zoomend", function (prev, current) { if (current < 12) map.setZoom(12); });

		for(var i = 0; i < spots.length; i++) {

			if (spots[i].isroute) {
				if (spots[i].latlngs.length > 1) {
					var points = [];
					for (var j = 0; j < spots[i].latlngs.length; j++) {
						points.push(new GLatLng(spots[i].latlngs[j].latitude, spots[i].latlngs[j].longitude));
					}
					map.addOverlay(new GPolyline(points, spots[i].color), 1);
				}
			}
			else {
				// set marker
				icon = new GIcon();
				icon.image = iconbasepath + "/" + spots[i].ident + ".png";
				icon.iconSize = new GSize(64, 64);
				icon.shadow = iconbasepath + "/kage.png";
				icon.shadowSize = new GSize(88, 64);
				icon.iconAnchor = new GPoint(32, 64);
				icon.infoWindowAnchor = new GPoint(32, 0);
				marker = new GMarker(new GLatLng(spots[i].latitude, spots[i].longitude), icon);
				map.addOverlay(marker);
				infowin = new Nanafuku.infoWindow(marker, spots[i]);
				GEvent.addListener(marker, "click", infowin.open);
			}
		}
	}
}

Nanafuku.infoWindow = function (marker, spot) {
	this.open = function () {
		marker.openInfoWindowHtml(
			'<div id="fukidashi">' +
			'<div class="caption">' + spot.caption + "</div>" +
			( spot.description == null ? "" : '<div class="description">' + spot.description + "</div>" ) +
			"</div>"
			, { "maxWidth": 300 }
		);
	}
}

// vim: set ts=4 sw=4:
