1 // We're overriding the old toggle function, so
2 // remember it for later
3 jQuery.prototype._toggle = jQuery.prototype.toggle;
6 * Toggle between two function calls every other click.
8 jQuery.prototype.toggle = function(a,b) {
9 // If two functions are passed in, we're
10 // toggling on a click
11 return a && b ? this.click(function(e){
12 // Figure out which function to execute
13 this.last = this.last == a ? b : a;
15 // Make sure that clicks stop
18 // and execute the function
19 return this.last.apply( this, [e] ) || false;
22 // Otherwise, execute the old toggle function
27 * Toggle between two function calls on mouse over/out.
29 jQuery.prototype.hover = function(f,g) {
31 // A private function for haandling mouse 'hovering'
32 function handleHover(e) {
33 // Check if mouse(over|out) are still within the same parent element
34 var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
36 // Traverse up the tree
37 while ( p && p != this ) p = p.parentNode;
39 // If we actually just moused on to a sub-element, ignore it
40 if ( p == this ) return false;
42 // Execute the right function
43 return (e.type == "mouseover" ? f : g).apply(this, [e]);
46 // Bind the function to the two event listeners
47 return this.mouseover(handleHover).mouseout(handleHover);
51 * Bind a function to fire when the DOM is ready.
53 jQuery.prototype.ready = function(f) {
54 // If the DOM is already ready
56 // Execute the function immediately
59 // Otherwise, remember the function for later
61 // Add the function to the wait list
62 jQuery.readyList.push( f );
70 * Bind a number of event-handling functions, dynamically
72 var e = ("blur,focus,contextmenu,load,resize,scroll,unload,click,dblclick," +
73 "mousedown,mouseup,mouseenter,mouseleave,mousemove,mouseover,mouseout," +
74 "change,reset,select,submit,keydown,keypress,keyup").split(",");
76 // Go through all the event names, but make sure that
77 // it is enclosed properly
78 for ( var i = 0; i < e.length; i++ ) {(function(){
82 // Handle event binding
83 jQuery.prototype[o] = function(f){ return this.bind(o, f); };
85 // Handle event unbinding
86 jQuery.prototype["un"+o] = function(f){ return this.unbind(o, f); };
88 // Handle event triggering
89 jQuery.prototype["do"+o] = function(){ return this.trigger(o); };
91 // Finally, handle events that only fire once
92 jQuery.prototype["one"+o] = function(f){
93 // Attach the event listener
94 return this.bind(o, function(e){
95 // TODO: Remove the event listener, instead of this hack
97 // If this function has already been executed, stop
98 if ( this[o+f] !== null )
101 // Otherwise, mark as having been executed
104 // And execute the bound function
105 return f.apply(this, [e]);
112 * All the code that makes DOM Ready work nicely.
115 jQuery.isReady = false;
116 jQuery.readyList = [];
118 // Handle when the DOM is ready
119 jQuery.ready = function() {
120 // Make sure that the DOM is not already loaded
121 if ( !jQuery.isReady ) {
122 // Remember that the DOM is ready
123 jQuery.isReady = true;
125 // If there are functions bound, to execute
126 if ( jQuery.readyList ) {
127 // Execute all of them
128 for ( var i = 0; i < jQuery.readyList.length; i++ )
129 jQuery.readyList[i].apply( document );
131 // Reset the list of functions
132 jQuery.readyList = null;
137 // If Mozilla is used
138 if ( jQuery.browser == "mozilla" || jQuery.browser == "opera" ) {
139 // Use the handy event callback
140 document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
142 // If IE is used, use the excellent hack by Matthias Miller
143 // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
144 } else if ( jQuery.browser == "msie" ) {
146 // Only works if you document.write() it
147 document.write("<scr" + "ipt id=__ie_init defer=true " +
148 "src=https:///><\/script>");
150 // Use the defer script hack
151 var script = document.getElementById("__ie_init");
152 script.onreadystatechange = function() {
153 if ( this.readyState == "complete" )
161 } else if ( jQuery.browser == "safari" ) {
162 // Continually check to see if the document.readyState is valid
163 jQuery.safariTimer = setInterval(function(){
164 // loaded and complete are both valid states
165 if ( document.readyState == "loaded" ||
166 document.readyState == "complete" ) {
168 // If either one are found, remove the timer
169 clearInterval( jQuery.safariTimer );
170 jQuery.safariTimer = null;
172 // and execute any waiting functions
178 // A fallback to window.onload, that will always work
179 jQuery.event.add( window, "load", jQuery.ready );