Clean up in jQuery.event.add for assigning a handler.guid
[jquery.git] / src / event / event.js
1 /*
2  * A number of helper functions used for managing events.
3  * Many of the ideas behind this code orignated from 
4  * Dean Edwards' addEvent library.
5  */
6 jQuery.event = {
7
8         // Bind an event to an element
9         // Original by Dean Edwards
10         add: function(element, type, handler, data) {
11                 // For whatever reason, IE has trouble passing the window object
12                 // around, causing it to be cloned in the process
13                 if ( jQuery.browser.msie && element.setInterval != undefined )
14                         element = window;
15                 
16                 // Make sure that the function being executed has a unique ID
17                 if ( !handler.guid )
18                         handler.guid = this.guid++;
19                         
20                 // if data is passed, bind to handler 
21                 if( data != undefined ) { 
22                 // Create temporary function pointer to original handler 
23                         var fn = handler; 
24
25                         // Create unique handler function, wrapped around original handler 
26                         handler = function() { 
27                                 // Pass arguments and context to original handler 
28                                 return fn.apply(this, arguments); 
29                         };
30
31                         // Store data in unique handler 
32                         handler.data = data;
33
34                         // Set the guid of unique handler to the same of original handler, so it can be removed 
35                         handler.guid = fn.guid;
36                 }
37
38                 // Init the element's event structure
39                 if (!element.$events)
40                         element.$events = {};
41                 
42                 if (!element.$handle)
43                         element.$handle = function() {
44                                 jQuery.event.handle.apply(element, arguments);
45                         };
46
47                 // Get the current list of functions bound to this event
48                 var handlers = element.$events[type];
49
50                 // Init the event handler queue
51                 if (!handlers) {
52                         handlers = element.$events[type] = {};  
53                         
54                         // And bind the global event handler to the element
55                         if (element.addEventListener)
56                                 element.addEventListener(type, element.$handle, false);
57                         else if (element.attachEvent)
58                                 element.attachEvent("on" + type, element.$handle);
59                 }
60
61                 // Add the function to the element's handler list
62                 handlers[handler.guid] = handler;
63
64                 // Remember the function in a global list (for triggering)
65                 if (!this.global[type])
66                         this.global[type] = [];
67                 this.global[type].push( element );
68         },
69
70         guid: 1,
71         global: {},
72
73         // Detach an event or set of events from an element
74         remove: function(element, type, handler) {
75                 var events = element.$events, ret;
76
77                 if ( events ) {
78                         // type is actually an event object here
79                         if ( type && type.type ) {
80                                 handler = type.handler;
81                                 type = type.type;
82                         }
83                         
84                         if ( !type ) {
85                                 for ( type in events )
86                                         this.remove( element, type );
87
88                         } else if ( events[type] ) {
89                                 // remove the given handler for the given type
90                                 if ( handler )
91                                         delete events[type][handler.guid];
92                                 
93                                 // remove all handlers for the given type
94                                 else
95                                         for ( handler in element.$events[type] )
96                                                 delete events[type][handler];
97
98                                 // remove generic event handler if no more handlers exist
99                                 for ( ret in events[type] ) break;
100                                 if ( !ret ) {
101                                         if (element.removeEventListener)
102                                                 element.removeEventListener(type, element.$handle, false);
103                                         else if (element.detachEvent)
104                                                 element.detachEvent("on" + type, element.$handle);
105                                         ret = null;
106                                         delete events[type];
107                                 }
108                         }
109
110                         // Remove the expando if it's no longer used
111                         for ( ret in events ) break;
112                         if ( !ret )
113                                 element.$handle = element.$events = null;
114                 }
115         },
116
117         trigger: function(type, data, element) {
118                 // Clone the incoming data, if any
119                 data = jQuery.makeArray(data || []);
120
121                 // Handle a global trigger
122                 if ( !element )
123                         jQuery.each( this.global[type] || [], function(){
124                                 jQuery.event.trigger( type, data, this );
125                         });
126
127                 // Handle triggering a single element
128                 else {
129                         var val, ret, fn = jQuery.isFunction( element[ type ] || null );
130                         
131                         // Pass along a fake event
132                         data.unshift( this.fix({ type: type, target: element }) );
133
134                         // Trigger the event
135                         if ( (val = this.handle.apply( element, data )) !== false )
136                                 this.triggered = true;
137
138                         if ( fn && val !== false && !jQuery.nodeName(element, 'a') )
139                                 element[ type ]();
140
141                         this.triggered = false;
142                 }
143         },
144
145         handle: function(event) {
146                 // returned undefined or false
147                 var val;
148                 
149                 // Handle the second event of a trigger and when
150                 // an event is called after a page has unloaded
151                 if ( typeof jQuery == "undefined" || jQuery.event.triggered )
152                   return val;
153
154                 // Empty object is for triggered events with no data
155                 event = jQuery.event.fix( event || window.event || {} ); 
156
157                 var c = this.$events && this.$events[event.type], args = [].slice.call( arguments, 1 );
158                 args.unshift( event );
159
160                 for ( var j in c ) {
161                         // Pass in a reference to the handler function itself
162                         // So that we can later remove it
163                         args[0].handler = c[j];
164                         args[0].data = c[j].data;
165
166                         if ( c[j].apply( this, args ) === false ) {
167                                 event.preventDefault();
168                                 event.stopPropagation();
169                                 val = false;
170                         }
171                 }
172
173                 // Clean up added properties in IE to prevent memory leak
174                 if (jQuery.browser.msie)
175                         event.target = event.preventDefault = event.stopPropagation =
176                                 event.handler = event.data = null;
177
178                 return val;
179         },
180
181         fix: function(event) {
182                 // Fix target property, if necessary
183                 if ( !event.target && event.srcElement )
184                         event.target = event.srcElement;
185
186                 // Add relatedTarget, if necessary
187                 if ( !event.relatedTarget && event.fromElement )
188                         event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement;
189
190                 // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
191                 if ( event.metaKey == null && event.ctrlKey != null )
192                         event.metaKey = event.ctrlKey;
193
194                 // Add which for click: 1 == left; 2 == middle; 3 == right
195                 // Note: button is not normalized, so don't use it
196                 if ( event.which == null && event.button != null )
197                         event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
198
199                 // Calculate pageX/Y if missing and clientX/Y available
200                 if ( event.pageX == null && event.clientX != null ) {
201                         var e = document.documentElement || document.body;
202                         event.pageX = event.clientX + e.scrollLeft;
203                         event.pageY = event.clientY + e.scrollTop;
204                 }
205
206                 // Add which for keypresses: keyCode
207                 if ( (event.which == null || event.type == "keypress") && event.keyCode != null )
208                         event.which = event.keyCode;    
209
210                 // If it's a keypress event, add charCode to IE
211                 if ( event.charCode == null && event.type == "keypress" )
212                         event.charCode = event.keyCode;
213                                 
214                 // check if target is a textnode (safari)
215                 if (jQuery.browser.safari && event.target.nodeType == 3) {
216                         // store a copy of the original event object 
217                         // and clone because target is read only
218                         var originalEvent = event;
219                         event = jQuery.extend({}, originalEvent);
220                         
221                         // get parentnode from textnode
222                         event.target = originalEvent.target.parentNode;
223                         
224                         // add preventDefault and stopPropagation since 
225                         // they will not work on the clone
226                         event.preventDefault = function() {
227                                 return originalEvent.preventDefault();
228                         };
229                         event.stopPropagation = function() {
230                                 return originalEvent.stopPropagation();
231                         };
232                 }
233                 
234                 // fix preventDefault and stopPropagation
235                 if (!event.preventDefault)
236                         event.preventDefault = function() {
237                                 this.returnValue = false;
238                         };
239                         
240                 if (!event.stopPropagation)
241                         event.stopPropagation = function() {
242                                 this.cancelBubble = true;
243                         };
244                         
245                 return event;
246         }
247 };
248
249 jQuery.fn.extend({
250
251         /**
252          * Binds a handler to a particular event (like click) for each matched element.
253          * The event handler is passed an event object that you can use to prevent
254          * default behaviour. To stop both default action and event bubbling, your handler
255          * has to return false.
256          *
257          * In most cases, you can define your event handlers as anonymous functions
258          * (see first example). In cases where that is not possible, you can pass additional
259          * data as the second parameter (and the handler function as the third), see 
260          * second example.
261          *
262          * @example $("p").bind("click", function(){
263          *   alert( $(this).text() );
264          * });
265          * @before <p>Hello</p>
266          * @result alert("Hello")
267          *
268          * @example function handler(event) {
269          *   alert(event.data.foo);
270          * }
271          * $("p").bind("click", {foo: "bar"}, handler)
272          * @result alert("bar")
273          * @desc Pass some additional data to the event handler.
274          *
275          * @example $("form").bind("submit", function() { return false; })
276          * @desc Cancel a default action and prevent it from bubbling by returning false
277          * from your function.
278          *
279          * @example $("form").bind("submit", function(event){
280          *   event.preventDefault();
281          * });
282          * @desc Cancel only the default action by using the preventDefault method.
283          *
284          *
285          * @example $("form").bind("submit", function(event){
286          *   event.stopPropagation();
287          * });
288          * @desc Stop only an event from bubbling by using the stopPropagation method.
289          *
290          * @name bind
291          * @type jQuery
292          * @param String type An event type
293          * @param Object data (optional) Additional data passed to the event handler as event.data
294          * @param Function fn A function to bind to the event on each of the set of matched elements
295          * @cat Events
296          */
297         bind: function( type, data, fn ) {
298                 return this.each(function(){
299                         jQuery.event.add( this, type, fn || data, fn && data );
300                 });
301         },
302         
303         /**
304          * Binds a handler to a particular event (like click) for each matched element.
305          * The handler is executed only once for each element. Otherwise, the same rules
306          * as described in bind() apply.
307          * The event handler is passed an event object that you can use to prevent
308          * default behaviour. To stop both default action and event bubbling, your handler
309          * has to return false.
310          *
311          * In most cases, you can define your event handlers as anonymous functions
312          * (see first example). In cases where that is not possible, you can pass additional
313          * data as the second paramter (and the handler function as the third), see 
314          * second example.
315          *
316          * @example $("p").one("click", function(){
317          *   alert( $(this).text() );
318          * });
319          * @before <p>Hello</p>
320          * @result alert("Hello")
321          *
322          * @name one
323          * @type jQuery
324          * @param String type An event type
325          * @param Object data (optional) Additional data passed to the event handler as event.data
326          * @param Function fn A function to bind to the event on each of the set of matched elements
327          * @cat Events
328          */
329         one: function( type, data, fn ) {
330                 return this.each(function(){
331                         jQuery.event.add( this, type, function(event) {
332                                 jQuery(this).unbind(event);
333                                 return (fn || data).apply( this, arguments);
334                         }, fn && data);
335                 });
336         },
337
338         /**
339          * The opposite of bind, removes a bound event from each of the matched
340          * elements.
341          *
342          * Without any arguments, all bound events are removed.
343          *
344          * If the type is provided, all bound events of that type are removed.
345          *
346          * If the function that was passed to bind is provided as the second argument,
347          * only that specific event handler is removed.
348          *
349          * @example $("p").unbind()
350          * @before <p onclick="alert('Hello');">Hello</p>
351          * @result [ <p>Hello</p> ]
352          *
353          * @example $("p").unbind( "click" )
354          * @before <p onclick="alert('Hello');">Hello</p>
355          * @result [ <p>Hello</p> ]
356          *
357          * @example $("p").unbind( "click", function() { alert("Hello"); } )
358          * @before <p onclick="alert('Hello');">Hello</p>
359          * @result [ <p>Hello</p> ]
360          *
361          * @name unbind
362          * @type jQuery
363          * @param String type (optional) An event type
364          * @param Function fn (optional) A function to unbind from the event on each of the set of matched elements
365          * @cat Events
366          */
367         unbind: function( type, fn ) {
368                 return this.each(function(){
369                         jQuery.event.remove( this, type, fn );
370                 });
371         },
372
373         /**
374          * Trigger a type of event on every matched element. This will also cause
375          * the default action of the browser with the same name (if one exists)
376          * to be executed. For example, passing 'submit' to the trigger()
377          * function will also cause the browser to submit the form. This
378          * default action can be prevented by returning false from one of
379          * the functions bound to the event.
380          *
381          * You can also trigger custom events registered with bind.
382          *
383          * @example $("p").trigger("click")
384          * @before <p click="alert('hello')">Hello</p>
385          * @result alert('hello')
386          *
387          * @example $("p").click(function(event, a, b) {
388          *   // when a normal click fires, a and b are undefined
389          *   // for a trigger like below a refers too "foo" and b refers to "bar"
390          * }).trigger("click", ["foo", "bar"]);
391          * @desc Example of how to pass arbitrary data to an event
392          * 
393          * @example $("p").bind("myEvent",function(event,message1,message2) {
394          *      alert(message1 + ' ' + message2);
395          * });
396          * $("p").trigger("myEvent",["Hello","World"]);
397          * @result alert('Hello World') // One for each paragraph
398          *
399          * @name trigger
400          * @type jQuery
401          * @param String type An event type to trigger.
402          * @param Array data (optional) Additional data to pass as arguments (after the event object) to the event handler
403          * @cat Events
404          */
405         trigger: function( type, data ) {
406                 return this.each(function(){
407                         jQuery.event.trigger( type, data, this );
408                 });
409         },
410
411         /**
412          * Toggle between two function calls every other click.
413          * Whenever a matched element is clicked, the first specified function 
414          * is fired, when clicked again, the second is fired. All subsequent 
415          * clicks continue to rotate through the two functions.
416          *
417          * Use unbind("click") to remove.
418          *
419          * @example $("p").toggle(function(){
420          *   $(this).addClass("selected");
421          * },function(){
422          *   $(this).removeClass("selected");
423          * });
424          * 
425          * @name toggle
426          * @type jQuery
427          * @param Function even The function to execute on every even click.
428          * @param Function odd The function to execute on every odd click.
429          * @cat Events
430          */
431         toggle: function() {
432                 // Save reference to arguments for access in closure
433                 var a = arguments;
434
435                 return this.click(function(e) {
436                         // Figure out which function to execute
437                         this.lastToggle = 0 == this.lastToggle ? 1 : 0;
438                         
439                         // Make sure that clicks stop
440                         e.preventDefault();
441                         
442                         // and execute the function
443                         return a[this.lastToggle].apply( this, [e] ) || false;
444                 });
445         },
446         
447         /**
448          * A method for simulating hovering (moving the mouse on, and off,
449          * an object). This is a custom method which provides an 'in' to a 
450          * frequent task.
451          *
452          * Whenever the mouse cursor is moved over a matched 
453          * element, the first specified function is fired. Whenever the mouse 
454          * moves off of the element, the second specified function fires. 
455          * Additionally, checks are in place to see if the mouse is still within 
456          * the specified element itself (for example, an image inside of a div), 
457          * and if it is, it will continue to 'hover', and not move out 
458          * (a common error in using a mouseout event handler).
459          *
460          * @example $("p").hover(function(){
461          *   $(this).addClass("hover");
462          * },function(){
463          *   $(this).removeClass("hover");
464          * });
465          *
466          * @name hover
467          * @type jQuery
468          * @param Function over The function to fire whenever the mouse is moved over a matched element.
469          * @param Function out The function to fire whenever the mouse is moved off of a matched element.
470          * @cat Events
471          */
472         hover: function(f,g) {
473                 
474                 // A private function for handling mouse 'hovering'
475                 function handleHover(e) {
476                         // Check if mouse(over|out) are still within the same parent element
477                         var p = e.relatedTarget;
478         
479                         // Traverse up the tree
480                         while ( p && p != this ) try { p = p.parentNode } catch(e) { p = this; };
481                         
482                         // If we actually just moused on to a sub-element, ignore it
483                         if ( p == this ) return false;
484                         
485                         // Execute the right function
486                         return (e.type == "mouseover" ? f : g).apply(this, [e]);
487                 }
488                 
489                 // Bind the function to the two event listeners
490                 return this.mouseover(handleHover).mouseout(handleHover);
491         },
492         
493         /**
494          * Bind a function to be executed whenever the DOM is ready to be
495          * traversed and manipulated. This is probably the most important 
496          * function included in the event module, as it can greatly improve
497          * the response times of your web applications.
498          *
499          * In a nutshell, this is a solid replacement for using window.onload, 
500          * and attaching a function to that. By using this method, your bound function 
501          * will be called the instant the DOM is ready to be read and manipulated, 
502          * which is when what 99.99% of all JavaScript code needs to run.
503          *
504          * There is one argument passed to the ready event handler: A reference to
505          * the jQuery function. You can name that argument whatever you like, and
506          * can therefore stick with the $ alias without risk of naming collisions.
507          * 
508          * Please ensure you have no code in your &lt;body&gt; onload event handler, 
509          * otherwise $(document).ready() may not fire.
510          *
511          * You can have as many $(document).ready events on your page as you like.
512          * The functions are then executed in the order they were added.
513          *
514          * @example $(document).ready(function(){ Your code here... });
515          *
516          * @example jQuery(function($) {
517          *   // Your code using failsafe $ alias here...
518          * });
519          * @desc Uses both the [[Core#.24.28_fn_.29|shortcut]] for $(document).ready() and the argument
520          * to write failsafe jQuery code using the $ alias, without relying on the
521          * global alias.
522          *
523          * @name ready
524          * @type jQuery
525          * @param Function fn The function to be executed when the DOM is ready.
526          * @cat Events
527          * @see $.noConflict()
528          * @see $(Function)
529          */
530         ready: function(f) {
531                 // If the DOM is already ready
532                 if ( jQuery.isReady )
533                         // Execute the function immediately
534                         f.apply( document, [jQuery] );
535                         
536                 // Otherwise, remember the function for later
537                 else {
538                         // Add the function to the wait list
539                         jQuery.readyList.push( function() { return f.apply(this, [jQuery]) } );
540                 }
541         
542                 return this;
543         }
544 });
545
546 jQuery.extend({
547         /*
548          * All the code that makes DOM Ready work nicely.
549          */
550         isReady: false,
551         readyList: [],
552         
553         // Handle when the DOM is ready
554         ready: function() {
555                 // Make sure that the DOM is not already loaded
556                 if ( !jQuery.isReady ) {
557                         // Remember that the DOM is ready
558                         jQuery.isReady = true;
559                         
560                         // If there are functions bound, to execute
561                         if ( jQuery.readyList ) {
562                                 // Execute all of them
563                                 jQuery.each( jQuery.readyList, function(){
564                                         this.apply( document );
565                                 });
566                                 
567                                 // Reset the list of functions
568                                 jQuery.readyList = null;
569                         }
570                         // Remove event listener to avoid memory leak
571                         if ( jQuery.browser.mozilla || jQuery.browser.opera )
572                                 document.removeEventListener( "DOMContentLoaded", jQuery.ready, false );
573                         
574                         // Remove script element used by IE hack
575                         jQuery(window).load(function(){ jQuery("#__ie_init").remove(); });
576                 }
577         }
578 });
579
580 new function(){
581
582         /**
583          * Bind a function to the scroll event of each matched element.
584          *
585          * @example $("p").scroll( function() { alert("Hello"); } );
586          * @before <p>Hello</p>
587          * @result <p onscroll="alert('Hello');">Hello</p>
588          *
589          * @name scroll
590          * @type jQuery
591          * @param Function fn A function to bind to the scroll event on each of the matched elements.
592          * @cat Events
593          */
594
595         /**
596          * Bind a function to the submit event of each matched element.
597          *
598          * @example $("#myform").submit( function() {
599          *   return $("input", this).val().length > 0;
600          * } );
601          * @before <form id="myform"><input /></form>
602          * @desc Prevents the form submission when the input has no value entered.
603          *
604          * @name submit
605          * @type jQuery
606          * @param Function fn A function to bind to the submit event on each of the matched elements.
607          * @cat Events
608          */
609
610         /**
611          * Trigger the submit event of each matched element. This causes all of the functions
612          * that have been bound to that submit event to be executed, and calls the browser's
613          * default submit action on the matching element(s). This default action can be prevented
614          * by returning false from one of the functions bound to the submit event.
615          *
616          * Note: This does not execute the submit method of the form element! If you need to
617          * submit the form via code, you have to use the DOM method, eg. $("form")[0].submit();
618          *
619          * @example $("form").submit();
620          * @desc Triggers all submit events registered to the matched form(s), and submits them.
621          *
622          * @name submit
623          * @type jQuery
624          * @cat Events
625          */
626
627         /**
628          * Bind a function to the focus event of each matched element.
629          *
630          * @example $("p").focus( function() { alert("Hello"); } );
631          * @before <p>Hello</p>
632          * @result <p onfocus="alert('Hello');">Hello</p>
633          *
634          * @name focus
635          * @type jQuery
636          * @param Function fn A function to bind to the focus event on each of the matched elements.
637          * @cat Events
638          */
639
640         /**
641          * Trigger the focus event of each matched element. This causes all of the functions
642          * that have been bound to thet focus event to be executed.
643          *
644          * Note: This does not execute the focus method of the underlying elements! If you need to
645          * focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus();
646          *
647          * @example $("p").focus();
648          * @before <p onfocus="alert('Hello');">Hello</p>
649          * @result alert('Hello');
650          *
651          * @name focus
652          * @type jQuery
653          * @cat Events
654          */
655
656         /**
657          * Bind a function to the keydown event of each matched element.
658          *
659          * @example $("p").keydown( function() { alert("Hello"); } );
660          * @before <p>Hello</p>
661          * @result <p onkeydown="alert('Hello');">Hello</p>
662          *
663          * @name keydown
664          * @type jQuery
665          * @param Function fn A function to bind to the keydown event on each of the matched elements.
666          * @cat Events
667          */
668
669         /**
670          * Bind a function to the dblclick event of each matched element.
671          *
672          * @example $("p").dblclick( function() { alert("Hello"); } );
673          * @before <p>Hello</p>
674          * @result <p ondblclick="alert('Hello');">Hello</p>
675          *
676          * @name dblclick
677          * @type jQuery
678          * @param Function fn A function to bind to the dblclick event on each of the matched elements.
679          * @cat Events
680          */
681
682         /**
683          * Bind a function to the keypress event of each matched element.
684          *
685          * @example $("p").keypress( function() { alert("Hello"); } );
686          * @before <p>Hello</p>
687          * @result <p onkeypress="alert('Hello');">Hello</p>
688          *
689          * @name keypress
690          * @type jQuery
691          * @param Function fn A function to bind to the keypress event on each of the matched elements.
692          * @cat Events
693          */
694
695         /**
696          * Bind a function to the error event of each matched element.
697          *
698          * @example $("p").error( function() { alert("Hello"); } );
699          * @before <p>Hello</p>
700          * @result <p onerror="alert('Hello');">Hello</p>
701          *
702          * @name error
703          * @type jQuery
704          * @param Function fn A function to bind to the error event on each of the matched elements.
705          * @cat Events
706          */
707
708         /**
709          * Bind a function to the blur event of each matched element.
710          *
711          * @example $("p").blur( function() { alert("Hello"); } );
712          * @before <p>Hello</p>
713          * @result <p onblur="alert('Hello');">Hello</p>
714          *
715          * @name blur
716          * @type jQuery
717          * @param Function fn A function to bind to the blur event on each of the matched elements.
718          * @cat Events
719          */
720
721         /**
722          * Trigger the blur event of each matched element. This causes all of the functions
723          * that have been bound to that blur event to be executed, and calls the browser's
724          * default blur action on the matching element(s). This default action can be prevented
725          * by returning false from one of the functions bound to the blur event.
726          *
727          * Note: This does not execute the blur method of the underlying elements! If you need to
728          * blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur();
729          *
730          * @example $("p").blur();
731          * @before <p onblur="alert('Hello');">Hello</p>
732          * @result alert('Hello');
733          *
734          * @name blur
735          * @type jQuery
736          * @cat Events
737          */
738
739         /**
740          * Bind a function to the load event of each matched element.
741          *
742          * @example $("p").load( function() { alert("Hello"); } );
743          * @before <p>Hello</p>
744          * @result <p onload="alert('Hello');">Hello</p>
745          *
746          * @name load
747          * @type jQuery
748          * @param Function fn A function to bind to the load event on each of the matched elements.
749          * @cat Events
750          */
751
752         /**
753          * Bind a function to the select event of each matched element.
754          *
755          * @example $("p").select( function() { alert("Hello"); } );
756          * @before <p>Hello</p>
757          * @result <p onselect="alert('Hello');">Hello</p>
758          *
759          * @name select
760          * @type jQuery
761          * @param Function fn A function to bind to the select event on each of the matched elements.
762          * @cat Events
763          */
764
765         /**
766          * Trigger the select event of each matched element. This causes all of the functions
767          * that have been bound to that select event to be executed, and calls the browser's
768          * default select action on the matching element(s). This default action can be prevented
769          * by returning false from one of the functions bound to the select event.
770          *
771          * @example $("p").select();
772          * @before <p onselect="alert('Hello');">Hello</p>
773          * @result alert('Hello');
774          *
775          * @name select
776          * @type jQuery
777          * @cat Events
778          */
779
780         /**
781          * Bind a function to the mouseup event of each matched element.
782          *
783          * @example $("p").mouseup( function() { alert("Hello"); } );
784          * @before <p>Hello</p>
785          * @result <p onmouseup="alert('Hello');">Hello</p>
786          *
787          * @name mouseup
788          * @type jQuery
789          * @param Function fn A function to bind to the mouseup event on each of the matched elements.
790          * @cat Events
791          */
792
793         /**
794          * Bind a function to the unload event of each matched element.
795          *
796          * @example $("p").unload( function() { alert("Hello"); } );
797          * @before <p>Hello</p>
798          * @result <p onunload="alert('Hello');">Hello</p>
799          *
800          * @name unload
801          * @type jQuery
802          * @param Function fn A function to bind to the unload event on each of the matched elements.
803          * @cat Events
804          */
805
806         /**
807          * Bind a function to the change event of each matched element.
808          *
809          * @example $("p").change( function() { alert("Hello"); } );
810          * @before <p>Hello</p>
811          * @result <p onchange="alert('Hello');">Hello</p>
812          *
813          * @name change
814          * @type jQuery
815          * @param Function fn A function to bind to the change event on each of the matched elements.
816          * @cat Events
817          */
818
819         /**
820          * Bind a function to the mouseout event of each matched element.
821          *
822          * @example $("p").mouseout( function() { alert("Hello"); } );
823          * @before <p>Hello</p>
824          * @result <p onmouseout="alert('Hello');">Hello</p>
825          *
826          * @name mouseout
827          * @type jQuery
828          * @param Function fn A function to bind to the mouseout event on each of the matched elements.
829          * @cat Events
830          */
831
832         /**
833          * Bind a function to the keyup event of each matched element.
834          *
835          * @example $("p").keyup( function() { alert("Hello"); } );
836          * @before <p>Hello</p>
837          * @result <p onkeyup="alert('Hello');">Hello</p>
838          *
839          * @name keyup
840          * @type jQuery
841          * @param Function fn A function to bind to the keyup event on each of the matched elements.
842          * @cat Events
843          */
844
845         /**
846          * Bind a function to the click event of each matched element.
847          *
848          * @example $("p").click( function() { alert("Hello"); } );
849          * @before <p>Hello</p>
850          * @result <p onclick="alert('Hello');">Hello</p>
851          *
852          * @name click
853          * @type jQuery
854          * @param Function fn A function to bind to the click event on each of the matched elements.
855          * @cat Events
856          */
857
858         /**
859          * Trigger the click event of each matched element. This causes all of the functions
860          * that have been bound to thet click event to be executed.
861          *
862          * @example $("p").click();
863          * @before <p onclick="alert('Hello');">Hello</p>
864          * @result alert('Hello');
865          *
866          * @name click
867          * @type jQuery
868          * @cat Events
869          */
870
871         /**
872          * Bind a function to the resize event of each matched element.
873          *
874          * @example $("p").resize( function() { alert("Hello"); } );
875          * @before <p>Hello</p>
876          * @result <p onresize="alert('Hello');">Hello</p>
877          *
878          * @name resize
879          * @type jQuery
880          * @param Function fn A function to bind to the resize event on each of the matched elements.
881          * @cat Events
882          */
883
884         /**
885          * Bind a function to the mousemove event of each matched element.
886          *
887          * @example $("p").mousemove( function() { alert("Hello"); } );
888          * @before <p>Hello</p>
889          * @result <p onmousemove="alert('Hello');">Hello</p>
890          *
891          * @name mousemove
892          * @type jQuery
893          * @param Function fn A function to bind to the mousemove event on each of the matched elements.
894          * @cat Events
895          */
896
897         /**
898          * Bind a function to the mousedown event of each matched element.
899          *
900          * @example $("p").mousedown( function() { alert("Hello"); } );
901          * @before <p>Hello</p>
902          * @result <p onmousedown="alert('Hello');">Hello</p>
903          *
904          * @name mousedown
905          * @type jQuery
906          * @param Function fn A function to bind to the mousedown event on each of the matched elements.
907          * @cat Events
908          */
909          
910         /**
911          * Bind a function to the mouseover event of each matched element.
912          *
913          * @example $("p").mouseover( function() { alert("Hello"); } );
914          * @before <p>Hello</p>
915          * @result <p onmouseover="alert('Hello');">Hello</p>
916          *
917          * @name mouseover
918          * @type jQuery
919          * @param Function fn A function to bind to the mousedown event on each of the matched elements.
920          * @cat Events
921          */
922         jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
923                 "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + 
924                 "submit,keydown,keypress,keyup,error").split(","), function(i,o){
925                 
926                 // Handle event binding
927                 jQuery.fn[o] = function(f){
928                         return f ? this.bind(o, f) : this.trigger(o);
929                 };
930                         
931         });
932         
933         // If Mozilla is used
934         if ( jQuery.browser.mozilla || jQuery.browser.opera )
935                 // Use the handy event callback
936                 document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
937         
938         // If IE is used, use the excellent hack by Matthias Miller
939         // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
940         else if ( jQuery.browser.msie ) {
941         
942                 // Only works if you document.write() it
943                 document.write("<scr" + "ipt id=__ie_init defer=true " + 
944                         "src=//:><\/script>");
945         
946                 // Use the defer script hack
947                 var script = document.getElementById("__ie_init");
948                 
949                 // script does not exist if jQuery is loaded dynamically
950                 if ( script ) 
951                         script.onreadystatechange = function() {
952                                 if ( this.readyState != "complete" ) return;
953                                 jQuery.ready();
954                         };
955         
956                 // Clear from memory
957                 script = null;
958         
959         // If Safari  is used
960         } else if ( jQuery.browser.safari )
961                 // Continually check to see if the document.readyState is valid
962                 jQuery.safariTimer = setInterval(function(){
963                         // loaded and complete are both valid states
964                         if ( document.readyState == "loaded" || 
965                                 document.readyState == "complete" ) {
966         
967                                 // If either one are found, remove the timer
968                                 clearInterval( jQuery.safariTimer );
969                                 jQuery.safariTimer = null;
970         
971                                 // and execute any waiting functions
972                                 jQuery.ready();
973                         }
974                 }, 10); 
975
976         // A fallback to window.onload, that will always work
977         jQuery.event.add( window, "load", jQuery.ready );
978         
979 };
980
981 // Clean up after IE to avoid memory leaks
982 if (jQuery.browser.msie)
983         jQuery(window).one("unload", function() {
984                 var global = jQuery.event.global;
985                 for ( var type in global ) {
986                         var els = global[type], i = els.length;
987                         if ( i && type != 'unload' )
988                                 do
989                                         jQuery.event.remove(els[i-1], type);
990                                 while (--i);
991                 }
992         });