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