/* 
 * Interface functions for the Google Maps API
 */

var geocoder;
var map;
var markers = [];
var gmapsAPIKey = 'ABQIAAAAsXCjHF8Ax-MxIENQCOM5SxSsnVyGhG8wRw6i49jNGhPUGaoNJBQXmkQoKahVBaIv6XLtdJh_jOaDuA';
/*var marker = new google.maps.Marker({
            map: null
    });
*/
var infowindow = new google.maps.InfoWindow();
;/* = new google.maps.InfoWindow({
        content: 'infoWindowContent'
    });*/

var defaultLat = 41.411466;
var defaultLong = -81.528103;

/**
 * Data for the markers consisting of a name, a LatLng, a zIndex for
 * the order in which these markers should display on top of each
 * other, and content for the infoWindow
 */
var communities = [
  ['Lamplight Inn of Maple Heights', defaultLat, defaultLong, 4, '5500 Northfield Road<br/>Maple Heights, OH 44137<br/>Phone: (216) 510-4336<br/><a href="./../our-communities/maple-heights-ohio.php" target="_blank">View Community Page</a><br/><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=5500+Northfield+Road,+Maple+Heights,+OH+44137+(Lamplight+Inn+of+Maple+Heights)&sll=41.411466,-81.528103&sspn=0.010444,0.018196&gl=us&ie=UTF8&hq=&hnear=5500+Northfield+Rd,+Maple+Heights,+Cuyahoga,+Ohio+44137&ll=41.410983,-81.528103&spn=0.010444,0.018196&t=h&z=16" target="_blank">Get Directions</a>'],
  ['Lamplight Inn of Fort Wayne', 41.077610, -85.134640, 5, '300 East Washington Blvd.<br/>Fort Wayne, IN 46802<br/>Phone: (260) 422-5511<br/><a href="./../our-communities/fort-wayne-indiana.php" target="_blank">View Community Page</a><br/><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=300+E+Washington+Blvd,+Fort+Wayne,+IN+46802+(Lamplight+Inn+of+Fort+Wayne)&sll=41.077912,-85.135996&sspn=0.010223,0.022724&ie=UTF8&hq=&hnear=300+E+Washington+Blvd,+Fort+Wayne,+Allen,+Indiana+46802&ll=41.077491,-85.135996&spn=0.010627,0.022724&t=h&z=16&iwloc=A" target="_blank">Get Directions</a>'],
  ['Lamplight Inn of Dayton', 39.64215, -84.22778, 6, '3797 Summit Glen Drive<br/>Dayton, OH 45449<br/>Phone: (937) 436-6155<br/><a href="./../our-communities/dayton.php" target="_blank">View Community Page</a><br/><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=3797+Summit+Glen+Drive+Dayton,+OH+45449+(Lamplight+Inn+of+Dayton)&sll=37.0625,-95.677068&sspn=45.688268,93.076172&ie=UTF8&hq=&hnear=3797+Summit+Glen+Dr,+Dayton,+Montgomery,+Ohio+45449&ll=39.642181,-84.2278&spn=0.043622,0.090895&z=14" target="_blank">Get Directions</a>']
];

var upcomingCommunities = [
  ['Lamplight Inn of Toronto', 40.47802, -80.60411, 4, '300 Belmont Street<br/>Toronto, OH 43964<br/><a href="./../our-communities/toronto-ohio.php" target="_blank">View Community Page</a>'],
  ['Lamplight Inn at the Leland', 39.82762, -84.88999, 5, '900 South A Street<br/>Richmond, IN 47374<br/><a href="./../our-communities/leland.php" target="_blank">View Community Page</a>']
];

var LampLightimage = new google.maps.MarkerImage('./../images/map_icon_lamp.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    new google.maps.Size(30, 33),
    // The origin for this image is 0,0.
    new google.maps.Point(0,0),
    // The anchor for this image is the base of the flagpole at 0,32.
    new google.maps.Point(15, 33));

