	'use strict';
 
angular.module('qrzk')
.directive('autoFocus', function() {
    return {
        link: {
            pre: function preLink(scope, element, attr) {
                console.debug('prelink called');
                // this fails since the element hasn't rendered
                //element[0].focus();
            },
            post: function postLink(scope, element, attr) {
                console.debug('postlink called');
                // this succeeds since the element has been rendered
                element[0].focus();
            }
        }
    }
})

.directive('focusMe', function($timeout) {
	return {
	    scope: { trigger: '@focusMe' },
	    link: function(scope, element) {
	      scope.$watch('trigger', function(value) {
	        if(value === "true") { 
	          $timeout(function() {
	            element[0].focus(); 
	          });
	        }
	      });
	    }
	  };
})

.directive('validInputValueRange', function(){
	return {
		require : 'ngModel',
		link : function(scope, ele, attrs, ngModel) {
			scope.$watch(attrs.ngModel, function() {
				var minValue = attrs.minValue;
				var maxValue = attrs.maxValue;
				//console.log('-------------------- ngModel ---- minValue: ' + minValue + " ---- maxValue: " + maxValue);
				//console.log(ngModel);
				if(ngModel.$modelValue != null && ngModel.$modelValue.length > 0){
					var modelValue = ngModel.$modelValue;
					
					if(angular.isString(ngModel.$modelValue)){
						var reg = new RegExp('^[1-9][0-9]*$');
						if(reg.test(ngModel.$modelValue)){
							modelValue = parseInt(ngModel.$modelValue);
						}else{
							modelValue = null;
						}
					}

					if(modelValue == null || modelValue > maxValue || modelValue < minValue){
						ngModel.$setValidity('inRange', false);
					}else{
						ngModel.$setValidity('inRange', true);
					}
				}else{
					ngModel.$setValidity('inRange', true);
				}
			});
		}
	};
})
;