// JavaScript Document

// デフォルト中心座標
var posY = 36.6887; // 経度
var posX = 137.252836; // 緯度

window.onload = function() {

  // wheelmouse
  wheelmouseInit();

  // GoogleMap
  map = new GMap2(document.getElementById("gmap"));
  map.setCenter(new GLatLng(posY, posX), 19);
  map.addControl(new GMapTypeControl());
  map.addControl(new GLargeMapControl());
  map.addControl(new GOverviewMapControl(new GSize(160,120)));
  map.addControl(new GScaleControl());

  /* データ取得 */
  var msec = (new Date()).getTime();
  httpObj = GXmlHttp.create();
  httpObj.open("get", "js/mapdata.xml?cache="+msec);
  httpObj.onreadystatechange = function() {
    if((httpObj.readyState == 4) && (httpObj.status == 200)) {
      xmlDataAction(0);
    }
  }
  httpObj.send(null);

}


// データ処理
function xmlDataAction(cate) {
  var xmlData = httpObj.responseXML;
  var pointData = xmlData.getElementsByTagName("point");

  //alert(pointData.length);
  map.clearOverlays();

  for ( var i=0; i<pointData.length; i++) {

    var point = pointData[i];

    wrking = point.getElementsByTagName("id")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    mid = tmpValue;

    wrking = point.getElementsByTagName("x")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = 0; }
    mx = tmpValue;

    wrking = point.getElementsByTagName("y")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = 0; }
    my = tmpValue;

    wrking = point.getElementsByTagName("name")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    mname = tmpValue;

    wrking = point.getElementsByTagName("cate")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    mcate = tmpValue;

    wrking = point.getElementsByTagName("postal")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    mpostal = tmpValue;

    wrking = point.getElementsByTagName("addr")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    maddr = tmpValue;

    wrking = point.getElementsByTagName("addr2")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    maddr2 = tmpValue;

    wrking = point.getElementsByTagName("email")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    memail = tmpValue;

    wrking = point.getElementsByTagName("tel")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    mtel = tmpValue;

    wrking = point.getElementsByTagName("url")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    murl = tmpValue;

    if(mx != 0 && my != 0) {
      if(cate == 0 || cate == mcate){ // 対象カテゴリ表示
        //innerHtml = "<div><div><a href='shop" + mid + ".html'><img src='img/shop" + mid + "_icon.jpg' width='85' height='59' alt='" + mname + "' /></a></div>";
        innerHtml = "<div class='clrBlack'>" + mname + "</div>";
        innerHtml += "<div class='clrBlack'>〒" + mpostal + "</div>";
        innerHtml += "<div class='clrBlack'>" + maddr + "</div>";
        innerHtml += "<div class='clrBlack'>" + maddr2 + "</div>";
        innerHtml += "<div class='clrBlack'>TEL : " + mtel + "</div>";
        innerHtml += "<div class='clrBlack'>email : <a href='mailto:" + memail + "'>" + memail + "</a></div>";
        innerHtml += "<div class='clrBlack'>URL : <a href='" + murl + "' target='_blank'>ホームページへ</a></div>";
        addMarkerNormal(new GLatLng(my, mx), innerHtml, mname);
      }
    }
    if(cate == 0 ) {
      map.setCenter(new GLatLng(posY, posX), 19);
    } else if(cate == mcate) {
      map.setCenter(new GLatLng(my, mx), 19);
    }

  } // end of for i

}

// マーカー処理
function addMarker(point, msg, name) {

  var compIcon = new GIcon();
  compIcon.image = "/map/img/gmap_icon.gif";
  compIcon.iconSize = new GSize(31, 31);
  compIcon.shadowSize = new GSize(50, 50);	
  compIcon.iconAnchor = new GPoint(15, 15);
  compIcon.infoWindowAnchor = new GPoint(15, 15);
  compIcon.infoShadowAnchor = new GPoint(0, 0);

  var marker = new GMarker(point, {title:name, icon:compIcon});
  map.addOverlay(marker);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(msg);
  });
}

// マーカー処理
function addMarkerNormal(point, msg, name) {
  var marker = new GMarker(point, {title:name});
  map.addOverlay(marker);
  //marker.openInfoWindowHtml(msg);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(msg);
  });
}

/* リーク対策 */
window.onunload = GUnload;

/* ホイール処理 */
function wheelmouseInit(){
  if( navigator.userAgent.match( "MSIE"   ) ){ document.getElementById( "gmap" ).attachEvent( "onmousewheel" , mouseWheelZooming ); }
  if( navigator.userAgent.match( "Gecko"  ) ){ document.getElementById( "gmap" ).addEventListener( "DOMMouseScroll" , mouseWheelZooming , false ); }
  if( navigator.userAgent.match( "Safari" ) ){ document.getElementById( "gmap" ).onmousewheel = mouseWheelZooming; }
}

function mouseWheelZooming( event ){
  //マウスホイールの上／下の取得と、スクロールのキャンセル
  if( navigator.userAgent.match( "MSIE"   ) ){ var delta = event.wheelDelta;   event.returnValue = false; }	//IE
  if( navigator.userAgent.match( "Gecko"  ) ){ var delta = event.detail * -1;  event.preventDefault();    }	//Gecko
  if( navigator.userAgent.match( "Safari" ) ){ var delta = event.wheelDelta;   event.returnValue = false; }	//Safari
  map.setZoom(map.getZoom() + ( delta < 0 ? -1 : 1 ) );
}

