/**
 * FrameWork "Tama"
 * JavaScript(要prototype.js、GoogleMapsAPI)
 * 
 * 地図関係スクリプト
 * 
 * $Id: $
 */


//グローバル変数
var mapObj;					//GMap2オブジェクト
var mapGeocoder;			//ジオコーダオブジェクト
var mapStreetView;			//ストリートビューオブジェクト
var mapDirection;			//ルート案内オブジェクト
var mapFormMarker;			//登録用マーカのオブジェクト
var mapPointArea;			//ポイントのエリアのGPolylineオブジェクト
var mapForm;				//フォームオブジェクト
var mapPoint = {};			//釣り場マップ関係オブジェクト
var mapMsgTimer;			//メッセージのタイマーオブジェクト

var mapDefaultZoomLevel = 11;	//デフォルトのズームレベル
var mapPointShowZoomLevel = 14;	//デフォルトのズームレベル
var mapLatOffset = 0.004;	//ポリゴンの緯度オフセット（デフォルト値）
var mapLngOffset = 0.005;	//ポリゴンの経度オフセット（デフォルト値）
var mapSmallAreaWidth = "100%";	//地図エリアのサイズ
var mapSmallAreaHeight = "220px";		//地図の高さ
var mapMidAreaWidth = "100%";	//地図エリアのサイズ（釣り場マップ）
var mapMidAreaHeight = "500px";		//地図の高さ（釣り場マップ）
var mapLargeAreaWidth = "940px";	//地図エリアのサイズ（拡大時）
var mapLargeAreaHeight = "500px";		//地図の高さ（拡大時）

var mapSwitch = false;		//地図拡大中
var mapLoadCase = "default";//どういったケースの地図か
var mapIcons = [];			//マーカアイコン
var mapPointMarkers = [];	//ポイント表示用マーカのオブジェクト

//onLoad時のイベント
Event.observe(window, 'load', mapApiLoad, false);

/**
 * function：GoogleMapsApiロード
 * 
 * 説明：
 */
function mapApiLoad() {
	if ($("map")) {
		mapShowMsg("地図の読み込み中･･･");
		google.load("maps", "2.147", {"callback" : mapLoad});
		//時間差で呼び出し
	//	setTimeout('google.load("maps", "2", {"callback" : mapLoad});', 750);
	}
}

/**
 * function：マップロード
 * 
 * 説明：
 */
function mapLoad() {
	if (GBrowserIsCompatible() && $("map")) {
		//フォーム読み込み
		mapForm = $("form1");
		
		//ケース読み込み
		mapLoadCase = mapGetLoadCase();

		//オフセット読み込み、セット
		mapSetLatLngOffset();

		//アイコン読み込み
		mapIcons = mapGetIcons();

		//地図作成
		mapObj = new GMap2($("map"));
		mapObj.setCenter(new GLatLng(35.51, 138.75), 4);
		
		//コントロール初期化
		var ctlOpt = mapObj.getDefaultUI();
		ctlOpt.maptypes.physical = true;	//地形マップ追加

		//コントロール追加
		mapObj.setUI(ctlOpt);
//		mapObj.setUIToDefault();

		//GoogleEarthマップ追加
//		mapObj.addMapType(G_SATELLITE_3D_MAP);
		
		//ケースによって初期化分岐
		switch (mapLoadCase) {
		case "form": //登録・変更画面
			mapLoadInitForm();
			break;
		case "formConfirm": //登録・変更確認画面
			mapLoadInitFormConfirm();
			break;
		case "show": //釣り場表示
			mapLoadInitShow();
			break;
		case "pointMap": //釣り場マップ表示
			mapLoadInitPointMap();
			break;
		default:
		}
		//onUnLoad時のマップアンロード
		Event.observe(window, 'unload', GUnload, false);
		
		mapShowMsg("地図の読み込み完了", 3000);

/*
	var mk = new GMarker(mapObj.getCenter());
	mapObj.addOverlay(mk);
	mk.showMapBlowup();
*/
	}
}

/**
 * function：マップのアイコン読み込み
 */
function mapGetIcons() {
	var icons = [];
	var iNum = [11, 12, 13, 14, 15,16,
				21, 22, 23, 24, 25,
				41,
				51, 52,
				61,
				0];
	for (var i=0; i<iNum.length; i++) {
		icons[iNum[i]] = new GIcon({
			"image": "/common/gfx/icon/maps/map_" + iNum[i] + ".png",
			"shadow": "/common/gfx/icon/maps/map_s.png",
			"iconSize": new GSize(50, 60),
			"shadowSize": new GSize(80, 60),
			"iconAnchor": new GPoint(25, 60),
			"infoWindowAnchor": new GPoint(25,30)
		});
	}
	return icons;
}

/**
 * function：マップのケース読み込み
 */
function mapGetLoadCase() {
	if ($("map_case")) return $F("map_case");
	return "default";
}

/**
 * function：マップのオフセット読み込み
 */
function mapSetLatLngOffset() {
	if ($("map_lat_offset")) mapLatOffset = $F("map_lat_offset") - 0;
	if ($("map_lng_offset")) mapLngOffset = $F("map_lng_offset") - 0;
}

/**
 * function：マップのメッセージ表示
 */
function mapShowMsg(msg, time) {
	//タイマーリセット
	if (mapMsgTimer) clearTimeout(mapMsgTimer);
	
	//釣り場一覧読み込みの場合
	if (mapPoint.pointLoading) {
		if (mapStreetView && mapStreetView.sw || mapDirection && mapDirection.sw) return false;
	//	time = 3000;
	}
	$("mapHeaderMsg").update(msg);
	
	//一定時間で消す場合
	if (time) {
		mapMsgTimer = setTimeout(function(){$("mapHeaderMsg").update("")}, time);
	}
}

/**
 * function：マップのケース変更
 */
function mapToggleLoadCase(loadCase) {
	//ケース変更
	mapLoadCase = loadCase;
	
	//地図再読み込み
	if (mapPoint.markerClicked) mapPoint.markerClicked = false;
	mapLoadPointFromMap();
	
	//右ペイン書き換え
	if (loadCase == "pointMap") {
		fOpenDiv("mapPointForm");
		fCloseDiv("mapFishForm");
		$("mapPointButton").style.backgroundPosition = "center bottom";
		$("mapFishButton").style.backgroundPosition = "center top";
	} else if (loadCase == "fishMap") {
		fOpenDiv("mapFishForm");
		fCloseDiv("mapPointForm");
		$("mapFishButton").style.backgroundPosition = "center bottom";
		$("mapPointButton").style.backgroundPosition = "center top";
	}
}

/**
 * function：登録・変更画面の初期化
 */
