1 // We're overriding the old toggle function, so
2 // remember it for later
3 $.fn._toggle = $.fn.toggle;
6 * Toggle between two function calls every other click.
8 $.fn.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 don't pass through
18 // and execute the function
19 return $.apply( this, this.last, [e] ) || false;
22 // Otherwise, execute the old toggle function
27 * Toggle between two function calls on mouse over/out.
29 $.fn.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.fromElement || e.toElement || e.relatedTarget;
35 while ( p && p != this ) p = p.parentNode;
37 // If we actually just moused on to a sub-element, ignore it
38 if ( p == this ) return false;
40 // Execute the right function
41 return $.apply(this,e.type == 'mouseover' ? f : g,[e]);
44 // Bind the function to the two event listeners
45 return this.mouseover(handleHover).mouseout(handleHover);
49 * Bind a function to fire when the DOM is ready.
51 $.fn.ready = function(f) {
52 // If the DOM is already ready
54 // Execute the function immediately
55 $.apply( document, f );
57 // Otherwise, remember the function for later
59 // Add the function to the wait list
60 $.readyList.push( f );
68 * Bind a number of event-handling functions, dynamically
70 var e = ["blur","focus","contextmenu","load","resize","scroll","unload",
71 "click","dblclick","mousedown","mouseup","mouseenter","mouseleave",
72 "mousemove","mouseover","mouseout","change","reset","select","submit",
73 "keydown","keypress","keyup","abort","error","ready"];
75 // Go through all the event names, but make sure that
76 // it is enclosed properly
77 for ( var i = 0; i < e.length; i++ ) {(function(){
81 // Handle event binding
82 $.fn[o] = function(f){ return this.bind(o, f); };
84 // Handle event unbinding
85 $.fn["un"+o] = function(f){ return this.unbind(o, f); };
87 // Handle event triggering
88 $.fn["do"+o] = function(){ return this.trigger(o); };
90 // Finally, handle events that only fire once
91 $.fn["one"+o] = function(f){
92 // Attach the event listener
93 return this.bind(o, function(e){
94 // TODO: Remove the event listener, instead of this hack
96 // If this function has already been executed, stop
97 if ( this[o+f] !== null )
100 // Otherwise, mark as having been executed
103 // And execute the bound function
104 return $.apply(this,f,[e]);
111 * All the code that makes DOM Ready work nicely.
117 // Handle when the DOM is ready
118 $.ready = function() {
119 // Make sure that the DOM hasn't already loaded
121 // Remember that the DOM is ready
124 // If there are functions bound, to execute
126 // Execute all of them
127 for ( var i = 0; i < $.readyList.length; i++ )
128 $.apply( document, $.readyList[i] );
130 // Reset the list of functions
136 // If Mozilla is used
137 if ( $.browser == "mozilla" || $.browser == "opera" ) {
138 // Use the handy event callback
139 $.event.add( document, "DOMContentLoaded", $.ready );
141 // If IE is used, use the excellent hack by Matthias Miller
142 // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
143 } else if ( $.browser == "msie" ) {
145 // Only works if you document.write() it
146 document.write('<scr' + 'ipt id=__ie_init defer=true ' +
147 'src=javascript:void(0)><\/script>');
149 // Use the defer script hack
150 var script = document.getElementById('__ie_init');
151 script.onreadystatechange = function() {
152 if ( this.readyState == 'complete' )
160 } else if ( $.browser == "safari" ) {
161 // Continually check to see if the document.readyState is valid
162 $.safariTimer = setInterval(function(){
163 // loaded and complete are both valid states
164 if ( document.readyState == "loaded" ||
165 document.readyState == "complete" ) {
167 // If either one are found, remove the timer
168 clearInterval( $.safariTimer );
169 $.safariTimer = null;
171 // and execute any waiting functions
177 // A fallback to window.onload, that will always work
178 $.event.add( window, "load", $.ready );