﻿(function ($) {
/**
 * Sets up a keypress listener on the selected elements that can be used to
 * handle situations where the caps lock may be activated (for example, on a
 * password field). The callback will be called in the scope of the current
 * element with one parameter - a boolean indicating whether or not the caps
 * lock is on.
 *
 * @param   Function    cb      The callback function to call
 * @return  jQuery              The jQuery object
 * @public
 */
jQuery.fn.caps = function(cb){
    return this.keypress(function(e){
        var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
        var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
        var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
        cb.call(this, c);
    });
};
})(jQuery);