function mapLoadInitForm() {	
	//ストリートビュー追加
	mapInitStreetView();
	//逆ジオコーディング追加
	mapInitReverseGeocoding();
	
	//セレクトボックス(県)の監視
	new Form.Element.EventObserver("f_pref", mapPanToPrefCity);
	//セレクトボックス(市郡)の監視
	new Form.Element.EventObserver("f_city", function () {
		mapPanToPrefCity();
	});
	//地図DIV拡大縮小のイベント
	Event.observe($("mapSwitch"), "click", mapToggleSize, false);
	//クリックでマーカ表示イベントリスナセット
	GEvent.addListener(mapObj, 'click', mapFormMarkerGCallback);
	//地図移動でマーカ表示イベントリスナセット
	GEvent.addListener(mapObj, 'moveend', mapLoadPointFromMap);

	//フォームに値があるかで初期表示分岐
	if (mapForm.point_lat.value && mapForm.point_lng.value) {
		//中心セット
		mapObj.setCenter(new GLatLng(mapForm.point_lat.value, mapForm.point_lng.value), mapDefaultZoomLevel);
		//マーカ作成
		mapSetFormMarker(mapObj.getCenter());
	} else {
		mapPanToPrefCity();
	}
	setTimeout('mapShowMsg("登録したい場所をクリックしてください")', 5000);

}

/**
 * function：登録・変更確認画面の初期化
 * 
 * 説明：
 */
function mapLoadInitFormConfirm() {
	//逆ジオコーディング追加
	mapInitReverseGeocoding();
	
	//地図のドラッグを無効に
	mapObj.disableDragging();

	//中心セット
	mapObj.setCenter(new GLatLng(mapForm.point_lat.value, mapForm.point_lng.value), mapDefaultZoomLevel);
	//マーカ作成
	mapSetFormMarker(mapObj.getCenter());
}

/**
 * function：釣り場表示画面の初期化
 * 
 * 説明：
 */
function mapLoadInitShow() {
	
	//地図DIV拡大縮小のイベント
	Event.observe($("mapSwitch"), "click", mapToggleSize, false);

	//ストリートビュー追加
	mapInitStreetView();
	//ルート案内追加
	mapInitDirection();
	//逆ジオコーディング追加
	mapInitReverseGeocoding();
	//中心セット
	mapObj.setCenter(new GLatLng(mapForm.point_lat.value, mapForm.point_lng.value), mapPointShowZoomLevel);
	
	//マーカ作成
	mapSetPointMarker({
		"point_id": mapForm.point_id.value,
		"point_name": mapForm.point_name.value,
		"point_lat": mapForm.point_lat.value,
		"point_lng": mapForm.point_lng.value,
		"point_category": mapForm.point_category.value
	});
	
	//エリア表示
	mapDisplayPointArea(new GLatLng(mapForm.point_lat.value, mapForm.point_lng.value));
}

/**
 * function：釣り場マップの初期化
 * 
 * 説明：
 */
function mapLoadInitPointMap() {
	//地図DIV拡大縮小のイベント
	Event.observe($("mapSwitch"), "click", mapToggleSize, false);
	//地図移動イベントリスナセット
	GEvent.addListener(mapObj, 'moveend', mapLoadPointFromMap);
	
	//ストリートビュー追加
	mapInitStreetView();
	//ルート案内追加
	mapInitDirection();
	//逆ジオコーディング追加
	mapInitReverseGeocoding();
	
	//フォームのサブミット時
	Event.observe(mapForm, "submit", function(evt) {
		Event.stop(evt);
		mapLoadPointFromMap();
	}, false);
	
	//フォームのリセット時
	Event.observe(mapForm, "reset", function(){setTimeout(mapLoadPointFromMap, 300)}, false);

	//セレクトボックス(カテゴリ)の監視
	if (mapForm.point_category) new Form.Element.EventObserver(mapForm.point_category, mapLoadPointFromMap);
	if (mapForm.fish_name) new Form.Element.EventObserver(mapForm.fish_name, function(){setTimeout(mapLoadPointFromMap, 300)});

	//記事・釣果ボタンの監視
	if ($("mapPointButton")) {
		Event.observe($("mapPointButton"), "click", function(){mapToggleLoadCase("pointMap");}, false);
		Event.observe($("mapFishButton"), "click", function(){mapToggleLoadCase("fishMap");}, false);
	}
		
	//フォームに値があるかで初期表示分岐
	if (mapForm.point_bounds.value) {
		var bound = [];
		bound = mapForm.point_bounds.value.split(',');
		var bounds = new GLatLngBounds(new GLatLng(bound[0], bound[1]), new GLatLng(bound[2], bound[3]));
		//中心セット
		mapObj.setCenter(bounds.getCenter(), mapObj.getBoundsZoomLevel(bounds));
		//地図タイプセット
		switch ( bound[4] ) { 
			case "satellite": mapObj.setMapType(G_SATELLITE_MAP); break; 
			case "hybrid": mapObj.setMapType(G_HYBRID_MAP); break; 
		}
	} else {
		mapPanToPrefCity();
	}
	
}

/**
 * function：登録フォーム用のマーカ作成
 * 
 * 説明：
 */
function mapSetFormMarker(latlng) {
	//マーカ作成
	mapFormMarker = new GMarker(latlng, {
		"icon": mapIcons[0],
		"draggable": true,
		"title": "位置の微調整はマーカーをドラッグ＆ドロップして移動させてください"
	});
	
	//確認画面以外の場合
	if (mapLoadCase == 'form') {
		//マーカドラッグエンドイベントリスナ
		GEvent.addListener(mapFormMarker, 'dragend', function(){
			mapFormUpdateLatLng(mapFormMarker.getLatLng());
			mapObj.panTo(mapFormMarker.getLatLng());
		});
	}
	//確認画面の場合マーカをロック
	else {
		mapFormMarker.disableDragging()
	}
	//ドキュメントに経度緯度を反映
	mapFormUpdateLatLng(mapFormMarker.getLatLng());
	
	//マーカ表示
	mapObj.addOverlay(mapFormMarker);
	
}


/**
 * function：ドキュメントに経度緯度を反映
 * 
 * 説明：登録フォーム用のマーカ移動時のHTML表示・フォームセット
 */
function mapFormUpdateLatLng(latlng) {
	//ポリゴン描画
	mapDisplayPointArea(latlng);
	
	mapForm.point_lat.value = latlng.lat();
	mapForm.point_lng.value = latlng.lng();
	if ($("map_lat") && $("map_lng")) {
		Element.update($("map_lat"), latlng.lat());
		Element.update($("map_lng"), latlng.lng());
	}
	mapShowMsg("経度：" + latlng.lat() + "付近、緯度：" + latlng.lng() + "付近を指定しています");

}

/**
 * function：登録フォーム用のマーカコールバック
 * 
 * 説明：地図クリック時のマーカの移動・作成
 */
function mapFormMarkerGCallback(overlay, latlng) {
	if (latlng) {
		if (mapFormMarker) {
			mapFormMarker.setLatLng(latlng);
			mapFormUpdateLatLng(mapFormMarker.getLatLng());
		} else {
			//マーカ作成
			mapSetFormMarker(latlng);
		}
		mapObj.panTo(latlng);
	}
}

