//Utility functions for Google Map-based Calendar of Activities, where multiple Activity types can be selected

//Update map with activities meeting criteria
function getActivities() {
  map.clearOverlays();
  var locationMap = new Object();
  var locKey = new String();
  
  //Create Associative Array of locations & activities at those locations
  for (actNum = 0; actNum < activityBeginDate.length; actNum++) {
    if ((activityBeginDate[actNum] >= startDate) && (activityBeginDate[actNum]<= endDate) && (selectedTypes[activityType[actNum]] == 1)) {
      locKey = activityLat[actNum] + "," + activityLng[actNum];
      locationMap[locKey] = ((locationMap[locKey] == undefined) ? "" : locationMap[locKey]) +  ";" + actNum;
    }
  }

  //Loop through Associative Array of locations & determine how will display if location "push pin" clicked
  //  1) if > 1 activity at that location, display list of activities
  //  2) if only one activity at that location, just display it
  for (var locIdx in locationMap) {
    //";" used as delimiter for list of activities; since starts with ";", if *ANY* delimiter after 1st position implies more than 1 activity
    if (locationMap[locIdx].indexOf(";", 1) > 0) {
      //  1) > 1 activity at that location, so display list of activities
      addCalendarMultiPoint(map, locIdx, locationMap[locIdx])
    }
    else {
      //  2) only one activity at that location, so just display it
      actNum = locationMap[locIdx].substr(1);
      addCalendarPoint(map, activityLat[actNum], activityLng[actNum], activityBriefDesc[actNum], actNum);
    }
  }
}

//Add location with multiple Calendar items to map: InfoWindow (i.e. "push pin" "balloon") will have list
//   of the Calendar items; user clicks on link for specific item to see detal.
function addCalendarMultiPoint(map, locIdx, locList) {
    var latLng = new Array();
    //Key for Associative Array was latitude & longitude, with "," as delimiter
    latLng = locIdx.split(",");
    var point = new GLatLng(latLng[0], latLng[1]);
    var marker = new GMarker(point);

    //Create array of activity IDs & then loop through, adding to text for InfoWindow (i.e. "push pin" "balloon")
    var locArray = new Array();
    locArray = locList.split(";");
    var myHtml = new String('<div class="infowindow">'); //Div needed for IE to set width & font, so multi-activity text fits in infowindow
    var actNum;
    //Start at 2nd element in Array, since 1st is empty (list of activity IDs started with ";" delimiter)
    for (actMultiNum = 1; actMultiNum < locArray.length; actMultiNum++) {
       actNum = locArray[actMultiNum];
       myHtml += '<br />' + shortActBriefDesc(activityBriefDesc[actNum]) + ' <a href="javascript:getDetails(' + actNum + ')">Show Details (appears above)</a>';
    }

    myHtml += '</div>'
    GEvent.addListener(marker, "click", function() {
        map.openInfoWindowHtml(point, myHtml);
    });
    map.addOverlay(marker);
}

//Shorten brief description of activity (if necessary): used for location with multiple Calendar items
function shortActBriefDesc(activityBriefDesc) {
   var maxLen = 45;
   if (activityBriefDesc.length < maxLen) {
      return activityBriefDesc
   }
   else {
      //Find the location of the last space before the maximum length
      var endPos = activityBriefDesc.lastIndexOf(" ", maxLen);
      //Return the text before the last space (adding " ...", to show some text was eliminated)
      return activityBriefDesc.substring(0, endPos) + " ...";
   }   
}

//Submit form:
//   1) Derive activity selection variables from input
//   2) Validate input
//   3) Update map with activities meeting criteria (if valid input)
function submitForm() {
  //1) Derive activity selection variables from input
  setUpVariablesForFormValidation();
    
  // 2) Validate input
  if (areValid(startDay, startMonth, startYear, endDay, endMonth, endYear, selectType)) {
     // 3) Update map with activities meeting criteria
     getActivities();
  }
}

//Do any additional setup (if needed) for activity types to display: 
//      Set variables (selectedTypes array) used by map for activity types to display
function setUpActivities(eNum) {
    selectedTypes.length = 0;
    
    //Determine if "All" was selected ...
    var wasAllActivitiesSelected = false;
    for (actIdx = 0; actIdx < document.forms["actSelect"].elements[eNum].options.length; actIdx++) {
        if (document.forms["actSelect"].elements[eNum].options[actIdx].value = "All") {
            if (document.forms["actSelect"].elements[eNum].options[actIdx].selected) {
                wasAllActivitiesSelected = true;
            }
            break;
        }
    }

    //Loop through all activity types & determine whether that activity type to be included
    for (actIdx = 0; actIdx < document.forms["actSelect"].elements[eNum].options.length; actIdx++) {
        //If "All" was selected, include every activity type (except for "All" itself, since that isn't value in DB!)
        //   Otherwise, include selected activity types
        if (wasAllActivitiesSelected || ((document.forms["actSelect"].elements[eNum].options[actIdx].selected) && (document.forms["actSelect"].elements[eNum].options[actIdx].value != "All"))) {
            selectedTypes[document.forms["actSelect"].elements[eNum].options[actIdx].value] = 1;
        }
    }
}

