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