/**
 * function：ポリゴン描画
 * 
 * 説明：ポリゴンの描画・削除
 */
function mapDisplayPointArea(latlng) {
	//すでにあるポリゴンは削除
	if (mapPointArea) {
		mapObj.removeOverlay(mapPointArea)
	}
	mapPointArea = new GPolyline([
		new GLatLng(latlng.lat() - 0 + mapLatOffset, latlng.lng() - 0 + mapLngOffset),
		new GLatLng(latlng.lat() - 0 - mapLatOffset, latlng.lng() - 0 + mapLngOffset),
		new GLatLng(latlng.lat() - 0 - mapLatOffset, latlng.lng() - 0 - mapLngOffset),
		new GLatLng(latlng.lat() - 0 + mapLatOffset, latlng.lng() - 0 - mapLngOffset),
		new GLatLng(latlng.lat() - 0 + mapLatOffset, latlng.lng() - 0 + mapLngOffset)
	], "#f33f00", 1, 0.5);
	mapObj.addOverlay(mapPointArea);
}

/**
 * function：地図DIVサイズ変更
 */
function mapToggleSize(evt) {
	var center = mapObj.getCenter();
	//小さくする時はストリートビュー・ルート案内を閉じる
	if (mapSwitch && mapStreetView && mapStreetView.sw) mapToggleStreetView(evt);
	if (mapSwitch && mapDirection && mapDirection.sw) mapToggleDirection(evt);
	//ケースにより分岐
	if (mapLoadCase == "pointMap" || mapLoadCase == "fishMap") {
		mapToggleSizeLarge(evt);
	} else {
		mapToggleSizeSmall(evt);
	}
	//地図のリサイズ
	if (mapStreetView && mapStreetView.pano) mapStreetView.pano.checkResize();
	mapObj.checkResize();
	mapObj.panTo(center);
	$("mapSwitch").blur();

	evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
}

/**
 * function：小さい地図DIVサイズ変更
 */
function mapToggleSizeSmall(evt) {
	if (!mapSwitch) {
		$("mapArea").style.width = mapLargeAreaWidth;
		if (isIE6()) { 
			//IE6用
			$("mapArea").style.position = "absolute";
			var elems = document.getElementsByTagName("select");
			for (var i=0; i<elems.length; i++) {
				elems[i].style.display = "none";
			}
		}
		$("map").style.height = mapLargeAreaHeight;
		mapSwitch = true;
	} else {
		$("mapArea").style.width = mapSmallAreaWidth;
		if (isIE6()) { 
			//IE6用
			$("mapArea").style.position = "relative";
			var elems = document.getElementsByTagName("select");
			for (var i=0; i<elems.length; i++) {
				elems[i].style.display = "inline";
			}
		}
		$("map").style.height = mapSmallAreaHeight;
		mapSwitch = false;
	}
}

/**
 * function：大きい地図DIVサイズ変更
 */
function mapToggleSizeLarge(evt) {
	var center = mapObj.getCenter();
	if (!mapSwitch) {
		$("mapArea").style.width = mapLargeAreaWidth;
		//IE6用
		if (isIE6()) $("mapArea").style.position = "absolute";
		mapSwitch = true;
	} else {
		$("mapArea").style.width = mapMidAreaWidth;
		//IE6用
		if (isIE6()) $("mapArea").style.position = "relative";
		mapSwitch = false;
	}
}


/**
 * function：指定の地域に移動
 * 
 * 説明：
 */
function mapPanToPrefCity() {
	var address = $F("f_pref") + $F("f_city");
	//市郡経度緯度取得
	new Ajax.Request(
		"/a/map_prefcity/",
		{
			"method": "get",
			"parameters": 'a=b&address=' + address,
			onSuccess: function(request) {
				var json;
				eval("json="+request.responseText);
				if (json.lat && json.lng) {
					//地図移動
					mapObj.setZoom(mapDefaultZoomLevel);
					mapObj.panTo(new GLatLng(json.lat, json.lng));
				} else {
					//GClientGeocoderで検索
					mapPanToByGeocoder(address);
				}
			}
		}
	);
	
}

/**
 * function：現在位置から指定の地域の釣り場のロード
 * 
 * 説明：
 */
function mapLoadPointFromMap() {

	//マーカ表示での移動なら戻る
	if (mapPoint.markerClicked) {
		return;
	}
	if (mapPoint.pointLoading) {
		return;
	}
	
	mapPoint.pointLoading = true;
	if (mapLoadCase != "form") mapShowMsg("釣り場：読み込み中･･･");
	
	//ケースにより呼ぶAPIを変える
	var api = (mapLoadCase == "fishMap")? "map_load_fish": "map_load_point";
	
	//セレクトボックス(カテゴリ)の取得
	var point_category = (mapForm.point_category && mapForm.point_category.value)? mapForm.point_category.value: '';
	var fish_name = (mapForm.fish_name && mapForm.fish_name.value)? mapForm.fish_name.value: '';

	//地図タイプの取得
	var mapType = mapObj.getCurrentMapType();
	var mapTypetext = ""; 
	switch ( mapType ) { 
		case G_NORMAL_MAP: mapTypetext = "normal"; break; 
		case G_SATELLITE_MAP: mapTypetext = "satellite"; break; 
		case G_HYBRID_MAP: mapTypetext = "hybrid"; break; 
	}
	
	//表示範囲を取得
	var bounds = mapObj.getBounds();
	var boundMax = bounds.getNorthEast();
	var boundMin = bounds.getSouthWest();
	
	//釣り場経度緯度取得
	new Ajax.Request(
		"/a/" + api + "/",
		{
			"method": "get",
			"parameters": 'a=b&point_lat_min=' + boundMin.lat() 
			+ '&point_lng_min=' + boundMin.lng() 
			+ '&point_lat_max=' + boundMax.lat() 
			+ '&point_lng_max=' + boundMax.lng()
			+ '&point_category=' + point_category
			+ '&log_fish_name=' + fish_name
			+ '&type=' + mapTypetext
			+ '&no_detail=true',
			onSuccess: function(request) {
				//ポイントマーカ削除
				for (var i=0; i<mapPointMarkers.length; i++) {
					if (mapPointMarkers[i]) mapObj.removeOverlay(mapPointMarkers[i]);
				}
				mapPointMarkers = [];
				//右インフォ削除
				if ($("mapInfo")) {
					$("mapInfo").update("");
				}
				var json;
				eval("json="+request.responseText);
				if (json) {
					for (var j=0; j<json.length; j++) {
						mapSetPointMarker(json[j], "_blank");
					}
					//メッセージ表示
					if (mapLoadCase != "form") { 
						var msgOpt = "条件に合う";
						if (mapLoadCase == "fishMap") {
							var msgOpt = (fish_name)? fish_name + "の釣果がある" :"条件に合う釣果がある";
						}
						mapShowMsg("釣り場：" + json.length + "件の" + msgOpt + "釣り場を表示しています");
					}
				}
				mapPoint.pointLoading = false;
			},
			onFailure: function(request) {   
				mapShowMsg("釣り場：読み込みに失敗しました。時間を空けて試してみてください");
				mapPoint.pointLoading = false;
			},
			onException: function (request) {   
				if (mapLoadCase != "form") { 
					mapShowMsg("釣り場：条件に合う釣り場は表示範囲内には見つかりませんでした");
					mapPoint.pointLoading = false;
				}
			}

		}
	);
}

