/** 
 * A simple jQuery plugin used to detect whether or not caps lock is on.
 * 
 * @author Matt Blackwood
 * @dependencies jQuery.js 
 */
(function($) {
	$.fn.capslock = function(options) {		
		if (options) {
			$.extend($.fn.capslock.options, options);
		}
		
		$(this).bind("caps_on", $.fn.capslock.options.caps_on);
		$(this).bind("caps_off", $.fn.capslock.options.caps_off);
		
		$(this).keypress(function(e) {
			var which = -1; // Determine what key was pressed safely across browsers.
			if (e.which) {
				which = e.which;
			} else if (e.keyCode) {
				which = e.keyCode;
			}
			
			var shift = false; // Retrieve the shift status safely across browsers.
			if (e.shiftKey) {
				shift = e.shiftKey;
			} else if (e.modifiers) {
				shift = !!(e.modifiers & 4);
			}
			
			// Uppercase letters are between 65 and 90. Lowercase letters are between 97 and 122.
			if (which != null && shift != null) {
				if (((which >= 65 && which <= 90) && !shift) 
						|| ((which >= 97 && which <= 122) && shift)) {
					$(this).trigger("caps_on");
				} else {
					$(this).trigger("caps_off");
				}
			}
		});
		
		return this;
	};
	
	$.fn.capslock.options = {
		caps_on : function() {},
		caps_off :	function() {}
	};
})(jQuery);
