/* Minification failed. Returning unminified contents.
(426,22-23): run-time error JS1300: Strict-mode does not allow assignment to undefined variables: i
(426,54-55): run-time error JS1294: Strict-mode does not allow ++ or -- on certain objects: i
 */
'use strict';
var a11yModule = angular.module('a11yModule', []);
a11yModule.factory('a11yCommon', ['$q', '$timeout', function ($q, $timeout) {
    return {
        getKeyCodes: function () {
            return {
                "backspace": 8,
                "tab": 9,
                "enter": 13,
                "shift": 16,
                "ctrl": 17,
                "alt": 18,
                "esc": 27,
                "space": 32,
                "pageup": 33,
                "pagedown": 34,
                "end": 35,
                "home": 36,
                "left": 37,
                "up": 38,
                "right": 39,
                "down": 40,
                "del": 46,
            };
        },
    };
}]);
'use strict';
a11yModule.directive('a11yTypeAhead', ['$timeout', '$sce', '$compile', 'a11yCommon', function ($timeout, $sce, $compile, a11yCommon) {
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        scope: {
            selectedItem: '=',
            config: '=',
        },
        templateUrl: 'Content/static/html/a11yTypeAhead.html',
        controller: function ($scope, $element) {
            var keys = a11yCommon.getKeyCodes();
            $scope.suggestions = null;
            var taTimerPromise = null;
            $scope.searchRequestCounter = 0;
            $scope.nomatchDisplay = "ng-hide";

            $timeout(function () {
                $scope.taLabel = $element.find('#' + $scope.config.a11yUid + '-label');
                $scope.taEdit = $element.find('#' + $scope.config.a11yUid + '-edit');
                $scope.taListBox = $element.find('#' + $scope.config.a11yUid + '-list');
                $scope.taOptions = $scope.taListBox.find('li');
                $scope.taSelected = $scope.taOptions.filter('.selected');
                if (!$scope.config.a11yAriaLabel) $scope.taLabel.text($scope.config.a11yUid + $scope.taLabel.text());
                if ($scope.config.hideLabel) $scope.taLabel.addClass('sr-only');
            }, 1);

            $scope.textBoxKeyDown = function (Event) {
                var currentItem = $scope.taOptions.filter('.selected');
                var currentIndex = $scope.taOptions.index(currentItem);

                switch (Event.keyCode) {
                    case keys.tab: {
                        $scope.selectOption(currentItem, false); //currentItem[0] !=== $scope.taSelected[0]);
                        if ($scope.isOpen() === true) {
                            $scope.hideOption();
            
                        }
                        $scope.nomatchDisplay = "ng-hide";
                        return true;
                        
                    }
                    case keys.esc: {
                        $scope.itemSelect($scope.taSelected, false);
                        $scope.taEdit.select();
                        if ($scope.isOpen() === true) {
                            $scope.hideOption();
                           
                        }
                        Event.stopPropagation();
                        $scope.nomatchDisplay = "ng-hide";
                        return false;
                    }
                    case keys.enter: {
                        if (Event.shiftKey || Event.altKey || Event.ctrlKey) {
                            return true;
                        }

                        if ($scope.isOpen() === false) {
                            if ($scope.taOptions.length > 0) {
                                $scope.showOption();
                                //$scope.nomatchDisplay = "ng-show";
                            }

                            if ($scope.config.onEnterKey) $scope.config.onEnterKey();
                        }
                        else {
                            $scope.selectOption(currentItem, currentItem[0] !== $scope.taSelected[0]);
                            $scope.clearOption();
                            $scope.nomatchDisplay = "ng-hide";
                            $timeout(function () { $scope.taEdit.focus(); }, 5);
                        }
                        $scope.taEdit.select();
                        Event.stopPropagation();
                        return false;
                    }
                    case keys.up: {
                        if (Event.shiftKey || Event.ctrlKey) {
                            return true;
                        }

                        if (Event.altKey && $scope.isOpen()) {
                            $scope.selectOption(currentItem, currentItem[0] !== $scope.taSelected[0]);
                            $scope.taEdit.select();
                            $scope.hideOption();
                            
                        }
                        else {
                            if (currentIndex > 0) {
                                var prev = $scope.taOptions.eq(currentIndex - 1);

                                currentItem.removeClass('selected');
                                prev.addClass('selected');

                                if ($scope.isOpen() === true) {
                                    $scope.itemSelect(prev, false);
                                    $scope.taListBox.scrollTop($scope.getScrollPos(prev));
                                }
                                else {
                                    $scope.itemSelect(prev, true);
                                }

                                $timeout(function () { $scope.taEdit.select(); }, 1);
                            }
                        }

                        Event.stopPropagation();
                        return false;
                    }
                    case keys.down: {
                        if (Event.shiftKey || Event.ctrlKey) {
                            return true;
                        }

                        if (Event.altKey && !$scope.isOpen() && $scope.taOptions.length > 0) {
                            $scope.showOption();
                            $scope.nomatchDisplay = "ng-show";
                        }
                        else {
                            if (currentIndex < $scope.taOptions.length - 1) {
                                var prev1 = $scope.taOptions.eq(currentIndex + 1);

                                currentItem.removeClass('selected');
                                prev1.addClass('selected');

                                if ($scope.isOpen() === true) {
                                    $scope.itemSelect(prev1, false);
                                    $scope.taListBox.scrollTop($scope.getScrollPos(prev1));
                                }
                                else {
                                    $scope.itemSelect(prev1, true);
                                }

                                $timeout(function () { $scope.taEdit.select(); }, 1);
                            }
                        }

                        Event.stopPropagation();
                        return false;
                    }
                }
                return true;
            };

            $scope.textBoxKeyUp = function (Event) {
                if (Event.shiftKey || Event.ctrlKey || Event.altKey) {
                    return true;
                }

                switch (Event.keyCode) {
                    case keys.shift:
                    case keys.ctrl:
                    case keys.alt:
                    case keys.esc:
                    case keys.tab:
                    case keys.enter:
                    case keys.left:
                    case keys.right:
                    case keys.up:
                    case keys.down:
                    case keys.home:
                    case keys.end: {
                        return true;
                    }
                }  //Check

                if (Event.keyCode === keys.backspace || Event.keyCode === keys.del) {
                    $scope.selectedItem = null;
                    $scope.clearOption();
                    $scope.nomatchDisplay = "ng-hide";
                }

                var searchString = $scope.taEdit.val();
                $scope.selectedItem = searchString;
                if (searchString.length >= $scope.config.searchTreshold) {
                    $scope.nomatchDisplay = "ng-hide";
                    $timeout.cancel($scope.searchTimer);
                    $scope.searchTimer = $timeout(function () {
                        $('#' + $scope.config.a11yUid + '-result').text('Please wait while we fetch suggestions.');
                        function searchClosure(searchString, searchCounter) {
                            $scope.searchString = searchString;
                            $scope.config.onSearch(searchString).then(function (result) {
                                if (searchCounter != $scope.searchRequestCounter) return;
                                $scope.searchRequestCounter = 0;
                                $scope.suggestions = result;
                               
                                if (result.length > 0) {
                                    $timeout(function () {
                                        $scope.taOptions = $scope.taListBox.find('li');
                                        if ($scope.isFocus()) {
                                            $('#' + $scope.config.a11yUid + '-result').text('We have received ' + result.length + ' suggestions.  Please use up and down arrow to navigate the list, and press enter to select the list item');

                                            if (!$scope.isOpen()) {
                                                $scope.showOption();
                                                
                                            }
                                        }
                                        if ($scope.taEdit.val().length < $scope.searchTreshold) {
                                            $scope.clearOption("No Suggestions available.");
                                            $scope.nomatchDisplay = "ng-show";
                                        } // check if this is needed
                                    }, 1);


                                }
                                else {
                                    $scope.clearOption("No Suggestions available.");
                                    $scope.nomatchDisplay = "ng-show";
                                }
           
                            });
                        }
                        searchClosure(searchString, ++$scope.searchRequestCounter);
                    }, 500);
                }
                else {
                    if ($scope.isOpen()) {
                        $scope.clearOption("No Suggestions available.");
                        $scope.nomatchDisplay = "ng-show";
                    }
                    
                }
                Event.stopPropagation();
                return false;
            }
       

            $scope.textBoxBlur = function (Event) {
                if ($scope.isOpen() === true) {
                    $scope.selectOption($scope.taOptions.filter('.selected'), false);
                    $timeout.cancel(taTimerPromise);
                    taTimerPromise = $timeout(function () {
                        $scope.hideOption();
                        $scope.nomatchDisplay = "ng-hide";
                    }, 200);
                }

                $('#' + $scope.config.a11yUid + '-result').text("");

                $timeout(function () {
                    $scope.taEdit.removeAttr('aria-owns').removeAttr('aria-activedescendant');
                }, 1000);

                return true;
            }

            $scope.textBoxFocus = function (Event) {
                $timeout(function () {
                    $scope.taEdit.removeAttr('aria-owns').removeAttr('aria-activedescendant');
                    if (!$scope.taEdit.attr('aria-activedescendant') || $scope.taEdit.attr('aria-activedescendant') === "") {
                        if ($scope.taSelected.attr('id')) {
                            $scope.taEdit.attr('aria-owns', $scope.config.a11yUid + '-list').attr('aria-activedescendant', $scope.taSelected.attr('id'));
                        }
                    }
                }, 1000);
            }

            $scope.listBoxFocus = function (Event) {
                $timeout.cancel(taTimerPromise);
                $scope.taEdit.focus();

                Event.stopPropagation();
                return false;
            }

            $scope.listBoxClick = function (Event) {

                if (angular.element(Event.target)[0].nodeName === "UL")
                    return true;

                var currentItemId;
                if (Event.target.nodeName != "LI") {
                    currentItemId = '#' + $(Event.target).parents('li').attr('id')
                }
                else {
                    currentItemId = '#' + Event.target.id;
                }

                var currentItem = $scope.taOptions.filter(currentItemId);
                $scope.selectOption(currentItem, currentItem[0] !== $scope.taSelected[0]);
                $scope.taEdit.select(); // Check

                $scope.hideOption();
                $scope.nomatchDisplay = "ng-hide";

                               
                
                $timeout(function () {
                    $scope.taEdit.focus();
                }, 200);

                Event.stopPropagation();
                
                return false;

            }

            $scope.itemSelect = function (itemSelected, itemSelectTrigger) {
                if (itemSelected.length === 0) return;
                                              
                var selectItemText;
                if ($scope.config.getOptionTemplate) {
                    selectItemText = $scope.config.getOptionText($scope.suggestions[$scope.taOptions.index(itemSelected)]);
                } else {
                    selectItemText = itemSelected.text();
                }

                $scope.taEdit.attr('aria-owns', $scope.config.a11yUid + '-list').attr('aria-activedescendant', itemSelected.attr('id'));
                if (itemSelectTrigger) {
                    $scope.taEdit.val(selectItemText);
                    $scope.taSelected = itemSelected;
                        $scope.selectedItem = $scope.suggestions[$scope.taOptions.index(itemSelected)];
                    $timeout(function () {
                        if ($scope.isFocus() && $scope.config.onSelect) $scope.config.onSelect();
                        $scope.clearOption();
                        //$scope.nomatchDisplay = "ng-show";
                    }, 10);

                }

                

            }

            $scope.isFocus = function () {
                return (document.activeElement.id === $scope.taEdit.get(0).id);
            }

            $scope.isOpen = function () {
                return ($scope.taListBox.css('display') === 'block');
            };

            $scope.hideOption = function () {
                $scope.taOptions.removeClass('selected');
                $scope.taSelected.addClass('selected');
                $scope.taListBox.hide();
                $scope.taEdit.attr('aria-expanded', 'false');
                
                }

            $scope.showOption = function () {
                $scope.taOptions.removeClass('selected');
                $scope.taSelected.addClass('selected');
                $scope.taListBox.show();
                $scope.taEdit.attr('aria-expanded', 'true');

                $scope.taListBox.css({
                    'margin-left': $scope.taEdit.offset().left - $scope.taLabel.offset().left,
                    'width': $scope.taEdit.outerWidth()
                }).scrollTop(0);//$scope.getScrollPos($scope.taSelected)
            }

            $scope.selectOption = function (selectedOption, itemSelectTrigger) {
                $scope.taOptions.removeClass('selected');
                $scope.taSelected = selectedOption.addClass('selected');
                $scope.itemSelect($scope.taSelected, itemSelectTrigger);
            }

            $scope.clearOption = function (ariaMessage) {
                $('#' + $scope.config.a11yUid + '-result').text(ariaMessage);
                $scope.suggestions = null;
                $scope.taOptions = $scope.taOptions.filter(function () { return false; });
                $scope.hideOption();
                
            }

            $scope.selectText = function (start, end) {
                var editNode = $scope.taEdit.get(0);
                if (editNode.setSelectionRange) {
                    editNode.setSelectionRange(start, end);
                }
            }

            $scope.getScrollPos = function (selectedOption) {
                var scrollHeight = 0;
                $scope.taOptions.filter(':lt(' + $scope.taOptions.index(selectedOption) + ')').each(function () {
                    scrollHeight += $(this).outerHeight();
                });
                return scrollHeight;
            }

            $scope.getHtml = function (suggestion) {
                var optionTemplate;
                if ($scope.config.getOptionTemplate) {
                    optionTemplate = $scope.config.getOptionTemplate($scope.searchString, suggestion);
                } else {
                    optionTemplate = $scope.defaultTemplate(suggestion);
                }
                return $sce.trustAsHtml(optionTemplate);
            }

            $scope.defaultTemplate = function (suggestion) {
                if (typeof (suggestion) != "object")
                    return suggestion.toString();
                var keyNames = Object.keys(suggestion);
                var returnValue = '';

                for (i = 0; i < keyNames.length - 1; i++) {
                    returnValue += suggestion[keyNames[i]] + ' ';
                }

                return returnValue.trim();
            }
        }
    }
}]).directive('setOptionAriaLabel', ['$compile', function ($compile) {
    return {
        scope: true,
        link: function (scope, element, attrs) {
            var elmnt;
            attrs.$observe('id', function (myTemplate) {
                if (angular.isDefined(myTemplate)) {
                    if (!scope.config.getOptionTemplate) return;
                    $(element).attr("aria-label", scope.config.getOptionText(scope.suggestion));
                }
            });
        }
    };
}]);

;
'use strict';
angular.module('a11yModule').directive('a11yScrollDiv', ['a11yCommon', function (a11yCommon) {
    return {
        scope: true,
        link: function (scope, element, attrs) {
            var keys = a11yCommon.getKeyCodes();
            scope.scrollDiv = $('#' + attrs['a11yScrollDiv']);
            $(element).on('keydown', function (e) {
                if (!(e.keyCode === keys.up || e.keyCode === keys.down || e.keyCode === keys.pageup || e.keyCode === keys.pagedown))
                    return;

                var multiplier = 1;
                if (e.keyCode === keys.up || e.keyCode === keys.pageup) multiplier = -1;
                scope.scrollDiv[0].scrollTop += multiplier * ((e.keyCode === keys.pageup || e.keyCode === keys.pagedown) ? 60 : 30);
                e.preventDefault();
            });
        }
    };
}]);
