(function () {
    "use strict";

    /** Product detail view controller */
    angular.module('app')
        .controller('DetailController', ['$scope', '$location', '$routeParams', 'catalogService',
            function ($scope, $location, $routeParams, catalogService) {

            $scope.product = {};

            catalogService.getProduct($routeParams.id).success(function (result) {
                $scope.product = result;
            });

            $scope.quantity = 1;


		    /** Returns rating for a given product. */
		    var getProductRating = function(comments) {
		        if (comments) {
		            var sumRatings = 0;
		            for ( var index = 0; index < comments.length; index++) {
		                var comment = comments[index];
		                sumRatings += comment.rate;
		            }
		            return (sumRatings / comments.length |0);  // like Math... but better :)
		        }
		        return 0;
		    };
		    
		    $scope.getImage = function (id) {
		        if (!id) {
		            return "";
		        } else {
		            return "/bookcat/img/catalog/" + id + ".jpg";
		        }
		    };
		    
		    /** Returns the CSS class for the average rating of a given product. */
		    $scope.getCSSRating = function(pItem) {
		        var css = ['rating'];

		        if (pItem !== undefined) {
		            switch (getProductRating(pItem)) {
		                case 1:
		                    css.push('one');
		                    break;
		                case 2:
		                    css.push('two');
		                    break;
		                case 3:
		                    css.push('three');
		                    break;
		                case 4:
		                    css.push('four');
		                    break;
		                case 5:
		                    css.push('five');
		                    break;
		                default :
		                    css.push('zero');
		            }
		        }
		        return css;
		    };


}]);
})();
@@ -192,16 +192,32 @@
 ervice',
+ 'ProductUtils',
 %0A       
@@ -278,16 +278,30 @@
 gService
+, ProductUtils
 ) %7B%0A%0A   
@@ -509,1523 +509,339 @@
  1;%0A
-%0A%0A%09%09    /** Returns rating for a given product. */%0A%09%09    var getProductRating = function(comments) %7B%0A%09%09        if (comments) %7B%0A%09%09            var sumRatings = 0;%0A%09%09            for ( var index = 0; index %3C comments.length; index++) %7B%0A%09%09                var comment = comments%5Bindex%5D;%0A%09%09                sumRatings += comment.rate;%0A%09%09            %7D%0A%09%09            return (sumRatings / comments.length %7C0);  // like Math... but better :)%0A%09%09        %7D%0A%09%09        return 0;%0A%09%09    %7D;%0A%09%09    %0A%09%09    $scope.getImage = function (id) %7B%0A%09%09        if (!id) %7B%0A%09%09            return %22%22;%0A%09%09        %7D else %7B%0A%09%09            return %22/bookcat/img/catalog/%22 + id + %22.jpg%22;%0A%09%09        %7D%0A%09%09    %7D;%0A%09%09    %0A%09%09    /** Returns the CSS class for the average rating of a given product. */%0A%09%09    $scope.getCSSRating = function(pItem) %7B%0A%09%09        var css = %5B'rating'%5D;%0A%0A%09%09        if (pItem !== undefined) %7B%0A%09%09            switch (getProductRating(pItem)) %7B%0A%09%09                case 1:%0A%09%09                    css.push('one');%0A%09%09                    break;%0A%09%09                case 2:%0A%09%09                    css.push('two');%0A%09%09                    break;%0A%09%09                case 3:%0A%09%09                    css.push('three');%0A%09%09                    break;%0A%09%09                case 4:%0A%09%09                    css.push('four');%0A%09%09                    break;%0A%09%09                case 5:%0A%09%09                    css.push('five');%0A%09%09                    break;%0A%09%09                default :%0A%09%09                    css.push('zero');%0A%09%09            %7D%0A%09%09        %7D%0A%09%09        return css;%0A%09%09    %7D;%0A
+%09    %0A%09%09    $scope.getImage = function (id) %7B%0A%09%09        if (!id) %7B%0A%09%09            return %22%22;%0A%09%09        %7D else %7B%0A%09%09            return %22/bookcat/img/catalog/%22 + id + %22.jpg%22;%0A%09%09        %7D%0A%09%09    %7D;%0A%09%09    %0A%09%09    /** Returns the CSS class for the average rating of a given product. */%0A%09%09    $scope.getCSSRating = ProductUtils.getRatingCss;
 %0A%0A%7D%5D

Update Detail controller in order to use productUtils service