//===================================================================================
//  システム名：イベントナビ
//  概要      ：Google Maps
//  ファイル名：gmap.js
//  作成日    ：
//  作成者    ：
//  更新履歴  ：
//===================================================================================

var map      = null;
var geocoder = null;
var marker   = null;
var message  = "";

//***************************************************************
//*     地図の初期設定
//***************************************************************
function InitMap(pName, pMapLat, pMapLng, pAddrLat, pAddrLng, pAddress) {
	if (GBrowserIsCompatible()) {
		map      = new GMap2(document.getElementById(pName));
		geocoder = new GClientGeocoder();

		if ((!map) || (!geocoder)) return false;

		// 初期座標・Zoom値設定
		pMapLat = pMapLat || 35.68239834096679;
		pMapLng = pMapLng || 139.76707935333252;
		map.setCenter(new GLatLng(pMapLat, pMapLng), 16);
		map.savePosition();

		// コントロール追加
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());

		// マーカー、ふき出しの設定
		marker = new GMarker(new GLatLng(pAddrLat, pAddrLng), { draggable: true });
		map.addOverlay(marker);
		if (pAddrLat && pAddrLng) {
			if (pAddress) {
				setMessage(pAddress);
			}
		} else {
			marker.hide();
		}

		// クリックでふき出しを再表示させる
		GEvent.addListener(marker, 'click', function() {
			if (message) {
				marker.openInfoWindowHtml(message);
			}
		});
		// マーカーの移動時にふき出しを消す
		GEvent.addListener(marker, 'dragstart', function() {
			marker.closeInfoWindow();
		});
	}
}

//***************************************************************
//*     地図の新規作成
//***************************************************************
function CreateMap(pName, pMapLat, pMapLng, pAddrLat, pAddrLng, pAddress) {
	if (window.addEventListener) {
		// for W3C DOM
		window.addEventListener('load', function() {
			InitMap(pName, pMapLat, pMapLng, pAddrLat, pAddrLng, pAddress);
		}, false);
		window.addEventListener('unload', function() {
			GUnload();
		}, false);
	} else if(window.attachEvent) {
		// for IE
		window.attachEvent('onload', function() {
			InitMap(pName, pMapLat, pMapLng, pAddrLat, pAddrLng, pAddress);
		});
		window.attachEvent('onunload', function() {
			GUnload();
		});
	} else {
		window['onload'] = function() {
			InitMap(pName, pMapLat, pMapLng, pAddrLat, pAddrLng, pAddress);
		};
		window['onunload'] = function() {
			GUnload();
		};
	}
}

//***************************************************************
//*     住所検索処理
//***************************************************************
function showAddress(address) {
	if ((!map) || (!geocoder)) return false;

	if (!address) {
		alert("住所を入力してください。");
		return false;
	}

	geocoder.getLatLng(address, function(point) {
		if (!point) {
			// 検索失敗
			alert(address + " は見つかりませんでした。");
		} else {
			// 検索成功　中心の座標を変更する
			map.setCenter(point);
			marker.setPoint(point);
			marker.show();

			// ふき出しの表示
			setMessage(address);
			marker.openInfoWindowHtml(message);
		}
	});
}

//***************************************************************
//*     ふき出しの文字列を設定する
//***************************************************************
function setMessage(address) {
	message = "<strong>住所：</strong><br />"
			+ "<span style=\"font-size:80%;\">" + address + "</span>"
			;
}

//***************************************************************
//*     マップ中心の緯度を取得する
//***************************************************************
function getMapLat() {
	if (!map) return "";
	return (map.getCenter()).lat();
}

//***************************************************************
//*     マップ中心の経度を取得する
//***************************************************************
function getMapLng() {
	if (!map) return "";
	return (map.getCenter()).lng();
}

//***************************************************************
//*     マーカーの緯度を取得する
//***************************************************************
function getAddrLat() {
	if (!marker) return "";
	return (marker.getPoint()).lat();
}

//***************************************************************
//*     マーカーの経度を取得する
//***************************************************************
function getAddrLng() {
	if (!marker) return "";
	return (marker.getPoint()).lng();
}

