/*************************************************
 * Copyright (c) 2015 Ansible, Inc.
 *
 * All Rights Reserved
 *************************************************/

/* jshint unused: vars */

export default
    [   'moment',
        function(moment) {
            return {
                restrict: 'E',
                scope: {
                    date: '=',
                    minDate: '='
                },
                templateUrl: '/static/js/system-tracking/date-picker/date-picker.partial.html',
                link: function(scope, element, attrs) {

                    // We need to make sure this _never_ recurses, which sometimes happens
                    // with two-way binding.
                    var mustUpdateValue = true;

                    scope.$watch('minDate', function(newValue) {
                        if (newValue) {
                            $('.date', element).systemTrackingDP('setStartDate', newValue.toString());
                        }
                    });

                    scope.$watch('date', function(newValue) {
                        if (newValue) {
                            mustUpdateValue = false;
                            scope.dateValue = newValue.format('L');
                        }
                    }, true);

                    scope.$watch('dateValue', function(newValue) {
                        var newDate = moment(newValue);

                        if (newValue && !newDate.isValid()) {
                            scope.error = "That is not a valid date.";
                        } else if (newValue) {
                            scope.date = newDate;
                        }
                        mustUpdateValue = true;
                    });

                    element.find(".DatePicker").addClass("input-prepend date");
                    element.find(".DatePicker").find(".DatePicker-icon").addClass("add-on");
                    $(".date").systemTrackingDP({
                        autoclose: true
                    });
                }
            };
        }
    ];

@@ -386,17 +386,55 @@
 ate: '='
+,%0A                    autoUpdate: '=?'
 %0A
-
         
@@ -780,16 +780,98 @@
  = true;
+%0A                    scope.autoUpdate = scope.autoUpdate === false ? false : true;
 %0A%0A      
@@ -961,32 +961,204 @@
 if (newValue) %7B%0A
+%0A                            if (scope.autoUpdate && scope.date.isBefore(newValue)) %7B%0A                                scope.date = newValue;%0A                            %7D%0A%0A
                 
@@ -1237,14 +1237,12 @@
 e.to
-String
+Date
 ());

Update right date when left is changed to something greater