/**
The MIT License

Copyright (c) 2008 Marcus Westin marcus@narcvs.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Version 1.0.1
$.fn.fingerprint = function(callback) {
	// iterate and hook up fingerprinting for each matched form element
	return this.each(function() {
		// Timestamp tracker
		var timeStamps = {};
		function resetTimestamps() {
			timeStamps = {
				keydown : [],
				keyup : []
			}
		}
		resetTimestamps();
		// Actual key press handler
		function handleKey(e) {
			if(timeStamps[e.type]) {
				timeStamps[e.type].push(e.timeStamp || Number(new Date));
			}
		}
		$(this)
		// Hook up trackers for the key strokes
		.find('input').each(function(i,el){
			if(el.type == 'password') {
				$(el).keydown(handleKey).keyup(handleKey).keypress(handleKey);
			}
		})
		.end()
		// Append hidden fields to store our values
		.append('<input type="hidden" name="fingerprint-keydown" id="fingerprint-keydown">')
		.append('<input type="hidden" name="fingerprint-keyup" id="fingerprint-keyup">')
		// Submit callback
		.submit(function(){
			// Normalize values before submission
			var t0 = timeStamps.keydown[0];
			for (var i=0; i < timeStamps.keydown.length; i++) {
				timeStamps.keydown[i] -= t0;
				timeStamps.keyup[i] -= t0;
			};
			if (timeStamps.keyup[0] < 0) {
				timeStamps.keyup.shift();
			}
			// callback if there is one
			if (callback) {
				callback(timeStamps);
			}
			// helper function for generating the hidden input value string
			function formatValue(type) {
				var result = sep = '';
				var length = timeStamps['key'+type].length;
				for (var i=0; i < length; i++) {
					result += sep + timeStamps['key'+type][i];
					sep = ',';							
				};
				return result;
			}
			// Find the fingerprint hidden inputs and set their values
			$(this).find('input').each(function(i,el){
				if (el.name == 'fingerprint-keydown') {
					el.value = formatValue('down');
				} else if (el.name == 'fingerprint-keyup') {
					el.value = formatValue('up');						
				}
			});
			resetTimestamps();
		});
	});		
};
