﻿var TripController = (function () {
  var deselectPriorTrip = function () {
    MapState.context = MapUseContext.none;
    if (TripController.tripRowSelected) {
      TripController.tripRowSelected.removeClass(TripController.tripRowSelected.attr("class"));
      TripController.tripRowSelected.addClass(TripController.tripRowPriorCss);
    }
  };

  var setSelectedTripRow = function (tripId, oRow) {
    deselectPriorTrip();
    MapState.context = MapUseContext.trip;

    TripController.tripRowPriorCss = oRow.attr("class");
    oRow.removeClass(TripController.tripRowPriorCss);
    oRow.addClass("widgetRowSelected");
    TripController.tripRowSelected = oRow;
    TripController.selectedTripId = tripId;
  };

  var getTripRow = function (tripId) {
    return $(".widgetGrid tr:contains('TripId=" + tripId + "')");
  };

  return {
    tripRowPriorCss: null,
    tripRowSelected: null,
    selectedTripId: null,
    getTripDetails: function (tripId) {
      if (tripId) {
        setSelectedTripRow(tripId, getTripRow(tripId));
        AjaxCommands.submitGet("/Trip/Details/" + tripId, "tripDetailsAjaxContainer", function () { $(document).trigger("trip-details-loaded", [tripId]); });
      }
    },
    getTrips: function (tripId) {
      AjaxCommands.submitGet("/Trip/Index/" + tripId, "tripAjaxContainer");
    },
    getAllTrips: function () {
      TripController.getTrips(0);
    },
    selectTrip: function (tripId) {
      if (tripId == 0)
        return;
      getTripRow(tripId).click();
    },
    createTrip: function () {
      deselectPriorTrip();
      UIDisplay.showTab("#tripsAndJournal");
      AjaxCommands.submitGet("/Trip/Create/", "ModalContent", function () { Modal.show(); });
    },
    newTrip: function (oForm) {
      AjaxCommands.submitHiddenForm(oForm, "Trip", "Create", "ModalContent");
    },
    editTrip: function (tripId) {
      TripController.selectedTripId = tripId;
      AjaxCommands.submitGet("/Trip/Edit/" + tripId, "ModalContent", function () { Modal.show(); });
    },
    updateTrip: function (oForm) {
      AjaxCommands.submitHiddenForm(oForm, "Trip", "Edit", "ModalContent");
    },
    deleteTrip: function (event, tripId) {
      event.stopPropagation();
      MapState.context = MapUseContext.trip;
      if (!confirm("This Trip may have Journal Pages or be linked to Trip Waypoints.\nDo you want to delete it?")) {
        return;
      }

      $(document).trigger("trip-deselected", tripId);
      AjaxCommands.submitGet("/Trip/Delete/" + tripId, "tripAjaxContainer");
    },
    linkMarkerToTrip: function () {
      UIDisplay.showTab("#tripsAndJournal");

      var request = {
        TripId: TripController.selectedTripId,
        MarkerId: MapState.selectedMarker.id
      };

      AjaxCommands.submitJson(request, "Trip", "LinkMarkerToTrip", "markersAjaxContainer");
    },
    deleteTripMarker: function (markerId) {
      if (!(confirm("Are you sure you want to delete this waypoint?"))) {
        event.stopPropagation();
        return;
      }

      var request = { TripId: this.selectedTripId, MarkerId: markerId };
      AjaxCommands.submitJson(request,
				    "Trip",
				    "DeleteTripMarker",
				    "markersAjaxContainer",
				    function () { MarkerController.getTripMarkers(this.selectedTripId); });
    }
  };
})();

