var map = null;
var xml_url = "marker.php";
var MyIcon = new Array();
var ar_icongif = new Array(
	"",
	"../images/icon_map_019.gif",
	"../images/icon_map_011.gif",
	"../images/icon_map_012.gif",
	"../images/icon_map_013.gif",
	"../images/icon_map_014.gif",
	"../images/icon_map_015.gif",
	"../images/icon_map_016.gif",
	"../images/icon_map_017.gif",
	"../images/icon_map_018.gif",
	"../images/icon_map_020.gif",
	"");

function init(){
	for(i=1;i<=10;i++){
	MyIcon[i] = new GIcon();
	str_i = String(i);
	if(str_i.length < 2){
		str_i = "0" + i;
	}
	MyIcon[i].image = "./image_map/" + str_i + ".png";
	MyIcon[i].shadow = "./image_map/shadow.png";
	MyIcon[i].iconSize = new GSize(33, 41);
	MyIcon[i].shadowSize = new GSize(48, 41);
	MyIcon[i].iconAnchor = new GPoint(17, 41);
	}
	
	loadGMap();
}

    function loadGMap() {
      if (GBrowserIsCompatible()) {
        // マップの生成
        var Hybrid_j = new GMapType(G_HYBRID_MAP.getTileLayers(),
        G_HYBRID_MAP.getProjection(), "地図+写真");
        map = new GMap2(document.getElementById("mapContent"),{
          size : new GSize(578, 403)
        });
        map.getMapTypes().length = 0;
        map.addMapType(G_NORMAL_MAP);
        map.addMapType(G_SATELLITE_MAP);
        map.addMapType(Hybrid_j);
        map.addControl(new GLargeMapControl(),
          new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0)));
        map.addControl(new GMapTypeControl());
        
        map.setCenter(new GLatLng(35.622751, 138.623536), 9, G_NORMAL_MAP);
        var kh = new GKeyboardHandler(map);
        
      }
    }

    function setMapData(ar_cate, str_area, str_span, kid){
        // マーカーの表示
        map.clearOverlays();
		var request = GXmlHttp.create();
		document.getElementById('mapCount').innerHTML = "検索中";
		document.getElementById('mapNews').innerHTML = "読み込み中です…";
		
		str_cate = ar_cate.join("_");
		get_url = xml_url + "?str_cate=" + str_cate + 
							"&str_area=" + str_area + 
							"&str_span=" + str_span;
		if(kid > 0){ get_url += "&kid=" + kid; }
		request.open("GET", get_url, true);

		request.onreadystatechange = function(){
			if(request.readyState == 4){
				document.getElementById('mapNews').innerHTML = "";
				
				function showInfo(){
					var info_htm = '';
					info_htm += "<div style='font-size:12px;'>";
					if(this.fnew > 0){
						info_htm += ' <img src="../images/icon_common_003.gif" border="0" width="28" height="9" style="vertical-align:top;margin-bottom:2px;">';
						info_htm += '<br />';
					}
					if(this.title != ""){
						info_htm += '<a href="' + this.l_ul + '" target="_blank">' + this.title + '</a><br />';
					}
					if(this.i_ul != ""){
						info_htm += '<img src="' + this.i_ul + '" /><br />';
					}
					info_htm += '<br />';
					info_htm += this.message + '<br />';
					info_htm += '<br />';
					if(this.dt != ""){
						info_htm += '■' + this.dt;
					}
					info_htm += '<br />';
					
					if(this.author != ""){
						info_htm += '■投稿:' + this.author + '<br />';
					}
					if(this.aname){
						info_htm += '■地域:' + this.aname + '<br />';
					}

					
					info_htm += '<br />';
					info_htm += "</div>";
					map.openInfoWindowHtml(new GLatLng(parseFloat(this.lat), parseFloat(this.lng)), info_htm, { maxWidth : 150});
				}

				var response = request.responseXML;
				var xmlDat = response.documentElement;
				var bmarkers = xmlDat.getElementsByTagName("blog_marker");
				var btitles = xmlDat.getElementsByTagName("blog_title");
				var bmessages = xmlDat.getElementsByTagName("blog_message");
				var bimages = xmlDat.getElementsByTagName("blog_image");
				var blinks = xmlDat.getElementsByTagName("blog_link");
				var bcategories = xmlDat.getElementsByTagName("blog_category");
				var bdates = xmlDat.getElementsByTagName("blog_date");
				var bauthors = xmlDat.getElementsByTagName("blog_author");
				var baname = xmlDat.getElementsByTagName("blog_area_name");
				var bnew = xmlDat.getElementsByTagName("blog_new");
				
				for(var i=0;i<bmarkers.length;i++){
					// アイコンの追加
					var cate = bcategories[i].firstChild.nodeValue;
					var flg_cate = 0;
					if(ar_cate.length == 0 || ar_cate.length == 8){
						flg_cate = 1;
					}else{
						for(j=0;j<ar_cate.length;j++){
							if(ar_cate[j] == cate){
								flg_cate = 1;
								break;
							}
						}
					}
					if(flg_cate){
					
						icon1 = MyIcon[cate];
					
						var lat = parseFloat(bmarkers[i].getAttribute("lat"));
						var lng = parseFloat(bmarkers[i].getAttribute("lng"));
					
						var bmarker = new GMarker(new GPoint(lng, lat), {icon:icon1});
						bmarker.title = btitles[i].firstChild.nodeValue;
						
						bmarker.message = bmessages[i].firstChild.nodeValue;
						bmarker.i_ul = bimages[i].getAttribute("ul");
						bmarker.l_ul = blinks[i].getAttribute("ul");
						
						bmarker.lat = lat;
						bmarker.lng = lng;
						if(bauthors[i].firstChild != null){
							bmarker.author = bauthors[i].firstChild.nodeValue;
						}else{
							bmarker.author = "";
						}
						bmarker.dt = bdates[i].firstChild.nodeValue;
						bmarker.fnew = bnew[i].firstChild.nodeValue;
						bmarker.aname = baname[i].firstChild.nodeValue;
					
						map.addOverlay(bmarker);
						GEvent.bind(bmarker,"click", bmarker, showInfo);
					
						// 新着情報の追加
						if(i < 20){
							var htm = '<li>';
							htm += '<div class="img"><img src="' + ar_icongif[cate] + '" alt=""></div>';
							htm += '<div class="txt">';
							htm += '<h4><a href="' + blinks[i].getAttribute("ul") + '">' + btitles[i].firstChild.nodeValue + '</a></h4>';
							htm += '<p>' + bmessages[i].firstChild.nodeValue + '</p>';
							htm += '<dl class="clearfix">';
							
							if(bnew[i].firstChild.nodeValue == 1){
								new_text = ' <img src="../images/icon_common_003.gif" border="0" width="28" height="9" />';
							}else{
								new_text = "";
							}
							htm += '<dt>■</dt><dd>' + bdates[i].firstChild.nodeValue + new_text + '</dd>';
							
							if(bauthors[i].firstChild != null){
								htm += '<dt>■ 投稿：</dt><dd>' + bauthors[i].firstChild.nodeValue + '</dd>';
							}
							if(baname[i].firstChild != null){
								htm += '<dt>■ 地域：</dt><dd>' + baname[i].firstChild.nodeValue + '</dd>';
							}
							
							htm += '</dl><!-- /#txt --></div></li>';
							document.getElementById('mapNews').innerHTML += htm;
							
						}
						
					}
				}
				document.getElementById('mapNews').innerHTML = 
					'<ul>' + document.getElementById('mapNews').innerHTML + '</ul>';
				document.getElementById('mapCount').innerHTML = "全" + bmarkers.length + "件";
			}
		}
		request.send(null);
	}
