var ResultsMapController = Class.create(MapController, {

  setLayer: function(options) {
    this.startingMarker = (options.markersAsNumberOrLetters=="letters") ? "A" : 1;

    this.results_overlay = new ResultsOverlay( { map: this.mapView.map,
                                                 label: this.mapView.label});
    this.markersBySection = $H({});
    this.polylineBySection = $H({});
  },
  
  tripSectionOver: function(tripIndex, tripSectionIndex) {
    if (tripSectionIndex != this.currentSectionIndexOver) {
      
      this.currentSectionIndexOver = tripSectionIndex;

      var poly = this.polylineBySection.get(tripSectionIndex - 1);
      if (poly!=undefined) {
        poly.setOptions( { strokeOpacity: 0.8,
                           strokeColor: "#ED7F00",
                           strokeWeight: 6} );
        poly.setMap( poly.getMap());
      }

      var sectionMarkers = this.markersBySection.get(tripSectionIndex - 1);

      sectionMarkers.each(function(marker) {
        try {
          var currentImage = marker.getIcon();
          var newImage = currentImage.split('.').first() + '_selected.png';
          marker.setOptions( { icon: newImage,
                               zIndex: 1000} );
        } catch(e) {
          // if the marker is not visible on the google map
          // it might be unavailable to change its image
          // so we catch and do nothing
        }
      });
    }
  },
  
  tripSectionOut: function(tripIndex, tripSectionIndex) {    
    if (tripSectionIndex == this.currentSectionIndexOver) {
      
      this.currentSectionIndexOver = null;

      var poly = this.polylineBySection.get(tripSectionIndex - 1);
      if (poly!=undefined) {
        poly.setOptions( { strokeOpacity: 0.7,
                           strokeColor: "#0033CC",
                           strokeWeight: 4} );
        poly.setMap( poly.getMap());
      }

      var sectionMarkers = this.markersBySection.get(tripSectionIndex - 1);

      sectionMarkers.each(function(marker) {
        try {
          var currentImage = marker.getIcon();
          var newImage = currentImage.sub('_selected', '');
          marker.setOptions( { icon: newImage,
                               zIndex: 100} );
        } catch(e) {
          // if the marker is not visible on the google map
          // it might be unavailable to change its image
          // so we catch and do nothing
        }
      });
    }
  },
  
  setTrips: function(trips) {
    this.trips = trips;
  },

  displayTrip: function(tripIndex) {
    // exit if mapView has not yet been defined
    if (this.mapView == null || this.mapView == undefined) {
      return;
    }

    var trip = this.trips[tripIndex];

    // this.mapView.cleanLocations("default");
    this.results_overlay.clear();

    // effacer la précédente solution de la carte
    this.markersBySection = $H(); 

    this.text_marker        = null;
    this.text_marker_status = null;

    var markerById = $H();
    trip.sections.each(function(section, sectionIndex) {

      if (this.markersBySection.get(sectionIndex)==undefined) {
        this.markersBySection.set(sectionIndex, $A());
      }
      var sectionMarkers = this.markersBySection.get(sectionIndex);

      var departure_stop  = section.departure_stop;
      var arrival_stop    = section.arrival_stop;
      
      var departureId = this.endSectionId("departure", departure_stop.name, arrival_stop.name);
      var arrivalId   = this.endSectionId("arrival",   departure_stop.name, arrival_stop.name);
      
      var departure_icon_path = itinerariesResponseMarkerDir + departureId + ".png";
      var arrival_icon_path = itinerariesResponseMarkerDir + arrivalId + ".png";

      var departureLatLng = new google.maps.LatLng( departure_stop.stop_area_lat,
                                            departure_stop.stop_area_lng);
      if (!markerById.get(departureId)) {
        var marker = new google.maps.Marker({
          map: this.mapView.map,
          position: departureLatLng,
          visible: false,
          text: departure_stop.name,
          icon: departure_icon_path,
          zIndex: 100});
        this.results_overlay.addMarker( marker);
        markerById.set(departureId,marker);
      }
      sectionMarkers.push( markerById.get(departureId));

      var arrivalLatLng = new google.maps.LatLng( arrival_stop.stop_area_lat,
                                            arrival_stop.stop_area_lng);
      if (!markerById.get(arrivalId)) {
        var marker = new google.maps.Marker({
          map: this.mapView.map,
          position: arrivalLatLng,
          visible: false,
          text: arrival_stop.name,
          icon: arrival_icon_path,
          zIndex: 100});
        this.results_overlay.addMarker( marker);
        markerById.set(arrivalId,marker);
      }
      sectionMarkers.push( markerById.get(arrivalId));

      if ( itinerariesMapDisplaySection)
        this.displaySection(section,sectionIndex,departureLatLng,arrivalLatLng);

    }.bind(this));

    this.results_overlay.draw();
    this.mapView.map.fitBounds( this.results_overlay.bounds());
  },

  displaySection: function(section,sectionIndex,departureLatLng,arrivalLatLng) {
      // display polyline
      if (section.type!="INTERCHANGE") {
        var path = section.stops.map(function(stop){
          return new google.maps.LatLng( stop.stop_area_lat,
                                         stop.stop_area_lng);
        });
        path.unshift(departureLatLng);
        path.push(arrivalLatLng);

        var poly = new google.maps.Polyline({
                      map: this.mapView.map,
                      path: path,
                      strokeColor: "#0033CC",
                      strokeOpacity: 0.7,
                      strokeWeight: 4});
        this.polylineBySection.set( sectionIndex, poly);
        this.results_overlay.addPolyline( poly);
      }

      // display inter-markers
      var myMap = this.mapView.map;
      var overlay = this.results_overlay;
      section.stops.each(function(stop){
        var marker = new google.maps.Marker({
          map: myMap,
          position: new google.maps.LatLng( stop.stop_area_lat,
                                            stop.stop_area_lng),
          visible: false,
          text: stop.name,
          icon: new google.maps.MarkerImage( "/images/markers/stop_small.png",
                new google.maps.Size( 10, 10),
                new google.maps.Point(0,0),
                new google.maps.Point(5,5)),
          zIndex: 100 });
        overlay.addMarker( marker);
      });
  },

  endSectionId: function(departure_or_arrival, departure_stop_name, arrival_stop_name) {
    
    if (!this.text_marker)
      this.text_marker = this.startingMarker;

    if (!this.text_marker_status)
      this.text_marker_status = 0;
    
    if (departure_or_arrival == "arrival") {
      if (departure_stop_name == arrival_stop_name) {
        this.text_marker_status = 1
       } else {
        this.text_marker = (this.text_marker).succ();
        this.text_marker_status = 0
      }
    }
    var separator = Object.isNumber(this.text_marker) ? "_" : "";

    return this.text_marker + separator +  this.text_marker_status;
  }
  
});

