'use strict';

angular
    .module('lmDirectives')
    .directive('lmCarousel', function() {
        return {
            restrict: 'E',
            controller: function($scope, $attrs, $timeout) {
                $scope.carousel = 0;

                var period = 0;
                var max = 0;
                var timeout;

                function run() {
                    // Out-of-range carousel needs to be immediately corrected.
                    if ($scope.carousel >= max) {
                        $scope.carousel = Math.max(0, max - 1);
                    }

                    // Don't start a timeout if it's already started or if the input parameters are
                    // invalid.
                    if (timeout || period <= 0 || max < 2) {
                        return;
                    }

                    timeout = $timeout(function() {
                        $scope.carousel = ($scope.carousel + 1) % max;
                        timeout = null;
                        run();
                    }, period * 1000);
                }

                // Follow changes to input parameters.
                $attrs.$observe('period', function(value) {
                    period = angular.isDefined(value) ? value : 4;
                    run();
                });
                $attrs.$observe('max', function(value) {
                    max = angular.isDefined(value) ? value : Number.MAX_VALUE;
                    run();
                });
            }
        };
    });

@@ -253,18 +253,124 @@
 var 
-period = 0
+DEFAULT_PERIOD = 4;%0A                var DEFAULT_MAX = Number.MAX_VALUE;%0A%0A                var period = DEFAULT_PERIOD
 ;%0A  
@@ -393,17 +393,27 @@
 r max = 
-0
+DEFAULT_MAX
 ;%0A      
@@ -758,70 +758,8 @@
 rted
- or if the input parameters are%0A                    // invalid
 .%0A  
@@ -791,34 +791,8 @@
 eout
- %7C%7C period %3C= 0 %7C%7C max %3C 2
 ) %7B%0A
@@ -885,33 +885,101 @@
 timeout(
-function
+onTimeout, period * 1000);%0A                %7D%0A%0A                function onTimeout
 () %7B%0A
-    
         
@@ -1049,36 +1049,32 @@
                 
-    
 timeout = null;%0A
@@ -1097,182 +1097,576 @@
     
-    run();%0A                    %7D, period * 1000);%0A                %7D%0A%0A                // Follow changes to input parameters.%0A                $attrs.$observe('period', function
+run();%0A                %7D%0A%0A                function onPeriodChange(value) %7B%0A                    if (angular.isDefined(value)) %7B%0A                        var num = parseFloat(value);%0A                        if (isNaN(num) %7C%7C num %3C= 0) %7B%0A                            throw new Error('lmCarousel: period must be a number %3E 0');%0A                        %7D%0A                        period = num;%0A                    %7D else %7B%0A                        period = DEFAULT_PERIOD;%0A                    %7D%0A                    run();%0A                %7D%0A%0A                function onMaxChange
 (val
@@ -1691,25 +1691,20 @@
         
-period = 
+if (
 angular.
@@ -1723,22 +1723,13 @@
 lue)
- ? value : 4;%0A
+) %7B%0A 
     
@@ -1748,195 +1748,548 @@
     
-run();%0A                %7D);%0A                $attrs.$observe('max', function(value) %7B%0A                    max = angular.isDefined(value) ? value : Number.MAX_VALUE;%0A                    run(
+   var num = parseInt(value, 10);%0A                        if (isNaN(num) %7C%7C num %3C 2) %7B%0A                            throw new Error('lmCarousel: max must be an integer %3E= 2');%0A                        %7D%0A                        max = num;%0A                    %7D else %7B%0A                        max = DEFAULT_MAX;%0A                    %7D%0A                    run();%0A                %7D%0A%0A                // Follow changes to input parameters.%0A                $attrs.$observe('period', onPeriodChange);%0A                $attrs.$observe('max', onMaxChange
 );%0A 
@@ -2303,17 +2303,20 @@
         
-%7D
+run(
 );%0A     

Update lmCarousel for compatibility with Angular 1.3.12