1 var jQuery = (function() {
3 // Define a local copy of jQuery
4 var jQuery = function( selector, context ) {
5 // The jQuery object is actually just the init constructor 'enhanced'
6 return new jQuery.fn.init( selector, context );
9 // Map over jQuery in case of overwrite
10 _jQuery = window.jQuery,
12 // Map over the $ in case of overwrite
15 // A central reference to the root jQuery(document)
18 // A simple way to check for HTML strings or ID strings
19 // (both of which we optimize for)
20 quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
22 // Is it a simple selector
23 isSimple = /^.[^:#\[\.,]*$/,
25 // Check if a string has a non-whitespace character in it
29 // Used for trimming whitespace
33 // Check for non-word characters
36 // Match a standalone tag
37 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
40 rvalidchars = /^[\],:{}\s]*$/,
41 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
42 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
43 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
46 rwebkit = /(webkit)[ \/]([\w.]+)/,
47 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
48 rmsie = /(msie) ([\w.]+)/,
49 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
51 // Keep a UserAgent string for use with jQuery.browser
52 userAgent = navigator.userAgent,
54 // For matching the engine and version of the browser
57 // Has the ready events already been bound?
60 // The functions to execute on DOM ready
63 // The ready event handler
66 // Save a reference to some core methods
67 toString = Object.prototype.toString,
68 hasOwn = Object.prototype.hasOwnProperty,
69 push = Array.prototype.push,
70 slice = Array.prototype.slice,
71 trim = String.prototype.trim,
72 indexOf = Array.prototype.indexOf,
74 // [[Class]] -> type pairs
77 jQuery.fn = jQuery.prototype = {
78 init: function( selector, context ) {
79 var match, elem, ret, doc;
81 // Handle $(""), $(null), or $(undefined)
86 // Handle $(DOMElement)
87 if ( selector.nodeType ) {
88 this.context = this[0] = selector;
93 // The body element only exists once, optimize finding it
94 if ( selector === "body" && !context ) {
95 this.context = document;
96 this[0] = document.body;
97 this.selector = "body";
102 // Handle HTML strings
103 if ( typeof selector === "string" ) {
104 // Are we dealing with HTML string or an ID?
105 match = quickExpr.exec( selector );
107 // Verify a match, and that no context was specified for #id
108 if ( match && (match[1] || !context) ) {
110 // HANDLE: $(html) -> $(array)
112 doc = (context ? context.ownerDocument || context : document);
114 // If a single string is passed in and it's a single tag
115 // just do a createElement and skip the rest
116 ret = rsingleTag.exec( selector );
119 if ( jQuery.isPlainObject( context ) ) {
120 selector = [ document.createElement( ret[1] ) ];
121 jQuery.fn.attr.call( selector, context, true );
124 selector = [ doc.createElement( ret[1] ) ];
128 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
129 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
132 return jQuery.merge( this, selector );
136 elem = document.getElementById( match[2] );
138 // Check parentNode to catch when Blackberry 4.6 returns
139 // nodes that are no longer in the document #6963
140 if ( elem && elem.parentNode ) {
141 // Handle the case where IE and Opera return items
142 // by name instead of ID
143 if ( elem.id !== match[2] ) {
144 return rootjQuery.find( selector );
147 // Otherwise, we inject the element directly into the jQuery object
152 this.context = document;
153 this.selector = selector;
158 } else if ( !context && !rnonword.test( selector ) ) {
159 this.selector = selector;
160 this.context = document;
161 selector = document.getElementsByTagName( selector );
162 return jQuery.merge( this, selector );
164 // HANDLE: $(expr, $(...))
165 } else if ( !context || context.jquery ) {
166 return (context || rootjQuery).find( selector );
168 // HANDLE: $(expr, context)
169 // (which is just equivalent to: $(context).find(expr)
171 return jQuery( context ).find( selector );
174 // HANDLE: $(function)
175 // Shortcut for document ready
176 } else if ( jQuery.isFunction( selector ) ) {
177 return rootjQuery.ready( selector );
180 if (selector.selector !== undefined) {
181 this.selector = selector.selector;
182 this.context = selector.context;
185 return jQuery.makeArray( selector, this );
188 // Start with an empty selector
191 // The current version of jQuery being used
194 // The default length of a jQuery object is 0
197 // The number of elements contained in the matched element set
202 toArray: function() {
203 return slice.call( this, 0 );
206 // Get the Nth element in the matched element set OR
207 // Get the whole matched element set as a clean array
208 get: function( num ) {
211 // Return a 'clean' array
214 // Return just the object
215 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
218 // Take an array of elements and push it onto the stack
219 // (returning the new matched element set)
220 pushStack: function( elems, name, selector ) {
221 // Build a new jQuery matched element set
224 if ( jQuery.isArray( elems ) ) {
225 push.apply( ret, elems );
228 jQuery.merge( ret, elems );
231 // Add the old object onto the stack (as a reference)
232 ret.prevObject = this;
234 ret.context = this.context;
236 if ( name === "find" ) {
237 ret.selector = this.selector + (this.selector ? " " : "") + selector;
239 ret.selector = this.selector + "." + name + "(" + selector + ")";
242 // Return the newly-formed element set
246 // Execute a callback for every element in the matched set.
247 // (You can seed the arguments with an array of args, but this is
248 // only used internally.)
249 each: function( callback, args ) {
250 return jQuery.each( this, callback, args );
253 ready: function( fn ) {
254 // Attach the listeners
257 // If the DOM is already ready
258 if ( jQuery.isReady ) {
259 // Execute the function immediately
260 fn.call( document, jQuery );
262 // Otherwise, remember the function for later
263 } else if ( readyList ) {
264 // Add the function to the wait list
265 readyList.push( fn );
274 this.slice( i, +i + 1 );
282 return this.eq( -1 );
286 return this.pushStack( slice.apply( this, arguments ),
287 "slice", slice.call(arguments).join(",") );
290 map: function( callback ) {
291 return this.pushStack( jQuery.map(this, function( elem, i ) {
292 return callback.call( elem, i, elem );
297 return this.prevObject || jQuery(null);
300 // For internal use only.
301 // Behaves like an Array's method, not like a jQuery method.
307 // Give the init function the jQuery prototype for later instantiation
308 jQuery.fn.init.prototype = jQuery.fn;
310 jQuery.extend = jQuery.fn.extend = function() {
311 // copy reference to target object
312 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy, copyIsArray;
314 // Handle a deep copy situation
315 if ( typeof target === "boolean" ) {
317 target = arguments[1] || {};
318 // skip the boolean and the target
322 // Handle case when target is a string or something (possible in deep copy)
323 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
327 // extend jQuery itself if only one argument is passed
328 if ( length === i ) {
333 for ( ; i < length; i++ ) {
334 // Only deal with non-null/undefined values
335 if ( (options = arguments[ i ]) != null ) {
336 // Extend the base object
337 for ( name in options ) {
338 src = target[ name ];
339 copy = options[ name ];
341 // Prevent never-ending loop
342 if ( target === copy ) {
346 // Recurse if we're merging plain objects or arrays
347 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
350 clone = src && jQuery.isArray(src) ? src : [];
353 clone = src && jQuery.isPlainObject(src) ? src : {};
356 // Never move original objects, clone them
357 target[ name ] = jQuery.extend( deep, clone, copy );
359 // Don't bring in undefined values
360 } else if ( copy !== undefined ) {
361 target[ name ] = copy;
367 // Return the modified object
372 noConflict: function( deep ) {
376 window.jQuery = _jQuery;
382 // Is the DOM ready to be used? Set to true once it occurs.
385 // A counter to track how many items to wait for before
386 // the ready event fires. See #6781
389 // Handle when the DOM is ready
390 ready: function( wait ) {
391 // A third-party is pushing the ready event forwards
392 if ( wait === true ) {
396 // Make sure that the DOM is not already loaded
397 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
398 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
399 if ( !document.body ) {
400 return setTimeout( jQuery.ready, 13 );
403 // Remember that the DOM is ready
404 jQuery.isReady = true;
406 // If a normal DOM Ready event fired, decrement, and wait if need be
407 if ( wait !== true && --jQuery.readyWait > 0 ) {
411 // If there are functions bound, to execute
413 // Execute all of them
415 while ( (fn = readyList[ i++ ]) ) {
416 fn.call( document, jQuery );
419 // Reset the list of functions
423 // Trigger any bound ready events
424 if ( jQuery.fn.triggerHandler ) {
425 jQuery( document ).triggerHandler( "ready" );
430 bindReady: function() {
437 // Catch cases where $(document).ready() is called after the
438 // browser event has already occurred.
439 if ( document.readyState === "complete" ) {
440 return jQuery.ready();
443 // Mozilla, Opera and webkit nightlies currently support this event
444 if ( document.addEventListener ) {
445 // Use the handy event callback
446 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
448 // A fallback to window.onload, that will always work
449 window.addEventListener( "load", jQuery.ready, false );
451 // If IE event model is used
452 } else if ( document.attachEvent ) {
453 // ensure firing before onload,
454 // maybe late but safe also for iframes
455 document.attachEvent("onreadystatechange", DOMContentLoaded);
457 // A fallback to window.onload, that will always work
458 window.attachEvent( "onload", jQuery.ready );
460 // If IE and not a frame
461 // continually check to see if the document is ready
462 var toplevel = false;
465 toplevel = window.frameElement == null;
468 if ( document.documentElement.doScroll && toplevel ) {
474 // See test/unit/core.js for details concerning isFunction.
475 // Since version 1.3, DOM methods and functions like alert
476 // aren't supported. They return false on IE (#2968).
477 isFunction: function( obj ) {
478 return jQuery.type(obj) === "function";
481 isArray: Array.isArray || function( obj ) {
482 return jQuery.type(obj) === "array";
485 // A crude way of determining if an object is a window
486 isWindow: function( obj ) {
487 return obj && typeof obj === "object" && "setInterval" in obj;
490 type: function( obj ) {
493 class2type[ toString.call(obj) ] || "object";
496 isPlainObject: function( obj ) {
497 // Must be an Object.
498 // Because of IE, we also have to check the presence of the constructor property.
499 // Make sure that DOM nodes and window objects don't pass through, as well
500 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
504 // Not own constructor property must be Object
505 if ( obj.constructor &&
506 !hasOwn.call(obj, "constructor") &&
507 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
511 // Own properties are enumerated firstly, so to speed up,
512 // if last one is own, then all properties are own.
515 for ( key in obj ) {}
517 return key === undefined || hasOwn.call( obj, key );
520 isEmptyObject: function( obj ) {
521 for ( var name in obj ) {
527 error: function( msg ) {
531 parseJSON: function( data ) {
532 if ( typeof data !== "string" || !data ) {
536 // Make sure leading/trailing whitespace is removed (IE can't handle it)
537 data = jQuery.trim( data );
539 // Make sure the incoming data is actual JSON
540 // Logic borrowed from http://json.org/json2.js
541 if ( rvalidchars.test(data.replace(rvalidescape, "@")
542 .replace(rvalidtokens, "]")
543 .replace(rvalidbraces, "")) ) {
545 // Try to use the native JSON parser first
546 return window.JSON && window.JSON.parse ?
547 window.JSON.parse( data ) :
548 (new Function("return " + data))();
551 jQuery.error( "Invalid JSON: " + data );
557 // Evalulates a script in a global context
558 globalEval: function( data ) {
559 if ( data && rnotwhite.test(data) ) {
560 // Inspired by code by Andrea Giammarchi
561 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
562 var head = document.getElementsByTagName("head")[0] || document.documentElement,
563 script = document.createElement("script");
565 script.type = "text/javascript";
567 if ( jQuery.support.scriptEval ) {
568 script.appendChild( document.createTextNode( data ) );
573 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
574 // This arises when a base node is used (#2709).
575 head.insertBefore( script, head.firstChild );
576 head.removeChild( script );
580 nodeName: function( elem, name ) {
581 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
584 // args is for internal usage only
585 each: function( object, callback, args ) {
587 length = object.length,
588 isObj = length === undefined || jQuery.isFunction(object);
592 for ( name in object ) {
593 if ( callback.apply( object[ name ], args ) === false ) {
598 for ( ; i < length; ) {
599 if ( callback.apply( object[ i++ ], args ) === false ) {
605 // A special, fast, case for the most common use of each
608 for ( name in object ) {
609 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
614 for ( var value = object[0];
615 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
622 // Use native String.trim function wherever possible
625 return text == null ?
630 // Otherwise use our own trimming functionality
632 return text == null ?
634 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
637 // results is for internal usage only
638 makeArray: function( array, results ) {
639 var ret = results || [];
641 if ( array != null ) {
642 // The window, strings (and functions) also have 'length'
643 // The extra typeof function check is to prevent crashes
644 // in Safari 2 (See: #3039)
645 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
646 var type = jQuery.type(array);
648 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
649 push.call( ret, array );
651 jQuery.merge( ret, array );
658 inArray: function( elem, array ) {
659 if ( array.indexOf ) {
660 return array.indexOf( elem );
663 for ( var i = 0, length = array.length; i < length; i++ ) {
664 if ( array[ i ] === elem ) {
672 merge: function( first, second ) {
673 var i = first.length, j = 0;
675 if ( typeof second.length === "number" ) {
676 for ( var l = second.length; j < l; j++ ) {
677 first[ i++ ] = second[ j ];
681 while ( second[j] !== undefined ) {
682 first[ i++ ] = second[ j++ ];
691 grep: function( elems, callback, inv ) {
692 var ret = [], retVal;
695 // Go through the array, only saving the items
696 // that pass the validator function
697 for ( var i = 0, length = elems.length; i < length; i++ ) {
698 retVal = !!callback( elems[ i ], i );
699 if ( inv !== retVal ) {
700 ret.push( elems[ i ] );
707 // arg is for internal usage only
708 map: function( elems, callback, arg ) {
711 // Go through the array, translating each of the items to their
712 // new value (or values).
713 for ( var i = 0, length = elems.length; i < length; i++ ) {
714 value = callback( elems[ i ], i, arg );
716 if ( value != null ) {
717 ret[ ret.length ] = value;
721 return ret.concat.apply( [], ret );
724 // A global GUID counter for objects
727 proxy: function( fn, proxy, thisObject ) {
728 if ( arguments.length === 2 ) {
729 if ( typeof proxy === "string" ) {
731 fn = thisObject[ proxy ];
734 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
740 if ( !proxy && fn ) {
742 return fn.apply( thisObject || this, arguments );
746 // Set the guid of unique handler to the same of original handler, so it can be removed
748 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
751 // So proxy can be declared as an argument
755 // Mutifunctional method to get and set values to a collection
756 // The value/s can be optionally by executed if its a function
757 access: function( elems, key, value, exec, fn, pass ) {
758 var length = elems.length;
760 // Setting many attributes
761 if ( typeof key === "object" ) {
762 for ( var k in key ) {
763 jQuery.access( elems, k, key[k], exec, fn, value );
768 // Setting one attribute
769 if ( value !== undefined ) {
770 // Optionally, function values get executed if exec is true
771 exec = !pass && exec && jQuery.isFunction(value);
773 for ( var i = 0; i < length; i++ ) {
774 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
780 // Getting an attribute
781 return length ? fn( elems[0], key ) : undefined;
785 return (new Date()).getTime();
788 // Use of jQuery.browser is frowned upon.
789 // More details: http://docs.jquery.com/Utilities/jQuery.browser
790 uaMatch: function( ua ) {
791 ua = ua.toLowerCase();
793 var match = rwebkit.exec( ua ) ||
796 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
799 return { browser: match[1] || "", version: match[2] || "0" };
805 // Populate the class2type map
806 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
807 class2type[ "[object " + name + "]" ] = name.toLowerCase();
810 browserMatch = jQuery.uaMatch( userAgent );
811 if ( browserMatch.browser ) {
812 jQuery.browser[ browserMatch.browser ] = true;
813 jQuery.browser.version = browserMatch.version;
816 // Deprecated, use jQuery.browser.webkit instead
817 if ( jQuery.browser.webkit ) {
818 jQuery.browser.safari = true;
822 jQuery.inArray = function( elem, array ) {
823 return indexOf.call( array, elem );
827 // Verify that \s matches non-breaking spaces
828 // (IE fails on this test)
829 if ( !rwhite.test( "\xA0" ) ) {
830 trimLeft = /^[\s\xA0]+/;
831 trimRight = /[\s\xA0]+$/;
834 // All jQuery objects should point back to these
835 rootjQuery = jQuery(document);
837 // Cleanup functions for the document ready method
838 if ( document.addEventListener ) {
839 DOMContentLoaded = function() {
840 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
844 } else if ( document.attachEvent ) {
845 DOMContentLoaded = function() {
846 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
847 if ( document.readyState === "complete" ) {
848 document.detachEvent( "onreadystatechange", DOMContentLoaded );
854 // The DOM ready check for Internet Explorer
855 function doScrollCheck() {
856 if ( jQuery.isReady ) {
861 // If IE is used, use the trick by Diego Perini
862 // http://javascript.nwbox.com/IEContentLoaded/
863 document.documentElement.doScroll("left");
865 setTimeout( doScrollCheck, 1 );
869 // and execute any waiting functions
873 // Expose jQuery to the global object
874 return (window.jQuery = window.$ = jQuery);