// "Hana wo mederu" map for Katsumaru Guide Site

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

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

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

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


Hanamede.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++) {

			// set marker
			icon = new GIcon();
			icon.image = iconbasepath + "/" + spots[i].iconfile;
			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 Hanamede.infoWindow(marker, spots[i]);
			GEvent.addListener(marker, "click", infowin.open);
		}
	}
}

Hanamede.infoWindow = function (marker, spot) {
	this.open = function () {
		marker.openInfoWindowHtml(
			'<div id="fukidashi">' +
			'<div class="floral">' + spot.floral + "</div>" +
			'<div class="caption">' + spot.caption + "</div>" +
			( spot.description == null ? "" : '<div class="description">' + spot.description + "</div>" ) +
			( spot.linkanchor == null ? ""
				: '<img src="/katsumaru/shared/img/arrow_r.gif" alt="詳細へリンク" /><a class="link" href="' + spot.linkanchor + '">' +
					( spot.linkcaption == null ? '詳細はこちら' : spot.linkcaption ) + "</a>" ) +
			"</div>"
			, { "maxWidth": 380 }
		);
	}
}

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

