// cheltenham software
var latitudeDefault = '36.062';
var longitudeDefault = '136.223';
var objectPointDefault = new GLatLng( latitudeDefault, longitudeDefault );
// アイコン
var imageIcon = new Image()
imageIcon.src = "../library/image/map/marker16.gif";
// 表示数
var viewQuantity = document.getElementById( 'ViewQuantity' ).value;
// GoogleMap表示エリア
var objectGoogleMap = document.getElementById( 'PanelGoogleMap' );
// GoogleMap縮尺
var zoomDefault = 13;
// URL
var urlDetail = '../';

//--------------------------------------------------------------------------------------------------------------------------------
// GoogleMapのロード
//--------------------------------------------------------------------------------------------------------------------------------
function functionMap_loadGoogleMap()
{
	map = new GMap2( objectGoogleMap );
	map.addControl( new GLargeMapControl() );
	map.addControl( new GScaleControl() );
	map.addControl( new GMapTypeControl() );
//	map.setMapType( G_SATELLITE_MAP );
//	map.enableScrollWheelZoom();
	map.enableInfoWindow();
	map.setCenter( objectPointDefault, zoomDefault );

	// マップがスクロールやズーム変更されたら関数を実行
	GEvent.addListener( map, 'dragend', functionMenu_submitSearchForm, false );
	GEvent.addListener( map, 'zoomend', functionMenu_submitSearchForm, false );
	// マップが移動し始めたらすべての浮遊物を閉じる
	GEvent.addListener( map, 'movestart', functionMap_removeAllFloaters );
}

//--------------------------------------------------------------------------------------------------------------------------------
// マーカー
//--------------------------------------------------------------------------------------------------------------------------------
function functionMap_addMarker( mapTarget, longitudeTarget, latitudeTarget )
{
	var marker = functionMap_createMarkerIcon( imageIcon );
	var markerPoint = new GMarker( new GPoint( longitudeTarget, latitudeTarget ), marker );
	mapTarget.addOverlay( markerPoint );

	return( markerPoint );
}
function functionMap_addMarkerDefault( mapTarget, longitudeTarget, latitudeTarget )
{
	var marker = new GMarker( new GLatLng( latitudeTarget, longitudeTarget ) );
	mapTarget.addOverlay( marker );

	return( marker );
}

// マーカーのアイコンを作る
function functionMap_createMarkerIcon( imageIcon )
{
	var objectIcon = new GIcon();
	objectIcon.image = imageIcon.src;
	objectIcon.iconSize = new GSize( imageIcon.width, imageIcon.height );
	objectIcon.iconAnchor = new GPoint( imageIcon.width / 2, imageIcon.height / 2 );
	return( objectIcon );
}

// 店舗アイコンをプロット
function functionMap_plotShopIcon( listShop )
{
	// 全店舗について
	for( var i = 0 ; i < listShop.length ; i++ )
	{
		// マーカーリストになければ追加
		var id = listShop[ i ].id;
		if( listShopLoaded[ id ] == null )
		{
			listShop[ i ].marker = functionMap_addMarker( map, listShop[ i ].locationLongitude, listShop[ i ].locationLatitude );
			listShop[ i ].marker.title = id;
			listShopLoaded[ id ] = listShop[ i ];
			var functionRefference = function()
			{
				// Remove All Floaters
				functionMap_removeAllFloaters();
				// Open Baloon Window
				functionMap_openShopMarkerInfoWindow( this.title );
				// Open Panel of Information
//				functionMap_openShopMarkerInfoPanel( this.title );
			}
			GEvent.addListener( listShopLoaded[ id ].marker, 'click', functionRefference );
		}
	}
}

// 全マーカーを取り除く
function functionMap_removeAllMarkers()
{
	map.closeInfoWindow();
	map.clearOverlays();
}

// すべての浮遊物を消去
function functionMap_removeAllFloaters()
{
	// 店舗詳細パネル
	functionDetail_activatePanelDetail(0);
	// Infoウィンドウ
	map.closeInfoWindow();
}

