commSphereApp.controller('dashCtrl', ['$scope', '$modal','$routeParams','ngEvents','ngRandomData','$http', function($scope, $modal,$routeParams,ngEvents,ngRandomData,$http) {

$scope.$parent.activeMenu='dashboard';
//console.log("test");
$http.get('/api/events/active').then(function(res){
//     console.log(res.data);
     if(res.data) {
         $scope.instances=res.data;
         getCompletionStatus();
         } else {
             alert('no data received, assign new id');
         }
    });
    
    
// if ($routeParams.draftStatus == null)
//    $routeParams.draftStatus = 'active';
// ngEvents.getEvents($routeParams.draftStatus).then(function(response) {
//   $scope.instances = response;
//   getCompletionStatus($scope);
// });


function getCompletionStatus() {    
for(var i = 0, l = $scope.instances.length; i < l; ++i){
    $scope.instances[i].randomNumber = ngRandomData.getRandomNumber();  //FOR "random" Mock data remove when real data has been implemented
    oneInstance = $scope.instances[i];
    var categoryCount = 0;
    var completedCount = 0;
    for (category in oneInstance.categories) {
                   if (oneInstance.categories.hasOwnProperty(category)) {
                 categoryCount++;
//                 console.log(oneInstance.categories[category].completedStatus);
                 if (oneInstance.categories[category].completedStatus)   
                           completedCount ++;      
             }
    }
      oneInstance.eventInstanceStatus = completedCount / categoryCount;
//    console.log('category count ' + categoryCount);
//    console.log('completed count = ' + completedCount);
    
};
}
//    console.log(ProgressStatus(oneInstance.categories));


  
$scope.showInfo = function(instance) {
   $scope.instance = instance;
   var modalInstance = $modal.open({
      scope:$scope,
      templateUrl: '/partials/infoModal',
      controller: infoModalInstanceCtrl,
      size: 'md',
      resolve: {
         instance: function () {
           return $scope.instance;
         }
       }
      
    });
};

var infoModalInstanceCtrl = function ($scope, $modalInstance) {

var instance = $scope.instance;
var categoryCount = 0;
var completedCount = 0;

for (category in instance.categories)
{     
  if (instance.categories.hasOwnProperty(category)) {
      var oneCategory = instance.categories[category];
    //  console.log(oneCategory);
      $scope.instance.categories[category].topicCount = getNodeCount(oneCategory.topics);
      for (topic in oneCategory.topics) {
          if (oneCategory.topics.hasOwnProperty(topic)) {
            var oneTopic = oneCategory.topics[topic];
        //    console.log(oneTopic);
            $scope.instance.categories[category].subtopicCount = getNodeCount(oneTopic.subTopics);
            
          }
      }
  }
}

function getNodeCount(document) { 
  var nodeCount = 0;
  for (node in document) {
         if (document.hasOwnProperty(node)) 
              nodeCount++;
                     
  }
  return nodeCount;
};
  $scope.ok = function () {
    $modalInstance.close();
  };

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


@@ -2205,16 +2205,124 @@
 t = 0;%0A%0A
+$scope.instance.categories%5Bcategory%5D.topicCount = 0;%0A$scope.instance.categories%5Bcategory%5D.subtopicCount=0;%0A%0A
 for (cat
@@ -2587,24 +2587,53 @@
 ry.topics);%0A
+      var subTopicCount = 0;%0A
       for (t
@@ -2782,18 +2782,16 @@
         
-//
     cons
@@ -2825,49 +2825,12 @@
     
-$scope.instance.categories%5Bcategory%5D.
 sub
-t
+T
 opic
@@ -2827,32 +2827,33 @@
   subTopicCount 
++
 = getNodeCount(o
@@ -2879,25 +2879,24 @@
 %0A           
- 
 %0A          %7D
@@ -2910,11 +2910,129 @@
 %7D%0A  
-%7D%0A%7D
+     $scope.instance.categories%5Bcategory%5D.subtopicCount = subTopicCount;%0A            %0A  %7D%0A%7D%0Aconsole.log($scope.instance);
 %0A%0Afu

correct subtopic count logic