//Utility functions for Google Map-based Calendar of Activities 

//Load Map
function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(37.98, -122.51), 9);
    map.setMapType(G_HYBRID_MAP);
    map.addControl(new GLargeMapControl);
    map.addControl(new GMapTypeControl);

    GEvent.addListener(map, "infowindowclose", function() {
       clearDetails();
    });
  }
  else {
    alert("We're sorry, but your browser is not compatible with Google Maps.");
  }
}

//Add Calendar item to map
function addCalendarPoint(map, latitude, logitude, briefDesc, itemNum) {
    var point = new GLatLng(latitude, logitude);
    var marker = new GMarker(point);
    //Div needed for IE to set width & font, so multi-activity text fits in infowindow, but
    //   use with single activity for consistent appearance
    var myHtml = '<div class="infowindow">' + wrapBriefDesc(briefDesc) + '<br /><a href="javascript:getDetails(' + itemNum + ')">Show Details (appears above)</a></div>';

    GEvent.addListener(marker, "click", function() {
      map.openInfoWindowHtml(point, myHtml);
    });
    map.addOverlay(marker);
}

//Wrap brief description of activity to multiple lines (if necessary: so that description is not so wide
//   that the "x" ("close" button) for the "InfoWindow" (i.e. "push pin balloon") is not visible.
function wrapBriefDesc(activityBriefDesc) {
    var maxLen = 60;  //Can be longer than for locations with multiple activities, since "Show Details ..." on 2nd line
   if (activityBriefDesc.length < maxLen) {
      return activityBriefDesc
   }
   else {
      //Find the location of the last space before the maximum length & initially cut the description there
      var endPos = activityBriefDesc.lastIndexOf(" ", maxLen);
      var wrappedText = activityBriefDesc.substring(0, endPos);
      var remainderText = activityBriefDesc.substring(endPos);
      while (endPos > 0) {
         //If the rest of the description is less than the maximum line length, add it as a new line
         if (remainderText.length < maxLen) {
            wrappedText = wrappedText + "<br />" + remainderText;
            break;
         }
         //Otherwise, cut what's left of the description at the last space before the maximum length, and keep looping
         else {
            endPos = remainderText.lastIndexOf(" ", maxLen);
            wrappedText = wrappedText + "<br />" + remainderText.substring(0, endPos);
            remainderText = remainderText.substring(endPos);
         }
      }
      return wrappedText 
   }   
}

//Show Activity Details
function getDetails(itemNum) {
   var detailHtml = '<b>Activity Details:</b><br />' + activityDetailHtml[itemNum];
   document.getElementById("top_bar").innerHTML = detailHtml;
}

//Clear Activity Details
function clearDetails() {
  document.getElementById("top_bar").innerHTML = "<b>Activity Details:</b><br />";
}