/**
 * function：ポイントマーカ作成
 */
function mapSetPointMarker(pointRow, target) {
	//座標無ければ戻る
	if (!pointRow.point_lat || !pointRow.point_lng) {
		return;
	}
	
	//すでにあれば戻る
	if (mapPointMarkers[pointRow.point_id]) {
		return;
	}
	
	var iconNo = (pointRow.point_category)? pointRow.point_category - 0: 0;
	pointRow.point_id = pointRow.point_id - 0;

	//マーカ作成
	mapPointMarkers[pointRow.point_id] = new GMarker(new GLatLng(pointRow.point_lat - 0, pointRow.point_lng - 0), {
		"icon": mapIcons[iconNo],
		"clickable": true,
		"autoPan": true,
		"title": pointRow.point_name
	});

	
	//情報ウィンドウ作成
	if (!pointRow.noInfoWindow) {
		GEvent.addListener(mapPointMarkers[pointRow.point_id], "click", function() {
			//情報ウィンドウ読み込み
			mapOpenInfo(pointRow);
		});
		//右インフォの追加
		if ($("mapInfo")) {
			//釣果の場合
			if (mapLoadCase == "fishMap") {
				for (var i=0; i<pointRow.log_fish_data.length; i++) {
					var info = document.createElement('div');
					info.id = "info" + pointRow.point_id + i;
					$("mapInfo").appendChild(info);
					$("info" + pointRow.point_id + i).update(
						"<table><tr>"
						 + "<td class=\"width20\" rowspan=\"3\"><img src=\"/common/gfx/icon/point/" + pointRow.point_category + "_36.png\" align=\"left\"></td>"
						 + "<td><a href=\"/point/show/" + pointRow.point_id + "/\" onclick=\"mapOpenInfo({'point_id':'" + pointRow.point_id + "'}); return false;\">" + pointRow.log_fish_data[i].log_fish_name + "</a></td>"
						 + "<td align=\"right\"><a href=\"#\" onclick=\"mapFishNameInput('" +pointRow.log_fish_data[i].log_fish_name + "'); return false;\"><span class=\"comment3\">この魚種で絞込み</span></a></td></tr>"
						 + "<tr><td colspan=\"2\">" + pointRow.log_fish_data[i].log_fish_size_display + "　" + pointRow.log_fish_data[i].log_fish_no + "匹</a></td></tr>"
						 + "<tr><td colspan=\"2\" class=\"comment3\">" + pointRow.point_name  + "</td>"
						 + "</tr></table>"
					);
				}
			}
			//釣り場の場合
			else {
				var info = document.createElement('div');
				info.id = "info" + pointRow.point_id;
				$("mapInfo").appendChild(info);
				$("info" + pointRow.point_id).update(
					"<table><tr>"
					 + "<td class=\"width20\" rowspan=\"2\"><img src=\"/common/gfx/icon/point/" + pointRow.point_category + "_36.png\" align=\"left\"></td>"
					 + "<td><a href=\"/point/show/" + pointRow.point_id + "/\" onclick=\"mapOpenInfo({'point_id':'" + pointRow.point_id + "'}); return false;\">" + pointRow.point_name + "</a></td>"
					 + "<tr><td class=\"comment3\">" + pointRow.point_pref + pointRow.point_city + "</td>"
					 + "</tr></table>"
				);
			}
		}
	}
	
	//マーカ表示
	mapObj.addOverlay(mapPointMarkers[pointRow.point_id]);
	
}

/**
 * function：情報ウィンドウ読み込み
 * 
 * 説明：
 */
function mapOpenInfo(pointRow) {
	if (mapPoint.windowLoading) {
		return;
	}
	mapPoint.windowLoading = true;
	mapShowMsg("釣り場情報：読み込み中･･･");
	
	//釣り場取得
	new Ajax.Request(
		"/a/map_load_point/",
		{
			"method": "get",
			"parameters": 'a=b&point_id=' + pointRow.point_id,
			onSuccess: function(request) {
				var json;
				eval("json="+request.responseText);
				if (json[0]) {
					//本文・写真初期化
					if (!json[0].point_text) json[0].point_text = '';
					if (!json[0].photo) {
						json[0].photo = '';
					 } else  {
						json[0].photo = '<img src="' + json[0].photo + '" align="right" style="margin:3px">';
					}
					var directionLink = "";
					if (mapDirection) {
						directionLink = "<div class=\"mapDirectionLink\" style=\"height:20px;\"><p><a href=\"#\" onclick=\"mapSetToDirectionByPoint(" + pointRow.point_id + "); return false;\">ここに到着</a>";
						directionLink += "　　　<a href=\"#\" onclick=\"mapSetFromDirectionByPoint(" + pointRow.point_id + "); return false;\">ここから出発</a></p></div>";
					}
					//ウィンドウオープン
					mapPointMarkers[pointRow.point_id].openInfoWindowHtml(
						'<div class="mapPointInfo" style="width:400px;height:125px">'
						+'	<div class="mapPointCategory" style="margin:3px 0">' + json[0].point_category_name + '</div>'
						+'	<h3 style="margin:3px 0"><a href="/point/show/' + json[0].point_id + '/">' + json[0].point_name + '</a></h3>'
						+'		' + json[0].photo + ''
						+'	<p class="text90">' + json[0].point_text + '</p>'
						+'</div>'
						+ directionLink
					, {"maxWidth": 400});
					//メッセージ表示
					mapShowMsg("釣り場：" + json[0].point_name + "の情報を表示しています");

					//ポリゴン描画
					mapDisplayPointArea(mapPointMarkers[pointRow.point_id].getPoint());
					//ウィンドウオープン中フラグ
					mapPoint.markerClicked = true;
					if(!mapPoint.infoCloseListener) mapPoint.infoCloseListener = GEvent.addListener(mapObj, "infowindowclose", function(){ mapPoint.markerClicked = false;});
				}
				mapPoint.windowLoading = false;
			},
			onFailure: function(request) {   
				mapShowMsg("釣り場：読み込みに失敗しました。時間を空けて試してみてください");
				mapPoint.windowLoading = false;
			},
			onException: function (request) {   
				mapShowMsg("釣り場：読み込みに失敗しました");
				mapPoint.windowLoading = false;
			}
		}
	);
}

/**
 * function：指定の魚種で絞込み
 */
