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