/* @projectDescription jQuery Password Strength Plugin - A jQuery plugin to provide accessibility functions
 * @author Tane Piper (digitalspaghetti@gmail.com)
 * @version 2.0
 * @website: http://digitalspaghetti.me.uk/digitalspaghetti
 * @license MIT License: http://www.opensource.org/licenses/mit-license.php
*/
var pstrength = {
	'defaults' : {
		'displayMinChar': true,
		'minChar': 6,
		'minCharText': 'Você deve digitar no mínimo %d caracteres',
		'colors': ['#f00', '#c06', '#f60', '#3c0', '#3f0'],
		'scores': [20, 30, 43, 50],
		'verdicts':	['Fraca', 'Normal', 'Média', 'Segura', 'Muito Segura'],
		'raisePower': 1.4,
		'useAnimation': false
	},
	'ruleScores' : {
		'length': 0,
		'lowercase': 1,
		'uppercase': 3,
		'one_number': 3,
		'three_numbers': 5,
		'one_special_char': 3,
		'two_special_char': 5,
		'upper_lower_combo': 2,
		'letter_number_combo': 2,
		'letter_number_char_combo': 2
	},
	'rules' : {
		'length': true,
		'lowercase': true,
		'uppercase': true,
		'one_number': true,
		'three_numbers': true,
		'one_special_char': true,
		'two_special_char': true,
		'upper_lower_combo': true,
		'letter_number_combo': true,
		'letter_number_char_combo': true
	},
	'validationRules': {
		'length': function(word, score){
			var lenScore = Math.pow(word.length, pstrength.options.raisePower);
		return word.length < pstrength.options.minChar ? lenScore - 100 : lenScore;
		},
		'lowercase': function(word, score){ return word.match(/[a-z]/) && score; },
		'uppercase': function(word, score){ return word.match(/[A-Z]/) && score; },
		'one_number': function(word, score){ return word.match(/\d+/) && score; },
		'three_numbers': function(word, score){ return word.match(/(.*[0-9].*[0-9].*[0-9])/) && score; },
		'one_special_char': function(word, score){ return word.match(/.[!,@,#,$,%,\^,&,*,?,_,~]/) && score; },
		'two_special_char': function(word, score){ return word.match(/(.*[!,@,#,$,%,\^,&,*,?,_,~].*[!,@,#,$,%,\^,&,*,?,_,~])/) && score; },
		'upper_lower_combo': function(word, score){ return word.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/) && score; },
		'letter_number_combo': function(word, score){ return word.match(/([a-zA-Z])/) && word.match(/([0-9])/) && score; },
		'letter_number_char_combo': function(word, score){ return word.match(/([a-zA-Z0-9].*[!,@,#,$,%,\^,&,*,?,_,~])|([!,@,#,$,%,\^,&,*,?,_,~].*[a-zA-Z0-9])/) && score; }
	},
	'attachWidget': function(element){
		var output = pstrength.options.displayMinChar ? '<span class="password-min-char">' + pstrength.options.minCharText.replace('%d', pstrength.options.minChar) + '</span>' : '';
		jQuery(element).after('<div class="password-strength">'+ output + '<span class="password-strength-bar"></span><span class="password-strength-text"></span></div>');
	},
	'addRule': function(name, method, score, active){
		pstrength.rules[name] = active;
		pstrength.ruleScores[name] = score;
		pstrength.validationRules[name] = method;
	},
	'init': function(element, options){
		pstrength.options = jQuery.extend({}, pstrength.defaults, options);
		pstrength.attachWidget(element);
		jQuery(element).keyup(function(){ pstrength.calculateScore(jQuery(this).val()); });
		pstrength.calculateScore(jQuery(element).val());
	},
	'calculateScore': function(word){
		pstrength.totalscore = 0;
		pstrength.width = 0;
		pstrength.verdict = 0;
		for(var key in pstrength.rules){
			if(pstrength.rules.hasOwnProperty(key)){
				if(pstrength.rules[key] === true){
					var result = pstrength.validationRules[key](word,  pstrength.ruleScores[key]);
					if(result)
						pstrength.totalscore += result;
				}
				if(pstrength.totalscore === -200){
					pstrength.width =  '0';
				}else if(pstrength.totalscore < 0 && pstrength.totalscore > -199){
					pstrength.width =  '1';
				}else if(pstrength.totalscore <= pstrength.options.scores[0]){
					pstrength.width =  '1';
				}else if(pstrength.totalscore > pstrength.options.scores[0] && pstrength.totalscore <= pstrength.options.scores[1]){
					pstrength.verdict = 1;
					pstrength.width =  '25';
				}else if(pstrength.totalscore > pstrength.options.scores[1] && pstrength.totalscore <= pstrength.options.scores[2]){
					pstrength.verdict = 2;
					pstrength.width =  '50';
				}else if(pstrength.totalscore > pstrength.options.scores[2] && pstrength.totalscore <= pstrength.options.scores[3]){
					pstrength.verdict = 3;
					pstrength.width =  '75';
				}else{
					pstrength.verdict = 4;
					pstrength.width =  '99';
				}
				pstrength.strColor = pstrength.options.colors[pstrength.verdict];
				pstrength.strText = pstrength.options.verdicts[pstrength.verdict];
				/*if(pstrength.options.displayMinChar)
					jQuery('.password-min-char').css({'display': (word.length >= pstrength.options.minChar ? 'none' : 'block')});*/
				if(pstrength.options.useAnimation){
					jQuery('.password-strength-bar').stop();
					jQuery('.password-strength-bar').animate({opacity: 0.5}, 'fast', 'linear', function(){
						jQuery(this).css({'display': 'block', 'background-color': pstrength.strColor, 'width': pstrength.width + '%', 'height': '2px'});
						jQuery(this).animate({opacity: 1}, 'fast', 'linear');
					});
				}else{
					jQuery('.password-strength-bar').css({'display': 'block', 'background-color': pstrength.strColor, 'width': pstrength.width + '%', 'height': '2px'});
				}
				jQuery('.password-strength-text').css({'color': pstrength.strColor}).text(pstrength.strText);
			}
		}
	}
};

jQuery.extend(jQuery.fn, { 'pstrength': function(options){ return this.each(function(){ pstrength.init(this, options); }); } });