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