window.onload = function() {
	if (GBrowserIsCompatible()) {
		var shop_marker = new Object;
		var loadShop = function(){
			var center = map.getCenter();
			var bound = map.getBounds();
			GDownloadUrl('map?lat=' + center.lat() + '&lng=' + center.lng() + '&range=' + Math.floor(center.distanceFrom(bound.getSouthWest())),
			function(res_data, status_code){
				if(status_code == 200){
					eval('var shop_list = ' + res_data);
					//var cnt = 0;
					for(var i = 0, max = shop_list.length; i < max; i++){
						if(typeof(shop_marker[shop_list[i].id]) == 'undefined'){
							var marker = new GMarker(new GLatLng(shop_list[i].lat, shop_list[i].lng));
							GEvent.addListener(marker, "click", (function(shop){
								return function(){
									this.openInfoWindowHtml('<div><a href="detail/' + shop.id + '">' + shop.name + '</a></div><div>' + shop.address + '</div>');
								}
							})(shop_list[i]));
							marker.is_display = true;
							map.addOverlay(marker);
							shop_marker[shop_list[i].id] = marker;
							//cnt++;
						}
					}
					//console.info(cnt + '/' + shop_list.length);
				}
			});return;
			for(var id in shop_marker){
				if(bound.containsLatLng(shop_marker[id].getLatLng())){
					if(!shop_marker[id].is_display){
						shop_marker[id].is_display = true;
						map.addOverlay(shop_marker[id]);
					}
				}else{
					if(shop_marker[id].is_display){
						shop_marker[id].is_display = false;
						map.removeOverlay(shop_marker[id]);
					}
				}
			}
		};

		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(coord[0], coord[1]), 16);
		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl());

		GEvent.addListener(map, "moveend", loadShop);
		loadShop();
	}
};
window.onunload = GUnload;

