       filtered[0]
   }
 
+.service 'ProfileService', () ->
+  this.newLocation = undefined
+  null
+
 .service 'LocationService', ($http, filterFilter) ->
   locationPromise = undefined
   this.getLocations = () ->

  $scope.profiles = ProfilesFactory.profiles
  return null

.controller 'SingleProfileCtrl', ($scope, $stateParams, ProfilesFactory, LocationService) ->
  $scope.profile = ProfilesFactory.getProfile (parseInt $stateParams.profileId)

  LocationService.getLocations().success (data) ->
    $scope.locations = data
    if LocationService.currentLocation?
      $scope.profile.location = LocationService.currentLocation

    $scope.location = LocationService.getLocation $scope.locations, $scope.profile.location
    LocationService.currentLocation = $scope?.location?.locationId

  update = (newLocation) ->
    if newLocation?
      LocationService.currentLocation = undefined
      $scope.profile.location = newLocation
      $scope.location = LocationService.getLocation $scope.locations, $scope.profile.location

  $scope.$watch (() -> LocationService.currentLocation), update

  return null

.controller 'AppCtrl', () ->
  null

.controller 'LocationsCtrl', ($scope, LocationService, ProfileService) ->
  LocationService.getLocations().success (data) ->
    $scope.locations = data

  this.current = LocationService.currentLocation
  $scope.$watch (() => this.current), (newVal) ->
    ProfileService.currentLocation = newVal

  null

