// Basic map for Katsumaru Guide Site

var katsumap;
function drawMap(elementID, spotID, iconBasePath, dataBasePath) {
	katsumap = new Katsumap(elementID, spotID, iconBasePath, dataBasePath);
}

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

Katsumap = function (elementID, spotID, iconBasePath, dataBasePath) {
	this.main = new Katsumap.main(elementID, spotID, iconBasePath);

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


Katsumap.main = function (elementid, spotid, iconbasepath) {

	var map;

	this.run = function(data, code) {

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

		zoomlevel = null;

		// get center position
		for(var latlng = null, i = 0; i < spots.length; i++)
			if (spots[i].ident == spotid) {
				latlng = new GLatLng(spots[i].latitude, spots[i].longitude);
				zoomlevel = spots[i].zoomlevel;
				break;
			}

		if (latlng == null) return;

		var map = new GMap2(document.getElementById(elementid));
		map.setCenter(latlng, ( zoomlevel == null ? 15 : 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].isarea) continue;

			// 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 Katsumap.infoWindow(marker, spots[i]);
			GEvent.addListener(marker, "click", infowin.open);
			if (spots[i].ident == spotid) infowin.open();
		}
	}
}

Katsumap.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>" ) +
			( spot.linkuri == null
				? '<img src="/katsumaru/shared/img/arrow_r.gif" alt="詳細へリンク" /><a class="link" href="/katsumaru/area/' + spot.ident + '/index.html">詳細はこちら</a>'
				: '<img src="/katsumaru/shared/img/arrow_r2.gif" alt="別ウインドウで開きます" /><a class="link" href="' + spot.linkuri + '" target="_blank">' + spot.linkcaption + "</a>" ) +
			"</div>"
			, { "maxWidth": 380 }
		);
	}
}

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