var LampLightimageUpcoming = new google.maps.MarkerImage('./../images/map_icon_lamp_upcoming.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    new google.maps.Size(30, 33),
    // The origin for this image is 0,0.
    new google.maps.Point(0,0),
    // The anchor for this image is the base of the flagpole at 0,32.
    new google.maps.Point(15, 33));
    
var LampLightshadow = new google.maps.MarkerImage('./../images/map_icon_lamp_shadow.png',
    // The shadow image is larger in the horizontal dimension
    // while the position and offset are the same as for the main image.
    new google.maps.Size(46, 21),
    new google.maps.Point(0,0),
    new google.maps.Point(9, 20));



function contactMap() {
  /*
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(defaultLat, defaultLong);
  var myOptions = {
    zoom: 5,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("contact_map_canvas"), myOptions);
  */

 /* temporarily use communities_map code */
 geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(defaultLat, defaultLong);
  var myOptions = {
    zoom: 5,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("contact_map_canvas"), myOptions);

  // set markers for current communities
  for (var i = 0; i < communities.length; i++) {
    setMarkers(map, communities[i], LampLightimage);
  }

  // set markers for upcoming communities
  for (var i = 0; i < upcomingCommunities.length; i++) {
    setMarkers(map, upcomingCommunities[i], LampLightimageUpcoming);
  }
}

function moveToMarkerLampLight(addressName) {
  
  var lc_addressName = addressName.toLowerCase();
  var this_markerTitle = '';
  var temp_address = '';
  var infoWindowContent = '';
  var c;

  // check current communities address for city, state
  for (c in communities) {
    temp_address = communities[c][4].toLowerCase();

    if (temp_address.search(lc_addressName) != -1) {
      this_markerTitle = communities[c][0];
      infoWindowContent = '<strong>'+ communities[c][0] +'</strong><br/>' + communities[c][4];
      break;
    }
  }
  
  // check current upcoming communities address for city, state
  if (this_markerTitle == '') {
    for (c in upcomingCommunities) {
      temp_address = upcomingCommunities[c][4].toLowerCase();

      if (temp_address.search(lc_addressName) != -1) {
        this_markerTitle = upcomingCommunities[c][0];
        infoWindowContent = '<strong>'+ upcomingCommunities[c][0] +'</strong><br/>' + upcomingCommunities[c][4];
        break;
      }
    }
  }

  // if marker found zoom to it
  if (this_markerTitle != '') {
    for (m in markers) {
      if (markers[m].getTitle() == this_markerTitle) {
         map.setCenter(markers[m].getPosition());
         map.setZoom(8);
         
         infowindow.setContent(infoWindowContent);
         infowindow.open(map, markers[m]);
         break;
      }
    }
  }
}

function communitiesMap() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(defaultLat, defaultLong);
  var myOptions = {
    zoom: 5,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("communities_map_canvas"), myOptions);

  // set markers for current communities
  for (var i = 0; i < communities.length; i++) {
    setMarkers(map, communities[i], LampLightimage);
  }

  // set markers for upcoming communities
  for (var i = 0; i < upcomingCommunities.length; i++) {
    setMarkers(map, upcomingCommunities[i], LampLightimageUpcoming);
  }

}

function setMarkers(map, community, icon) {
  // Add markers to the map
  //for (var i = 0; i < locations.length; i++) {
    //var community = locations[i];
    var myLatLng = new google.maps.LatLng(community[1], community[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        shadow: LampLightshadow,
        icon: icon,
        //shape: shape,
        title: community[0],
        zIndex: community[3]
    });

    var infoWindowContent = '<strong>'+ community[0] +'</strong><br/>' + community[4];

    /*var infowindow = new google.maps.InfoWindow({
        content: infoWindowContent
    });*/


    google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent(infoWindowContent);
      infowindow.open(map,marker);
    });

    markers.push(marker);
  //}
}
