/*
 * Capslock 0.1 - jQuery plugin to detect if a user's caps lock is on or not.
 * 
 * Provides global events, "caps_lock_on" and "caps_lock_off", that custom functions can be bound to.
 * This saves you having to bind to each text box or area that needs watching.
 *
 * Copyright (c) 2009 Arthur McLean
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */

; // Just in case the previously included plug-in failed to close with a semi-colon.
(function(jQuery) {
   jQuery.fn.capslock = function() {
      // Variable to hold all fields being checked for Caps Lock
      var capsFields = this;

      // Fucntion to check if Caps Lock is enabled
      function check_caps_lock(e, capsField, capsFieldID)
      {
         var ascii_code = e.which;
         var shift_key = e.shiftKey;
         if((ascii_code >= 65) && (ascii_code <= 90) && !shift_key)
         {
            // Caps Lock is On
            if (jQuery(capsField).hasClass("capsLockOff"))
            {
               jQuery(capsField).removeClass("capsLockOff");
               jQuery(capsField).addClass("capsLockOn");
               jQuery(capsField).css({'margin': '0', 'background-color': '#FFF6BF', 'border-color': '#FFD324', 'border-width': '2px', 'border-bottom-width': '0'});
               jQuery(capsField).nextAll('.capsWarning-' + capsFieldID).show();
            }
         }
         else
         {
            // Caps Lock is Off
            if (jQuery(capsField).hasClass("capsLockOn"))
            {
               jQuery(capsField).removeClass("capsLockOn");
               jQuery(capsField).addClass("capsLockOff");
               jQuery(capsField).nextAll('.capsWarning-' + capsFieldID).hide();
               jQuery(capsField).css({'margin': '', 'background-color': '', 'border-color': '', 'border-width': '', 'border-bottom-width': ''});
            }
         }
      }
      return jQuery(function() {
         capsFields.each(function(i) {
            jQuery(capsFields[i]).addClass("capsLockOff");
            jQuery(capsFields[i]).keypress(function(e){
               check_caps_lock(e, capsFields[i], i);
            });
            jQuery(capsFields[i]).parent().append('<div class="capsWarning-' + i + '" style="display: none; width: ' + (jQuery(capsFields[i]).width() - 21) + 'px; background: #FFD324 url(\'images/icons/messages/warning.png\') 8px 50% no-repeat; padding: 6px 5px 6px 30px; color: #403509"><span style="font-weight: bold">Warning:</span> CAPS Lock is ON</div>');
         });
      });
   };
})(jQuery);
