var map;
var geocoder;
var mgr;
function loadMap() {
  if(GBrowserIsCompatible()) {
	  map = new GMap2(document.getElementById("allMap"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		map.setCenter(new GLatLng(36.50, 135.60), 5);
		mgr = new MarkerManager(map);
		loadIndexData("all");
  }
}
//マーカーファイル取得
function loadIndexData(data) {
  obj = document.area.pref;
  var adr = obj.selectedIndex;
	var address = obj.options[adr].value;
	obj = document.mushi.semi;
	var sem = obj.selectedIndex;
	semi = obj.options[sem].value;
	getJson(address,semi);
}
//データ取得
function getJson(address, semi) {
	if ( semi != "all" ) {
		var semif = "_" + semi;
	}

	if ( address != "all" && semi != "all") {
		var jsonfile =  "data/pref" + address + semif + ".dat";
		GDownloadUrl(jsonfile, loadAllMaker);
	} else if ( address != "all" && semi == "all") {
		var jsonfile =  "data/pref" + address + ".dat";
		GDownloadUrl(jsonfile, loadIndexMaker);
	} else if ( address == "all" && semi != "all") {
		var jsonfile =  "data/index" + semif + ".dat";
		GDownloadUrl(jsonfile, loadIndexMaker);
	} else {
		var jsonfile = "data/index.dat";
		GDownloadUrl(jsonfile, loadIndexMaker);
	}
	mgr.clearMarkers();
	
}
//マーカーデータ取得 [Index]
function loadIndexMaker(jsondata, statusCode) {
	var json = eval("(" + jsondata + ")");
	var batch = [];
	for (var i = 0; i < json.index.length; i++) {
		var semi = json.index[i].semi;
		var lat = json.index[i].lat;
		var lon = json.index[i].lon;
		var message = "<div align='left'>" + json.index[i].semi + "みつけた！" + json.index[i].date + "<hr style='width:50px'><font color='red'><b>" + json.index[i].member + "</b></font>さん：" + json.index[i].pref + "<div class='text10'>" + json.index[i].comment + "</div>";
		batch.push(descMarker(semi, lat, lon, message));
  }
	mgr.addMarkers(batch, 3);
	mgr.refresh();
}
//マーカーデータ取得 [All]
function loadAllMaker(jsondata, statusCode) {
	var json = eval("(" + jsondata + ")");
	var batch = [];
	for (var i = 0; i < json.all.length; i++) {
		var semi = json.all[i].semi;
		var lat = json.all[i].lat;
		var lon = json.all[i].lon;
		var message = "<div align='left'>" + json.all[i].semi + "みつけた！" + json.all[i].date + "<hr style='width:50px'><font color='red'><b>" + json.all[i].member + "</b></font>さん：" + json.all[i].pref + "<div class='text10'>" + json.all[i].comment + "</div>";
		batch.push(descMarker(semi, lat, lon, message));
  }
	mgr.addMarkers(batch, 3);
	mgr.refresh();
}
//マーカー描画
function descMarker(semi, lat, lon, message){
	var gmarkeroptions = new Object();
	gmarkeroptions.icon = new GIcon();
	if ( semi == "ニイニイゼミ" ) //BLUE
		gmarkeroptions.icon.image = "../images/gicons/semi_blue8.png";
	if ( semi == "アブラゼミ" ) //RED
		gmarkeroptions.icon.image = "../images/gicons/semi_red8.png";
	if ( semi == "クマゼミ" ) //ORANGE
		gmarkeroptions.icon.image = "../images/gicons/semi_orange8.png";
	if ( semi == "ツクツクボウシ" ) //YELLOW
		gmarkeroptions.icon.image = "../images/gicons/semi_yellow8.png";
	if ( semi == "ミンミンゼミ" ) //GREEN
		gmarkeroptions.icon.image = "../images/gicons/semi_green8.png";
	if ( semi == "ヒグラシ" ) //GRAY
		gmarkeroptions.icon.image = "../images/gicons/semi_gray8.png";
	gmarkeroptions.icon.shadow = "../images/gicons/shadow-semi8.png";
	gmarkeroptions.draggable = false;
	gmarkeroptions.icon.iconSize = new GSize(15, 20);
	gmarkeroptions.icon.shadowSize = new GSize(26, 20);
	gmarkeroptions.icon.iconAnchor = new GPoint(0, 20);
	gmarkeroptions.icon.infoWindowAnchor = new GPoint(10,10);
  var marker = new GMarker(new GLatLng(lat, lon), gmarkeroptions);

  GEvent.addListener(marker, "click", function(){
    marker.openInfoWindowHtml(message);
  });

  return marker;
}
// 都道府県切り替え
function selectPrefSemi() {
	obj = document.area.pref;
	adr = obj.selectedIndex;
	var npref = obj.options[adr].value;
	var nscl = 10;
	if ( npref == "1") {
	  address = "北海道";
		nscl = 7;
	} else if ( npref == "2") {
		address = "青森県";
	} else if ( npref == "3") {
		address = "岩手県";
	} else if ( npref == "4") {
		address = "宮城県";
	} else if ( npref == "5") {
		address = "秋田県";
	} else if ( npref == "6") {
		address = "山形県";
	} else if ( npref == "7") {
		address = "福島県";
	} else if ( npref == "8") {
		address = "茨城県";
	} else if ( npref == "9") {
		address = "栃木県";
	} else if ( npref == "10") {
		address = "群馬県";
	} else if ( npref == "11") {
		address = "埼玉県";
	} else if ( npref == "12") {
		address = "千葉県";
	} else if ( npref == "13") {
		address = "東京都";
  } else if ( npref == "14") {
		address = "神奈川県";
  } else if ( npref == "15") {
		address = "新潟県";
  } else if ( npref == "16") {
		address = "富山県";
  } else if ( npref == "17") {
		address = "石川県";
  } else if ( npref == "18") {
		address = "福井県";
  } else if ( npref == "19") {
		address = "山梨県";
  } else if ( npref == "20") {
		address = "長野県";
  } else if ( npref == "21") {
		address = "岐阜県";
  } else if ( npref == "22") {
		address = "静岡県";
  } else if ( npref == "23") {
		address = "愛知県";
  } else if ( npref == "24") {
		address = "三重県";
  } else if ( npref == "25") {
		address = "滋賀県";
  } else if ( npref == "26") {
		address = "京都府";
  } else if ( npref == "27") {
		address = "大阪府";
  } else if ( npref == "28") {
		address = "兵庫県";
  } else if ( npref == "29") {
		address = "奈良県";
  } else if ( npref == "30") {
		address = "和歌山県";
  } else if ( npref == "31") {
		address = "鳥取県";
  } else if ( npref == "32") {
		address = "島根県";
  } else if ( npref == "33") {
		address = "岡山県";
  } else if ( npref == "34") {
		address = "広島県";
  } else if ( npref == "35") {
		address = "山口県";
  } else if ( npref == "36") {
		address = "徳島県";
  } else if ( npref == "37") {
		address = "香川県";
  } else if ( npref == "38") {
		address = "愛媛県";
  } else if ( npref == "39") {
		address = "高知県";
  } else if ( npref == "40") {
		address = "福岡県";
  } else if ( npref == "41") {
		address = "佐賀県";
  } else if ( npref == "42") {
		address = "長崎県";
  } else if ( npref == "43") {
		address = "熊本県";
  } else if ( npref == "44") {
		address = "大分県";
  } else if ( npref == "45") {
		address = "宮崎県";
  } else if ( npref == "46") {
		address = "鹿児島県";
  } else if ( npref == "47") {
		address = "沖縄県";
		nscl = 7;
	} else {
		address = "日本";
		nscl = 5;
	}
	geocoder = new GClientGeocoder();
	if (geocoder) {
		geocoder.getLatLng(address,
	    function(point) {
					map.setCenter(point, nscl);
			}
		);
	}
	GDownloadUrl("w.php?s=1", answer);
	loadIndexData(address);
}
function answer() {
	return true;
}
function selectSemi() {
	obj = document.area.pref;
	adr = obj.selectedIndex;
	var npref = obj.options[adr].value;

	GDownloadUrl("w.php?s=1", answer);
	loadIndexData();
}