<file_sep>var flickrApp = angular.module('flickrApp', [
    'ngRoute',
    'flickrControllers',
    'flickrServices',
    'ui.bootstrap'
]);

flickrApp
    .constant('CONFIG', {
        apiKey: '622326fd51e4ffecd5ae17a5771e4335'
    });

flickrApp.config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider.
            when('/photos', {
                templateUrl: 'partials/photo-list.html',
                controller: 'PhotoListCtrl'
            }).
            when('/photo/:photoId', {
                templateUrl: 'partials/photo-details.html',
                controller: 'PhotoDetailCtrl'
            }).
            when('/search/:keywords', {
                templateUrl: 'partials/photo-list.html',
                controller: 'SearchCtrl'
            }).
            otherwise({
                redirectTo: '/photos'
            });
    }
]);<file_sep>var flickrControllers = angular.module('flickrControllers', []);

flickrControllers.controller('SearchCtrl', ['$scope', '$location', '$routeParams', 'Photo', function ($scope, $location, $routeParams, Photo) {
    $scope.search = function () {
        $location.path('/search/' + $scope.keywords);
    };

    $scope.keywords = $routeParams.keywords;

    $scope.$watch('currentPage', function (newValue, oldValue) {
        if (newValue !== oldValue) {
            Photo.query({
                text: $routeParams.keywords,
                page: newValue
            }, function (response) {
                $scope.photos = response.photos.photo;
                $scope.totalItems = response.photos.total;
            });
        }
    });

    if ($routeParams.keywords !== undefined) {
        Photo.query({
            text: $routeParams.keywords
        }, function (response) {
            $scope.photos = response.photos.photo;
            $scope.totalItems = response.photos.total;
        });
    }
}]);

flickrControllers.controller('PhotoListCtrl', ['$scope', '$modal', 'Photo', 'Person', function ($scope, $modal, Photo, Person) {
    $scope.showPosition = function (position) {
        $scope.lat = position.coords.latitude;
        $scope.lon = position.coords.longitude;

        $scope.currentPage = 1;
        $scope.$digest();
    }

    $scope.getLocation = function () {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition($scope.showPosition);
        }
    }

    $scope.$watch('currentPage', function (newValue, oldValue) {
        if (newValue !== oldValue) {
            Photo.query({
                lat: $scope.lat,
                lon: $scope.lon,
                page: newValue
            }, function (response) {
                $scope.photos = response.photos.photo;
                $scope.totalItems = response.photos.total;
            });
        }
    });

    $scope.showOwner = function (owner) {
        Person.query({
            user_id: owner
        }, function (response) {
            $modal.open({
                templateUrl: 'partials/modal.html',
                controller: 'ModalCtrl',
                resolve: {
                    person: function () {
                        return response.person;
                    }
                }
            });
        });
    };

    $scope.getLocation();
}]);

flickrControllers.controller('PhotoDetailCtrl', ['$scope', '$routeParams', 'Photo', function ($scope, $routeParams, Photo) {
    Photo.query({
        method: 'flickr.photos.getInfo',
        photo_id: $routeParams.photoId
    }, function (response) {
        $scope.photo = response.photo;
        console.log(response.photo);
    });
}]);

flickrControllers.controller('ModalCtrl', function ($scope, $modalInstance, person) {
    $scope.person = person;

    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    };
});