/* Minification failed. Returning unminified contents.
(110,39-40): run-time error JS1195: Expected expression: >
(115,4-5): run-time error JS1195: Expected expression: )
(117,57-58): run-time error JS1003: Expected ':': }
(117,61-62): run-time error JS1195: Expected expression: >
(139,3-4): run-time error JS1002: Syntax error: }
(144,32-33): run-time error JS1004: Expected ';': {
 */
/*
var cboxOptions = {
  width: '75%',
  height: '75%',
  //maxWidth: '960px',
  //maxHeight: '960px',
  //transition: 'fade',
  open: true,
}
*/
var activities = [];
//var map;
//var bounds;
var ActiveTab;
var StoryTitle, GalleryTitle, TrainingTitle;
let chariot = null;
let metadata = {
  "eventName": "PMC",
  "amountEntered": 0,
  "riders": [{
    "pmcid": eGiftID,
    "amount": 0
  }]
};
let riders = [];

$(function () {
  //  If there is an active tab set then switch to that tab
  //
  if (ActiveTab) {
    $('.nav-tabs a[href="#' + ActiveTab + '_md"]').tab('show');
    $('.nav-pills a[href="#' + ActiveTab + '_xs"]').tab('show');
  }

  //  Set up the Map links
  //
  $('.fa-map').on('click', function () {
    //testMap();
    //debugger;
    // If no activity id we want the heat map
    if ($(this).parent().data('activityid')==null) {
      showHeatMap();  
    } else {
      showMap($(this).parent().data('activityid'));
    }
  });

  //  2022-08-03  hmw Modified to use HREF on Tabs
  /*
  //  Setup the on click for the tabs and pills to change the og:title
  //
  $('.nav-tabs>li>a, .nav-pills>li>a').on('click', function () {
    var tab = $(this).data('tab'),
      url = location.href.replace('/gallery', '').replace('/training', '').replace('/weekend', ''),
      title = tab == 'story' ? StoryTitle : tab == 'gallery' ? GalleryTitle : TrainingTitle;

    if (url.indexOf('?') != -1) {
      url = url.substring(0, url.indexOf('?'));
    }

    if (tab != 'story') {
      url += "/" + tab;
    }

    location.replace(url);
    return;

    $('meta[property="og:title"]').attr('content', title);
    $('meta[property="og:url"]').attr('content', url);

    console.log($('meta[property="og:title"]').attr('content'));
    console.log($('meta[property="og:url"]').attr('content'));
  });
  */

  //  Display the Gallery when click on teh profile photo
  //
  $('.photoProfile').click(function () {
    //$("a[rel='photogallery']").colorbox({ open: true });
    //$("a[rel='photogallery']").colorbox(cboxOptions);
    $('.nav-tabs a[href="#gallery_md"]').tab('show');
    $('.nav-pills a[href="#gallery_xs"]').tab('show');
  });

  //  Setup the Venmo Onclick
  //
  $('.venmo').on('click', function () {
    $('#ModalDialog').modal();
    return false;
  });

  // Chariot implementation
  //
  chariot = document.getElementById('chariot');

  // define your alterations using valid properties from the table below
  /*const alterations = {
    textSizeMain: "text-xs",
    corners: "rounded-[4px]",
    yPadding: "py-3"
  };

  // register your new theme with a custom name
  chariot.registerTheme("myTheme", alterations);*/

  var chariotPath = BaseUrl + 'Chariot';

  // set up data for chariot
  //
  chariot.onDonationRequest(async () => {
    return {
      amount: 0,
      metadata: metadata
    };
  });

  chariot.addEventListener('CHARIOT_SUCCESS', ({ detail }) => {
    // Record the grant intent information so that you can
    // complete the transaction once your form is submitted.
    console.log(detail);
    $.ajax({
      type: "POST",
      url: chariotPath,
      data: detail,
      beforeSend: function () {
        myApp.showPleaseWait();
      },
      complete: function () {
        myApp.hidePleaseWait();
      },
      success: function (data) {
        if (data.status === "OK") {
          showDafComplete(data);
        } else {
          myApp.showModalDialog("An Error Occurred", data.message, false);
        }
      }
    });
  });

});


