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
67 promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
69 // The ready event handler
72 // Save a reference to some core methods
73 toString = Object.prototype.toString,
74 hasOwn = Object.prototype.hasOwnProperty,
75 push = Array.prototype.push,
76 slice = Array.prototype.slice,
77 trim = String.prototype.trim,
78 indexOf = Array.prototype.indexOf,
80 // [[Class]] -> type pairs
83 jQuery.fn = jQuery.prototype = {
85 init: function( selector, context, rootjQuery ) {
86 var match, elem, ret, doc;
88 // Handle $(""), $(null), or $(undefined)
93 // Handle $(DOMElement)
94 if ( selector.nodeType ) {
95 this.context = this[0] = selector;
100 // The body element only exists once, optimize finding it
101 if ( selector === "body" && !context && document.body ) {
102 this.context = document;
103 this[0] = document.body;
104 this.selector = "body";
109 // Handle HTML strings
110 if ( typeof selector === "string" ) {
111 // Are we dealing with HTML string or an ID?
112 match = quickExpr.exec( selector );
114 // Verify a match, and that no context was specified for #id
115 if ( match && (match[1] || !context) ) {
117 // HANDLE: $(html) -> $(array)
119 context = context instanceof jQuery ? context[0] : context;
120 doc = (context ? context.ownerDocument || context : document);
122 // If a single string is passed in and it's a single tag
123 // just do a createElement and skip the rest
124 ret = rsingleTag.exec( selector );
127 if ( jQuery.isPlainObject( context ) ) {
128 selector = [ document.createElement( ret[1] ) ];
129 jQuery.fn.attr.call( selector, context, true );
132 selector = [ doc.createElement( ret[1] ) ];
136 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
137 selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
140 return jQuery.merge( this, selector );
144 elem = document.getElementById( match[2] );
146 // Check parentNode to catch when Blackberry 4.6 returns
147 // nodes that are no longer in the document #6963
148 if ( elem && elem.parentNode ) {
149 // Handle the case where IE and Opera return items
150 // by name instead of ID
151 if ( elem.id !== match[2] ) {
152 return rootjQuery.find( selector );
155 // Otherwise, we inject the element directly into the jQuery object
160 this.context = document;
161 this.selector = selector;
166 } else if ( !context && !rnonword.test( selector ) ) {
167 this.selector = selector;
168 this.context = document;
169 selector = document.getElementsByTagName( selector );
170 return jQuery.merge( this, selector );
172 // HANDLE: $(expr, $(...))
173 } else if ( !context || context.jquery ) {
174 return (context || rootjQuery).find( selector );
176 // HANDLE: $(expr, context)
177 // (which is just equivalent to: $(context).find(expr)
179 return this.constructor( context ).find( selector );
182 // HANDLE: $(function)
183 // Shortcut for document ready
184 } else if ( jQuery.isFunction( selector ) ) {
185 return rootjQuery.ready( selector );
188 if (selector.selector !== undefined) {
189 this.selector = selector.selector;
190 this.context = selector.context;
193 return jQuery.makeArray( selector, this );
196 // Start with an empty selector
199 // The current version of jQuery being used
202 // The default length of a jQuery object is 0
205 // The number of elements contained in the matched element set
210 toArray: function() {
211 return slice.call( this, 0 );
214 // Get the Nth element in the matched element set OR
215 // Get the whole matched element set as a clean array
216 get: function( num ) {
219 // Return a 'clean' array
222 // Return just the object
223 ( num < 0 ? this[ this.length + num ] : this[ num ] );
226 // Take an array of elements and push it onto the stack
227 // (returning the new matched element set)
228 pushStack: function( elems, name, selector ) {
229 // Build a new jQuery matched element set
230 var ret = this.constructor();
232 if ( jQuery.isArray( elems ) ) {
233 push.apply( ret, elems );
236 jQuery.merge( ret, elems );
239 // Add the old object onto the stack (as a reference)
240 ret.prevObject = this;
242 ret.context = this.context;
244 if ( name === "find" ) {
245 ret.selector = this.selector + (this.selector ? " " : "") + selector;
247 ret.selector = this.selector + "." + name + "(" + selector + ")";
250 // Return the newly-formed element set
254 // Execute a callback for every element in the matched set.
255 // (You can seed the arguments with an array of args, but this is
256 // only used internally.)
257 each: function( callback, args ) {
258 return jQuery.each( this, callback, args );
262 // Attach the listeners
265 // Change ready & apply
266 return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
272 this.slice( i, +i + 1 );
280 return this.eq( -1 );
284 return this.pushStack( slice.apply( this, arguments ),
285 "slice", slice.call(arguments).join(",") );
288 map: function( callback ) {
289 return this.pushStack( jQuery.map(this, function( elem, i ) {
290 return callback.call( elem, i, elem );
295 return this.prevObject || this.constructor(null);
298 // For internal use only.
299 // Behaves like an Array's method, not like a jQuery method.
305 // Give the init function the jQuery prototype for later instantiation
306 jQuery.fn.init.prototype = jQuery.fn;
308 jQuery.extend = jQuery.fn.extend = function() {
309 var options, name, src, copy, copyIsArray, clone,
310 target = arguments[0] || {},
312 length = arguments.length,
315 // Handle a deep copy situation
316 if ( typeof target === "boolean" ) {
318 target = arguments[1] || {};
319 // skip the boolean and the target
323 // Handle case when target is a string or something (possible in deep copy)
324 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
328 // extend jQuery itself if only one argument is passed
329 if ( length === i ) {
334 for ( ; i < length; i++ ) {
335 // Only deal with non-null/undefined values
336 if ( (options = arguments[ i ]) != null ) {
337 // Extend the base object
338 for ( name in options ) {
339 src = target[ name ];
340 copy = options[ name ];
342 // Prevent never-ending loop
343 if ( target === copy ) {
347 // Recurse if we're merging plain objects or arrays
348 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
351 clone = src && jQuery.isArray(src) ? src : [];
354 clone = src && jQuery.isPlainObject(src) ? src : {};
357 // Never move original objects, clone them
358 target[ name ] = jQuery.extend( deep, clone, copy );
360 // Don't bring in undefined values
361 } else if ( copy !== undefined ) {
362 target[ name ] = copy;
368 // Return the modified object
373 noConflict: function( deep ) {
377 window.jQuery = _jQuery;
383 // Is the DOM ready to be used? Set to true once it occurs.
386 // A counter to track how many items to wait for before
387 // the ready event fires. See #6781
390 // Handle when the DOM is ready
391 ready: function( wait ) {
392 // A third-party is pushing the ready event forwards
393 if ( wait === true ) {
397 // Make sure that the DOM is not already loaded
398 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
399 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
400 if ( !document.body ) {
401 return setTimeout( jQuery.ready, 1 );
404 // Remember that the DOM is ready
405 jQuery.isReady = true;
407 // If a normal DOM Ready event fired, decrement, and wait if need be
408 if ( wait !== true && --jQuery.readyWait > 0 ) {
412 // If there are functions bound, to execute
413 readyList.fire( document , [ jQuery ] );
415 // Trigger any bound ready events
416 if ( jQuery.fn.trigger ) {
417 jQuery( document ).trigger( "ready" ).unbind( "ready" );
422 bindReady: function() {
429 // Catch cases where $(document).ready() is called after the
430 // browser event has already occurred.
431 if ( document.readyState === "complete" ) {
432 // Handle it asynchronously to allow scripts the opportunity to delay ready
433 return setTimeout( jQuery.ready, 1 );
436 // Mozilla, Opera and webkit nightlies currently support this event
437 if ( document.addEventListener ) {
438 // Use the handy event callback
439 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
441 // A fallback to window.onload, that will always work
442 window.addEventListener( "load", jQuery.ready, false );
444 // If IE event model is used
445 } else if ( document.attachEvent ) {
446 // ensure firing before onload,
447 // maybe late but safe also for iframes
448 document.attachEvent("onreadystatechange", DOMContentLoaded);
450 // A fallback to window.onload, that will always work
451 window.attachEvent( "onload", jQuery.ready );
453 // If IE and not a frame
454 // continually check to see if the document is ready
455 var toplevel = false;
458 toplevel = window.frameElement == null;
461 if ( document.documentElement.doScroll && toplevel ) {
467 // See test/unit/core.js for details concerning isFunction.
468 // Since version 1.3, DOM methods and functions like alert
469 // aren't supported. They return false on IE (#2968).
470 isFunction: function( obj ) {
471 return jQuery.type(obj) === "function";
474 isArray: Array.isArray || function( obj ) {
475 return jQuery.type(obj) === "array";
478 // A crude way of determining if an object is a window
479 isWindow: function( obj ) {
480 return obj && typeof obj === "object" && "setInterval" in obj;
483 isNaN: function( obj ) {
484 return obj == null || !rdigit.test( obj ) || isNaN( obj );
487 type: function( obj ) {
490 class2type[ toString.call(obj) ] || "object";
493 isPlainObject: function( obj ) {
494 // Must be an Object.
495 // Because of IE, we also have to check the presence of the constructor property.
496 // Make sure that DOM nodes and window objects don't pass through, as well
497 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
501 // Not own constructor property must be Object
502 if ( obj.constructor &&
503 !hasOwn.call(obj, "constructor") &&
504 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
508 // Own properties are enumerated firstly, so to speed up,
509 // if last one is own, then all properties are own.
512 for ( key in obj ) {}
514 return key === undefined || hasOwn.call( obj, key );
517 isEmptyObject: function( obj ) {
518 for ( var name in obj ) {
524 error: function( msg ) {
528 parseJSON: function( data ) {
529 if ( typeof data !== "string" || !data ) {
533 // Make sure leading/trailing whitespace is removed (IE can't handle it)
534 data = jQuery.trim( data );
536 // Make sure the incoming data is actual JSON
537 // Logic borrowed from http://json.org/json2.js
538 if ( rvalidchars.test(data.replace(rvalidescape, "@")
539 .replace(rvalidtokens, "]")
540 .replace(rvalidbraces, "")) ) {
542 // Try to use the native JSON parser first
543 return window.JSON && window.JSON.parse ?
544 window.JSON.parse( data ) :
545 (new Function("return " + data))();
548 jQuery.error( "Invalid JSON: " + data );
552 // Cross-browser xml parsing
553 // (xml & tmp used internally)
554 parseXML: function( data , xml , tmp ) {
556 if ( window.DOMParser ) { // Standard
557 tmp = new DOMParser();
558 xml = tmp.parseFromString( data , "text/xml" );
560 xml = new ActiveXObject( "Microsoft.XMLDOM" );
565 tmp = xml.documentElement;
567 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
568 jQuery.error( "Invalid XML: " + data );
576 // Evalulates a script in a global context
577 globalEval: function( data ) {
578 if ( data && rnotwhite.test(data) ) {
579 // Inspired by code by Andrea Giammarchi
580 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
581 var head = document.getElementsByTagName("head")[0] || document.documentElement,
582 script = document.createElement("script");
584 script.type = "text/javascript";
586 if ( jQuery.support.scriptEval ) {
587 script.appendChild( document.createTextNode( data ) );
592 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
593 // This arises when a base node is used (#2709).
594 head.insertBefore( script, head.firstChild );
595 head.removeChild( script );
599 nodeName: function( elem, name ) {
600 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
603 // args is for internal usage only
604 each: function( object, callback, args ) {
606 length = object.length,
607 isObj = length === undefined || jQuery.isFunction(object);
611 for ( name in object ) {
612 if ( callback.apply( object[ name ], args ) === false ) {
617 for ( ; i < length; ) {
618 if ( callback.apply( object[ i++ ], args ) === false ) {
624 // A special, fast, case for the most common use of each
627 for ( name in object ) {
628 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
633 for ( var value = object[0];
634 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
641 // Use native String.trim function wherever possible
644 return text == null ?
649 // Otherwise use our own trimming functionality
651 return text == null ?
653 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
656 // results is for internal usage only
657 makeArray: function( array, results ) {
658 var ret = results || [];
660 if ( array != null ) {
661 // The window, strings (and functions) also have 'length'
662 // The extra typeof function check is to prevent crashes
663 // in Safari 2 (See: #3039)
664 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
665 var type = jQuery.type(array);
667 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
668 push.call( ret, array );
670 jQuery.merge( ret, array );
677 inArray: function( elem, array ) {
678 if ( array.indexOf ) {
679 return array.indexOf( elem );
682 for ( var i = 0, length = array.length; i < length; i++ ) {
683 if ( array[ i ] === elem ) {
691 merge: function( first, second ) {
692 var i = first.length,
695 if ( typeof second.length === "number" ) {
696 for ( var l = second.length; j < l; j++ ) {
697 first[ i++ ] = second[ j ];
701 while ( second[j] !== undefined ) {
702 first[ i++ ] = second[ j++ ];
711 grep: function( elems, callback, inv ) {
712 var ret = [], retVal;
715 // Go through the array, only saving the items
716 // that pass the validator function
717 for ( var i = 0, length = elems.length; i < length; i++ ) {
718 retVal = !!callback( elems[ i ], i );
719 if ( inv !== retVal ) {
720 ret.push( elems[ i ] );
727 // arg is for internal usage only
728 map: function( elems, callback, arg ) {
731 // Go through the array, translating each of the items to their
732 // new value (or values).
733 for ( var i = 0, length = elems.length; i < length; i++ ) {
734 value = callback( elems[ i ], i, arg );
736 if ( value != null ) {
737 ret[ ret.length ] = value;
741 // Flatten any nested arrays
742 return ret.concat.apply( [], ret );
745 // A global GUID counter for objects
748 proxy: function( fn, proxy, thisObject ) {
749 if ( arguments.length === 2 ) {
750 if ( typeof proxy === "string" ) {
752 fn = thisObject[ proxy ];
755 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
761 if ( !proxy && fn ) {
763 return fn.apply( thisObject || this, arguments );
767 // Set the guid of unique handler to the same of original handler, so it can be removed
769 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
772 // So proxy can be declared as an argument
776 // Mutifunctional method to get and set values to a collection
777 // The value/s can be optionally by executed if its a function
778 access: function( elems, key, value, exec, fn, pass ) {
779 var length = elems.length;
781 // Setting many attributes
782 if ( typeof key === "object" ) {
783 for ( var k in key ) {
784 jQuery.access( elems, k, key[k], exec, fn, value );
789 // Setting one attribute
790 if ( value !== undefined ) {
791 // Optionally, function values get executed if exec is true
792 exec = !pass && exec && jQuery.isFunction(value);
794 for ( var i = 0; i < length; i++ ) {
795 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
801 // Getting an attribute
802 return length ? fn( elems[0], key ) : undefined;
806 return (new Date()).getTime();
809 // Create a simple deferred (one callbacks list)
810 _Deferred: function() {
812 var // callbacks list
814 // stored [ context , args ]
816 // to avoid firing when already doing so
818 // flag to know if the deferred has been cancelled
820 // the deferred itself
823 // done( f1, f2, ...)
828 var args = arguments,
840 for ( i = 0, length = args.length ; i < length ; i++ ) {
842 type = jQuery.type( elem );
843 if ( type === "array" ) {
844 deferred.done.apply( deferred , elem );
845 } else if ( type === "function" ) {
846 callbacks.push( elem );
851 deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
858 // resolve with given context and args
859 fire: function( context , args ) {
860 if ( ! cancelled && ! fired && ! firing ) {
865 while( callbacks[ 0 ] ) {
866 callbacks.shift().apply( context , args );
870 fired = [ context , args ];
877 // resolve with this as context and given arguments
878 resolve: function() {
879 deferred.fire( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments );
883 // Has this deferred been resolved?
884 isResolved: function() {
885 return !!( firing || fired );
899 // Full fledged deferred (two callbacks list)
900 // Typical success/error system
901 Deferred: function( func ) {
903 var deferred = jQuery._Deferred(),
904 failDeferred = jQuery._Deferred(),
907 // Add errorDeferred methods, then and promise
908 jQuery.extend( deferred , {
910 then: function( doneCallbacks , failCallbacks ) {
911 deferred.done( doneCallbacks ).fail( failCallbacks );
914 fail: failDeferred.done,
915 fireReject: failDeferred.fire,
916 reject: failDeferred.resolve,
917 isRejected: failDeferred.isResolved,
918 // Get a promise for this deferred
919 // If obj is provided, the promise aspect is added to the object
920 // (i is used internally)
921 promise: function( obj , i ) {
928 i = promiseMethods.length;
930 obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
937 // Make sure only one callback list will be used
938 deferred.then( failDeferred.cancel , deferred.cancel );
941 delete deferred.cancel;
943 // Call given func if any
945 func.call( deferred , deferred );
952 when: function( object ) {
953 var args = arguments,
954 length = args.length,
955 deferred = length <= 1 && object && jQuery.isFunction( object.promise ) ?
958 promise = deferred.promise(),
962 resolveArray = new Array( length );
963 jQuery.each( args, function( index, element, args ) {
964 jQuery.when( element ).done( function( value ) {
966 resolveArray[ index ] = args.length > 1 ? slice.call( args , 0 ) : value;
968 deferred.fire( promise, resolveArray );
970 }).fail( function() {
971 deferred.fireReject( promise, arguments );
973 return !deferred.isRejected();
975 } else if ( deferred !== object ) {
976 deferred.resolve( object );
981 // Use of jQuery.browser is frowned upon.
982 // More details: http://docs.jquery.com/Utilities/jQuery.browser
983 uaMatch: function( ua ) {
984 ua = ua.toLowerCase();
986 var match = rwebkit.exec( ua ) ||
989 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
992 return { browser: match[1] || "", version: match[2] || "0" };
995 subclass: function(){
996 function jQuerySubclass( selector, context ) {
997 return new jQuerySubclass.fn.init( selector, context );
999 jQuerySubclass.superclass = this;
1000 jQuerySubclass.fn = jQuerySubclass.prototype = this();
1001 jQuerySubclass.fn.constructor = jQuerySubclass;
1002 jQuerySubclass.subclass = this.subclass;
1003 jQuerySubclass.fn.init = function init( selector, context ) {
1004 if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){
1005 context = jQuerySubclass(context);
1007 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
1009 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
1010 var rootjQuerySubclass = jQuerySubclass(document);
1011 return jQuerySubclass;
1017 // Create readyList deferred
1018 readyList = jQuery._Deferred();
1020 // Populate the class2type map
1021 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
1022 class2type[ "[object " + name + "]" ] = name.toLowerCase();
1025 browserMatch = jQuery.uaMatch( userAgent );
1026 if ( browserMatch.browser ) {
1027 jQuery.browser[ browserMatch.browser ] = true;
1028 jQuery.browser.version = browserMatch.version;
1031 // Deprecated, use jQuery.browser.webkit instead
1032 if ( jQuery.browser.webkit ) {
1033 jQuery.browser.safari = true;
1037 jQuery.inArray = function( elem, array ) {
1038 return indexOf.call( array, elem );
1042 // Verify that \s matches non-breaking spaces
1043 // (IE fails on this test)
1044 if ( !rwhite.test( "\xA0" ) ) {
1045 trimLeft = /^[\s\xA0]+/;
1046 trimRight = /[\s\xA0]+$/;
1049 // All jQuery objects should point back to these
1050 rootjQuery = jQuery(document);
1052 // Cleanup functions for the document ready method
1053 if ( document.addEventListener ) {
1054 DOMContentLoaded = function() {
1055 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1059 } else if ( document.attachEvent ) {
1060 DOMContentLoaded = function() {
1061 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1062 if ( document.readyState === "complete" ) {
1063 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1069 // The DOM ready check for Internet Explorer
1070 function doScrollCheck() {
1071 if ( jQuery.isReady ) {
1076 // If IE is used, use the trick by Diego Perini
1077 // http://javascript.nwbox.com/IEContentLoaded/
1078 document.documentElement.doScroll("left");
1080 setTimeout( doScrollCheck, 1 );
1084 // and execute any waiting functions
1088 // Expose jQuery to the global object
1089 return (window.jQuery = window.$ = jQuery);