//--------------------------------------------------------------------------------------------------------------------------------
// 情報ウィンドウ
//--------------------------------------------------------------------------------------------------------------------------------
// 情報ウィンドウを表示
function functionMap_openShopMarkerInfoWindow( idTarget )
{
	var urlIcon = "./image/icon/coffee.gif";
	var objectShop = listShopLoaded[ idTarget ];
	var objectPointShop = new GLatLng( objectShop.locationLatitude, objectShop.locationLongitude );

	var textOutput = '<b>' + objectShop.nameTotal + '</b><br />';
	textOutput += '' + objectShop.locationCity + objectShop.locationBlockAll + '<br />';
	textOutput += '' + objectShop.telephoneHome + '<br />';
//	textOutput += '<a href = "javascript:functionDetail_sendPostData(null,'+ idTarget + ');">詳細表示</a>';
	textOutput += '<a href = "' + urlDetail + idTarget + '/" target = "_blank">詳細表示</a>';

	// 中心を移動
	map.panTo( objectPointShop );
	// 情報ウィンドウを表示
	var functionRefference = function()
	{
		map.openInfoWindowHtml( objectPointShop , textOutput, new GSize( 0, 0 ) );
	}
	setTimeout( functionRefference, 500 );
}
// 情報ウィンドウを表示
function functionMap_openPointInfoWindow( textOutput, longitudeNow, latitudeNow )
{
	var objectPointNow = new GPoint( longitudeNow, latitudeNow );
	map.openInfoWindowHtml( objectPointNow, textOutput, new GSize( 0, 0 ) );
}

//--------------------------------------------------------------------------------------------------------------------------------
// 情報ウィンドウ
//--------------------------------------------------------------------------------------------------------------------------------
// 情報ウィンドウを表示
function functionMap_openShopMarkerInfoPanel( idTarget )
{
	var objectShop = listShopLoaded[ idTarget ];
	var objectPointShop = new GLatLng( objectShop.locationLatitude, objectShop.locationLongitude );

	// 中心を移動
	map.panTo( objectPointShop );
	// 情報パネルを表示
	var functionRefference = function()
	{
		functionDetail_sendPostData( null, objectShop.id );
	}
	setTimeout( functionRefference, 500 );
}

//--------------------------------------------------------------------------------------------------------------------------------
// 地図の領域をゲット
function functionMap_getMapBound()
{
	// 領域の境界をチェック
	var bounds = map.getBounds();
	var pointSouthWest = bounds.getSouthWest();
	var pointNorthEast = bounds.getNorthEast();
	var listLongitude = new Array();
	var listLatitude = new Array();
	listLongitude[0] = Math.min( pointNorthEast.lng(), pointSouthWest.lng() );
	listLongitude[1] = Math.max( pointNorthEast.lng(), pointSouthWest.lng() );
	listLatitude[0] = Math.min( pointNorthEast.lat(), pointSouthWest.lat() );
	listLatitude[1] = Math.max( pointNorthEast.lat(), pointSouthWest.lat() );
	var percentageMarginX = 0.01;
	var percentageMarginY = 0.01;
	// 検索フォームに表示範囲を渡す
	document.getElementById( 'LongitudeMin' ).value = listLongitude[0] + percentageMarginX * ( listLongitude[1] - listLongitude[0] );
	document.getElementById( 'LongitudeMax' ).value = listLongitude[1] - percentageMarginX * ( listLongitude[1] - listLongitude[0] );
	document.getElementById( 'LatitudeMin' ).value = listLatitude[0] + percentageMarginY * ( listLatitude[1] - listLatitude[0] );
	document.getElementById( 'LatitudeMax' ).value = listLatitude[1] - percentageMarginY * ( listLatitude[1] - listLatitude[0] );
}

// リスナー設定
cheltenhamEvent_addListener( window, 'load', functionMap_loadGoogleMap, false );
cheltenhamEvent_addListener( window, 'unload', GUnload, false );

