                                                             (-> current-city :attractions :data)])]]
               [:div {:class "ui fourteen wide column row basic segment"}
                [:div {:class "fourteen wide column"}
-                (om/build attraction-map-view [current-city journey])]]
+                (om/build attraction-map-view [current-city journey transit-journey])]]
               ]]))))

        []
        all-directions))))

(defn create-renderers [owner all-directions]
  (reduce
    (fn [renderers [index directions]]
      (let [renderer (js/google.maps.DirectionsRenderer.
                       #js {:polylineOptions #js {:strokeColor (next-color index)}
                            :suppressMarkers true})]
        (.setMap renderer (om/get-state owner :google-map))
        (.setDirections renderer (clj->js (:directions directions)))
        (conj renderers renderer)))
    []
    (map vector (range) all-directions)))

(defn gen-new-renderers [owner waypoints journey all-directions]
  (let [all-renderers (create-renderers owner all-directions)]
    (om/set-state! owner :all-renderers all-renderers)
    (om/update!
      (models/transit-journey)
      (annotate-directions
        (-> journey :start-place :address)
        (-> journey :end-place :address)
        waypoints
        all-directions))))

(defn get-start-location [transit-directions]
  (-> transit-directions :directions :routes (get 0) :legs (get 0) :start_location))

(defn get-end-location [transit-directions]
  (-> transit-directions :directions :routes (get 0) :legs (get 0) :end_location))

(defn create-markers [owner]
  (reduce
    (fn [partial-markers [place-id location]]
      (conj
        partial-markers
        (js/google.maps.Marker.
          #js {:position location 
               :map (om/get-state owner :google-map)
               :title (str place-id) 
               :label (str (inc place-id)) 
               })))
    []
    (vec 
      (map vector
           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           (conj
             (vec (map get-start-location (models/transit-journey)))
             (get-end-location (last (models/transit-journey))))))))

;; TODO - make sure that this doesn't cause a memory leak
;; since I don't actually delete the object

                  (if (> msg-count 0)
                    (recur (dec msg-count) (conj partial-directions (<! response-channel)))
                    (do
                      (om/update!
                        transit-journey
                        (make-transit-journey waypoints journey partial-directions)))))))))))))

(defn attraction-map-view [[current-city journey transit-journey] owner]
  (reify
    ;; initialize the google maps objects and store them as local state to the map view
    om/IDidMount
    (did-mount [_]
      (let [google-map-container (om/get-node owner)
            google-directions-service (js/google.maps.DirectionsService.)
            google-city-center (geojson->goog-latlng (-> current-city :city :data :center :coordinates))]
        (let [google-map (js/google.maps.Map.
                           google-map-container
                           #js {:center google-city-center
                                :zoom 9})]
          (om/set-state! owner :google-map google-map)
          (om/set-state! owner :google-directions-service google-directions-service)
          (gen-new-renderers owner transit-journey)
          (gen-new-markers owner transit-journey))))

    ;; recompute the route upon journey update
    om/IDidUpdate
    (did-update [_ [prev-city prev-journey prev-transit-journey] _]
      (let [[city journey transit-journey] (om/get-props owner)]
        (when (valid-journey? journey)
          (when-not (= transit-journey prev-transit-journey)
            (remove-old-markers owner)
            (remove-old-renderers owner)
            (gen-new-renderers owner transit-journey)
            (gen-new-markers owner transit-journey)))
        (if-not (= (-> city :city :data :center :coordinates) (-> prev-city :city :data :center :coordinates))
          (.setCenter (om/get-state owner :google-map) (geojson->goog-latlng (-> current-city :city :data :center :coordinates))))))

