
var mapCenterPos ="";
var mapCenterAddress = "";
var mapObj;
var gPos;
var pointList = [];
var zoomSize = 7;

function mapLoad(){
	if (GBrowserIsCompatible()) {
		mapObj = new GMap2(document.getElementById("map"));
		mapObj.addControl(new GLargeMapControl());
		if(mapCenterPos){
			var pos = mapCenterPos.split(",");
			gPos = new GLatLng(pos[0], pos[1]);
			mapObj.setCenter(gPos, 15);
			mapObj.addOverlay(new GMarker(gPos));
		}else{
			var gGeo = new GClientGeocoder();
			gGeo.getLatLng(mapCenterAddress, geoLoad);
		}
	}else{
		$("#map").html("地図が表示できません");
	}
}

function geoLoad(gPosObj) {
	if (gPosObj) {
		gPos = gPosObj;
		mapObj.setCenter(gPosObj, 15);
		mapObj.addOverlay(new GMarker(gPosObj));
	}else{
		$("#map").html("地図が見つかりませんでした");
	}
}


function mapLoad2(){
	if (GBrowserIsCompatible()) {
		mapObj = new GMap2(document.getElementById("map"));
		mapObj.addControl(new GSmallMapControl());
		var gGeo = new GClientGeocoder();
		gGeo.getLatLng(mapCenterAddress, geoLoad2);
	}else{
		$("#map").html("地図が表示できません");
	}
}

function geoLoad2(gPosObj) {
	if (gPosObj) {
		gPos = gPosObj;
		mapObj.setCenter(gPosObj, zoomSize);
		var i = 0;
		var clearId = setInterval(function(){
			if(pointList.length == i){
				clearInterval(clearId);
				return;
			}
			if(pointList[i].pos){
				var pos = pointList[i].pos.split(",");
				pos = new GLatLng(pos[0], pos[1]);
				var m = new GMarker(pos);
				mapObj.addOverlay(m);
				addBalloon(m, i);
			}else{
				var gGeo = new GClientGeocoder();
				gGeo.getLatLng(pointList[i].address, geoLoad3(i));
			}
			i++;
		}, 500);

/*
		for(var i = 0; i < pointList.length; i++){
			if(pointList[i].pos){
				var pos = pointList[i].pos.split(",");
				pos = new GLatLng(pos[0], pos[1]);
				var m = new GMarker(pos);
				mapObj.addOverlay(m);
				addBalloon(m, i);
			}else{
				var gGeo = new GClientGeocoder();
				gGeo.getLatLng(pointList[i].address, geoLoad3(i));
			}
		}
*/

	}else{
		$("#map").html("地図が見つかりませんでした");
	}
}

function geoLoad3(id) {
	var f = function(gPosObj){
		if (gPosObj) {
			var m = new GMarker(gPosObj);
			mapObj.addOverlay(m);
			addBalloon(m, id);
		}
	}
	return f;
}

function addBalloon(m, index){
	GEvent.addListener(m, "click", function(){
		m.openInfoWindowHtml(pointList[index].name);
	});
}


function creatMapTool(address){
	$('#gtool').css("display","block");
	$('#map').css("width","647px");
	$('#map').css("height","548px");
	$('#gtoolad').val(address);
	mapCenterPos = "";
	mapCenterAddress = address;
	mapLoad();
	$(window).unload(GUnload);
}

function creatMapTool2(pos){
	if(pos){
		$('#gtool').css("display","block");
		$('#map').css("width","647px");
		$('#map').css("height","548px");
		mapCenterPos = pos;
		mapCenterAddress = "";
		mapLoad();
		$(window).unload(GUnload);
	}else{
		alert("座標が入力されていません");
	}
}

function getCenterPos(){
	var cObj = mapObj.getCenter();
	if(cObj){
		$('#map_f').val(cObj.y+","+cObj.x);
	}
}

