window.onload = function() {
  var DIVs = document.getElementsByTagName('div');
  var MapCount = 0;
  for (var Count = 0; Count < DIVs.length; Count ++) {
    if (DIVs[Count].id.match(/^aagmap\d*$/)) {
      var Info = document.getElementById(DIVs[Count].id).innerHTML.split("|");
      Info[0] = Info[0].replace(/^\s*/, "");
      CreateMap(DIVs[Count].id, Info);
    }
  }
}

function CreateMap(GmapID, Info) {
  if (Info[1]) {
    var Lat = Info[1].replace(/(\d+\.\d+),\d+\.\d+/, "$1");
    var Lng = Info[1].replace(/\d+\.\d+,(\d+\.\d+)/, "$1");
    ViewMap(GmapID, new google.maps.LatLng(Lat, Lng), Info);
  } else {
    var Address = Info[0];
    Address = Address.replace(/[\s　].+/, "");
    var Geocoder = new google.maps.Geocoder();
    Geocoder.geocode(
      {
        address: Address
      },
      function(Results, Status) {
        if (Status == google.maps.GeocoderStatus.OK) {
          ViewMap(GmapID, Results[0].geometry.location, Info);
        } else {
          document.getElementById(GmapID).innerHTML = '現在、システムが混みあっています。';
          document.getElementById(GmapID).style.visibility = "visible";
        }
      }
    );
  }
}

function ViewMap(GmapID, TargetLatLng, Info) {
  var ViewInfo = Info[2] || Info[0];
  var ZoomLevel = parseInt(Info[3]);
  ZoomLevel = isNaN(ZoomLevel) ? 17 : ZoomLevel;

  var Map = new google.maps.Map(
    document.getElementById(GmapID),
    {
      zoom: ZoomLevel,
      center: TargetLatLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
  );

  var Marker = new google.maps.Marker(
    {
      map: Map,
      position: TargetLatLng
    }
  );

  var InfoWindow = new google.maps.InfoWindow(
    {
      content: ViewInfo
    }
  );

  google.maps.event.addListener(Marker, 'click', function() {
    InfoWindow.open(Map, Marker);
  })

  if (Info[4]) {
    var StartLatLng;
    if (Info[4].match(/\d+\.\d+,\d+\.\d+/)) {
      var Lat = Info[4].replace(/(\d+\.\d+),\d+\.\d+/, "$1");
      var Lng = Info[4].replace(/\d+\.\d+,(\d+\.\d+)/, "$1");
      ViewRoute(Map, new google.maps.LatLng(Lat, Lng), TargetLatLng);
    } else {
      var Address = Info[4];
      Address = Address.replace(/[\s　].+/, "");
      var Geocoder = new google.maps.Geocoder();
      Geocoder.geocode(
        {
          address: Address
        },
        function(Results, Status) {
          if (Status == google.maps.GeocoderStatus.OK) {
            ViewRoute(Map, Results[0].geometry.location, TargetLatLng);
          } else {
            document.getElementById(GmapID).innerHTML = '現在、システムが混みあっています。';
            document.getElementById(GmapID).style.visibility = "visible";
          }
        }
      );
    }
  }

  document.getElementById(GmapID).style.visibility = "visible";

  InfoWindow.open(Map, Marker);
}

function ViewRoute(Map, StartLatLng, TargetLatLng) {
  var DirectionsService = new google.maps.DirectionsService();
  DirectionsService.route(
    {
      origin: StartLatLng,
      destination: TargetLatLng,
      travelMode: google.maps.DirectionsTravelMode.WALKING
    },
    function(Result, Status) {
      if (Status == google.maps.DirectionsStatus.OK) {
        DirectionsDisplay.setDirections(Result);
      }
    }
  );

  var DirectionsDisplay = new google.maps.DirectionsRenderer();
  DirectionsDisplay.setMap(Map);
}

function swapimg(obj) {
  if(obj.src.match(/_f2.(jpg|gif|png)/)) {
    obj.src = obj.src.replace('_f2', '');
  } else {
    obj.src = obj.src.replace(/\.(jpg|gif|png)$/, "_f2.$1");
  }
}

