$(function()
{
  // Hide focus rectangles by default
  $.keyboardNavigation = false;
  $('a, input').attr('hideFocus', 'true');
  // Show focus rectangles if the keyboard is used for navigation
  $(document).keydown(
    function()
    {
      $.keyboardNavigation = true;
      $('a, input').removeAttr('hideFocus');
    }
  );
  // Hide them again as soon as the mouse is used
  $(document).mousedown(
    function()
    {
      $.keyboardNavigation = false;
      $('a, input').attr('hideFocus', 'true');
    }
  );
  // Disable dragging entirely
  $(document.body).bind('dragstart', 
    function(event)
    {
      event.preventDefault();
    }
  );
  // Also disable text selection (except in input fields)
  $.hoveredInput = 0;
  $(document).bind('selectstart',
    function(event)
    {
      var clickedElement = $(event.srcElement);
      if (clickedElement.is('input') && (clickedElement.get(0) == $.hoveredInput))
      {
        return true;
      }
      return false;
    }
  );
  $('input').hover(
    function ()
    {
      $.hoveredInput = $(this).get(0);
    }, 
    function ()
    {
      $.hoveredInput = 0;
    }
  );
});