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 // Check if a string has a non-whitespace character in it
25 // Used for trimming whitespace
32 // Match a standalone tag
33 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
36 rvalidchars = /^[\],:{}\s]*$/,
37 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
38 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
39 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
42 rwebkit = /(webkit)[ \/]([\w.]+)/,
43 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
44 rmsie = /(msie) ([\w.]+)/,
45 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
47 // Keep a UserAgent string for use with jQuery.browser
48 userAgent = navigator.userAgent,
50 // For matching the engine and version of the browser
53 // Has the ready events already been bound?
56 // The deferred used on DOM ready
59 // Promise methods (with equivalent for invert)
61 then: 0, // will be overwritten for invert
64 isResolved: "isRejected",
65 isRejected: "isResolved",
70 // The ready event handler
73 // Save a reference to some core methods
74 toString = Object.prototype.toString,
75 hasOwn = Object.prototype.hasOwnProperty,
76 push = Array.prototype.push,
77 slice = Array.prototype.slice,
78 trim = String.prototype.trim,
79 indexOf = Array.prototype.indexOf,
81 // [[Class]] -> type pairs
84 jQuery.fn = jQuery.prototype = {
86 init: function( selector, context, rootjQuery ) {
87 var match, elem, ret, doc;
89 // Handle $(""), $(null), or $(undefined)
94 // Handle $(DOMElement)
95 if ( selector.nodeType ) {
96 this.context = this[0] = selector;
101 // The body element only exists once, optimize finding it
102 if ( selector === "body" && !context && document.body ) {
103 this.context = document;
104 this[0] = document.body;
105 this.selector = "body";
110 // Handle HTML strings
111 if ( typeof selector === "string" ) {
112 // Are we dealing with HTML string or an ID?
113 match = quickExpr.exec( selector );
115 // Verify a match, and that no context was specified for #id
116 if ( match && (match[1] || !context) ) {
118 // HANDLE: $(html) -> $(array)
120 context = context instanceof jQuery ? context[0] : context;
121 doc = (context ? context.ownerDocument || context : document);
123 // If a single string is passed in and it's a single tag
124 // just do a createElement and skip the rest
125 ret = rsingleTag.exec( selector );
128 if ( jQuery.isPlainObject( context ) ) {
129 selector = [ document.createElement( ret[1] ) ];
130 jQuery.fn.attr.call( selector, context, true );
133 selector = [ doc.createElement( ret[1] ) ];
137 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
138 selector = (ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment).childNodes;
141 return jQuery.merge( this, selector );
145 elem = document.getElementById( match[2] );
147 // Check parentNode to catch when Blackberry 4.6 returns
148 // nodes that are no longer in the document #6963
149 if ( elem && elem.parentNode ) {
150 // Handle the case where IE and Opera return items
151 // by name instead of ID
152 if ( elem.id !== match[2] ) {
153 return rootjQuery.find( selector );
156 // Otherwise, we inject the element directly into the jQuery object
161 this.context = document;
162 this.selector = selector;
166 // HANDLE: $(expr, $(...))
167 } else if ( !context || context.jquery ) {
168 return (context || rootjQuery).find( selector );
170 // HANDLE: $(expr, context)
171 // (which is just equivalent to: $(context).find(expr)
173 return this.constructor( context ).find( selector );
176 // HANDLE: $(function)
177 // Shortcut for document ready
178 } else if ( jQuery.isFunction( selector ) ) {
179 return rootjQuery.ready( selector );
182 if (selector.selector !== undefined) {
183 this.selector = selector.selector;
184 this.context = selector.context;
187 return jQuery.makeArray( selector, this );
190 // Start with an empty selector
193 // The current version of jQuery being used
196 // The default length of a jQuery object is 0
199 // The number of elements contained in the matched element set
204 toArray: function() {
205 return slice.call( this, 0 );
208 // Get the Nth element in the matched element set OR
209 // Get the whole matched element set as a clean array
210 get: function( num ) {
213 // Return a 'clean' array
216 // Return just the object
217 ( num < 0 ? this[ this.length + num ] : this[ num ] );
220 // Take an array of elements and push it onto the stack
221 // (returning the new matched element set)
222 pushStack: function( elems, name, selector ) {
223 // Build a new jQuery matched element set
224 var ret = this.constructor();
226 if ( jQuery.isArray( elems ) ) {
227 push.apply( ret, elems );
230 jQuery.merge( ret, elems );
233 // Add the old object onto the stack (as a reference)
234 ret.prevObject = this;
236 ret.context = this.context;
238 if ( name === "find" ) {
239 ret.selector = this.selector + (this.selector ? " " : "") + selector;
241 ret.selector = this.selector + "." + name + "(" + selector + ")";
244 // Return the newly-formed element set
248 // Execute a callback for every element in the matched set.
249 // (You can seed the arguments with an array of args, but this is
250 // only used internally.)
251 each: function( callback, args ) {
252 return jQuery.each( this, callback, args );
255 ready: function( fn ) {
256 // Attach the listeners
260 readyList.done( fn );
268 this.slice( i, +i + 1 );
276 return this.eq( -1 );
280 return this.pushStack( slice.apply( this, arguments ),
281 "slice", slice.call(arguments).join(",") );
284 map: function( callback ) {
285 return this.pushStack( jQuery.map(this, function( elem, i ) {
286 return callback.call( elem, i, elem );
291 return this.prevObject || this.constructor(null);
294 // For internal use only.
295 // Behaves like an Array's method, not like a jQuery method.
301 // Give the init function the jQuery prototype for later instantiation
302 jQuery.fn.init.prototype = jQuery.fn;
304 jQuery.extend = jQuery.fn.extend = function() {
305 var options, name, src, copy, copyIsArray, clone,
306 target = arguments[0] || {},
308 length = arguments.length,
311 // Handle a deep copy situation
312 if ( typeof target === "boolean" ) {
314 target = arguments[1] || {};
315 // skip the boolean and the target
319 // Handle case when target is a string or something (possible in deep copy)
320 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
324 // extend jQuery itself if only one argument is passed
325 if ( length === i ) {
330 for ( ; i < length; i++ ) {
331 // Only deal with non-null/undefined values
332 if ( (options = arguments[ i ]) != null ) {
333 // Extend the base object
334 for ( name in options ) {
335 src = target[ name ];
336 copy = options[ name ];
338 // Prevent never-ending loop
339 if ( target === copy ) {
343 // Recurse if we're merging plain objects or arrays
344 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
347 clone = src && jQuery.isArray(src) ? src : [];
350 clone = src && jQuery.isPlainObject(src) ? src : {};
353 // Never move original objects, clone them
354 target[ name ] = jQuery.extend( deep, clone, copy );
356 // Don't bring in undefined values
357 } else if ( copy !== undefined ) {
358 target[ name ] = copy;
364 // Return the modified object
369 noConflict: function( deep ) {
373 window.jQuery = _jQuery;
379 // Is the DOM ready to be used? Set to true once it occurs.
382 // A counter to track how many items to wait for before
383 // the ready event fires. See #6781
386 // Handle when the DOM is ready
387 ready: function( wait ) {
388 // A third-party is pushing the ready event forwards
389 if ( wait === true ) {
393 // Make sure that the DOM is not already loaded
394 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
395 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
396 if ( !document.body ) {
397 return setTimeout( jQuery.ready, 1 );
400 // Remember that the DOM is ready
401 jQuery.isReady = true;
403 // If a normal DOM Ready event fired, decrement, and wait if need be
404 if ( wait !== true && --jQuery.readyWait > 0 ) {
408 // If there are functions bound, to execute
409 readyList.resolveWith( document, [ jQuery ] );
411 // Trigger any bound ready events
412 if ( jQuery.fn.trigger ) {
413 jQuery( document ).trigger( "ready" ).unbind( "ready" );
418 bindReady: function() {
425 // Catch cases where $(document).ready() is called after the
426 // browser event has already occurred.
427 if ( document.readyState === "complete" ) {
428 // Handle it asynchronously to allow scripts the opportunity to delay ready
429 return setTimeout( jQuery.ready, 1 );
432 // Mozilla, Opera and webkit nightlies currently support this event
433 if ( document.addEventListener ) {
434 // Use the handy event callback
435 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
437 // A fallback to window.onload, that will always work
438 window.addEventListener( "load", jQuery.ready, false );
440 // If IE event model is used
441 } else if ( document.attachEvent ) {
442 // ensure firing before onload,
443 // maybe late but safe also for iframes
444 document.attachEvent("onreadystatechange", DOMContentLoaded);
446 // A fallback to window.onload, that will always work
447 window.attachEvent( "onload", jQuery.ready );
449 // If IE and not a frame
450 // continually check to see if the document is ready
451 var toplevel = false;
454 toplevel = window.frameElement == null;
457 if ( document.documentElement.doScroll && toplevel ) {
463 // See test/unit/core.js for details concerning isFunction.
464 // Since version 1.3, DOM methods and functions like alert
465 // aren't supported. They return false on IE (#2968).
466 isFunction: function( obj ) {
467 return jQuery.type(obj) === "function";
470 isArray: Array.isArray || function( obj ) {
471 return jQuery.type(obj) === "array";
474 // A crude way of determining if an object is a window
475 isWindow: function( obj ) {
476 return obj && typeof obj === "object" && "setInterval" in obj;
479 isNaN: function( obj ) {
480 return obj == null || !rdigit.test( obj ) || isNaN( obj );
483 type: function( obj ) {
486 class2type[ toString.call(obj) ] || "object";
489 isPlainObject: function( obj ) {
490 // Must be an Object.
491 // Because of IE, we also have to check the presence of the constructor property.
492 // Make sure that DOM nodes and window objects don't pass through, as well
493 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
497 // Not own constructor property must be Object
498 if ( obj.constructor &&
499 !hasOwn.call(obj, "constructor") &&
500 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
504 // Own properties are enumerated firstly, so to speed up,
505 // if last one is own, then all properties are own.
508 for ( key in obj ) {}
510 return key === undefined || hasOwn.call( obj, key );
513 isEmptyObject: function( obj ) {
514 for ( var name in obj ) {
520 error: function( msg ) {
524 parseJSON: function( data ) {
525 if ( typeof data !== "string" || !data ) {
529 // Make sure leading/trailing whitespace is removed (IE can't handle it)
530 data = jQuery.trim( data );
532 // Make sure the incoming data is actual JSON
533 // Logic borrowed from http://json.org/json2.js
534 if ( rvalidchars.test(data.replace(rvalidescape, "@")
535 .replace(rvalidtokens, "]")
536 .replace(rvalidbraces, "")) ) {
538 // Try to use the native JSON parser first
539 return window.JSON && window.JSON.parse ?
540 window.JSON.parse( data ) :
541 (new Function("return " + data))();
544 jQuery.error( "Invalid JSON: " + data );
548 // Cross-browser xml parsing
549 // (xml & tmp used internally)
550 parseXML: function( data , xml , tmp ) {
552 if ( window.DOMParser ) { // Standard
553 tmp = new DOMParser();
554 xml = tmp.parseFromString( data , "text/xml" );
556 xml = new ActiveXObject( "Microsoft.XMLDOM" );
561 tmp = xml.documentElement;
563 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
564 jQuery.error( "Invalid XML: " + data );
572 // Evalulates a script in a global context
573 globalEval: function( data ) {
574 if ( data && rnotwhite.test(data) ) {
575 // Inspired by code by Andrea Giammarchi
576 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
577 var head = document.getElementsByTagName("head")[0] || document.documentElement,
578 script = document.createElement("script");
580 script.type = "text/javascript";
582 if ( jQuery.support.scriptEval() ) {
583 script.appendChild( document.createTextNode( data ) );
588 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
589 // This arises when a base node is used (#2709).
590 head.insertBefore( script, head.firstChild );
591 head.removeChild( script );
595 nodeName: function( elem, name ) {
596 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
599 // args is for internal usage only
600 each: function( object, callback, args ) {
602 length = object.length,
603 isObj = length === undefined || jQuery.isFunction(object);
607 for ( name in object ) {
608 if ( callback.apply( object[ name ], args ) === false ) {
613 for ( ; i < length; ) {
614 if ( callback.apply( object[ i++ ], args ) === false ) {
620 // A special, fast, case for the most common use of each
623 for ( name in object ) {
624 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
629 for ( var value = object[0];
630 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
637 // Use native String.trim function wherever possible
640 return text == null ?
645 // Otherwise use our own trimming functionality
647 return text == null ?
649 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
652 // results is for internal usage only
653 makeArray: function( array, results ) {
654 var ret = results || [];
656 if ( array != null ) {
657 // The window, strings (and functions) also have 'length'
658 // The extra typeof function check is to prevent crashes
659 // in Safari 2 (See: #3039)
660 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
661 var type = jQuery.type(array);
663 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
664 push.call( ret, array );
666 jQuery.merge( ret, array );
673 inArray: function( elem, array ) {
674 if ( array.indexOf ) {
675 return array.indexOf( elem );
678 for ( var i = 0, length = array.length; i < length; i++ ) {
679 if ( array[ i ] === elem ) {
687 merge: function( first, second ) {
688 var i = first.length,
691 if ( typeof second.length === "number" ) {
692 for ( var l = second.length; j < l; j++ ) {
693 first[ i++ ] = second[ j ];
697 while ( second[j] !== undefined ) {
698 first[ i++ ] = second[ j++ ];
707 grep: function( elems, callback, inv ) {
708 var ret = [], retVal;
711 // Go through the array, only saving the items
712 // that pass the validator function
713 for ( var i = 0, length = elems.length; i < length; i++ ) {
714 retVal = !!callback( elems[ i ], i );
715 if ( inv !== retVal ) {
716 ret.push( elems[ i ] );
723 // arg is for internal usage only
724 map: function( elems, callback, arg ) {
727 // Go through the array, translating each of the items to their
728 // new value (or values).
729 for ( var i = 0, length = elems.length; i < length; i++ ) {
730 value = callback( elems[ i ], i, arg );
732 if ( value != null ) {
733 ret[ ret.length ] = value;
737 // Flatten any nested arrays
738 return ret.concat.apply( [], ret );
741 // A global GUID counter for objects
744 proxy: function( fn, proxy, thisObject ) {
745 if ( arguments.length === 2 ) {
746 if ( typeof proxy === "string" ) {
748 fn = thisObject[ proxy ];
751 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
757 if ( !proxy && fn ) {
759 return fn.apply( thisObject || this, arguments );
763 // Set the guid of unique handler to the same of original handler, so it can be removed
765 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
768 // So proxy can be declared as an argument
772 // Mutifunctional method to get and set values to a collection
773 // The value/s can be optionally by executed if its a function
774 access: function( elems, key, value, exec, fn, pass ) {
775 var length = elems.length;
777 // Setting many attributes
778 if ( typeof key === "object" ) {
779 for ( var k in key ) {
780 jQuery.access( elems, k, key[k], exec, fn, value );
785 // Setting one attribute
786 if ( value !== undefined ) {
787 // Optionally, function values get executed if exec is true
788 exec = !pass && exec && jQuery.isFunction(value);
790 for ( var i = 0; i < length; i++ ) {
791 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
797 // Getting an attribute
798 return length ? fn( elems[0], key ) : undefined;
802 return (new Date()).getTime();
805 // Create a simple deferred (one callbacks list)
806 _Deferred: function() {
807 var // callbacks list
809 // stored [ context , args ]
811 // to avoid firing when already doing so
813 // flag to know if the deferred has been cancelled
815 // the deferred itself
818 // done( f1, f2, ...)
821 var args = arguments,
831 for ( i = 0, length = args.length; i < length; i++ ) {
833 type = jQuery.type( elem );
834 if ( type === "array" ) {
835 deferred.done.apply( deferred, elem );
836 } else if ( type === "function" ) {
837 callbacks.push( elem );
841 deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
847 // resolve with given context and args
848 resolveWith: function( context, args ) {
849 if ( !cancelled && !fired && !firing ) {
852 while( callbacks[ 0 ] ) {
853 callbacks.shift().apply( context, args );
857 fired = [ context, args ];
864 // resolve with this as context and given arguments
865 resolve: function() {
866 deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this, arguments );
870 // Has this deferred been resolved?
871 isResolved: function() {
872 return !!( firing || fired );
886 // Full fledged deferred (two callbacks list)
887 Deferred: function( func ) {
888 var deferred = jQuery._Deferred(),
889 failDeferred = jQuery._Deferred(),
892 // Add errorDeferred methods, then, promise and invert
893 jQuery.extend( deferred, {
894 then: function( doneCallbacks, failCallbacks ) {
895 deferred.done( doneCallbacks ).fail( failCallbacks );
898 fail: failDeferred.done,
899 rejectWith: failDeferred.resolveWith,
900 reject: failDeferred.resolve,
901 isRejected: failDeferred.isResolved,
902 // Get a promise for this deferred
903 // If obj is provided, the promise aspect is added to the object
904 promise: function( obj ) {
911 for( var methodName in promiseMethods ) {
912 obj[ methodName ] = deferred[ methodName ];
916 // Get the invert promise for this deferred
917 // If obj is provided, the invert promise aspect is added to the object
918 invert: function( obj ) {
925 for( var methodName in promiseMethods ) {
926 obj[ methodName ] = promiseMethods[ methodName ] && deferred[ promiseMethods[methodName] ];
928 obj.then = invert.then || function( doneCallbacks, failCallbacks ) {
929 deferred.done( failCallbacks ).fail( doneCallbacks );
935 // Make sure only one callback list will be used
936 deferred.then( failDeferred.cancel, deferred.cancel );
938 delete deferred.cancel;
939 // Call given func if any
941 func.call( deferred, deferred );
947 when: function( object ) {
948 var args = arguments,
949 length = args.length,
950 deferred = length <= 1 && object && jQuery.isFunction( object.promise ) ?
953 promise = deferred.promise(),
957 resolveArray = new Array( length );
958 jQuery.each( args, function( index, element ) {
959 jQuery.when( element ).then( function( value ) {
960 resolveArray[ index ] = arguments.length > 1 ? slice.call( arguments, 0 ) : value;
962 deferred.resolveWith( promise, resolveArray );
964 }, deferred.reject );
966 } else if ( deferred !== object ) {
967 deferred.resolve( object );
972 // Use of jQuery.browser is frowned upon.
973 // More details: http://docs.jquery.com/Utilities/jQuery.browser
974 uaMatch: function( ua ) {
975 ua = ua.toLowerCase();
977 var match = rwebkit.exec( ua ) ||
980 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
983 return { browser: match[1] || "", version: match[2] || "0" };
987 function jQuerySubclass( selector, context ) {
988 return new jQuerySubclass.fn.init( selector, context );
990 jQuery.extend( true, jQuerySubclass, this );
991 jQuerySubclass.superclass = this;
992 jQuerySubclass.fn = jQuerySubclass.prototype = this();
993 jQuerySubclass.fn.constructor = jQuerySubclass;
994 jQuerySubclass.subclass = this.subclass;
995 jQuerySubclass.fn.init = function init( selector, context ) {
996 if ( context && context instanceof jQuery && !(context instanceof jQuerySubclass) ) {
997 context = jQuerySubclass(context);
1000 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
1002 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
1003 var rootjQuerySubclass = jQuerySubclass(document);
1004 return jQuerySubclass;
1010 // Create readyList deferred
1011 readyList = jQuery._Deferred();
1013 // Populate the class2type map
1014 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
1015 class2type[ "[object " + name + "]" ] = name.toLowerCase();
1018 browserMatch = jQuery.uaMatch( userAgent );
1019 if ( browserMatch.browser ) {
1020 jQuery.browser[ browserMatch.browser ] = true;
1021 jQuery.browser.version = browserMatch.version;
1024 // Deprecated, use jQuery.browser.webkit instead
1025 if ( jQuery.browser.webkit ) {
1026 jQuery.browser.safari = true;
1030 jQuery.inArray = function( elem, array ) {
1031 return indexOf.call( array, elem );
1035 // IE doesn't match non-breaking spaces with \s
1036 if ( rnotwhite.test( "\xA0" ) ) {
1037 trimLeft = /^[\s\xA0]+/;
1038 trimRight = /[\s\xA0]+$/;
1041 // All jQuery objects should point back to these
1042 rootjQuery = jQuery(document);
1044 // Cleanup functions for the document ready method
1045 if ( document.addEventListener ) {
1046 DOMContentLoaded = function() {
1047 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1051 } else if ( document.attachEvent ) {
1052 DOMContentLoaded = function() {
1053 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1054 if ( document.readyState === "complete" ) {
1055 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1061 // The DOM ready check for Internet Explorer
1062 function doScrollCheck() {
1063 if ( jQuery.isReady ) {
1068 // If IE is used, use the trick by Diego Perini
1069 // http://javascript.nwbox.com/IEContentLoaded/
1070 document.documentElement.doScroll("left");
1072 setTimeout( doScrollCheck, 1 );
1076 // and execute any waiting functions
1080 // Expose jQuery to the global object
1081 return (window.jQuery = window.$ = jQuery);