'use strict';
angular.module('genie.eventsMap')
.controller('MapCtrl', ['$scope', 'mapService', 'ZoomLevel',
  'tweetService',
  function($scope, mapService, ZoomLevel, tweetService) {

  // Set init values
  $scope.inputs = {zoomLevel: 18, minutesAgo: 1440};
  $scope.map = {};
  var zoomLevelObj = new ZoomLevel();
  zoomLevelObj.events = [];
  $scope.zoomLevelObj = zoomLevelObj;

  // Watch user inputs and fetch zoomlevel object
  $scope.$watchCollection(
    function(scope) {
      return scope.inputs;
    },
    getZoomLevel
  );

  // Watch live hashtags
  $scope.liveTags = tweetService.getHashtags();

  function getZoomLevel() {
    mapService.getZoomLevel($scope.inputs)
    .then(function(zoomLevelObj) {
      if (!$scope.map.getCenter()) { // center not set
        $scope.map.setCenter(zoomLevelObj.centerPoint);
      }
      $scope.zoomLevelObj = zoomLevelObj;
      //jqcloud tag collection
      // TODO: remove .uniq() once the server has TagCloud api
      $scope.tags = _.uniq(_.map(zoomLevelObj.events, function(event) {
        return {text: event.tag, weight: event.weight};
      }), 'text');
    })
  }
}]);

@@ -688,24 +688,53 @@
   .then(
-function
+updateMap);%0A  %7D%0A%0A  function updateMap
 (zoomLev
@@ -738,26 +738,24 @@
 LevelObj) %7B%0A
-  
     if (!$sc
@@ -797,26 +797,24 @@
 t set%0A      
- 
- 
 $scope.map.s
@@ -849,18 +849,16 @@
 Point);%0A
-  
     %7D%0A  
@@ -855,26 +855,24 @@
 ;%0A    %7D%0A    
-  
 $scope.zoomL
@@ -899,18 +899,16 @@
 bj;%0A    
-  
 //jqclou
@@ -932,94 +932,19 @@
     
-  // TODO: remove .uniq() once the server has TagCloud api%0A      $scope.
+var 
 tags = 
-_.uniq(
 _.ma
@@ -980,26 +980,24 @@
 on(event) %7B%0A
-  
       return
@@ -1042,20 +1042,110 @@
 t%7D;%0A    
-  
 %7D)
+;%0A    // TODO: remove .uniq() once the server has TagCloud api%0A    $scope.tags = _.uniq(tags
 , 'text'
@@ -1151,15 +1151,8 @@
 ');%0A
-    %7D)%0A
   %7D%0A

refactor map ctrl for readability