function mapFishNameInput(fishName) {
	mapForm.fish_name.value = fishName;
	mapLoadPointFromMap();
}

/**
 * function：GClientGeocoderで指定の地域に移動
 */
function mapPanToByGeocoder(address) {
	if (!mapGeocoder) {
		// GClientGeocoderを初期化
		mapGeocoder = new GClientGeocoder();
	}
	if (mapGeocoder) {
		mapGeocoder.getLatLng(
			address,
			function(latLng) {
				if (latLng) {
					//地図移動
					mapObj.setZoom(mapDefaultZoomLevel);
					mapObj.panTo(latLng);
				} else {
					alert("見つかりませんでした");
				}
			}
		);
	}
}

/**
 * function：ストリートビューの初期化
 */
function mapInitStreetView() {
	if (!$("mapStreetViewSwitch")) return false;
	//変数初期化
	mapStreetView = {
		"obj" : null,		//ストリートビューオブジェクト
		"overlay" : null,	//ストリートビュー地図オーバーレイオブジェクト
		"pano" : null,		//ストリートビューパノラマオブジェクト
		"markers" : [],		//ストリートビューマーカ
		"yaw" : 0,			//ストリートビューの向き
		"markerNo" : 1,		//現在の向きのマーカ番号
		"sw" : false,	//ストリートビュー使用中フラグ
		"tmpLatlng" : null	//ドラッグ開始位置位置オブジェクト
	};
	//スイッチ表示
	$("mapStreetViewSwitch").style.display = "block";
	
	//切り替えイベントリスナセット
	Event.observe($("mapStreetViewSwitch"), "click", mapToggleStreetView, false);
	//ストリートビュー切り替えボタン表示
	Event.observe($("mapStreetViewSwitch"), "mouseover", function(){
		$("mapStreetViewSwitch").style.backgroundPosition = "left -36px"
	}, false);
	Event.observe($("mapStreetViewSwitch"), "mouseout", function(){
		$("mapStreetViewSwitch").style.backgroundPosition = (mapStreetView.sw)? "left bottom": "left top";
	}, false);
}

/**
 * function：ストリートビューの切り替え
 */
function mapToggleStreetView(evt) {
	if (!$("mapPano")) return false;
	var center = mapObj.getCenter();
	
	//ストリートビューの読み込み
	if (!mapStreetView.obj) {
		mapLoadStreetView();
	}
	//地図を大きくする
	if (!mapSwitch) mapToggleSize({});

	if (!mapStreetView.sw) {
		//ルート案内を消す
		if (mapDirection && mapDirection.sw) mapToggleDirection({});
		//地図サイズの変更
		$("map").style.height = "250px";
		$("mapPano").style.display = "block";
		$("mapStreetViewSwitch").style.backgroundPosition = "left bottom";
		mapObj.addOverlay(mapStreetView.overlay);
		mapObj.addOverlay(mapStreetView.markers[mapStreetView.markerNo]);
		if (mapStreetView.pano && mapStreetView.pano.isHidden()) mapStreetView.pano.show();
		if (mapStreetView.pano) {
			mapShowMsg("ストリートビュー：表示中");
		} else {
			mapShowMsg("ストリートビュー：矢印アイコンを青い道路の上にドラッグしてください");
		}
		mapStreetView.sw = true;
	} else {
		//地図サイズの変更
		$("map").style.height = "500px";
		$("mapPano").style.display = "none";
		$("mapStreetViewSwitch").style.backgroundPosition = "left top";
		if (mapStreetView.pano) mapStreetView.pano.show();
		mapObj.removeOverlay(mapStreetView.overlay);
		mapObj.removeOverlay(mapStreetView.markers[mapStreetView.markerNo]);
		mapStreetView.sw = false;
		mapShowMsg("");
	}
	mapObj.checkResize();
	if (mapStreetView.pano) mapStreetView.pano.checkResize();
	mapObj.panTo(center);
	
	$("mapStreetViewSwitch").blur();
	evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
}

/**
 * function：ストリートビューの読み込み
 */
function mapLoadStreetView() {
	
	//ストリートビュー追加
	mapStreetView.obj = new GStreetviewClient();
	
	//地図アイコン作成
	var icons = [];
	var iNum = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
	for (var i=0; i<iNum.length; i++) {
		icons[iNum[i]] = new GIcon({
			"image": "/common/gfx/icon/maps/map_streetview_" + iNum[i] + ".png",
			"shadow": "/common/gfx/icon/maps/map_streetview_s.png",
			"iconSize": new GSize(36, 36),
			"shadowSize": new GSize(36, 36),
			"iconAnchor": new GPoint(17, 17)
		});
		mapStreetView.markers[iNum[i]] =  new GMarker(mapObj.getCenter(), {
			"icon": icons[iNum[i]],
			"draggable": true,
			"title": "ドラッグで動かせます"
		});
		//地図アイコンドラッグ時のイベント
		GEvent.addListener(mapStreetView.markers[iNum[i]],"dragstart", function() {
			mapStreetView.tmpLatlng = mapStreetView.markers[mapStreetView.markerNo].getLatLng();
		});
		GEvent.addListener(mapStreetView.markers[iNum[i]],"dragend", function() {
			mapStreetView.obj.getNearestPanoramaLatLng(mapStreetView.markers[mapStreetView.markerNo].getLatLng(), mapMoveStreetViewPanorama);
		});
	}
	//パノラマを表示できれば表示
	mapStreetView.obj.getNearestPanoramaLatLng(mapStreetView.markers[mapStreetView.markerNo].getLatLng(), mapMoveStreetViewPanorama);

	//地図クリック時のイベント
	GEvent.addListener(mapObj,"click", function(overlay,point) {
		if (mapStreetView.sw) mapStreetView.obj.getNearestPanoramaLatLng(point, mapMoveStreetViewPanorama);
	});
	
	//地図上に有効なビューポイントを表示
	mapStreetView.overlay = new GStreetviewOverlay();
}

/**
 * function：ストリートビューパノラマの移動
 */
function mapMoveStreetViewPanorama(latlng) {
	if (!latlng) {
		if (mapStreetView.tmpLatlng) mapStreetView.markers[mapStreetView.markerNo].setLatLng(mapStreetView.tmpLatlng);
		return false;
	}
	mapShowMsg("ストリートビュー：表示中");
	
	if (!mapStreetView.pano) {
		mapLoadStreetViewPanorama(latlng);
	} else {
		mapStreetView.pano.setLocationAndPOV(latlng, {"yaw":mapStreetView.yaw});
	}
	mapStreetView.markers[mapStreetView.markerNo].setLatLng(latlng);
	mapObj.panTo(latlng);
	mapStreetView.tmpLatlng = null;
}

/**
 * function：ストリートビューパノラマの読み込み
 */
