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