angular.module("app").directive("sqComboInput", function ($rootScope, $state, SQEvents, SQConstants, $timeout, $document) {
    return {
        restrict: 'E',
        replace: false,
        scope: {
            sqModel: '=',
            options: '=',
            optionsValue: '@',
            optionsDisplay: '@',
            label: '@',
            tooltip: '=?',
            change: '&',
            ngModelOptions: '=?',
            req: '@',
            readonly: '@',
            sqOneline: '@',
            name: '@',
            currentAction: '=',
            ngDisabled: '=',
            single: '='
        },
        templateUrl: "../app/directives/sqComboInput/sqComboInput.html",
        controller: function ($scope, $element) {

            $scope.ngModelOptions = angular.isDefined($scope.ngModelOptions) ? $scope.ngModelOptions : {
                updateOn: 'default'
            };
            $scope.setSelectSize = function () {
                $scope.selectHeight = $scope.options.length;
                if ($scope.selectHeight > 10) $scope.selectHeight = 10;
            }
            $scope.selectChange = function (item) {
                $scope.sqModel = item;
                $timeout(function () {
                    $($element).find("select").hide();
                }, 0, false);
            }

            $scope.dropdownSelect = false;

            $scope.inputClick = function () {
                $($element).find("select").show();
            };
        },
        link: function (scope, element, attrs) {
            scope.setSelectSize();

            var watcher1 = scope.$watch('options', function (newValue, oldValue) {
                scope.setSelectSize();
            }, true);

            scope.$on('$destroy', function (event, args) {
                watcher1();
            }, true);

            $document.bind('click', function () {
                $(element).find("select").hide();
            })

            element.bind('click', function (e) {
                e.stopPropagation();
            });

            $(document).on('click', 'body, .sqn-settings-body, .sqn-settings-tabs, .sqn-databanks, .modal-dialog', function () {
                $timeout(function () {
                    $(element).find("select").hide();
                }, 0, false);
            });
        }
    };
});