function mapLoadStreetViewPanorama(latlng) {
	mapStreetView.pano = new GStreetviewPanorama($("mapPano"), {"latlng": latlng});

	//パノラマ移動時のイベント
	GEvent.addListener(mapStreetView.pano, "initialized", function(viewLocation) {
		mapStreetView.markers[mapStreetView.markerNo].setLatLng(viewLocation.latlng);
		mapObj.panTo(viewLocation.latlng);
	});
	//パノラマ回転時のイベント
	GEvent.addListener(mapStreetView.pano, "yawchanged", function(yaw) {
		var point = mapStreetView.markers[mapStreetView.markerNo].getLatLng();
		mapStreetView.yaw = yaw;
		mapObj.removeOverlay(mapStreetView.markers[mapStreetView.markerNo]);
		
		var tmpNo = Math.ceil((yaw + 11.25) / 22.5);
		mapStreetView.markerNo = (tmpNo > 16)? 16: tmpNo;

		mapStreetView.markers[mapStreetView.markerNo].setLatLng(point);
		mapObj.addOverlay(mapStreetView.markers[mapStreetView.markerNo]);
	});
}

/**
 * function：ルート案内の初期化
 */
function mapInitDirection() {
	if (!$("mapDirectionSwitch")) return false;
	if (isIE6()) { 
		//IE6用
		$("mapDirectionMsg").style.padding = "0";
		$("mapDirectionMsg").style.width = "255px";
	}
	if (!mapGeocoder) {
		// GClientGeocoderを初期化
		mapGeocoder = new GClientGeocoder();
	}
	//変数初期化
	mapDirection = {
		"obj" : null,		//ルート案内オブジェクト
		"from" : {},		//ルート案内スタート地点位置オブジェクト
		"to" : {},			//ルート案内ゴール地点位置オブジェクト
		"icons" : {			//ルート案内アイコンオブジェクト
			"from": new GIcon({
				"image": "/common/gfx/icon/maps/map_A.png",
				"shadow": "/common/gfx/icon/maps/map_s_2.png",
				"iconSize": new GSize(24, 38),
				"shadowSize": new GSize(37, 34),
				"iconAnchor": new GPoint(12, 38)
			}),
			"to": new GIcon({
				"image": "/common/gfx/icon/maps/map_B.png",
				"shadow": "/common/gfx/icon/maps/map_s_2.png",
				"iconSize": new GSize(24, 38),
				"shadowSize": new GSize(37, 34),
				"iconAnchor": new GPoint(12, 38)
			})
		},
		"opt": {			//ルート案内オプションオブジェクト
			"preserveViewport" : false,				//実行時に地図をそのままにするか
			"travelMode" : G_TRAVEL_MODE_DRIVING,	//車
			"avoidHighways" : false,				//高速を避ける
			"getSteps" : true						//ステップ表示
		},
		"loading" : false,		//ルート表示中フラグ
		"show" : false,		//ルート表示中フラグ
		"sw" : false		//ルート案内使用中フラグ
	};
	//切り替えイベントリスナセット
	Event.observe($("mapDirectionSwitch"), "click", mapToggleDirection, false);
	
	//スイッチ表示
	$("mapDirectionSwitch").style.display = "block";
	//ルート案内切り替えボタン表示
	Event.observe($("mapDirectionSwitch"), "mouseover", function(){
		$("mapDirectionSwitch").style.backgroundPosition = "left -36px"
	}, false);
	Event.observe($("mapDirectionSwitch"), "mouseout", function(){
		$("mapDirectionSwitch").style.backgroundPosition = (mapDirection.sw)? "left bottom": "left top";
	}, false);
	
	//釣り場・船宿表示の場合、ゴール地点を登録
	if (mapLoadCase == "show" && $("mapPointAccess")) {
		$("mapPointAccess").update("<a href=\"#\" onclick=\"mapSetToDirectionByPoint(" + mapForm.point_id.value + ");\">地図でルートを確認</a>");
	}
}

/**
 * function：ルート案内の切り替え
 */
function mapToggleDirection(evt) {
	if (!$("mapPano")) return false;
	var center = mapObj.getCenter();

	//ルート案内の読み込み
	if (!mapDirection.obj) {
		mapLoadDirection();
	}
	//地図を大きくする
	if (!mapSwitch) mapToggleSize({});

	if (!mapDirection.sw) {
		//ストリートビューを消す
		if (mapStreetView && mapStreetView.sw) mapToggleStreetView({});
		//ルート案内表示の切り替え
		$("mapDirectionArea").style.display = "block";
		$("mapDirectionSwitch").style.backgroundPosition = "left bottom";
		mapDirection.sw = true;
		if (mapDirection.from.marker) mapObj.addOverlay(mapDirection.from.marker);
		if (mapDirection.to.marker) mapObj.addOverlay(mapDirection.to.marker);
		//両地点があればルート案内実行
		mapLoadDirectionFromPoints();
	} else {
		//ルート消去
		mapDirection.obj.clear();
		if (mapDirection.from.marker) mapObj.removeOverlay(mapDirection.from.marker);
		if (mapDirection.to.marker) mapObj.removeOverlay(mapDirection.to.marker);
		//ルート案内表示の切り替え
		$("mapDirectionArea").style.display = "none";
		$("mapDirectionSwitch").style.backgroundPosition = "left top";
		mapDirection.sw = false;
		mapShowMsg("");
	}
	mapObj.panTo(center);
	
	$("mapDirectionSwitch").blur();
	evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
}

/**
 * function：ルート案内の読み込み
 */
function mapLoadDirection() {
	//ルート追加
	mapDirection.obj = new GDirections(mapObj, $("mapDirectionMsg"));
	
	//釣り場・船宿表示の場合、ゴール地点を登録
	if (mapLoadCase == "show") mapSetToDirection(new GLatLng(mapForm.point_lat.value, mapForm.point_lng.value), mapForm.point_name.value);
	
	//地図クリック時のイベント
	GEvent.addListener(mapObj,"click", function(overlay,point) {
		if (mapDirection.sw && !mapDirection.show) {
			if (point) {
				if (!mapDirection.from.latLng) {
					mapSetFromDirection(point);
					if (mapDirection.from.marker) mapDirection.from.marker.setLatLng(point);
				} else if (!mapDirection.to.latLng) {
					mapSetToDirection(point);
					if (mapDirection.to.marker) mapDirection.to.marker.setLatLng(point);
				}
			}
		}
	});
	//オプションパネル表示
	mapShowDirectionOption();

}

/**
 * function：釣り場情報ウィンドウから出発地点の登録
 */
function mapSetFromDirectionByPoint(point_id) {
	//ルート案内を開く
	if (!mapDirection.sw) {
		mapToggleDirection({});
	}
	mapSetFromDirection(mapPointMarkers[point_id].getLatLng(), mapPointMarkers[point_id].getTitle());
}

/**
 * function：釣り場情報ウィンドウから到着地点の登録
 */
