(function($) {
      var emptyPasswordResult  = -2;
      var shortPasswordResult  = -1;
      var badPasswordResult    = 0;
      var goodPasswordResult   = 1;
      var strongPasswordResult = 2;
      
      $.fn.pscAttach = function(options) {
            var opts = $.extend({}, $.fn.pscAttach.defaults, options);
            
            return this.each(function() {
                  var o = $.meta ? $.extend({}, opts, $(this).data()) : opts;
                  
                  $(this).keyup(function() {
                        render(this, o);
                  });
                  
                  render(this, o);
            });
            
            return true;
      };
      
      $.fn.pscAttach.defaults = {
            minimumLength:      8,
            imagePath:          'images/',
            imageShort:         'shortPassword.png',
            imageBad:           'badPassword.png',
            imageGood:          'goodPassword.png',
            imageStrong:        'strongPassword.png',
            backgroundRepeat:   'no-repeat',
            backgroundPosition: 'center right'
      };
      
      $.fn.pscDetach = function() {
            return this.each(function() {
                  $(element).css('background-image', 'none');
                  $(this).unbind('keyup');
            });
      };
      
      function render(element, options) {
            $(element).css('background-repeat', options.backgroundRepeat);
            $(element).css('background-position', options.backgroundPosition);
            
            var resultImage = null;
            switch(strengthCheck($(element).val(), options)) {
                  case shortPasswordResult:
                        resultImage = options.imageShort;
                        break;
                  case badPasswordResult:
                        resultImage = options.imageBad;
                        break;
                  case goodPasswordResult:
                        resultImage = options.imageGood;
                        break;
                  case strongPasswordResult:
                        resultImage = options.imageStrong;
                        break;
                  case emptyPasswordResult:
                  default:
                        resultImage = 'none';
            }
            
            if(resultImage.length && resultImage != 'none') {
                  $(element).css('background-image', 'url(' + options.imagePath + resultImage + ')');
            }
            else {
                  $(element).css('background-image', 'none');
            }
            
            return true;
      };

      function strengthCheck(password, options) {
            if(password.length == 0) {
                  return emptyPasswordResult;
            }
            else if(password.length < options.minimumLength) {
                  return shortPasswordResult;
            }
            
            var numbers    = 0;
            var specials   = 0;
            var smallChars = 0;
            var bigChars   = 0;
            
            for(i = 0; i < password.length; i++) {
                  var check = password.substr(i, 1);
                  
                  if(check.match(/[0-9]/)) {
                        numbers++;
                  }
                  else if(check.match(/[^a-zA-Z0-9]/)) {
                        specials++;
                  }
                  else if(check.match(/[a-z]/)) {
                        smallChars++;
                  }
                  else if(check.match(/[A-Z]/)) {
                        bigChars++;
                  }
            }
            
            var score = (password.length * 4);
            score += (numbers * 10);
            score += (specials * 15);
            score += (smallChars * 2.5);
            score += (bigChars * 5);
            score -= numbers == 0 ? 20 : 0;
            score -= specials == 0 ? 30 : 0;
            score -= smallChars == 0 ? 5 : 0;
            score -= bigChars == 0 ? 10 : 0;
            
            return score > 68 ? strongPasswordResult : (score > 34 ? goodPasswordResult : badPasswordResult);
      };
})(jQuery);
