'use strict';

angular
  .module('mwl.calendar')
  .directive('mwlCalendar', function() {

    return {
      templateUrl: 'src/templates/calendar.html',
      restrict: 'EA',
      scope: {
        events: '=',
        view: '=',
        currentDay: '=',
        control: '=',
        eventClick: '&',
        eventEditClick: '&',
        eventDeleteClick: '&',
        editEventHtml: '=',
        deleteEventHtml: '=',
        autoOpen: '=',
        useIsoWeek: '=',
        eventLabel: '@',
        timeLabel: '@',
        dayViewStart: '@',
        dayViewEnd: '@',
        weekTitleLabel: '@',
        timespanClick: '&',
        dayViewSplit: '@'
      },
      controller: function($scope, $timeout, moment, calendarConfig) {

        var self = this;

        var weekTitleLabel = $scope.weekTitleLabel || calendarConfig.titleFormats.week;
        this.titleFunctions = {
          day: function(currentDay) {
            return moment(currentDay).format(calendarConfig.titleFormats.day);
          },
          week: function(currentDay) {
            return weekTitleLabel.replace('{week}', moment(currentDay).week()).replace('{year}', moment(currentDay).format('YYYY'));
          },
          month: function(currentDay) {
            return moment(currentDay).format(calendarConfig.titleFormats.month);
          },
          year: function(currentDay) {
            return moment(currentDay).format(calendarConfig.titleFormats.year);
          }
        };

        this.changeView = function(view, newDay) {
          $scope.view = view;
          $scope.currentDay = newDay;
        };

        $scope.control = $scope.control || {};

        $scope.control.prev = function() {
          $scope.currentDay = moment($scope.currentDay).subtract(1, $scope.view).toDate();
        };

        $scope.control.next = function() {
          $scope.currentDay = moment($scope.currentDay).add(1, $scope.view).toDate();
        };

        $scope.control.getTitle = function() {
          if (!self.titleFunctions[$scope.view]) {
            return '';
          }
          return self.titleFunctions[$scope.view]($scope.currentDay);
        };

        //Auto update the calendar when the locale changes
        var firstRunWatcher = true;
        var unbindWatcher = $scope.$watch(function() {
          return moment.locale();
        }, function(locale) {

          //Maintain backwards compatibility with the previous functionality of the calendar
          if ($scope.useIsoWeek === true) {
            moment.locale(locale, {
              week: {
                dow: 1 //set monday as the first day of the week
              }
            });
          }

          if (firstRunWatcher) { //dont run the first time the calendar is initialised
            firstRunWatcher = false;
            return;
          }
          var originalView = angular.copy($scope.view);
          $scope.view = 'redraw';
          $timeout(function() { //bit of a hacky way to redraw the calendar, should be refactored at some point
            $scope.view = originalView;
          });
        });

        //Remove the watcher when the calendar is destroyed
        var unbindDestroyListener = $scope.$on('$destroy', function() {
          unbindDestroyListener();
          unbindWatcher();
        });

      }
    };

  });

@@ -841,24 +841,25 @@
 s.week;%0A
+%0A
         
 this.tit
@@ -850,21 +850,20 @@
         
-this.
+var 
 titleFun
@@ -1600,32 +1600,37 @@
 %7D;%0A%0A        
-$scope.c
+var calendarC
 ontrol = $sc
@@ -1653,32 +1653,33 @@
 %7D;%0A%0A        
-$scope.c
+calendarC
 ontrol.prev 
@@ -1800,32 +1800,33 @@
 %7D;%0A%0A        
-$scope.c
+calendarC
 ontrol.next 
@@ -1946,24 +1946,25 @@
         
-$scope.c
+calendarC
 ontrol.g
@@ -2001,21 +2001,16 @@
    if (!
-self.
 titleFun
@@ -2088,13 +2088,8 @@
 urn 
-self.
 titl
@@ -2136,32 +2136,75 @@
 y);%0A        %7D;%0A%0A
+        $scope.control = calendarControl;%0A%0A
         //Auto u

Refactor the main calendar controller