function mapSetToDirectionByPoint(point_id) {
	//ルート案内を開く
	if (!mapDirection.sw) {
		mapToggleDirection({});
	}
	mapSetToDirection(mapPointMarkers[point_id].getLatLng(), mapPointMarkers[point_id].getTitle());
}

/**
 * function：ルート案内のスタート地点の登録
 */
function mapSetFromDirection(latLng, address) {
	mapDirection.from.latLng = latLng;
	mapObj.panTo(latLng);
	//アイコン追加
	if (!mapDirection.from.marker) {
		mapDirection.from.marker = new GMarker(latLng,{
			"icon": mapDirection.icons.from,
			"title": "ルート案内の出発地点です。ドラッグ＆ドロップで移動出来ます",
			"draggable": true
		});
		//地図アイコンドラッグ時のイベント
		GEvent.addListener(mapDirection.from.marker,"dragend", mapSetFromDirection);
		mapObj.addOverlay(mapDirection.from.marker);
	}
	//地点パネル無ければ作成
	if (!$("mapDirectionMsgFrom")) {
		var msgFrom = document.createElement('div');
		msgFrom.id = 'mapDirectionMsgFrom';
		$("mapDirectionMsg").appendChild(msgFrom);
	}
	if (address) {
		$("mapDirectionMsgFrom").update("<p>" + address + "</p>");
		mapDirection.from.address = address;
	} else {
		//GClientGeocoder実行
		mapGetAddressByGeocoder(latLng, 18, "mapSetFromDirectionCallback");
	}
	if (isIE6()) {
		$("mapDirectionMsgFrom").style.background = 'url("/theme/default/images/mapDirectionMsg.gif") no-repeat right top';
	}
	
	//ゴール地点が登録されていれば実行
	if (mapLoadDirectionFromPoints()) {
		return;
	}
	//オプションパネル更新
	mapShowDirectionOption();
}

/**
 * function：ルート案内のゴール地点の登録
 */
function mapSetToDirection(latLng, address) {
	mapDirection.to.latLng = latLng;
	mapObj.panTo(latLng);
	//アイコン追加
	if (!mapDirection.to.marker) {
		mapDirection.to.marker = new GMarker(latLng,{
			"icon": mapDirection.icons.to,
			"title": "ルート案内の到着地点です。ドラッグ＆ドロップで移動出来ます",
			"draggable": true
		});
		//地図アイコンドラッグ時のイベント
		GEvent.addListener(mapDirection.to.marker,"dragend", mapSetToDirection);
		mapObj.addOverlay(mapDirection.to.marker);
	}
	//地点パネル無ければ作成
	if (!$("mapDirectionMsgTo")) {
		var msgFrom = document.createElement('div');
		msgFrom.id = 'mapDirectionMsgTo';
		$("mapDirectionMsg").appendChild(msgFrom);
	}
	if (address) {
		$("mapDirectionMsgTo").update("<p>" + address + "</p>");
		mapDirection.to.address = address;
	} else {
		//GClientGeocoder実行
		mapGetAddressByGeocoder(latLng, 18, "mapSetToDirectionCallback");
	}
	if (isIE6()) {
		$("mapDirectionMsgTo").style.background = 'url("/theme/default/images/mapDirectionMsg.gif") no-repeat right bottom';
	}
	
	//スタート地点が登録されていれば実行
	if (mapLoadDirectionFromPoints()) {
		return;
	}
	//オプションパネル更新
	mapShowDirectionOption();
}
/**
 * function：ルート案内のスタート地点の登録コールバック
 */
function mapSetFromDirectionCallback(geo) {
	if (geo.success) {
		if (geo.address) mapDirection.from.address = geo.address + "付近";
		if (geo.road) mapDirection.from.address = geo.road + "付近";
	} else {
		mapDirection.from.address = "不明な地点 付近";
	}
	//地点パネル更新
	if ($("mapDirectionMsgFrom")) {
		$("mapDirectionMsgFrom").update("<p>" + mapDirection.from.address + "</p>");
	}
}

/**
 * function：ルート案内のゴール地点の登録コールバック
 */
function mapSetToDirectionCallback(geo) {
	if (geo.success) {
		if (geo.address) mapDirection.to.address = geo.address + "付近";
		if (geo.road) mapDirection.to.address = geo.road + "付近";
	} else {
		mapDirection.to.address = "不明な地点 付近";
	}
	//地点パネル更新
	if ($("mapDirectionMsgTo")) {
		$("mapDirectionMsgTo").update("<p>" + mapDirection.to.address + "</p>");
	}
}

/**
 * function：ルートのクリア
 */
function mapClearDirection(toFrom) {
	//ルート消去
	mapDirection.obj.clear();
	
	//出発地か両方消す場合
	if (toFrom != "to") {
		//ルート案内オプションメッセージ消去
		if($("mapDirectionMsgFrom")) $("mapDirectionMsg").removeChild($("mapDirectionMsgFrom"));
		
		//マーカ削除
		if (mapDirection.from.marker) mapObj.removeOverlay(mapDirection.from.marker);
		
		//地点削除
		mapDirection.from = {};

		//片方だけ消す場合、もう片方のマーカーと地点パネルを表示させる
		if (toFrom && mapDirection.to.marker) {
			mapObj.addOverlay(mapDirection.to.marker);
			mapSetToDirection(mapDirection.to.latLng, mapDirection.to.address);
		}
	}
	//到着地か両方消す場合
	if (toFrom != "from") {
		//ルート案内オプションメッセージ消去
		if($("mapDirectionMsgTo")) $("mapDirectionMsg").removeChild($("mapDirectionMsgTo"));
		
		//マーカ削除
		if (mapDirection.to.marker) mapObj.removeOverlay(mapDirection.to.marker);
		
		//地点削除
		mapDirection.to = {};
		
		//片方だけ消す場合、もう片方のマーカーと地点パネルを表示させる
		if (toFrom && mapDirection.from.marker) {
			mapObj.addOverlay(mapDirection.from.marker);
			mapSetFromDirection(mapDirection.from.latLng, mapDirection.from.address);
		}
	}
	
	//ルート案内オプションパネル表示
	mapShowDirectionOption();
	
	//メッセージ表示のため実行
	mapLoadDirectionFromPoints();
	mapDirection.show = false;
}

/**
 * function：ルート案内オプションの変更
 */
function mapToggleDirectionOption(opt) {
	//移動手段
	if (opt == "travelMode") {
		mapDirection.opt.travelMode = (mapDirection.opt.travelMode == G_TRAVEL_MODE_DRIVING)? G_TRAVEL_MODE_WALKING: G_TRAVEL_MODE_DRIVING;;
	}
	//高速回避
	if (opt == "avoidHighways") {
		mapDirection.opt.avoidHighways = (mapDirection.opt.avoidHighways)? false: true;
	}
	//ルート案内実行
	mapLoadDirectionFromPoints();
	//ルート案内オプションパネル表示
	mapShowDirectionOption();
}

/**
 * function：ルート案内オプションパネルの表示
 */
