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, rootjQuery );
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
39 // Match a standalone tag
40 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
43 rvalidchars = /^[\],:{}\s]*$/,
44 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
45 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
46 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
49 rwebkit = /(webkit)[ \/]([\w.]+)/,
50 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
51 rmsie = /(msie) ([\w.]+)/,
52 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
54 // Keep a UserAgent string for use with jQuery.browser
55 userAgent = navigator.userAgent,
57 // For matching the engine and version of the browser
60 // Has the ready events already been bound?
63 // The deferred used on DOM ready
66 // The ready event handler
69 // Save a reference to some core methods
70 toString = Object.prototype.toString,
71 hasOwn = Object.prototype.hasOwnProperty,
72 push = Array.prototype.push,
73 slice = Array.prototype.slice,
74 trim = String.prototype.trim,
75 indexOf = Array.prototype.indexOf,
77 // [[Class]] -> type pairs
80 jQuery.fn = jQuery.prototype = {
82 init: function( selector, context, rootjQuery ) {
83 var match, elem, ret, doc;
85 // Handle $(""), $(null), or $(undefined)
90 // Handle $(DOMElement)
91 if ( selector.nodeType ) {
92 this.context = this[0] = selector;
97 // The body element only exists once, optimize finding it
98 if ( selector === "body" && !context && document.body ) {
99 this.context = document;
100 this[0] = document.body;
101 this.selector = "body";
106 // Handle HTML strings
107 if ( typeof selector === "string" ) {
108 // Are we dealing with HTML string or an ID?
109 match = quickExpr.exec( selector );
111 // Verify a match, and that no context was specified for #id
112 if ( match && (match[1] || !context) ) {
114 // HANDLE: $(html) -> $(array)
116 context = context instanceof jQuery ? context[0] : context;
117 doc = (context ? context.ownerDocument || context : document);
119 // If a single string is passed in and it's a single tag
120 // just do a createElement and skip the rest
121 ret = rsingleTag.exec( selector );
124 if ( jQuery.isPlainObject( context ) ) {
125 selector = [ document.createElement( ret[1] ) ];
126 jQuery.fn.attr.call( selector, context, true );
129 selector = [ doc.createElement( ret[1] ) ];
133 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
134 selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
137 return jQuery.merge( this, selector );
141 elem = document.getElementById( match[2] );
143 // Check parentNode to catch when Blackberry 4.6 returns
144 // nodes that are no longer in the document #6963
145 if ( elem && elem.parentNode ) {
146 // Handle the case where IE and Opera return items
147 // by name instead of ID
148 if ( elem.id !== match[2] ) {
149 return rootjQuery.find( selector );
152 // Otherwise, we inject the element directly into the jQuery object
157 this.context = document;
158 this.selector = selector;
163 } else if ( !context && !rnonword.test( selector ) ) {
164 this.selector = selector;
165 this.context = document;
166 selector = document.getElementsByTagName( selector );
167 return jQuery.merge( this, selector );
169 // HANDLE: $(expr, $(...))
170 } else if ( !context || context.jquery ) {
171 return (context || rootjQuery).find( selector );
173 // HANDLE: $(expr, context)
174 // (which is just equivalent to: $(context).find(expr)
176 return this.constructor( context ).find( selector );
179 // HANDLE: $(function)
180 // Shortcut for document ready
181 } else if ( jQuery.isFunction( selector ) ) {
182 return rootjQuery.ready( selector );
185 if (selector.selector !== undefined) {
186 this.selector = selector.selector;
187 this.context = selector.context;
190 return jQuery.makeArray( selector, this );
193 // Start with an empty selector
196 // The current version of jQuery being used
199 // The default length of a jQuery object is 0
202 // The number of elements contained in the matched element set
207 toArray: function() {
208 return slice.call( this, 0 );
211 // Get the Nth element in the matched element set OR
212 // Get the whole matched element set as a clean array
213 get: function( num ) {
216 // Return a 'clean' array
219 // Return just the object
220 ( num < 0 ? this[ this.length + num ] : this[ num ] );
223 // Take an array of elements and push it onto the stack
224 // (returning the new matched element set)
225 pushStack: function( elems, name, selector ) {
226 // Build a new jQuery matched element set
227 var ret = this.constructor();
229 if ( jQuery.isArray( elems ) ) {
230 push.apply( ret, elems );
233 jQuery.merge( ret, elems );
236 // Add the old object onto the stack (as a reference)
237 ret.prevObject = this;
239 ret.context = this.context;
241 if ( name === "find" ) {
242 ret.selector = this.selector + (this.selector ? " " : "") + selector;
244 ret.selector = this.selector + "." + name + "(" + selector + ")";
247 // Return the newly-formed element set
251 // Execute a callback for every element in the matched set.
252 // (You can seed the arguments with an array of args, but this is
253 // only used internally.)
254 each: function( callback, args ) {
255 return jQuery.each( this, callback, args );
259 // Attach the listeners
262 // Change ready & apply
263 return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
269 this.slice( i, +i + 1 );
277 return this.eq( -1 );
281 return this.pushStack( slice.apply( this, arguments ),
282 "slice", slice.call(arguments).join(",") );
285 map: function( callback ) {
286 return this.pushStack( jQuery.map(this, function( elem, i ) {
287 return callback.call( elem, i, elem );
292 return this.prevObject || this.constructor(null);
295 // For internal use only.
296 // Behaves like an Array's method, not like a jQuery method.
302 // Give the init function the jQuery prototype for later instantiation
303 jQuery.fn.init.prototype = jQuery.fn;
305 jQuery.extend = jQuery.fn.extend = function() {
306 var options, name, src, copy, copyIsArray, clone,
307 target = arguments[0] || {},
309 length = arguments.length,
312 // Handle a deep copy situation
313 if ( typeof target === "boolean" ) {
315 target = arguments[1] || {};
316 // skip the boolean and the target
320 // Handle case when target is a string or something (possible in deep copy)
321 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
325 // extend jQuery itself if only one argument is passed
326 if ( length === i ) {
331 for ( ; i < length; i++ ) {
332 // Only deal with non-null/undefined values
333 if ( (options = arguments[ i ]) != null ) {
334 // Extend the base object
335 for ( name in options ) {
336 src = target[ name ];
337 copy = options[ name ];
339 // Prevent never-ending loop
340 if ( target === copy ) {
344 // Recurse if we're merging plain objects or arrays
345 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
348 clone = src && jQuery.isArray(src) ? src : [];
351 clone = src && jQuery.isPlainObject(src) ? src : {};
354 // Never move original objects, clone them
355 target[ name ] = jQuery.extend( deep, clone, copy );
357 // Don't bring in undefined values
358 } else if ( copy !== undefined ) {
359 target[ name ] = copy;
365 // Return the modified object
370 noConflict: function( deep ) {
374 window.jQuery = _jQuery;
380 // Is the DOM ready to be used? Set to true once it occurs.
383 // A counter to track how many items to wait for before
384 // the ready event fires. See #6781
387 // Handle when the DOM is ready
388 ready: function( wait ) {
389 // A third-party is pushing the ready event forwards
390 if ( wait === true ) {
394 // Make sure that the DOM is not already loaded
395 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
396 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
397 if ( !document.body ) {
398 return setTimeout( jQuery.ready, 1 );
401 // Remember that the DOM is ready
402 jQuery.isReady = true;
404 // If a normal DOM Ready event fired, decrement, and wait if need be
405 if ( wait !== true && --jQuery.readyWait > 0 ) {
409 // If there are functions bound, to execute
410 readyList.fire( document , [ jQuery ] );
412 // Trigger any bound ready events
413 if ( jQuery.fn.trigger ) {
414 jQuery( document ).trigger( "ready" ).unbind( "ready" );
419 bindReady: function() {
426 // Catch cases where $(document).ready() is called after the
427 // browser event has already occurred.
428 if ( document.readyState === "complete" ) {
429 // Handle it asynchronously to allow scripts the opportunity to delay ready
430 return setTimeout( jQuery.ready, 1 );
433 // Mozilla, Opera and webkit nightlies currently support this event
434 if ( document.addEventListener ) {
435 // Use the handy event callback
436 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
438 // A fallback to window.onload, that will always work
439 window.addEventListener( "load", jQuery.ready, false );
441 // If IE event model is used
442 } else if ( document.attachEvent ) {
443 // ensure firing before onload,
444 // maybe late but safe also for iframes
445 document.attachEvent("onreadystatechange", DOMContentLoaded);
447 // A fallback to window.onload, that will always work
448 window.attachEvent( "onload", jQuery.ready );
450 // If IE and not a frame
451 // continually check to see if the document is ready
452 var toplevel = false;
455 toplevel = window.frameElement == null;
458 if ( document.documentElement.doScroll && toplevel ) {
464 // See test/unit/core.js for details concerning isFunction.
465 // Since version 1.3, DOM methods and functions like alert
466 // aren't supported. They return false on IE (#2968).
467 isFunction: function( obj ) {
468 return jQuery.type(obj) === "function";
471 isArray: Array.isArray || function( obj ) {
472 return jQuery.type(obj) === "array";
475 // A crude way of determining if an object is a window
476 isWindow: function( obj ) {
477 return obj && typeof obj === "object" && "setInterval" in obj;
480 isNaN: function( obj ) {
481 return obj == null || !rdigit.test( obj ) || isNaN( obj );
484 type: function( obj ) {
487 class2type[ toString.call(obj) ] || "object";
490 isPlainObject: function( obj ) {
491 // Must be an Object.
492 // Because of IE, we also have to check the presence of the constructor property.
493 // Make sure that DOM nodes and window objects don't pass through, as well
494 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
498 // Not own constructor property must be Object
499 if ( obj.constructor &&
500 !hasOwn.call(obj, "constructor") &&
501 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
505 // Own properties are enumerated firstly, so to speed up,
506 // if last one is own, then all properties are own.
509 for ( key in obj ) {}
511 return key === undefined || hasOwn.call( obj, key );
514 isEmptyObject: function( obj ) {
515 for ( var name in obj ) {
521 error: function( msg ) {
525 parseJSON: function( data ) {
526 if ( typeof data !== "string" || !data ) {
530 // Make sure leading/trailing whitespace is removed (IE can't handle it)
531 data = jQuery.trim( data );
533 // Make sure the incoming data is actual JSON
534 // Logic borrowed from http://json.org/json2.js
535 if ( rvalidchars.test(data.replace(rvalidescape, "@")
536 .replace(rvalidtokens, "]")
537 .replace(rvalidbraces, "")) ) {
539 // Try to use the native JSON parser first
540 return window.JSON && window.JSON.parse ?
541 window.JSON.parse( data ) :
542 (new Function("return " + data))();
545 jQuery.error( "Invalid JSON: " + data );
549 // Cross-browser xml parsing
550 // (xml & tmp used internally)
551 parseXML: function( data , xml , tmp ) {
553 if ( window.DOMParser ) { // Standard
554 tmp = new DOMParser();
555 xml = tmp.parseFromString( data , "text/xml" );
557 xml = new ActiveXObject( "Microsoft.XMLDOM" );
562 tmp = xml.documentElement;
564 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
565 jQuery.error( "Invalid XML: " + data );
573 // Evalulates a script in a global context
574 globalEval: function( data ) {
575 if ( data && rnotwhite.test(data) ) {
576 // Inspired by code by Andrea Giammarchi
577 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
578 var head = document.getElementsByTagName("head")[0] || document.documentElement,
579 script = document.createElement("script");
581 script.type = "text/javascript";
583 if ( jQuery.support.scriptEval ) {
584 script.appendChild( document.createTextNode( data ) );
589 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
590 // This arises when a base node is used (#2709).
591 head.insertBefore( script, head.firstChild );
592 head.removeChild( script );
596 nodeName: function( elem, name ) {
597 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
600 // args is for internal usage only
601 each: function( object, callback, args ) {
603 length = object.length,
604 isObj = length === undefined || jQuery.isFunction(object);
608 for ( name in object ) {
609 if ( callback.apply( object[ name ], args ) === false ) {
614 for ( ; i < length; ) {
615 if ( callback.apply( object[ i++ ], args ) === false ) {
621 // A special, fast, case for the most common use of each
624 for ( name in object ) {
625 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
630 for ( var value = object[0];
631 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
638 // Use native String.trim function wherever possible
641 return text == null ?
646 // Otherwise use our own trimming functionality
648 return text == null ?
650 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
653 // results is for internal usage only
654 makeArray: function( array, results ) {
655 var ret = results || [];
657 if ( array != null ) {
658 // The window, strings (and functions) also have 'length'
659 // The extra typeof function check is to prevent crashes
660 // in Safari 2 (See: #3039)
661 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
662 var type = jQuery.type(array);
664 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
665 push.call( ret, array );
667 jQuery.merge( ret, array );
674 inArray: function( elem, array ) {
675 if ( array.indexOf ) {
676 return array.indexOf( elem );
679 for ( var i = 0, length = array.length; i < length; i++ ) {
680 if ( array[ i ] === elem ) {
688 merge: function( first, second ) {
689 var i = first.length,
692 if ( typeof second.length === "number" ) {
693 for ( var l = second.length; j < l; j++ ) {
694 first[ i++ ] = second[ j ];
698 while ( second[j] !== undefined ) {
699 first[ i++ ] = second[ j++ ];
708 grep: function( elems, callback, inv ) {
709 var ret = [], retVal;
712 // Go through the array, only saving the items
713 // that pass the validator function
714 for ( var i = 0, length = elems.length; i < length; i++ ) {
715 retVal = !!callback( elems[ i ], i );
716 if ( inv !== retVal ) {
717 ret.push( elems[ i ] );
724 // arg is for internal usage only
725 map: function( elems, callback, arg ) {
728 // Go through the array, translating each of the items to their
729 // new value (or values).
730 for ( var i = 0, length = elems.length; i < length; i++ ) {
731 value = callback( elems[ i ], i, arg );
733 if ( value != null ) {
734 ret[ ret.length ] = value;
738 // Flatten any nested arrays
739 return ret.concat.apply( [], ret );
742 // A global GUID counter for objects
745 proxy: function( fn, proxy, thisObject ) {
746 if ( arguments.length === 2 ) {
747 if ( typeof proxy === "string" ) {
749 fn = thisObject[ proxy ];
752 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
758 if ( !proxy && fn ) {
760 return fn.apply( thisObject || this, arguments );
764 // Set the guid of unique handler to the same of original handler, so it can be removed
766 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
769 // So proxy can be declared as an argument
773 // Mutifunctional method to get and set values to a collection
774 // The value/s can be optionally by executed if its a function
775 access: function( elems, key, value, exec, fn, pass ) {
776 var length = elems.length;
778 // Setting many attributes
779 if ( typeof key === "object" ) {
780 for ( var k in key ) {
781 jQuery.access( elems, k, key[k], exec, fn, value );
786 // Setting one attribute
787 if ( value !== undefined ) {
788 // Optionally, function values get executed if exec is true
789 exec = !pass && exec && jQuery.isFunction(value);
791 for ( var i = 0; i < length; i++ ) {
792 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
798 // Getting an attribute
799 return length ? fn( elems[0], key ) : undefined;
803 return (new Date()).getTime();
806 // Create a simple deferred (one callbacks list)
807 _Deferred: function() {
809 var // callbacks list
811 // stored [ context , args ]
813 // to avoid firing when already doing so
815 // flag to know if the deferred has been cancelled
817 // the deferred itself
820 // done( f1, f2, ...)
825 var args = arguments,
837 for ( i = 0, length = args.length ; i < length ; i++ ) {
839 type = jQuery.type( elem );
840 if ( type === "array" ) {
841 deferred.done.apply( deferred , elem );
842 } else if ( type === "function" ) {
843 callbacks.push( elem );
848 deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
855 // resolve with given context and args
856 fire: function( context , args ) {
857 if ( ! cancelled && ! fired && ! firing ) {
862 while( callbacks[ 0 ] ) {
863 callbacks.shift().apply( context , args );
867 fired = [ context , args ];
874 // resolve with this as context and given arguments
875 resolve: function() {
876 deferred.fire( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments );
880 // Has this deferred been resolved?
881 isResolved: function() {
882 return !!( firing || fired );
896 // Full fledged deferred (two callbacks list)
897 // Typical success/error system
898 Deferred: function( func ) {
900 var deferred = jQuery._Deferred(),
901 failDeferred = jQuery._Deferred();
903 // Add errorDeferred methods and redefine cancel
904 jQuery.extend( deferred , {
906 then: function( doneCallbacks , failCallbacks ) {
907 deferred.done( doneCallbacks ).fail( failCallbacks );
910 fail: failDeferred.done,
911 fireReject: failDeferred.fire,
912 reject: failDeferred.resolve,
913 isRejected: failDeferred.isResolved,
914 // Get a promise for this deferred
915 // If obj is provided, the promise aspect is added to the object
916 promise: function( obj ) {
918 jQuery.each( "then done fail isResolved isRejected".split( " " ) , function( _ , method ) {
919 obj[ method ] = deferred[ method ];
921 obj.promise = function() {
929 // Make sure only one callback list will be used
930 deferred.then( failDeferred.cancel , deferred.cancel );
933 delete deferred.cancel;
935 // Call given func if any
937 func.call( deferred , deferred );
944 when: function( object ) {
945 object = object && jQuery.isFunction( object.promise ) ?
947 jQuery.Deferred().resolve( object );
948 return object.promise();
951 // Use of jQuery.browser is frowned upon.
952 // More details: http://docs.jquery.com/Utilities/jQuery.browser
953 uaMatch: function( ua ) {
954 ua = ua.toLowerCase();
956 var match = rwebkit.exec( ua ) ||
959 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
962 return { browser: match[1] || "", version: match[2] || "0" };
965 subclass: function(){
966 function jQuerySubclass( selector, context ) {
967 return new jQuerySubclass.fn.init( selector, context );
969 jQuerySubclass.superclass = this;
970 jQuerySubclass.fn = jQuerySubclass.prototype = this();
971 jQuerySubclass.fn.constructor = jQuerySubclass;
972 jQuerySubclass.subclass = this.subclass;
973 jQuerySubclass.fn.init = function init( selector, context ) {
974 if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){
975 context = jQuerySubclass(context);
977 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
979 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
980 var rootjQuerySubclass = jQuerySubclass(document);
981 return jQuerySubclass;
987 // Create readyList deferred
988 readyList = jQuery._Deferred();
990 // Populate the class2type map
991 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
992 class2type[ "[object " + name + "]" ] = name.toLowerCase();
995 browserMatch = jQuery.uaMatch( userAgent );
996 if ( browserMatch.browser ) {
997 jQuery.browser[ browserMatch.browser ] = true;
998 jQuery.browser.version = browserMatch.version;
1001 // Deprecated, use jQuery.browser.webkit instead
1002 if ( jQuery.browser.webkit ) {
1003 jQuery.browser.safari = true;
1007 jQuery.inArray = function( elem, array ) {
1008 return indexOf.call( array, elem );
1012 // Verify that \s matches non-breaking spaces
1013 // (IE fails on this test)
1014 if ( !rwhite.test( "\xA0" ) ) {
1015 trimLeft = /^[\s\xA0]+/;
1016 trimRight = /[\s\xA0]+$/;
1019 // All jQuery objects should point back to these
1020 rootjQuery = jQuery(document);
1022 // Cleanup functions for the document ready method
1023 if ( document.addEventListener ) {
1024 DOMContentLoaded = function() {
1025 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1029 } else if ( document.attachEvent ) {
1030 DOMContentLoaded = function() {
1031 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1032 if ( document.readyState === "complete" ) {
1033 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1039 // The DOM ready check for Internet Explorer
1040 function doScrollCheck() {
1041 if ( jQuery.isReady ) {
1046 // If IE is used, use the trick by Diego Perini
1047 // http://javascript.nwbox.com/IEContentLoaded/
1048 document.documentElement.doScroll("left");
1050 setTimeout( doScrollCheck, 1 );
1054 // and execute any waiting functions
1058 // Expose jQuery to the global object
1059 return (window.jQuery = window.$ = jQuery);