﻿//********************* K'S Google Map API Framework ***************
//*********************copy rights (c) T.Karino 2009  **************
//　googleのキー、ライブラリを使用します。　無断転載禁止です。
//       (1)2009.4.26  (4)2009.5.25
//*********************copy rights (c) T.Karino 2009  **************
  var map;
  var listArea;
  var target="";
  var clustered=false;
  var mgr;
  var mcOptions;
  var mc;
  var listEnabled=true;
  var mapControl=new GLargeMapControl();
  var overviewControl=new GOverviewMapControl();
  var scaleControl= new GScaleControl();
  var typeControl= new GMapTypeControl();
  var lineCount;
  function initialize()
  {
        if (GBrowserIsCompatible()) {
        
          // Create and Center a Map
          map = new GMap2(document.getElementById("map_canvas"));
          map.addMapType(G_PHYSICAL_MAP );  //地形図表示が不要ならいらない
          listArea=document.getElementById("list_area");
          mcOptions = {gridSize: 50, maxZoom: 15};
          map.addControl(typeControl);
          map.addControl(mapControl);
          map.addControl(overviewControl);
          map.addControl(scaleControl);
          initialsetting();
          if(clustered){
            mc = new MarkerClusterer(map, GetMarkers(0),mcOptions);
          }
          else{
            mgr = new MarkerManager(map);
            for(var i=5;i<15;i++){
              mgr.addMarkers(GetMarkers(i),i);
            }
            mgr.refresh();
          }
          if(listEnabled){
            GEvent.addListener(map, 'moveend',function(){WriteList();});
            GEvent.addListener(map, 'zoomend',function(){WriteList();});
            WriteList();
          }
          else{
            GEvent.addListener(map, 'moveend',function(){WriteCenter();});
            GEvent.addListener(map, 'zoomend',function(){WriteCenter();});
            WriteCenter();
          }
        }
        else
        {
          alert("Google Map API(GMAP2)がサポートしていないブラウザのため表示できません。");
        }
  }
  function WriteList()
  {
     var bounds = map.getBounds();
     var zoom = map.getZoom();
     var southWest = bounds.getSouthWest();
     var northEast = bounds.getNorthEast();
     var str="";
     lineCount=0;
     for(var i=0;i<Places.length;i++){
        if(Places[i].lat>southWest.lat() && Places[i].lat<northEast.lat() && Places[i].lng>southWest.lng() && Places[i].lng<northEast.lng() && (clustered || zoom>=Places[i].zoom)){
          str=str+getLine(i);
        }
     }
     listArea.innerHTML =str+"<br>地図内　合計"+lineCount+"件";
     WriteCenter();
  }

  function WriteCenter()
  {
     var center = map.getCenter();
     document.getElementById("map_center").innerHTML = "地図の中心は&nbsp;&nbsp;北緯&nbsp;"+center.lat()+"&nbsp;&nbsp;東経&nbsp;"+center.lng()+
         "&nbsp;&nbsp;Zoom&nbsp;"+map.getZoom();
     resetInfoWindow();
  }

  function getLine(i)
  {
      var str1="";
      if(Places[i].picon==1){str1 = str1 + "<img src='http://chart.apis.google.com/chart?cht=mm&chs=16x16&chco=ffffff,ff0000,000000&ext=.png'>"; }
      if(Places[i].picon==2){str1 = str1 + "<img src='http://chart.apis.google.com/chart?cht=mm&chs=16x16&chco=ffffff,00CCCC,000000&ext=.png'>"; }
      if(Places[i].picon==3){str1 = str1 + "<img src='http://chart.apis.google.com/chart?cht=mm&chs=16x16&chco=ffffff,000099,000000&ext=.png'>"; }
      if(Places[i].picon==4){str1 = str1 + "<img src='http://chart.apis.google.com/chart?cht=mm&chs=16x16&chco=ffffff,ffcc00,000000&ext=.png'>"; }
      if(Places[i].picon==5){str1 = str1 + "<img src='http://chart.apis.google.com/chart?cht=mm&chs=16x16&chco=ffffff,00ffff,000000&ext=.png'>"; }
      if(Places[i].picon==6){str1 = str1 + "<img src='http://chart.apis.google.com/chart?cht=mm&chs=16x16&chco=ffffff,ff00ff,000000&ext=.png'>"; }
      if(Places[i].picon==7){str1 = str1 + "<img src='http://chart.apis.google.com/chart?cht=mm&chs=16x16&chco=ffffff,a0a0a0,000000&ext=.png'>"; }
//      str1 = str1 + "<a href='javascript:setCenter("+ Places[i].lat + ","+ Places[i].lng+ ")'>" + 
      str1 = str1 + "<a title='"+Places[i].comment+"の位置を確認する' href=\"javascript:focusOnPlace("+ i + ","+ (Number(Places[i].zoom)+2)+ ")\">" + 
         Places[i].name+"</a>&nbsp;&nbsp;"+"<a href=\""+Places[i].URL;
      if(Places[i].URL.substr(0,4)=="java")
      {
          str1 = str1+"\">操作</a><br>";
      }
      else
      {
          if(!(target==""))
          {  str1 = str1+ "\" target=\""+target ;  }
          str1 = str1+"\">"+Places[i].comment+"の360°くるくる写真</a>を見る<br>";
      }
      lineCount++;
      return str1;
  }

  function searchAndList(text)
  {
     lineCount=0;
     var str="「"+text+"」を含む場所検索結果<br>";
     for(var i=0;i<Places.length;i++){
        if(Places[i].name.indexOf(text)>=0){
          str=str+getLine(i);
        }
     }
     listArea.innerHTML =str+"<br>"+lineCount+"件　検索終了";
  }

  function searchByIconAndList(icon)
  {
     var str="Iconでの検索結果<br>";
     lineCount=0;
     for(var i=0;i<Places.length;i++){
        if(Places[i].picon==icon){
          str=str+getLine(i);
        }
     }
     listArea.innerHTML =str+"<br>"+lineCount+"件　検索終了";
  }

  function GetMarkers(zoom)
  {
     var Markers = [];
     for(var i=0;i<Places.length;i++){
        if(zoom==0 || zoom==Places[i].zoom){
           Markers.push(createMarker(i));
        }
     }
     return Markers;
  }

  function createMarker(number) {
    var newIcon = new GIcon(G_DEFAULT_ICON);
    if(Places[number].picon==1){newIcon.image = "http://chart.apis.google.com/chart?cht=mm&chs=32x64&chco=ffffff,ff0000,000000&ext=.png"; }
    if(Places[number].picon==2){newIcon.image = "http://chart.apis.google.com/chart?cht=mm&chs=32x64&chco=ffffff,00CCCC,000000&ext=.png"; }
    if(Places[number].picon==3){newIcon.image = "http://chart.apis.google.com/chart?cht=mm&chs=32x64&chco=ffffff,000099,000000&ext=.png"; }
    if(Places[number].picon==4){newIcon.image = "http://chart.apis.google.com/chart?cht=mm&chs=32x64&chco=ffffff,ffcc00,000000&ext=.png"; }
    if(Places[number].picon==5){newIcon.image = "http://chart.apis.google.com/chart?cht=mm&chs=32x64&chco=ffffff,00ffff,000000&ext=.png"; }
    if(Places[number].picon==6){newIcon.image = "http://chart.apis.google.com/chart?cht=mm&chs=32x64&chco=ffffff,ff00ff,000000&ext=.png"; }
    if(Places[number].picon==7){newIcon.image = "http://chart.apis.google.com/chart?cht=mm&chs=32x64&chco=ffffff,a0a0a0,000000&ext=.png"; }
//    var newIcon= MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#ff0000"});  
//    if(Places[number].picon==2){newIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#00ff00"});}
//    if(Places[number].picon==3){newIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#0000ff"});}
//    if(Places[number].picon==4){newIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#ffff00"});}
//    if(Places[number].picon==5){newIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#00ffff"});}
//    if(Places[number].picon==6){newIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#ff00ff"});}
    var marker = new GMarker(new GLatLng(Places[number].lat,Places[number].lng), {icon: newIcon,title:Places[number].name});
    GEvent.addListener(marker, "click", function() {
      var myHtml =  "<a href='"+Places[number].URL;
      if((!(Places[number].URL.substr(0,4)=="java"))&&( !(target=="")))
      {  myHtml = myHtml+ "' target='"+target ;  }
      myHtml = myHtml+ "'>"+Places[number].name + "</a><br><br><a href='"+Places[number].URL+"'>"+Places[number].comment+"&nbsp;の360°くるくる写真</a><br>を見る";
      map.openInfoWindowHtml(new GLatLng(Places[number].lat,Places[number].lng), myHtml);
    });
    return marker;
  }

  function setCenter(lat,lng)
  {
     map.setCenter(new GLatLng(lat,lng));
  }

  function setZoom(level)
  {
     map.setZoom(level);
  }

  function getZoom()
  {
     return map.getZoom();
  }

  function zoomOut()
  {
     map.zoomOut();
  }

  function zoomIn()
  {
     map.zoomIn();
  }

  function resetInfoWindow(){
     map.disableInfoWindow();
     map.enableInfoWindow();
  }

  function focusOnPlace(id,z)
  {
     map.setCenter(new GLatLng(Places[id].lat,Places[id].lng));
     map.setZoom(z);
  }

  function getNameOfPlace(id)
  {
     return Places[id].name;
  }

  function getURLOfPlace(id)
  {
     return Places[id].URL;
  }

  function getCommentOfPlace(id)
  {
     return Places[id].comment;
  }

  function getLatOfPlace(id)
  {
     return Places[id].lat;
  }

  function getLngOfPlace(id)
  {
     return Places[id].lng;
  }

  function getIconOfPlace(id)
  {
     return Places[id].icon;
  }
  function getZoomOfPlace(id)
  {
     return Places[id].zoom;
  }

  function Place(lat,lng,name,URL,comment,picon,zoom)
  {
     this.lat=lat;
     this.lng=lng;
     this.name=name;
     this.URL=URL;
     this.comment=comment;
     this.picon=picon;
     this.zoom=zoom
  }

