/* 
 *
 * 
 */
var GM = {};
GM.anchors = new Array();
GM.markers = new Array();

GM.naviListCnt = 14;

var geocoder = null;
var map = null;

GM.anchor_open = function(id){
	var request = GXmlHttp.create();
	var html = '';
	request.open('GET', '/shop_map/shop_info/' + id, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			html = request.responseText;
			GM.anchors[id].openInfoWindowHtml(html);
		}
	}
	request.send(null);
}

function showAnchorById(id, lat, lng) {

	var point = new GLatLng(lat,lng);
	map.setCenter(point, 13);
	GM.anchor_open(id);
}


function showAddress(address) {

	if (address.length == 0) {
		alert('何も入力されていません。入力し直してください。');

	} else {
		if (geocoder) {
			geocoder.getLatLng(
				address,
				function(point) {
					if (!point) {
						alert(address + '　へは移動できません。入力し直してください。');
					} else {
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						marker.openInfoWindowHtml(address);
					}
				}
				);
		}
	}

}

function initialize() {
	if (GBrowserIsCompatible()) {
		// define the crosshair tile layer and its required functions
		var crossLayer = new GTileLayer(new GCopyrightCollection(""), 0, 15);
		crossLayer.getTileUrl =  function(tile, zoom) {
			return "./include/tile_crosshairs.png";
		}
		crossLayer.isPng = function() {
			return true;
		}

		// Create a new map type incorporating the tile layer
		var layerTerCross = [ G_PHYSICAL_MAP.getTileLayers()[0],
		crossLayer ];
		var mtTerCross = new GMapType(layerTerCross,
			G_PHYSICAL_MAP.getProjection(), "Ter+");

		map = new GMap2(document.getElementById("map_canvas"));

		map.addMapType(G_PHYSICAL_MAP);
		map.addMapType(mtTerCross);
		map.setCenter(new GLatLng(35.664694, 139.700016), 5);
		map.addControl(new GLargeMapControl())

		var mapControl = new GHierarchicalMapTypeControl();

		// Set up map type menu relationships
		mapControl.clearRelationships();
		mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
		mapControl.addRelationship(G_PHYSICAL_MAP, mtTerCross, "Crosshairs");

		// Add control after you've specified the relationships
		map.addControl(mapControl);

		map.addControl(new GOverviewMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT));

		geocoder = new GClientGeocoder();

		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();

		function createIcon(point, markerOptions, id) {

			var marker = new GMarker(point, markerOptions);
			GEvent.addListener(marker, "click", function() {

				var request = GXmlHttp.create();
				var html = '';
				request.open('GET', '/shop_map/shop_info/' + id, true);
				request.onreadystatechange = function() {
					if (request.readyState == 4) {
						html = request.responseText;
						marker.openInfoWindowHtml(html);
					}
				}
				request.send(null);
			});
			return marker;
		}

		var gicon = new GIcon();
		gicon.image = "/images/shop_map/icon/book_mini.png";
		gicon.shadow = "/images/shop_map/icon/shadow-book_mini.png";
		gicon.iconSize = new GSize(17,20);
		gicon.shadowSize = new GSize(28,20);
		gicon.iconAnchor = new GPoint(8,10);
		gicon.infoWindowAnchor = new GPoint(8,10);

		// マーカー一覧
		var request = GXmlHttp.create();
		request.open('GET', '/shop_map/shop_list.xml', true);
		request.onreadystatechange = function() {
			
			if (request.readyState == 4) {
				var xmlDoc = request.responseXML;
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
				GM.markers = markers;

				var navihtml = '';
				navihtml = navihtml + '<table border="0" align="center" cellpadding="0" height="525">';
			
				var is_comment = false;
				for (var i = 0; i < GM.markers.length; i++) {
					
					var point = new GLatLng(markers[i].getAttribute("lat"), markers[i].getAttribute("lng"));
					var markerOptions = {
						icon:gicon
					};
					var id = markers[i].getAttribute("id");
					GM.anchors[id] = createIcon(point,markerOptions,id);
					map.addOverlay(GM.anchors[id]);

					// naviList
					if (i < GM.naviListCnt && GM.markers[i].getAttribute("comment_flg") == 1) {
						navihtml = navihtml + "<tr>";
						//					navihtml = navihtml + '<td><img src="/images/shop_map/shop_images/' + markers[i].getAttribute("s_image") + '" onClick="GM.anchor_open(' + GM.markers[i].getAttribute("id") + ');return false;">' + '</td>';
						navihtml = navihtml + '<td><img class="list_icon" src="/images/shop_map/icon/book_mini.png" onClick="GM.anchor_open(' + GM.markers[i].getAttribute("id") + ');return false;" alt="参加書店アイコン">' + '</td>';
						navihtml = navihtml + "<td>";
						navihtml = navihtml + '<div id="navi_shop_name">';
						navihtml = navihtml + '<a href="#" onClick="GM.anchor_open(' + GM.markers[i].getAttribute("id") + ');return false;">'+ markers[i].getAttribute("shop_name") + '</a>';
						navihtml = navihtml + "</div>";
						navihtml = navihtml + '<div id="navi_shop_date">';
						navihtml = navihtml + markers[i].getAttribute("registed");
						navihtml = navihtml + "</div>";
						navihtml = navihtml + "</td>";
						navihtml = navihtml + "</tr>";
						is_comment = true;
					}

				}
					
				/* コメント投稿されているもののみ、リスト表示 */
				if (is_comment == false) {

					navihtml = navihtml + "<tr>";
					navihtml = navihtml + "<td>";
					navihtml = navihtml + '<span style="color:#FFFFFF;">投稿している参加書店がありません。</span>';
					navihtml = navihtml + "</td>";
					navihtml = navihtml + "</tr>";
				}
				
				navihtml = navihtml + "</table>";

				$("#gnavi").html(navihtml);

				// ブログからのパーマリンク遷移
				var afn_shop_id = $("#refblog").val();
				if (afn_shop_id != null && afn_shop_id != "") {
					GM.anchor_open(afn_shop_id);
				}
				
			}
		}
		request.send(null);


	// non support
	} else {

		
}

	

}
