//<![CDATA[

var map;
var xmlHttp;
var geocoder = new GClientGeocoder();
var spot = new Object();

var icon = new GIcon();
icon.shadow = "http://nigaoe-sagaso.com/img/map/shadow.png";
icon.image  = "http://nigaoe-sagaso.com/img/map/marker.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(9, 34);
icon.infoWindowAnchor = new GPoint(9, 2);
icon.infoShadowAnchor = new GPoint(18, 25);

var icon2 = new GIcon();
icon2.shadow = "http://nigaoe-sagaso.com/img/map/shadow.png";
icon2.image  = "http://nigaoe-sagaso.com/img/map/marker2.png";
icon2.iconSize = new GSize(20, 34);
icon2.shadowSize = new GSize(37, 34);
icon2.iconAnchor = new GPoint(9, 34);
icon2.infoWindowAnchor = new GPoint(9, 2);
icon2.infoShadowAnchor = new GPoint(18, 25);

function init() {

	map = new GMap2(document.getElementById("site_map"));
	map.setCenter(new GLatLng(36.146408899699564, 136.437841796875), 6);
	//map.setCenter(new GLatLng(37.146408899699564, 136.437841796875), 6);
	//map.setCenter(new GLatLng(37.046408899699564, 137.537841796875), 6);
	map.addControl(new GLargeMapControl());
	map.addControl(new GOverviewMapControl());

	// マーカーを設定: XML データを読み込んで表示
	xmlHttp  = GXmlHttp.create(); // XmlHttpRequest オブジェクトを作成
	xmlHttp.open("GET", "http://nigaoe-sagaso.com/map/site.xml",	true);
	xmlHttp.onreadystatechange = showMarkers;
	xmlHttp.send(null); 
}

function showMarkers() {

	if (xmlHttp.readyState == 4) {

		var xmlDoc = xmlHttp.responseXML;
		var marker = xmlDoc.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < marker.length; i++) {
				addMarker(marker[i]);
		} // End of for
	}
}

function addMarker(xml) {

	var id         = xml.getAttribute("id");
	var address    = xml.getAttribute("address");
	var title      = xml.getAttribute("title");
	var body       = xml.getAttribute("body");
	var link       = xml.getAttribute("link");
	var is_come    = xml.getAttribute("is_come");
	var sample_img = xml.getAttribute("artist_sample_img");

	var lng        = xml.getAttribute("lng");
	var lat        = xml.getAttribute("lat");

	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {

					//alert(address + " not found" + lat);

					if(lng){
						var point_xml = new GLatLng(lat,lng);
						if(is_come == 1){
							spot[id] = new GMarker(point_xml,icon2);
						}else{
							spot[id] = new GMarker(point_xml,icon);
						}
						map.addOverlay(spot[id]);

						var img = '';
						if(sample_img != ''){
							img = '<img src="' + sample_img + '" style=width:120px>'
						}

						var html	 = '<div id="InfoWindow" style="width:200px;line-height:1.2;font-size:12px">'
								 + '<a href="' + link + '">'
								 + title
								 + '</a><br>'
								 + address + '<br>'
								 + img
								 + '</div>';
						GEvent.addListener(spot[id], "click", function() {
								spot[id].openInfoWindowHtml(html);
						});
					}

				} else {
					//var marker = new GMarker(point,icon);
					//map.addOverlay(marker);

					if(is_come == 1){
						spot[id] = new GMarker(point,icon2);
					}else{
						spot[id] = new GMarker(point,icon);
					}
					map.addOverlay(spot[id]);

					//marker.openInfoWindowHtml(address);

					var img = '';
					if(sample_img != ''){
						img = '<img src="' + sample_img + '" style=width:120px>'
					}

					var html	 = '<div id="InfoWindow" style="width:200px;line-height:1.2;font-size:12px">'
							 + '<a href="' + link + '">'
							 + title
							 + '</a><br>'
							 + address + '<br>'
							 + img
							 + '</div>';
					GEvent.addListener(spot[id], "click", function() {
							spot[id].openInfoWindowHtml(html);
					});

//							 + '<div style="font-size:12px;margin-top:8px;">' + address + "<br />" + body + '</div>'

				}
			}
		);
	}
}