function showDafComplete(data) {
  myApp.showModalDialog("Donation Complete", "Your donation to " + data.giftInfo.Details[0].ProfileName + " for $" + data.giftInfo.Amount + " has been submitted successfully.", false);
}

//Readmore
//$('story_xs').readmore({ collapsedHeight: 250, lessLink: '<a href="#">Read Less</a>' });
//$('story_md').readmore({ collapsedHeight: 350, lessLink: '<a href="#">Read Less</a>' });
//$('story_xs').css({ "display": "inline" });

$('story_md').shorten({
  "showChars": 1800,
  "moreText": "Read More",
  "lessText": "Read Less",
});

$('story_xs').shorten({
  "showChars": 530,
  "moreText": "Read More",
  "lessText": "Read Less",
});

//  This function displays a google map of the activity in a modal window
//
function showMap(activityid) {

  var activity = activities.filter(function (item) {
    return item.activityid == activityid;
  })[0];

  //  If there is a valid activity record display the map
  //
  if (activity) {
    var modal = $('#ModalMap'),
      label = $('#ModalDialogLabel'),
      description = $('#ModalDescription');      

    label.html(activity.title);
    description.html(activity.description);

    //  Note: replacing \ with \\
    //var path = google.maps.geometry.encoding.decodePath(activity.polyline.replace(/\\/g, "\\\\"));
    var path = google.maps.geometry.encoding.decodePath(activity.polyline);

    //  Setup the map so we can just re-draw over it
    //
    //if (!map) {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 3,
        center: { lat: path[0].lat(), lng: path[0].lng() },
        //center: { lat: 42, lng: -136 },
        mapTypeId: 'roadmap'
      });
    //}

    var polyline = new google.maps.Polyline({
      path: path,
      map: map,
      geodesic: true,
      strokeColor: '#FF0000',
      strokeOpacity: 1.0,
      strokeWeight: 2
    });

    var bounds = new google.maps.LatLngBounds();
    /*
    for (i = 0; i < 100; i++) {
      console.log(path[i].lat() + ':' + path[i].lng());
      //bounds.extend(path[i])
      var myLatLng = new google.maps.LatLng({ lat: path[i].lat(), lng: path[i].lng() });
      bounds.extend(myLatLng);
    }
    */

    //bounds.extend(path[0]);
    //bounds.extend(path[path.length - 1]);
    polyline.getPath().forEach(function (point, index) {
      bounds.extend(point);
    });

    modal.modal();
    map.fitBounds(bounds, 10);


  }

}

//  This function displays a google heat map of all activity in a modal window
//
function showHeatMap() {
  
  var modal = $('#ModalMap'),
    label = $('#ModalDialogLabel'),
    description = $('#ModalDescription');

  label.html("Activities Heat map");
  //description.html(activity.description);

  

  // Loop through activities and add points to array for heat map
  //
  var points = new Array();
  var i;
  for (i = 0; i < activities.length; i++) {
    if (activities[i].sport != "VirtualRide" && activities[i].Sport != "VirtualRun") {
      try {
        var path = google.maps.geometry.encoding.decodePath(activities[i].polyline);
        var point = new google.maps.LatLng(path[0].lat(), path[0].lng());
        points.push(point);
      } catch (err) {
        // some polylines weren't working
      }
    }
  }
  
  //  Setup the map so we can just re-draw over it
  //
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 3,
    center: { lat: path[0].lat(), lng: path[0].lng() },
    //center: { lat: 42, lng: -136 },
    mapTypeId: 'roadmap'
  });
  
  heatmap = new google.maps.visualization.HeatmapLayer({
    data: points,
    map: map
  });
  

  var bounds = new google.maps.LatLngBounds();
  /*
  for (i = 0; i < 100; i++) {
    console.log(path[i].lat() + ':' + path[i].lng());
    //bounds.extend(path[i])
    var myLatLng = new google.maps.LatLng({ lat: path[i].lat(), lng: path[i].lng() });
    bounds.extend(myLatLng);
  }
  */

  //bounds.extend(path[0]);
  //bounds.extend(path[path.length - 1]);
  points.forEach(function (point, index) {
    bounds.extend(point);
  });

  modal.modal();
  map.fitBounds(bounds, 10);


  

}

