
(function($) {
  /**
   * Backport of 'closest' function from JQuery 1.3 to 1.2
   */
  jQuery.fn.closest = function(selector) {
    var pos = jQuery(selector),
      closer = 0;

    return this.map(function() {
      var cur = this;
      while ( cur && cur.ownerDocument ) {
        if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) {
          jQuery.data(cur, "closest", closer);
          return cur;
        }
        cur = cur.parentNode;
        closer++;
      }
    });
  };

  /**
   * Hack to delay all 'focus' calls.
   * Needed because .show().focus() on a hidden thing doesn't work nicely
   * on all browsers.
   */
  var jQueryTriggerOld = jQuery.fn.trigger;
  jQuery.fn.trigger = function(name, data) {
    if (name == 'focus') {
      var self = this;
      setTimeout(function() { jQueryTriggerOld.call(self, 'focus', data); }, 0);
      return self;
    }

    return jQueryTriggerOld.call(this, name, data);
  }
  
  /**
   * Port of 'preventSelect' from Drupal jstools module, to go directly in jquery
   */
  jQuery.fn.preventSelect = function() {
    // IE hack to prevent selection of the text when users click.
    if (document.onselectstart) {
      this.onselectstart = function () {
        return false;
      }
    }
    else {
      $(this).mousedown(function () {
        return false;
      });
    }
  }
})(jQuery);
