/*********************************************************
**
**   Plugin by Small Hadron Collider
**   http://www.smallhadroncollider.com
**
**   Distributed under a Creative Commons by-sa License
**   http://creativecommons.org/licenses/by-sa/3.0/
**
**   For usage see:
**   http://plugins.jquery.com/project/chronoStrength
**
*********************************************************/

jQuery.fn.chronoStrength = function(options)
{
	var defaults = {  
		calculationsPerSecond: 10000000,
		placeInto: 'none'
	};  
	
	var options = jQuery.extend(defaults, options); 
	
	if (options.placeInto == 'none')
	{
		jQuery(this).after('<div class="chronostrength-box"></div>');
		var placeInto = 'div.chronostrength-box';
	}
	else
	{
		var placeInto = options.placeInto;
	}
	
	jQuery(this).keyup(function()
	{
		if (event.keyCode == 27)
		{
			jQuery(this).val("");
		}
		
		var password = $(this).val();
		
		if (password != "")
		{			
			var calculationsPerSecond = options.calculationsPerSecond;
			var possibleCharacters = 0;
			if (password.match(/[a-z]/)) { possibleCharacters += 26; }
			if (password.match(/[A-Z]/)) { possibleCharacters += 26; }
			if (password.match(/\d+/)) { possibleCharacters += 10; }
			if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) {possibleCharacters += 13};
			
			var possibleCombinations = Math.pow(possibleCharacters, password.length);
			var computerTimeInSecs = possibleCombinations / calculationsPerSecond;
			
			var arrayOfPeriods = new Array();
			arrayOfPeriods['minute'] = 60;
			arrayOfPeriods['hour'] = arrayOfPeriods['minute'] * 60;
			arrayOfPeriods['day'] = arrayOfPeriods['hour'] * 24;
			arrayOfPeriods['year'] = arrayOfPeriods['day'] * 365.25;
			arrayOfPeriods['thousand years'] = arrayOfPeriods['year'] * 1000;
			arrayOfPeriods['million years'] = arrayOfPeriods['thousand years'] * 1000;
			arrayOfPeriods['billion years'] = arrayOfPeriods['million years'] * 1000;
			arrayOfPeriods['trillion years'] = arrayOfPeriods['billion years'] * 1000;
			arrayOfPeriods['quadtrillion years'] = arrayOfPeriods['trillion years'] * 1000;
			arrayOfPeriods['quintillion years'] = arrayOfPeriods['quadtrillion years'] * 1000;
			arrayOfPeriods['sextillion years'] = arrayOfPeriods['quintillion years'] * 1000;
			arrayOfPeriods['septillion years'] = arrayOfPeriods['sextillion years'] * 1000;
			arrayOfPeriods['octillion years'] = arrayOfPeriods['septillion years'] * 1000;
			arrayOfPeriods['nonillion years'] = arrayOfPeriods['octillion years'] * 1000;
			
			var periodType = 'second';
			var strength = '';
			
			if (computerTimeInSecs < 1) { strength = computerTimeInSecs+" seconds";}
			else
			{
				var intoThousands = 's';
				var newTime = Math.floor(computerTimeInSecs);
				
				for (var i in arrayOfPeriods)
				{
					if (computerTimeInSecs < arrayOfPeriods[i]) { break; }
					else
					{
						if (i == "thousand years") { intoThousands = ''; }
						newTime = Math.floor(computerTimeInSecs / arrayOfPeriods[i]);
						periodType = i;
					}
				}
				
				if (newTime == 1) { strength = "About a "+periodType; }
				else
				{
					newTime += '';
					
					var regex = /(\d+)(\d{3})/;
					
					while (regex.test(newTime))
					{
						newTime = newTime.replace(regex, '$1' + ',' + '$2');
					}
					
					strength = newTime+" "+periodType+intoThousands;
				}
			}
			
			jQuery(placeInto).html(strength);
		}
		else
		{
			jQuery(placeInto).html('');
		}
	});
};
