
var SpotShow=
{
	g_gmap: null,
	
	init: function()
	{
		var map = document.getElementById('gmap');
		if (!GBrowserIsCompatible())
		{
			map.innerHTML = "Google Map に対応していないブラウザです";
			return ;
		}

		this.g_gmap = new GMap2(map);
		
		var lat = document.getElementById('latitude').value;
		var lng = document.getElementById('longitude').value;
		var latlng = new GLatLng(lat, lng);
		this.g_gmap.setCenter(latlng, 15, false);

		var valueActive = document.getElementById('map_active_icon').value;
		var activeParams = valueActive.split(',');
		var marker = this.createMarker(lat, lng, activeParams[4], activeParams[0], activeParams[1],
				activeParams[2], activeParams[3], 1);

		var valueNormal = document.getElementById('map_icon').value;
		var normalParams = valueNormal.split(',');
		marker = this.createMarker(lat, lng, normalParams[4], normalParams[0], normalParams[1],
				normalParams[2], normalParams[3], 2);

		this.g_gmap.addOverlay(marker);
	},
	
	createMarker: function(lat, lng, img, ax, ay, w, h, z)
	{
		var option = new Object();
		var icon = new GIcon();
		icon.image            = '/images/spot_icon/' + img;
		icon.iconSize         = new GSize(w, h);
		icon.iconAnchor       = new GPoint(ax, ay);
		option.icon = icon;
		option.zIndexProcess = function(){ return z; };
		return new GMarker(new GLatLng(lat, lng), option);
	},
	
//	onGMapMoveEnd: function()
//	{
//		SpotShow.g_geocoder.getLocations(SpotShow.g_gmap.getCenter(),
//											SpotShow.onGMapGetLocations);
//	},

/*	onGMapGetLocations: function(response)
	{
		var elm = document.getElementById("geocode");
		if (!response || response.Status.code != 200)
		{
			elm.innerHTML = '不明な住所です';
			return ;
		}

		var address = "";
		var country = response.Placemark[0].AddressDetails.Country;
		if (country.AdministrativeArea)
		{
			var adminArea = country.AdministrativeArea;
			address += adminArea.AdministrativeAreaName;
			if (adminArea.Locality)
			{
				var locality = adminArea.Locality;
				address += locality.LocalityName;
				if (locality.DependentLocality)
				{
					var depend = locality.DependentLocality;
					address += depend.DependentLocalityName;
//					if (depend.Thoroughfare)
//					{
//						address += depend.Thoroughfare.ThoroughfareName;
//					}
				}
			}
		}
		else
		{
			address += country.CountryName;
		}

		elm.innerHTML = address;
	},
*/
	searchAround: function()
	{
		var center = this.g_gmap.getCenter();
		new Ajax.Request("/spot/around/lat/"+center.lat()+"/lng/"+center.lng(),
			{
				method: 'get',
				onComplete: function(request, json)
				{
					if (json.result != 0)
					{
						window.location = "/top";
					}
					
					var num = json.spot.length;
					for (var i = 0; i < num; i ++)
					{
						var spot = json.spot[i];
						var marker = SpotShow.createMarker(spot.latitude, spot.longitude,
								spot.icon, 	spot.anchorX, spot.anchorY,
								spot.width, spot.height, 1);
						marker.spotId = spot.id;
						SpotShow.g_gmap.addOverlay(marker);
						GEvent.addListener(marker, 'click',
							function()
							{
								window.location = "/spot/show/spot_id/"+this.spotId;
							}
						);
					}
				}
			}
		);
	}
};

window.onload=function()
{
	SpotShow.init();
}