function round(value, decimals) {
  return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}

function testMap() {

  var modal = $('#ModalMap'),
    map = $('#map'),
    label = $('#ModalDialogLabel'),
    description = $('#ModalDescription');

  label.html('Title');
  description.html('Description');

  var polyline = "}{s`GxxhqLwAr@aAMq@i@kAeCWBiBbCmLbS}@p@sBjEqAzD_@xFvBhUsBzBmBnIoCxERbAfBxC`FfBbBjAnDjIhAfBjCdChAtCKXgFlCoN|EcCd@`@dEfE~Wz@zTv@lFP~K}Bf@_BnHm@n@y@DuAq@kAEkDx@_CjAeF|@dAzAjBnFdDrGtCnOTtD\\~NXpDInNkCtc@}@fJ_@h@yZnHmAEmDoAoBEmGrD_NrCeAzAqB|O]l@yNoDwFeEqG}CaCrGeAbB_MpGyN|KcEz@cLeBoI`BgCvAuCxDmIbGeE`FgBx@Qn@m@bKaA|D}@fL`@`K`@jAbE`GvDNbAzA|@~Cb@xFZhJPfA`@x@~BbC`BnDjAlAlA~Cd@lDzAnEnCpCZrBdAfDJnFY~@wEnBsNzBuLpC]`@BSH?Qr@x@fDLpDcBfPc@tMeGvE_GjHcE~JsApGhElBd[zIvFrBfHxAlBdBz@zBh@`DCrLf@zIx@dErA`ETlB@tB_@hE\\zFh@bCfHzFxSdGvG|DnBNnMq@xE{@`Ba@`BmBhBi@tH\\lIo@lMfAjFFfE[xJfBhHLz@VxE`ErDzBtQlFfE|C`DrE~GpHzC`CnE`CnBxDrA`ELnCk@nFNfApAnCxDzAjDjBf@p@j@tBp@f@dEc@`A^r@vAnArFhDtGl@vEj@tAhAfA|Cb@hBfAtExH|A`Fj@`AzCjAlJzBjHbI|HnF`Bb@~Nd@fAhAzBlEhNgBnH}DlLaKtCaDd@iBsPqn@q@iEuAmFmEwHmSaYwAkCyEyOcCeQ{AcE_CqDiAy@on@w\\wEgFeGmIaD}GcGiUmAqHyCi]i@_CmA\\yJjHeA`@eJ\\gEdAsMW}K~BcBtAwA}AuGmFuKwRsJoOw@{DGiAJsAjEoGXiB[kAoBoBw@wDaEkMAeC|@{DNeCQeC_AiEAcAZoA~BiDb@_BOoFt@aMxAiFnBkD`@kBPiJQsEn@aFPaLi@yDjYyZzAmCtB_KjN_@Dw@z@wChWmx@l@gAJyD_@cB_GwNqFaKwAsFgBoDGsB`A_NXiAdMgFdFwD|@qAP_DqAsIRmW\\yMhCsU~@iDlAyB~GkFnB_C|AiDvAiFBu@lBsEHce@kAsn@EwHJyAb@iAfDqEhDqKzHgGv@sA`Ax@NbA`EnKnGkBs@uCRuFgAeBwBq@kADm@x@a@zB}@I_BoA_C_Gq@{@cCg@aIgDIeA~@sFCy@_@gAmCsCuAkDYgBZaCCy@[H}As@}CsBeAX}@hBo@f@?VpAtCGz@qMbTi@|C";

  var points = google.maps.geometry.encoding.decodePath(polyline);

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 5,
    center: { lat: points[0].lat(), lng: points[0].lng() },
    mapTypeId: 'roadmap'
  });

  var path = new google.maps.Polyline({
    path: points,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
  });
  path.setMap(map);


  var bounds = new google.maps.LatLngBounds();
  //for (i = 0; i++; i < points.length) { bounds.extend(points[i]) }
  bounds.extend(points[0]);
  bounds.extend(points[points.length-1]);


  map.fitBounds(bounds)

  modal.modal();

};
