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
19 quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$/,
21 // Check if a string has a non-whitespace character in it
24 // Used for trimming whitespace
28 // Check for non-word characters
34 // Match a standalone tag
35 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
38 rvalidchars = /^[\],:{}\s]*$/,
39 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
40 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
41 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
44 rwebkit = /(webkit)[ \/]([\w.]+)/,
45 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
46 rmsie = /(msie) ([\w.]+)/,
47 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
49 // Keep a UserAgent string for use with jQuery.browser
50 userAgent = navigator.userAgent,
52 // For matching the engine and version of the browser
55 // Has the ready events already been bound?
58 // The deferred used on DOM ready
62 promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
64 // The ready event handler
67 // Save a reference to some core methods
68 toString = Object.prototype.toString,
69 hasOwn = Object.prototype.hasOwnProperty,
70 push = Array.prototype.push,
71 slice = Array.prototype.slice,
72 trim = String.prototype.trim,
73 indexOf = Array.prototype.indexOf,
75 // [[Class]] -> type pairs
78 jQuery.fn = jQuery.prototype = {
80 init: function( selector, context, rootjQuery ) {
81 var match, elem, ret, doc;
83 // Handle $(""), $(null), or $(undefined)
88 // Handle $(DOMElement)
89 if ( selector.nodeType ) {
90 this.context = this[0] = selector;
95 // Handle HTML strings
96 if ( typeof selector === "string" ) {
97 // Are we dealing with HTML string
98 if ( (match = quickExpr.exec( selector )) ) {
99 context = context instanceof jQuery ? context[0] : context;
100 doc = (context ? context.ownerDocument || context : document);
102 // If a single string is passed in and it's a single tag
103 // just do a createElement and skip the rest
104 ret = rsingleTag.exec( selector );
107 if ( jQuery.isPlainObject( context ) ) {
108 selector = [ document.createElement( ret[1] ) ];
109 jQuery.fn.attr.call( selector, context, true );
112 selector = [ doc.createElement( ret[1] ) ];
116 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
117 selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
120 return jQuery.merge( this, selector );
122 // HANDLE: $(expr, $(...))
123 } else if ( !context || context.jquery ) {
124 return (context || rootjQuery).find( selector );
126 // HANDLE: $(expr, context)
127 // (which is just equivalent to: $(context).find(expr)
129 return this.constructor( context ).find( selector );
132 // HANDLE: $(function)
133 // Shortcut for document ready
134 } else if ( jQuery.isFunction( selector ) ) {
135 return rootjQuery.ready( selector );
138 if (selector.selector !== undefined) {
139 this.selector = selector.selector;
140 this.context = selector.context;
143 return jQuery.makeArray( selector, this );
146 // Start with an empty selector
149 // The current version of jQuery being used
152 // The default length of a jQuery object is 0
155 // The number of elements contained in the matched element set
160 toArray: function() {
161 return slice.call( this, 0 );
164 // Get the Nth element in the matched element set OR
165 // Get the whole matched element set as a clean array
166 get: function( num ) {
169 // Return a 'clean' array
172 // Return just the object
173 ( num < 0 ? this[ this.length + num ] : this[ num ] );
176 // Take an array of elements and push it onto the stack
177 // (returning the new matched element set)
178 pushStack: function( elems, name, selector ) {
179 // Build a new jQuery matched element set
180 var ret = this.constructor();
182 if ( jQuery.isArray( elems ) ) {
183 push.apply( ret, elems );
186 jQuery.merge( ret, elems );
189 // Add the old object onto the stack (as a reference)
190 ret.prevObject = this;
192 ret.context = this.context;
194 if ( name === "find" ) {
195 ret.selector = this.selector + (this.selector ? " " : "") + selector;
197 ret.selector = this.selector + "." + name + "(" + selector + ")";
200 // Return the newly-formed element set
204 // Execute a callback for every element in the matched set.
205 // (You can seed the arguments with an array of args, but this is
206 // only used internally.)
207 each: function( callback, args ) {
208 return jQuery.each( this, callback, args );
212 // Attach the listeners
215 // Change ready & apply
216 return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
222 this.slice( i, +i + 1 );
230 return this.eq( -1 );
234 return this.pushStack( slice.apply( this, arguments ),
235 "slice", slice.call(arguments).join(",") );
238 map: function( callback ) {
239 return this.pushStack( jQuery.map(this, function( elem, i ) {
240 return callback.call( elem, i, elem );
245 return this.prevObject || this.constructor(null);
248 // For internal use only.
249 // Behaves like an Array's method, not like a jQuery method.
255 // Give the init function the jQuery prototype for later instantiation
256 jQuery.fn.init.prototype = jQuery.fn;
258 jQuery.extend = jQuery.fn.extend = function() {
259 var options, name, src, copy, copyIsArray, clone,
260 target = arguments[0] || {},
262 length = arguments.length,
265 // Handle a deep copy situation
266 if ( typeof target === "boolean" ) {
268 target = arguments[1] || {};
269 // skip the boolean and the target
273 // Handle case when target is a string or something (possible in deep copy)
274 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
278 // extend jQuery itself if only one argument is passed
279 if ( length === i ) {
284 for ( ; i < length; i++ ) {
285 // Only deal with non-null/undefined values
286 if ( (options = arguments[ i ]) != null ) {
287 // Extend the base object
288 for ( name in options ) {
289 src = target[ name ];
290 copy = options[ name ];
292 // Prevent never-ending loop
293 if ( target === copy ) {
297 // Recurse if we're merging plain objects or arrays
298 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
301 clone = src && jQuery.isArray(src) ? src : [];
304 clone = src && jQuery.isPlainObject(src) ? src : {};
307 // Never move original objects, clone them
308 target[ name ] = jQuery.extend( deep, clone, copy );
310 // Don't bring in undefined values
311 } else if ( copy !== undefined ) {
312 target[ name ] = copy;
318 // Return the modified object
323 noConflict: function( deep ) {
327 window.jQuery = _jQuery;
333 // Is the DOM ready to be used? Set to true once it occurs.
336 // A counter to track how many items to wait for before
337 // the ready event fires. See #6781
340 // Handle when the DOM is ready
341 ready: function( wait ) {
342 // A third-party is pushing the ready event forwards
343 if ( wait === true ) {
347 // Make sure that the DOM is not already loaded
348 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
349 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
350 if ( !document.body ) {
351 return setTimeout( jQuery.ready, 1 );
354 // Remember that the DOM is ready
355 jQuery.isReady = true;
357 // If a normal DOM Ready event fired, decrement, and wait if need be
358 if ( wait !== true && --jQuery.readyWait > 0 ) {
362 // If there are functions bound, to execute
363 readyList.fire( document , [ jQuery ] );
365 // Trigger any bound ready events
366 if ( jQuery.fn.trigger ) {
367 jQuery( document ).trigger( "ready" ).unbind( "ready" );
372 bindReady: function() {
379 // Catch cases where $(document).ready() is called after the
380 // browser event has already occurred.
381 if ( document.readyState === "complete" ) {
382 // Handle it asynchronously to allow scripts the opportunity to delay ready
383 return setTimeout( jQuery.ready, 1 );
386 // Mozilla, Opera and webkit nightlies currently support this event
387 if ( document.addEventListener ) {
388 // Use the handy event callback
389 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
391 // A fallback to window.onload, that will always work
392 window.addEventListener( "load", jQuery.ready, false );
394 // If IE event model is used
395 } else if ( document.attachEvent ) {
396 // ensure firing before onload,
397 // maybe late but safe also for iframes
398 document.attachEvent("onreadystatechange", DOMContentLoaded);
400 // A fallback to window.onload, that will always work
401 window.attachEvent( "onload", jQuery.ready );
403 // If IE and not a frame
404 // continually check to see if the document is ready
405 var toplevel = false;
408 toplevel = window.frameElement == null;
411 if ( document.documentElement.doScroll && toplevel ) {
417 // See test/unit/core.js for details concerning isFunction.
418 // Since version 1.3, DOM methods and functions like alert
419 // aren't supported. They return false on IE (#2968).
420 isFunction: function( obj ) {
421 return jQuery.type(obj) === "function";
424 isArray: Array.isArray || function( obj ) {
425 return jQuery.type(obj) === "array";
428 // A crude way of determining if an object is a window
429 isWindow: function( obj ) {
430 return obj && typeof obj === "object" && "setInterval" in obj;
433 isNaN: function( obj ) {
434 return obj == null || !rdigit.test( obj ) || isNaN( obj );
437 type: function( obj ) {
440 class2type[ toString.call(obj) ] || "object";
443 isPlainObject: function( obj ) {
444 // Must be an Object.
445 // Because of IE, we also have to check the presence of the constructor property.
446 // Make sure that DOM nodes and window objects don't pass through, as well
447 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
451 // Not own constructor property must be Object
452 if ( obj.constructor &&
453 !hasOwn.call(obj, "constructor") &&
454 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
458 // Own properties are enumerated firstly, so to speed up,
459 // if last one is own, then all properties are own.
462 for ( key in obj ) {}
464 return key === undefined || hasOwn.call( obj, key );
467 isEmptyObject: function( obj ) {
468 for ( var name in obj ) {
474 error: function( msg ) {
478 parseJSON: function( data ) {
479 if ( typeof data !== "string" || !data ) {
483 // Make sure leading/trailing whitespace is removed (IE can't handle it)
484 data = jQuery.trim( data );
486 // Make sure the incoming data is actual JSON
487 // Logic borrowed from http://json.org/json2.js
488 if ( rvalidchars.test(data.replace(rvalidescape, "@")
489 .replace(rvalidtokens, "]")
490 .replace(rvalidbraces, "")) ) {
492 // Try to use the native JSON parser first
493 return window.JSON && window.JSON.parse ?
494 window.JSON.parse( data ) :
495 (new Function("return " + data))();
498 jQuery.error( "Invalid JSON: " + data );
502 // Cross-browser xml parsing
503 // (xml & tmp used internally)
504 parseXML: function( data , xml , tmp ) {
506 if ( window.DOMParser ) { // Standard
507 tmp = new DOMParser();
508 xml = tmp.parseFromString( data , "text/xml" );
510 xml = new ActiveXObject( "Microsoft.XMLDOM" );
515 tmp = xml.documentElement;
517 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
518 jQuery.error( "Invalid XML: " + data );
526 // Evalulates a script in a global context
527 globalEval: function( data ) {
528 if ( data && rnotwhite.test(data) ) {
529 // Inspired by code by Andrea Giammarchi
530 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
531 var head = document.getElementsByTagName("head")[0] || document.documentElement,
532 script = document.createElement("script");
534 script.type = "text/javascript";
536 if ( jQuery.support.scriptEval() ) {
537 script.appendChild( document.createTextNode( data ) );
542 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
543 // This arises when a base node is used (#2709).
544 head.insertBefore( script, head.firstChild );
545 head.removeChild( script );
549 nodeName: function( elem, name ) {
550 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
553 // args is for internal usage only
554 each: function( object, callback, args ) {
556 length = object.length,
557 isObj = length === undefined || jQuery.isFunction(object);
561 for ( name in object ) {
562 if ( callback.apply( object[ name ], args ) === false ) {
567 for ( ; i < length; ) {
568 if ( callback.apply( object[ i++ ], args ) === false ) {
574 // A special, fast, case for the most common use of each
577 for ( name in object ) {
578 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
583 for ( var value = object[0];
584 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
591 // Use native String.trim function wherever possible
594 return text == null ?
599 // Otherwise use our own trimming functionality
601 return text == null ?
603 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
606 // results is for internal usage only
607 makeArray: function( array, results ) {
608 var ret = results || [];
610 if ( array != null ) {
611 // The window, strings (and functions) also have 'length'
612 // The extra typeof function check is to prevent crashes
613 // in Safari 2 (See: #3039)
614 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
615 var type = jQuery.type(array);
617 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
618 push.call( ret, array );
620 jQuery.merge( ret, array );
627 inArray: function( elem, array ) {
628 if ( array.indexOf ) {
629 return array.indexOf( elem );
632 for ( var i = 0, length = array.length; i < length; i++ ) {
633 if ( array[ i ] === elem ) {
641 merge: function( first, second ) {
642 var i = first.length,
645 if ( typeof second.length === "number" ) {
646 for ( var l = second.length; j < l; j++ ) {
647 first[ i++ ] = second[ j ];
651 while ( second[j] !== undefined ) {
652 first[ i++ ] = second[ j++ ];
661 grep: function( elems, callback, inv ) {
662 var ret = [], retVal;
665 // Go through the array, only saving the items
666 // that pass the validator function
667 for ( var i = 0, length = elems.length; i < length; i++ ) {
668 retVal = !!callback( elems[ i ], i );
669 if ( inv !== retVal ) {
670 ret.push( elems[ i ] );
677 // arg is for internal usage only
678 map: function( elems, callback, arg ) {
681 // Go through the array, translating each of the items to their
682 // new value (or values).
683 for ( var i = 0, length = elems.length; i < length; i++ ) {
684 value = callback( elems[ i ], i, arg );
686 if ( value != null ) {
687 ret[ ret.length ] = value;
691 // Flatten any nested arrays
692 return ret.concat.apply( [], ret );
695 // A global GUID counter for objects
698 proxy: function( fn, proxy, thisObject ) {
699 if ( arguments.length === 2 ) {
700 if ( typeof proxy === "string" ) {
702 fn = thisObject[ proxy ];
705 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
711 if ( !proxy && fn ) {
713 return fn.apply( thisObject || this, arguments );
717 // Set the guid of unique handler to the same of original handler, so it can be removed
719 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
722 // So proxy can be declared as an argument
726 // Mutifunctional method to get and set values to a collection
727 // The value/s can be optionally by executed if its a function
728 access: function( elems, key, value, exec, fn, pass ) {
729 var length = elems.length;
731 // Setting many attributes
732 if ( typeof key === "object" ) {
733 for ( var k in key ) {
734 jQuery.access( elems, k, key[k], exec, fn, value );
739 // Setting one attribute
740 if ( value !== undefined ) {
741 // Optionally, function values get executed if exec is true
742 exec = !pass && exec && jQuery.isFunction(value);
744 for ( var i = 0; i < length; i++ ) {
745 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
751 // Getting an attribute
752 return length ? fn( elems[0], key ) : undefined;
756 return (new Date()).getTime();
759 // Create a simple deferred (one callbacks list)
760 _Deferred: function() {
762 var // callbacks list
764 // stored [ context , args ]
766 // to avoid firing when already doing so
768 // flag to know if the deferred has been cancelled
770 // the deferred itself
773 // done( f1, f2, ...)
778 var args = arguments,
790 for ( i = 0, length = args.length ; i < length ; i++ ) {
792 type = jQuery.type( elem );
793 if ( type === "array" ) {
794 deferred.done.apply( deferred , elem );
795 } else if ( type === "function" ) {
796 callbacks.push( elem );
801 deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
808 // resolve with given context and args
809 fire: function( context , args ) {
810 if ( ! cancelled && ! fired && ! firing ) {
815 while( callbacks[ 0 ] ) {
816 callbacks.shift().apply( context , args );
820 fired = [ context , args ];
827 // resolve with this as context and given arguments
828 resolve: function() {
829 deferred.fire( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments );
833 // Has this deferred been resolved?
834 isResolved: function() {
835 return !!( firing || fired );
849 // Full fledged deferred (two callbacks list)
850 // Typical success/error system
851 Deferred: function( func ) {
853 var deferred = jQuery._Deferred(),
854 failDeferred = jQuery._Deferred(),
857 // Add errorDeferred methods, then and promise
858 jQuery.extend( deferred , {
860 then: function( doneCallbacks , failCallbacks ) {
861 deferred.done( doneCallbacks ).fail( failCallbacks );
864 fail: failDeferred.done,
865 fireReject: failDeferred.fire,
866 reject: failDeferred.resolve,
867 isRejected: failDeferred.isResolved,
868 // Get a promise for this deferred
869 // If obj is provided, the promise aspect is added to the object
870 // (i is used internally)
871 promise: function( obj , i ) {
878 i = promiseMethods.length;
880 obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
887 // Make sure only one callback list will be used
888 deferred.then( failDeferred.cancel , deferred.cancel );
891 delete deferred.cancel;
893 // Call given func if any
895 func.call( deferred , deferred );
902 when: function( object ) {
903 var args = arguments,
904 length = args.length,
905 deferred = length <= 1 && object && jQuery.isFunction( object.promise ) ?
908 promise = deferred.promise(),
912 resolveArray = new Array( length );
913 jQuery.each( args, function( index, element, args ) {
914 jQuery.when( element ).done( function( value ) {
916 resolveArray[ index ] = args.length > 1 ? slice.call( args , 0 ) : value;
918 deferred.fire( promise, resolveArray );
920 }).fail( function() {
921 deferred.fireReject( promise, arguments );
923 return !deferred.isRejected();
925 } else if ( deferred !== object ) {
926 deferred.resolve( object );
931 // Use of jQuery.browser is frowned upon.
932 // More details: http://docs.jquery.com/Utilities/jQuery.browser
933 uaMatch: function( ua ) {
934 ua = ua.toLowerCase();
936 var match = rwebkit.exec( ua ) ||
939 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
942 return { browser: match[1] || "", version: match[2] || "0" };
945 subclass: function(){
946 function jQuerySubclass( selector, context ) {
947 return new jQuerySubclass.fn.init( selector, context );
949 jQuerySubclass.superclass = this;
950 jQuerySubclass.fn = jQuerySubclass.prototype = this();
951 jQuerySubclass.fn.constructor = jQuerySubclass;
952 jQuerySubclass.subclass = this.subclass;
953 jQuerySubclass.fn.init = function init( selector, context ) {
954 if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){
955 context = jQuerySubclass(context);
957 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
959 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
960 var rootjQuerySubclass = jQuerySubclass(document);
961 return jQuerySubclass;
967 // Create readyList deferred
968 readyList = jQuery._Deferred();
970 // Populate the class2type map
971 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
972 class2type[ "[object " + name + "]" ] = name.toLowerCase();
975 browserMatch = jQuery.uaMatch( userAgent );
976 if ( browserMatch.browser ) {
977 jQuery.browser[ browserMatch.browser ] = true;
978 jQuery.browser.version = browserMatch.version;
981 // Deprecated, use jQuery.browser.webkit instead
982 if ( jQuery.browser.webkit ) {
983 jQuery.browser.safari = true;
987 jQuery.inArray = function( elem, array ) {
988 return indexOf.call( array, elem );
992 // IE doesn't match non-breaking spaces with \s
993 if ( rnotwhite.test( "\xA0" ) ) {
994 trimLeft = /^[\s\xA0]+/;
995 trimRight = /[\s\xA0]+$/;
998 // All jQuery objects should point back to these
999 rootjQuery = jQuery(document);
1001 // Cleanup functions for the document ready method
1002 if ( document.addEventListener ) {
1003 DOMContentLoaded = function() {
1004 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1008 } else if ( document.attachEvent ) {
1009 DOMContentLoaded = function() {
1010 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1011 if ( document.readyState === "complete" ) {
1012 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1018 // The DOM ready check for Internet Explorer
1019 function doScrollCheck() {
1020 if ( jQuery.isReady ) {
1025 // If IE is used, use the trick by Diego Perini
1026 // http://javascript.nwbox.com/IEContentLoaded/
1027 document.documentElement.doScroll("left");
1029 setTimeout( doScrollCheck, 1 );
1033 // and execute any waiting functions
1037 // Expose jQuery to the global object
1038 return (window.jQuery = window.$ = jQuery);