function RatingViewModel(service) {
    var self = this;
    this.loaded = ko.observable(false);

    this.competitors = ko.observableArray();
    this.selectedCompetitor = ko.observable();
    this.ratings = ko.observableArray();
    this.newRatingJudge = ko.observable();
    this.newRatingRating = ko.observable();
    this.judges = ko.observableArray();
    this.outcome = ko.observable();
    
    this.deleteRating = function (rating) {
        service.deleteRating(rating.Id)
            .done(function () { self.loadCompetitor(self.selectedCompetitor()); })
            .fail(function (jqxhr, textStatus, error) {
                alert(error);
            });

    };
    this.saveNewRating = function () {
        service.saveNewRating(self.selectedCompetitor().Id, self.newRatingJudge().Id, self.newRatingRating())
            .done(function () { self.loadCompetitor(self.selectedCompetitor()); })
            .fail(function (jqxhr, textStatus, error) {
                alert(error);
            })
            .always(function () {
                self.newRatingJudge(null);
                self.newRatingRating(null);
            });

    };
    this.loadCompetitor = function (newValue) {
        service.getPersonRatings(newValue.Id)
          .done(self.ratings)
          .fail(function (jqxhr, textStatus, error) {
              alert(error);
          });

        service.getAvailableJudgesForPerson(newValue.Id)
            .done(self.judges)
            .fail(function (jqxhr, textStatus, error) {
                alert(error);
            });

        service.getOutcome(newValue.Id)
            .done(self.outcome)
            .fail(function (jqxhr, textStatus, error) {
                alert(error);
            });
    };
    this.loadCompetitors = function () {
        service.getCompetitors()
               .done(vm.competitors)
               .fail(function (jqxhr, textStatus, error) {
                   alert(error);
               })
               .always(function () { self.loaded(true); });
    };

    this.selectedCompetitor.subscribe(this.loadCompetitor);
};