// JavaScript Document

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

//var startCate =0; // 設定でスタート時のカテゴリー指定

window.onload = function() {

  // wheelmouse
  wheelmouseInit();

  // GoogleMap
  map = new GMap2(document.getElementById("gmap"));
  map.setCenter(new GLatLng(posY, posX), 10);
  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", "mapdata.xml?cache="+msec);
  httpObj.onreadystatechange = function() {
    if((httpObj.readyState == 4) && (httpObj.status == 200)) {
      xmlDataAction(1);
    }
  }
  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("addr")[0].firstChild;
    if(wrking) { tmpValue = wrking.nodeValue; } else { tmpValue = ""; }
    maddr = 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>" + mname + "</div>";
        innerHtml += "<div>" + maddr + "</div>";
        innerHtml += "<div>email : <a href='mailto:" + memail + "'>" + memail + "</a></div>";
        innerHtml += "<div>TEL/FAX : " + mtel + "</div>";
        //innerHtml += "<div>URL : <a href='" + murl + "' target='_blank'>viewsite</a></div>";
        addMarkerNormal(new GLatLng(my, mx), innerHtml, mname);
      }
      if(mcate == 9){ // 対象カテゴリ表示
        innerHtml = "<div>" + mname + "</div>";
        innerHtml += "<div>" + maddr + "</div>";
        addMarkerNormal(new GLatLng(my, mx), innerHtml, mname);
      }
    }
    if(cate == 0 ) {
      map.setCenter(new GLatLng(posY, posX), 18);
    } else if(cate == mcate) {
      map.setCenter(new GLatLng(my, mx), 18);
    }

  } // 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);
  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 ) );
}


/* ホイール処理 (Full画面用) */
/*
function wheelmouse(event){
  var delta = 0;
  if (!event) event = window.event;
  if (event.wheelDelta) {
    delta = event.wheelDelta / 120; 
    if (window.opera) { delta = -delta; }
  } else if (event.detail) {
    delta = -event.detail / 3;
  }
  if (delta) { map.setZoom(map.getZoom() + ( delta < 0 ? -1 : 1 ) ); }
}

if (window.addEventListener) {
  window.addEventListener('DOMMouseScroll', wheelmouse, false);
}
window.onmousewheel = document.onmousewheel = wheelmouse;
*/
