(function ()
    {
    var modular = angular.module('repositories', []);

    modular.controller('RepositoriesController', ['$http', '$scope', '$rootScope', function ($http, $scope, $rootScope)
        {
        var repositories = this;
        repositories.list = [];
        repositories.current = "";
        repositories.$http = $http;
        repositories.$rootScope = $rootScope;

        repositories.loadCurrent = function ()
            {
            repositories.$http.get('/controller/repository').success(function(result)
                {
                if (result !== repositories.current)
                    {
                    repositories.current = result;
                    repositories.$rootScope.$broadcast('repositoryLoaded', result);
                    }
                });
            };

        repositories.loadList = function ()
            {
            repositories.$http.get('/controller/repositories').success(function(result)
                {
                repositories.list = result;
                repositories.loadCurrent();
                });
            };

        repositories.load = function(repository)
            {
            repositories.$http.post('/controller/repository/load/' + repository).success(function (result)
                {
                repositories.loadCurrent();
                });
            };

        $scope.$on('repositoryUploaded', function (event, args)
            {
            repositories.loadList();
            });

        repositories.loadList();
        }]);
    })();

	