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