// JavaScript Document

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

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

window.onload = function() {

  // wheelmouse
  wheelmouseInit();

  // GoogleMap
  map = new GMap2(document.getElementById("gmap"));
  map.setCenter(new GLatLng(posY, posX), 12);
  map.addControl(new GMapTypeControl());
  map.addControl(new GLargeMapControl());
  map.addControl(new GOverviewMapControl(new GSize(160,120)));
  map.addControl(new GScaleControl());
  addPhoto(137.009811, 36.79539, 137.180443, 36.643631, "../img/gmap_area_img.gif");

  /* データ取得 */
  var msec = (new Date()).getTime();
  httpObj = GXmlHttp.create();
  httpObj.open("get", "../cmn/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>" + mname + "</div>";
        innerHtml += "<div>〒" + mpostal + "</div>";
        innerHtml += "<div>" + maddr + "</div>";
        innerHtml += "<div>" + maddr2 + "</div>";
        innerHtml += "<div>TEL : " + mtel + "</div>";
        innerHtml += "<div>URL : <a href='" + murl + "' target='_blank'>ホームページへ</a></div>";
        addMarkerNormal(new GLatLng(my, mx), innerHtml, mname);
      }
    }
    if(cate == 0 ) {
      map.setCenter(new GLatLng(posY, posX), 12);
    } 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);
  });
}

function addPhoto(x1,y1,x2,y2, imgURL) {
	var photo = new TPhoto();
	photo.id = "AreaImg";
	photo.src = imgURL;
	photo.percentOpacity = 70;
	photo.anchorTopLeft = new GLatLng(y1, x1);
	photo.anchorBottomRight = new GLatLng(y2, x2);
	map.addTPhoto(photo);
}


/* リーク対策 */
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 ) );
}