function mapShowDirectionOption() {
	//オプション表示
	if (mapDirection.opt.travelMode == G_TRAVEL_MODE_DRIVING) {
		var avoidHighways = (mapDirection.opt.avoidHighways)? "優先しない": "優先";
		var msg = '<p>移動手段：<a href="#" onclick="mapToggleDirectionOption(\'travelMode\'); return false;">車</a>　高速道路：<a href="#" onclick="mapToggleDirectionOption(\'avoidHighways\'); return false;">' + avoidHighways + '</a></p>';
	} else {
		var msg = '<p>移動手段 <a href="#" onclick="mapToggleDirectionOption(\'travelMode\'); return false;">徒歩</a></p>';
	}
	
	if (mapDirection.from.latLng || mapDirection.to.latLng) {
		msg += "<p>";
		if (mapDirection.from.latLng) msg += "出発:<a href=\"#\" onclick=\"mapClearDirection('from'); return false;\">クリア</a>　";
		if (mapDirection.to.latLng) msg += "到着:<a href=\"#\" onclick=\"mapClearDirection('to'); return false;\">クリア</a>　";
		if (mapDirection.from.latLng && mapDirection.to.latLng) {
			msg += '両方:<a href="#" onclick="mapClearDirection(\'travelMode\'); return false;">クリア</a>';
		}
		msg += "</p>";
	}
	if (!$("mapDirectionMsgOpt")) {
		var msgFrom = document.createElement('div');
		msgFrom.id = 'mapDirectionMsgOpt';
		$("mapDirectionMsg").appendChild(msgFrom);
	}
	$("mapDirectionMsgOpt").update(msg);
}

/**
 * function：ルート案内の実行
 */
function mapLoadDirectionFromPoints() {
	if (!mapDirection.from.latLng) {
		mapShowMsg("ルート案内：出発地点を設定ください。地図をクリックするか、釣り場情報の吹き出しの「ここから出発」をクリックします");
		return false;
	} else if (!mapDirection.to.latLng) {
		mapShowMsg("ルート案内：到着地点を設定ください。地図をクリックするか、釣り場情報の吹き出しの「ここに到着」をクリックします");
		return false;
	}
	
	if (mapDirection.loading) {
		return;
	}
	mapDirection.loading = true;
	var points = [
		mapDirection.from.latLng,
		mapDirection.to.latLng
	];
	//実行
	mapDirection.obj.loadFromWaypoints(points,mapDirection.opt);
	
	//結果取得時のイベント
	GEvent.addListener(mapDirection.obj, 'error', function() {
		mapShowMsg("ルートを取得できませんでした");
		//オプションパネル表示
		mapShowDirectionOption();
		mapDirection.loading = false;
	});
	GEvent.addListener(mapDirection.obj, 'load', function(routeObj) {
		//地点パネル消去
		if($("mapDirectionMsgFrom")) $("mapDirectionMsg").removeChild($("mapDirectionMsgFrom"));
		if($("mapDirectionMsgTo")) $("mapDirectionMsg").removeChild($("mapDirectionMsgTo"));
		//アイコン消去
		if (mapDirection.from.marker) mapObj.removeOverlay(mapDirection.from.marker);
		if (mapDirection.to.marker) mapObj.removeOverlay(mapDirection.to.marker);
		//オプションパネル表示
		mapShowDirectionOption();
		mapShowMsg("ルート案内：表示中");
		mapDirection.show = true;
		mapDirection.loading = false;
	});
	return true;
}

/**
 * function：逆ジオコーディングの初期化
 */
function mapInitReverseGeocoding() {
	if (!mapGeocoder) {
		// GClientGeocoderを初期化
		mapGeocoder = new GClientGeocoder();
	}
	//地図移動イベントリスナセット
	GEvent.addListener(mapObj, 'move', function() {
		//タイマーリセット
		if (mapGeocoder && mapGeocoder.ReverseGeocodingTimer) {
			clearTimeout(mapGeocoder.ReverseGeocodingTimer);
		}
		//メッセージエリアを隠す
		$("mapGeocodeArea").style.display = "none";
	});
	
	//タイマーセット
	GEvent.addListener(mapObj, 'moveend', function() {
		mapGeocoder.ReverseGeocodingTimer = setTimeout(mapShowAddressByGeocoder, 1500);
	});

}

/**
 * function：GClientGeocoderで中心地の住所を表示
 */
function mapShowAddressByGeocoder() {
	var center = mapObj.getCenter();
	var zoom = mapObj.getZoom();
	
	//ストリートビューを開いている場合
	if (mapStreetView && mapStreetView.sw) { 
		center = mapStreetView.markers[mapStreetView.markerNo].getLatLng();
		zoom = 18;
	}
	//GClientGeocoder実行
	mapGetAddressByGeocoder(center, zoom, "mapShowAddressByGeocoderCallback");
}

/**
 * function：GClientGeocoderで中心地の住所を表示
 */
function mapShowAddressByGeocoderCallback(geo) {
	if (geo.success) {
		if (geo.address) $("mapGeocodeMsg").update(geo.address + "付近");
		if (geo.road) $("mapGeocodeMsg").update(geo.road + "付近");
		//メッセージエリアを表示
		$("mapGeocodeArea").style.display = "block";
	}
}

/**
 * function：GClientGeocoderで指定の位置の住所を取得
 */
function mapGetAddressByGeocoder(latLng, zoom, callback) {
	var ret = {
		"success": false,
		"address": null,
		"road": null
	};
	
	if (mapGeocoder && zoom >= 8) {
		mapGeocoder.getLocations(
			latLng,
			function(response) {
				if (!response || response.Status.code != 200) {
//					alert("Status Code:" + response.Status.code);
				} else {
					place = response.Placemark[0].AddressDetails.Country;
					
					//メッセージを更新
					//住所の場合
					if (place.AdministrativeArea) {
						if (zoom >= 8 && place.AdministrativeArea.AdministrativeAreaName)  ret.address = place.AdministrativeArea.AdministrativeAreaName + " ";
						if (place.AdministrativeArea.Locality) {
							var locality = place.AdministrativeArea.Locality;
							if (zoom >= 10 && locality.LocalityName) ret.address += locality.LocalityName + " ";
							if (locality.DependentLocality) {
								var dependentLocality = locality.DependentLocality;
								if (zoom >= 15 && dependentLocality.DependentLocalityName) ret.address += dependentLocality.DependentLocalityName + " ";
								if (dependentLocality.Thoroughfare) {
									var thoroughfare = dependentLocality.Thoroughfare;
									if (zoom >= 16 && thoroughfare.ThoroughfareName) ret.address += thoroughfare.ThoroughfareName + " ";
								}
							}
						}
						ret.success = true;
					} else if (place.AddressLine) {
						ret.success = true;
						ret.road = place.AddressLine + " ";
					}
				}
				eval(callback + "(ret)");
			}
		);
	}
}

