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
60 promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
62 // The ready event handler
65 // Save a reference to some core methods
66 toString = Object.prototype.toString,
67 hasOwn = Object.prototype.hasOwnProperty,
68 push = Array.prototype.push,
69 slice = Array.prototype.slice,
70 trim = String.prototype.trim,
71 indexOf = Array.prototype.indexOf,
73 // [[Class]] -> type pairs
76 jQuery.fn = jQuery.prototype = {
78 init: function( selector, context, rootjQuery ) {
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 && document.body ) {
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 context = context instanceof jQuery ? context[0] : context;
113 doc = (context ? context.ownerDocument || context : document);
115 // If a single string is passed in and it's a single tag
116 // just do a createElement and skip the rest
117 ret = rsingleTag.exec( selector );
120 if ( jQuery.isPlainObject( context ) ) {
121 selector = [ document.createElement( ret[1] ) ];
122 jQuery.fn.attr.call( selector, context, true );
125 selector = [ doc.createElement( ret[1] ) ];
129 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
130 selector = (ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment).childNodes;
133 return jQuery.merge( this, selector );
137 elem = document.getElementById( match[2] );
139 // Check parentNode to catch when Blackberry 4.6 returns
140 // nodes that are no longer in the document #6963
141 if ( elem && elem.parentNode ) {
142 // Handle the case where IE and Opera return items
143 // by name instead of ID
144 if ( elem.id !== match[2] ) {
145 return rootjQuery.find( selector );
148 // Otherwise, we inject the element directly into the jQuery object
153 this.context = document;
154 this.selector = selector;
158 // HANDLE: $(expr, $(...))
159 } else if ( !context || context.jquery ) {
160 return (context || rootjQuery).find( selector );
162 // HANDLE: $(expr, context)
163 // (which is just equivalent to: $(context).find(expr)
165 return this.constructor( context ).find( selector );
168 // HANDLE: $(function)
169 // Shortcut for document ready
170 } else if ( jQuery.isFunction( selector ) ) {
171 return rootjQuery.ready( selector );
174 if (selector.selector !== undefined) {
175 this.selector = selector.selector;
176 this.context = selector.context;
179 return jQuery.makeArray( selector, this );
182 // Start with an empty selector
185 // The current version of jQuery being used
188 // The default length of a jQuery object is 0
191 // The number of elements contained in the matched element set
196 toArray: function() {
197 return slice.call( this, 0 );
200 // Get the Nth element in the matched element set OR
201 // Get the whole matched element set as a clean array
202 get: function( num ) {
205 // Return a 'clean' array
208 // Return just the object
209 ( num < 0 ? this[ this.length + num ] : this[ num ] );
212 // Take an array of elements and push it onto the stack
213 // (returning the new matched element set)
214 pushStack: function( elems, name, selector ) {
215 // Build a new jQuery matched element set
216 var ret = this.constructor();
218 if ( jQuery.isArray( elems ) ) {
219 push.apply( ret, elems );
222 jQuery.merge( ret, elems );
225 // Add the old object onto the stack (as a reference)
226 ret.prevObject = this;
228 ret.context = this.context;
230 if ( name === "find" ) {
231 ret.selector = this.selector + (this.selector ? " " : "") + selector;
233 ret.selector = this.selector + "." + name + "(" + selector + ")";
236 // Return the newly-formed element set
240 // Execute a callback for every element in the matched set.
241 // (You can seed the arguments with an array of args, but this is
242 // only used internally.)
243 each: function( callback, args ) {
244 return jQuery.each( this, callback, args );
247 ready: function( fn ) {
248 // Attach the listeners
252 readyList.done( fn );
260 this.slice( i, +i + 1 );
268 return this.eq( -1 );
272 return this.pushStack( slice.apply( this, arguments ),
273 "slice", slice.call(arguments).join(",") );
276 map: function( callback ) {
277 return this.pushStack( jQuery.map(this, function( elem, i ) {
278 return callback.call( elem, i, elem );
283 return this.prevObject || this.constructor(null);
286 // For internal use only.
287 // Behaves like an Array's method, not like a jQuery method.
293 // Give the init function the jQuery prototype for later instantiation
294 jQuery.fn.init.prototype = jQuery.fn;
296 jQuery.extend = jQuery.fn.extend = function() {
297 var options, name, src, copy, copyIsArray, clone,
298 target = arguments[0] || {},
300 length = arguments.length,
303 // Handle a deep copy situation
304 if ( typeof target === "boolean" ) {
306 target = arguments[1] || {};
307 // skip the boolean and the target
311 // Handle case when target is a string or something (possible in deep copy)
312 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
316 // extend jQuery itself if only one argument is passed
317 if ( length === i ) {
322 for ( ; i < length; i++ ) {
323 // Only deal with non-null/undefined values
324 if ( (options = arguments[ i ]) != null ) {
325 // Extend the base object
326 for ( name in options ) {
327 src = target[ name ];
328 copy = options[ name ];
330 // Prevent never-ending loop
331 if ( target === copy ) {
335 // Recurse if we're merging plain objects or arrays
336 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
339 clone = src && jQuery.isArray(src) ? src : [];
342 clone = src && jQuery.isPlainObject(src) ? src : {};
345 // Never move original objects, clone them
346 target[ name ] = jQuery.extend( deep, clone, copy );
348 // Don't bring in undefined values
349 } else if ( copy !== undefined ) {
350 target[ name ] = copy;
356 // Return the modified object
361 noConflict: function( deep ) {
365 window.jQuery = _jQuery;
371 // Is the DOM ready to be used? Set to true once it occurs.
374 // A counter to track how many items to wait for before
375 // the ready event fires. See #6781
378 // Handle when the DOM is ready
379 ready: function( wait ) {
380 // A third-party is pushing the ready event forwards
381 if ( wait === true ) {
385 // Make sure that the DOM is not already loaded
386 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
387 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
388 if ( !document.body ) {
389 return setTimeout( jQuery.ready, 1 );
392 // Remember that the DOM is ready
393 jQuery.isReady = true;
395 // If a normal DOM Ready event fired, decrement, and wait if need be
396 if ( wait !== true && --jQuery.readyWait > 0 ) {
400 // If there are functions bound, to execute
401 readyList.resolveWith( document, [ jQuery ] );
403 // Trigger any bound ready events
404 if ( jQuery.fn.trigger ) {
405 jQuery( document ).trigger( "ready" ).unbind( "ready" );
410 bindReady: function() {
417 // Catch cases where $(document).ready() is called after the
418 // browser event has already occurred.
419 if ( document.readyState === "complete" ) {
420 // Handle it asynchronously to allow scripts the opportunity to delay ready
421 return setTimeout( jQuery.ready, 1 );
424 // Mozilla, Opera and webkit nightlies currently support this event
425 if ( document.addEventListener ) {
426 // Use the handy event callback
427 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
429 // A fallback to window.onload, that will always work
430 window.addEventListener( "load", jQuery.ready, false );
432 // If IE event model is used
433 } else if ( document.attachEvent ) {
434 // ensure firing before onload,
435 // maybe late but safe also for iframes
436 document.attachEvent("onreadystatechange", DOMContentLoaded);
438 // A fallback to window.onload, that will always work
439 window.attachEvent( "onload", jQuery.ready );
441 // If IE and not a frame
442 // continually check to see if the document is ready
443 var toplevel = false;
446 toplevel = window.frameElement == null;
449 if ( document.documentElement.doScroll && toplevel ) {
455 // See test/unit/core.js for details concerning isFunction.
456 // Since version 1.3, DOM methods and functions like alert
457 // aren't supported. They return false on IE (#2968).
458 isFunction: function( obj ) {
459 return jQuery.type(obj) === "function";
462 isArray: Array.isArray || function( obj ) {
463 return jQuery.type(obj) === "array";
466 // A crude way of determining if an object is a window
467 isWindow: function( obj ) {
468 return obj && typeof obj === "object" && "setInterval" in obj;
471 isNaN: function( obj ) {
472 return obj == null || !rdigit.test( obj ) || isNaN( obj );
475 type: function( obj ) {
478 class2type[ toString.call(obj) ] || "object";
481 isPlainObject: function( obj ) {
482 // Must be an Object.
483 // Because of IE, we also have to check the presence of the constructor property.
484 // Make sure that DOM nodes and window objects don't pass through, as well
485 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
489 // Not own constructor property must be Object
490 if ( obj.constructor &&
491 !hasOwn.call(obj, "constructor") &&
492 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
496 // Own properties are enumerated firstly, so to speed up,
497 // if last one is own, then all properties are own.
500 for ( key in obj ) {}
502 return key === undefined || hasOwn.call( obj, key );
505 isEmptyObject: function( obj ) {
506 for ( var name in obj ) {
512 error: function( msg ) {
516 parseJSON: function( data ) {
517 if ( typeof data !== "string" || !data ) {
521 // Make sure leading/trailing whitespace is removed (IE can't handle it)
522 data = jQuery.trim( data );
524 // Make sure the incoming data is actual JSON
525 // Logic borrowed from http://json.org/json2.js
526 if ( rvalidchars.test(data.replace(rvalidescape, "@")
527 .replace(rvalidtokens, "]")
528 .replace(rvalidbraces, "")) ) {
530 // Try to use the native JSON parser first
531 return window.JSON && window.JSON.parse ?
532 window.JSON.parse( data ) :
533 (new Function("return " + data))();
536 jQuery.error( "Invalid JSON: " + data );
540 // Cross-browser xml parsing
541 // (xml & tmp used internally)
542 parseXML: function( data , xml , tmp ) {
544 if ( window.DOMParser ) { // Standard
545 tmp = new DOMParser();
546 xml = tmp.parseFromString( data , "text/xml" );
548 xml = new ActiveXObject( "Microsoft.XMLDOM" );
553 tmp = xml.documentElement;
555 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
556 jQuery.error( "Invalid XML: " + data );
564 // Evalulates a script in a global context
565 globalEval: function( data ) {
566 if ( data && rnotwhite.test(data) ) {
567 // Inspired by code by Andrea Giammarchi
568 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
569 var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
570 script = document.createElement( "script" );
572 if ( jQuery.support.scriptEval() ) {
573 script.appendChild( document.createTextNode( data ) );
578 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
579 // This arises when a base node is used (#2709).
580 head.insertBefore( script, head.firstChild );
581 head.removeChild( script );
585 nodeName: function( elem, name ) {
586 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
589 // args is for internal usage only
590 each: function( object, callback, args ) {
592 length = object.length,
593 isObj = length === undefined || jQuery.isFunction(object);
597 for ( name in object ) {
598 if ( callback.apply( object[ name ], args ) === false ) {
603 for ( ; i < length; ) {
604 if ( callback.apply( object[ i++ ], args ) === false ) {
610 // A special, fast, case for the most common use of each
613 for ( name in object ) {
614 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
619 for ( var value = object[0];
620 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
627 // Use native String.trim function wherever possible
630 return text == null ?
635 // Otherwise use our own trimming functionality
637 return text == null ?
639 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
642 // results is for internal usage only
643 makeArray: function( array, results ) {
644 var ret = results || [];
646 if ( array != null ) {
647 // The window, strings (and functions) also have 'length'
648 // The extra typeof function check is to prevent crashes
649 // in Safari 2 (See: #3039)
650 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
651 var type = jQuery.type(array);
653 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
654 push.call( ret, array );
656 jQuery.merge( ret, array );
663 inArray: function( elem, array ) {
664 if ( array.indexOf ) {
665 return array.indexOf( elem );
668 for ( var i = 0, length = array.length; i < length; i++ ) {
669 if ( array[ i ] === elem ) {
677 merge: function( first, second ) {
678 var i = first.length,
681 if ( typeof second.length === "number" ) {
682 for ( var l = second.length; j < l; j++ ) {
683 first[ i++ ] = second[ j ];
687 while ( second[j] !== undefined ) {
688 first[ i++ ] = second[ j++ ];
697 grep: function( elems, callback, inv ) {
698 var ret = [], retVal;
701 // Go through the array, only saving the items
702 // that pass the validator function
703 for ( var i = 0, length = elems.length; i < length; i++ ) {
704 retVal = !!callback( elems[ i ], i );
705 if ( inv !== retVal ) {
706 ret.push( elems[ i ] );
713 // arg is for internal usage only
714 map: function( elems, callback, arg ) {
717 // Go through the array, translating each of the items to their
718 // new value (or values).
719 for ( var i = 0, length = elems.length; i < length; i++ ) {
720 value = callback( elems[ i ], i, arg );
722 if ( value != null ) {
723 ret[ ret.length ] = value;
727 // Flatten any nested arrays
728 return ret.concat.apply( [], ret );
731 // A global GUID counter for objects
734 proxy: function( fn, proxy, thisObject ) {
735 if ( arguments.length === 2 ) {
736 if ( typeof proxy === "string" ) {
738 fn = thisObject[ proxy ];
741 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
747 if ( !proxy && fn ) {
749 return fn.apply( thisObject || this, arguments );
753 // Set the guid of unique handler to the same of original handler, so it can be removed
755 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
758 // So proxy can be declared as an argument
762 // Mutifunctional method to get and set values to a collection
763 // The value/s can be optionally by executed if its a function
764 access: function( elems, key, value, exec, fn, pass ) {
765 var length = elems.length;
767 // Setting many attributes
768 if ( typeof key === "object" ) {
769 for ( var k in key ) {
770 jQuery.access( elems, k, key[k], exec, fn, value );
775 // Setting one attribute
776 if ( value !== undefined ) {
777 // Optionally, function values get executed if exec is true
778 exec = !pass && exec && jQuery.isFunction(value);
780 for ( var i = 0; i < length; i++ ) {
781 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
787 // Getting an attribute
788 return length ? fn( elems[0], key ) : undefined;
792 return (new Date()).getTime();
795 // Create a simple deferred (one callbacks list)
796 _Deferred: function() {
797 var // callbacks list
799 // stored [ context , args ]
801 // to avoid firing when already doing so
803 // flag to know if the deferred has been cancelled
805 // the deferred itself
808 // done( f1, f2, ...)
811 var args = arguments,
821 for ( i = 0, length = args.length; i < length; i++ ) {
823 type = jQuery.type( elem );
824 if ( type === "array" ) {
825 deferred.done.apply( deferred, elem );
826 } else if ( type === "function" ) {
827 callbacks.push( elem );
831 deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
837 // resolve with given context and args
838 resolveWith: function( context, args ) {
839 if ( !cancelled && !fired && !firing ) {
842 while( callbacks[ 0 ] ) {
843 callbacks.shift().apply( context, args );
846 // We have to add a catch block for
847 // IE prior to 8 or else the finally
848 // block will never get executed
853 fired = [ context, args ];
860 // resolve with this as context and given arguments
861 resolve: function() {
862 deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this, arguments );
866 // Has this deferred been resolved?
867 isResolved: function() {
868 return !!( firing || fired );
882 // Full fledged deferred (two callbacks list)
883 Deferred: function( func ) {
884 var deferred = jQuery._Deferred(),
885 failDeferred = jQuery._Deferred(),
887 // Add errorDeferred methods, then and promise
888 jQuery.extend( deferred, {
889 then: function( doneCallbacks, failCallbacks ) {
890 deferred.done( doneCallbacks ).fail( failCallbacks );
893 fail: failDeferred.done,
894 rejectWith: failDeferred.resolveWith,
895 reject: failDeferred.resolve,
896 isRejected: failDeferred.isResolved,
897 // Get a promise for this deferred
898 // If obj is provided, the promise aspect is added to the object
899 promise: function( obj ) {
906 var i = promiseMethods.length;
908 obj[ promiseMethods[i] ] = deferred[ promiseMethods[i] ];
913 // Make sure only one callback list will be used
914 deferred.done( failDeferred.cancel ).fail( deferred.cancel );
916 delete deferred.cancel;
917 // Call given func if any
919 func.call( deferred, deferred );
925 when: function( object ) {
926 var lastIndex = arguments.length,
927 deferred = lastIndex <= 1 && object && jQuery.isFunction( object.promise ) ?
930 promise = deferred.promise();
932 if ( lastIndex > 1 ) {
933 var array = slice.call( arguments, 0 ),
935 iCallback = function( index ) {
936 return function( value ) {
937 array[ index ] = arguments.length > 1 ? slice.call( arguments, 0 ) : value;
938 if ( !( --count ) ) {
939 deferred.resolveWith( promise, array );
943 while( ( lastIndex-- ) ) {
944 object = array[ lastIndex ];
945 if ( object && jQuery.isFunction( object.promise ) ) {
946 object.promise().then( iCallback(lastIndex), deferred.reject );
952 deferred.resolveWith( promise, array );
954 } else if ( deferred !== object ) {
955 deferred.resolve( object );
960 // Use of jQuery.browser is frowned upon.
961 // More details: http://docs.jquery.com/Utilities/jQuery.browser
962 uaMatch: function( ua ) {
963 ua = ua.toLowerCase();
965 var match = rwebkit.exec( ua ) ||
968 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
971 return { browser: match[1] || "", version: match[2] || "0" };
975 function jQuerySubclass( selector, context ) {
976 return new jQuerySubclass.fn.init( selector, context );
978 jQuery.extend( true, jQuerySubclass, this );
979 jQuerySubclass.superclass = this;
980 jQuerySubclass.fn = jQuerySubclass.prototype = this();
981 jQuerySubclass.fn.constructor = jQuerySubclass;
982 jQuerySubclass.subclass = this.subclass;
983 jQuerySubclass.fn.init = function init( selector, context ) {
984 if ( context && context instanceof jQuery && !(context instanceof jQuerySubclass) ) {
985 context = jQuerySubclass(context);
988 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
990 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
991 var rootjQuerySubclass = jQuerySubclass(document);
992 return jQuerySubclass;
998 // Create readyList deferred
999 readyList = jQuery._Deferred();
1001 // Populate the class2type map
1002 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
1003 class2type[ "[object " + name + "]" ] = name.toLowerCase();
1006 browserMatch = jQuery.uaMatch( userAgent );
1007 if ( browserMatch.browser ) {
1008 jQuery.browser[ browserMatch.browser ] = true;
1009 jQuery.browser.version = browserMatch.version;
1012 // Deprecated, use jQuery.browser.webkit instead
1013 if ( jQuery.browser.webkit ) {
1014 jQuery.browser.safari = true;
1018 jQuery.inArray = function( elem, array ) {
1019 return indexOf.call( array, elem );
1023 // IE doesn't match non-breaking spaces with \s
1024 if ( rnotwhite.test( "\xA0" ) ) {
1025 trimLeft = /^[\s\xA0]+/;
1026 trimRight = /[\s\xA0]+$/;
1029 // All jQuery objects should point back to these
1030 rootjQuery = jQuery(document);
1032 // Cleanup functions for the document ready method
1033 if ( document.addEventListener ) {
1034 DOMContentLoaded = function() {
1035 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1039 } else if ( document.attachEvent ) {
1040 DOMContentLoaded = function() {
1041 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1042 if ( document.readyState === "complete" ) {
1043 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1049 // The DOM ready check for Internet Explorer
1050 function doScrollCheck() {
1051 if ( jQuery.isReady ) {
1056 // If IE is used, use the trick by Diego Perini
1057 // http://javascript.nwbox.com/IEContentLoaded/
1058 document.documentElement.doScroll("left");
1060 setTimeout( doScrollCheck, 1 );
1064 // and execute any waiting functions
1068 // Expose jQuery to the global object