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 );
9 // Map over jQuery in case of overwrite
10 _jQuery = window.jQuery,
12 // Map over the $ in case of overwrite
15 // A central reference to the root jQuery(document)
18 // A simple way to check for HTML strings or ID strings
19 // (both of which we optimize for)
20 quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
22 // Is it a simple selector
23 isSimple = /^.[^:#\[\.,]*$/,
25 // Check if a string has a non-whitespace character in it
29 // Used for trimming whitespace
33 // Check for non-word characters
39 // Match a standalone tag
40 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
43 rvalidchars = /^[\],:{}\s]*$/,
44 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
45 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
46 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
49 rwebkit = /(webkit)[ \/]([\w.]+)/,
50 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
51 rmsie = /(msie) ([\w.]+)/,
52 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
54 // Keep a UserAgent string for use with jQuery.browser
55 userAgent = navigator.userAgent,
57 // For matching the engine and version of the browser
60 // Has the ready events already been bound?
63 // The deferred used on DOM ready
66 // The ready event handler
69 // Save a reference to some core methods
70 toString = Object.prototype.toString,
71 hasOwn = Object.prototype.hasOwnProperty,
72 push = Array.prototype.push,
73 slice = Array.prototype.slice,
74 trim = String.prototype.trim,
75 indexOf = Array.prototype.indexOf,
77 // [[Class]] -> type pairs
80 // Marker for deferred
83 jQuery.fn = jQuery.prototype = {
84 init: function( selector, context ) {
85 var match, elem, ret, doc;
87 // Handle $(""), $(null), or $(undefined)
92 // Handle $(DOMElement)
93 if ( selector.nodeType ) {
94 this.context = this[0] = selector;
99 // The body element only exists once, optimize finding it
100 if ( selector === "body" && !context && document.body ) {
101 this.context = document;
102 this[0] = document.body;
103 this.selector = "body";
108 // Handle HTML strings
109 if ( typeof selector === "string" ) {
110 // Are we dealing with HTML string or an ID?
111 match = quickExpr.exec( selector );
113 // Verify a match, and that no context was specified for #id
114 if ( match && (match[1] || !context) ) {
116 // HANDLE: $(html) -> $(array)
118 doc = (context ? context.ownerDocument || context : document);
120 // If a single string is passed in and it's a single tag
121 // just do a createElement and skip the rest
122 ret = rsingleTag.exec( selector );
125 if ( jQuery.isPlainObject( context ) ) {
126 selector = [ document.createElement( ret[1] ) ];
127 jQuery.fn.attr.call( selector, context, true );
130 selector = [ doc.createElement( ret[1] ) ];
134 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
135 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
138 return jQuery.merge( this, selector );
142 elem = document.getElementById( match[2] );
144 // Check parentNode to catch when Blackberry 4.6 returns
145 // nodes that are no longer in the document #6963
146 if ( elem && elem.parentNode ) {
147 // Handle the case where IE and Opera return items
148 // by name instead of ID
149 if ( elem.id !== match[2] ) {
150 return rootjQuery.find( selector );
153 // Otherwise, we inject the element directly into the jQuery object
158 this.context = document;
159 this.selector = selector;
164 } else if ( !context && !rnonword.test( selector ) ) {
165 this.selector = selector;
166 this.context = document;
167 selector = document.getElementsByTagName( selector );
168 return jQuery.merge( this, selector );
170 // HANDLE: $(expr, $(...))
171 } else if ( !context || context.jquery ) {
172 return (context || rootjQuery).find( selector );
174 // HANDLE: $(expr, context)
175 // (which is just equivalent to: $(context).find(expr)
177 return jQuery( context ).find( selector );
180 // HANDLE: $(function)
181 // Shortcut for document ready
182 } else if ( jQuery.isFunction( selector ) ) {
183 return rootjQuery.ready( selector );
186 if (selector.selector !== undefined) {
187 this.selector = selector.selector;
188 this.context = selector.context;
191 return jQuery.makeArray( selector, this );
194 // Start with an empty selector
197 // The current version of jQuery being used
200 // The default length of a jQuery object is 0
203 // The number of elements contained in the matched element set
208 toArray: function() {
209 return slice.call( this, 0 );
212 // Get the Nth element in the matched element set OR
213 // Get the whole matched element set as a clean array
214 get: function( num ) {
217 // Return a 'clean' array
220 // Return just the object
221 ( num < 0 ? this[ this.length + num ] : this[ num ] );
224 // Take an array of elements and push it onto the stack
225 // (returning the new matched element set)
226 pushStack: function( elems, name, selector ) {
227 // Build a new jQuery matched element set
230 if ( jQuery.isArray( elems ) ) {
231 push.apply( ret, elems );
234 jQuery.merge( ret, elems );
237 // Add the old object onto the stack (as a reference)
238 ret.prevObject = this;
240 ret.context = this.context;
242 if ( name === "find" ) {
243 ret.selector = this.selector + (this.selector ? " " : "") + selector;
245 ret.selector = this.selector + "." + name + "(" + selector + ")";
248 // Return the newly-formed element set
252 // Execute a callback for every element in the matched set.
253 // (You can seed the arguments with an array of args, but this is
254 // only used internally.)
255 each: function( callback, args ) {
256 return jQuery.each( this, callback, args );
260 // Attach the listeners
263 // Change ready & apply
264 return ( jQuery.fn.ready = readyList.then ).apply( this , arguments );
270 this.slice( i, +i + 1 );
278 return this.eq( -1 );
282 return this.pushStack( slice.apply( this, arguments ),
283 "slice", slice.call(arguments).join(",") );
286 map: function( callback ) {
287 return this.pushStack( jQuery.map(this, function( elem, i ) {
288 return callback.call( elem, i, elem );
293 return this.prevObject || jQuery(null);
296 // For internal use only.
297 // Behaves like an Array's method, not like a jQuery method.
303 // Give the init function the jQuery prototype for later instantiation
304 jQuery.fn.init.prototype = jQuery.fn;
306 jQuery.extend = jQuery.fn.extend = function() {
307 var options, name, src, copy, copyIsArray, clone,
308 target = arguments[0] || {},
310 length = arguments.length,
313 // Handle a deep copy situation
314 if ( typeof target === "boolean" ) {
316 target = arguments[1] || {};
317 // skip the boolean and the target
321 // Handle case when target is a string or something (possible in deep copy)
322 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
326 // extend jQuery itself if only one argument is passed
327 if ( length === i ) {
332 for ( ; i < length; i++ ) {
333 // Only deal with non-null/undefined values
334 if ( (options = arguments[ i ]) != null ) {
335 // Extend the base object
336 for ( name in options ) {
337 src = target[ name ];
338 copy = options[ name ];
340 // Prevent never-ending loop
341 if ( target === copy ) {
345 // Recurse if we're merging plain objects or arrays
346 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
349 clone = src && jQuery.isArray(src) ? src : [];
352 clone = src && jQuery.isPlainObject(src) ? src : {};
355 // Never move original objects, clone them
356 target[ name ] = jQuery.extend( deep, clone, copy );
358 // Don't bring in undefined values
359 } else if ( copy !== undefined ) {
360 target[ name ] = copy;
366 // Return the modified object
371 noConflict: function( deep ) {
375 window.jQuery = _jQuery;
381 // Is the DOM ready to be used? Set to true once it occurs.
384 // A counter to track how many items to wait for before
385 // the ready event fires. See #6781
388 // Handle when the DOM is ready
389 ready: function( wait ) {
390 // A third-party is pushing the ready event forwards
391 if ( wait === true ) {
395 // Make sure that the DOM is not already loaded
396 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
397 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
398 if ( !document.body ) {
399 return setTimeout( jQuery.ready, 1 );
402 // Remember that the DOM is ready
403 jQuery.isReady = true;
405 // If a normal DOM Ready event fired, decrement, and wait if need be
406 if ( wait !== true && --jQuery.readyWait > 0 ) {
410 // If there are functions bound, to execute
411 readyList.fire( document , [ jQuery ] );
413 // Trigger any bound ready events
414 if ( jQuery.fn.trigger ) {
415 jQuery( document ).trigger( "ready" ).unbind( "ready" );
420 bindReady: function() {
427 // Catch cases where $(document).ready() is called after the
428 // browser event has already occurred.
429 if ( document.readyState === "complete" ) {
430 // Handle it asynchronously to allow scripts the opportunity to delay ready
431 return setTimeout( jQuery.ready, 1 );
434 // Mozilla, Opera and webkit nightlies currently support this event
435 if ( document.addEventListener ) {
436 // Use the handy event callback
437 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
439 // A fallback to window.onload, that will always work
440 window.addEventListener( "load", jQuery.ready, false );
442 // If IE event model is used
443 } else if ( document.attachEvent ) {
444 // ensure firing before onload,
445 // maybe late but safe also for iframes
446 document.attachEvent("onreadystatechange", DOMContentLoaded);
448 // A fallback to window.onload, that will always work
449 window.attachEvent( "onload", jQuery.ready );
451 // If IE and not a frame
452 // continually check to see if the document is ready
453 var toplevel = false;
456 toplevel = window.frameElement == null;
459 if ( document.documentElement.doScroll && toplevel ) {
465 // See test/unit/core.js for details concerning isFunction.
466 // Since version 1.3, DOM methods and functions like alert
467 // aren't supported. They return false on IE (#2968).
468 isFunction: function( obj ) {
469 return jQuery.type(obj) === "function";
472 isArray: Array.isArray || function( obj ) {
473 return jQuery.type(obj) === "array";
476 // A crude way of determining if an object is a window
477 isWindow: function( obj ) {
478 return obj && typeof obj === "object" && "setInterval" in obj;
481 isNaN: function( obj ) {
482 return obj == null || !rdigit.test( obj ) || isNaN( obj );
485 type: function( obj ) {
488 class2type[ toString.call(obj) ] || "object";
491 isPlainObject: function( obj ) {
492 // Must be an Object.
493 // Because of IE, we also have to check the presence of the constructor property.
494 // Make sure that DOM nodes and window objects don't pass through, as well
495 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
499 // Not own constructor property must be Object
500 if ( obj.constructor &&
501 !hasOwn.call(obj, "constructor") &&
502 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
506 // Own properties are enumerated firstly, so to speed up,
507 // if last one is own, then all properties are own.
510 for ( key in obj ) {}
512 return key === undefined || hasOwn.call( obj, key );
515 isEmptyObject: function( obj ) {
516 for ( var name in obj ) {
522 error: function( msg ) {
526 parseJSON: function( data ) {
527 if ( typeof data !== "string" || !data ) {
531 // Make sure leading/trailing whitespace is removed (IE can't handle it)
532 data = jQuery.trim( data );
534 // Make sure the incoming data is actual JSON
535 // Logic borrowed from http://json.org/json2.js
536 if ( rvalidchars.test(data.replace(rvalidescape, "@")
537 .replace(rvalidtokens, "]")
538 .replace(rvalidbraces, "")) ) {
540 // Try to use the native JSON parser first
541 return window.JSON && window.JSON.parse ?
542 window.JSON.parse( data ) :
543 (new Function("return " + data))();
546 jQuery.error( "Invalid JSON: " + data );
552 // Evalulates a script in a global context
553 globalEval: function( data ) {
554 if ( data && rnotwhite.test(data) ) {
555 // Inspired by code by Andrea Giammarchi
556 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
557 var head = document.getElementsByTagName("head")[0] || document.documentElement,
558 script = document.createElement("script");
560 script.type = "text/javascript";
562 if ( jQuery.support.scriptEval ) {
563 script.appendChild( document.createTextNode( data ) );
568 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
569 // This arises when a base node is used (#2709).
570 head.insertBefore( script, head.firstChild );
571 head.removeChild( script );
575 nodeName: function( elem, name ) {
576 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
579 // args is for internal usage only
580 each: function( object, callback, args ) {
582 length = object.length,
583 isObj = length === undefined || jQuery.isFunction(object);
587 for ( name in object ) {
588 if ( callback.apply( object[ name ], args ) === false ) {
593 for ( ; i < length; ) {
594 if ( callback.apply( object[ i++ ], args ) === false ) {
600 // A special, fast, case for the most common use of each
603 for ( name in object ) {
604 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
609 for ( var value = object[0];
610 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
617 // Use native String.trim function wherever possible
620 return text == null ?
625 // Otherwise use our own trimming functionality
627 return text == null ?
629 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
632 // results is for internal usage only
633 makeArray: function( array, results ) {
634 var ret = results || [];
636 if ( array != null ) {
637 // The window, strings (and functions) also have 'length'
638 // The extra typeof function check is to prevent crashes
639 // in Safari 2 (See: #3039)
640 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
641 var type = jQuery.type(array);
643 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
644 push.call( ret, array );
646 jQuery.merge( ret, array );
653 inArray: function( elem, array ) {
654 if ( array.indexOf ) {
655 return array.indexOf( elem );
658 for ( var i = 0, length = array.length; i < length; i++ ) {
659 if ( array[ i ] === elem ) {
667 merge: function( first, second ) {
668 var i = first.length,
671 if ( typeof second.length === "number" ) {
672 for ( var l = second.length; j < l; j++ ) {
673 first[ i++ ] = second[ j ];
677 while ( second[j] !== undefined ) {
678 first[ i++ ] = second[ j++ ];
687 grep: function( elems, callback, inv ) {
688 var ret = [], retVal;
691 // Go through the array, only saving the items
692 // that pass the validator function
693 for ( var i = 0, length = elems.length; i < length; i++ ) {
694 retVal = !!callback( elems[ i ], i );
695 if ( inv !== retVal ) {
696 ret.push( elems[ i ] );
703 // arg is for internal usage only
704 map: function( elems, callback, arg ) {
707 // Go through the array, translating each of the items to their
708 // new value (or values).
709 for ( var i = 0, length = elems.length; i < length; i++ ) {
710 value = callback( elems[ i ], i, arg );
712 if ( value != null ) {
713 ret[ ret.length ] = value;
717 // Flatten any nested arrays
718 return ret.concat.apply( [], ret );
721 // A global GUID counter for objects
724 proxy: function( fn, proxy, thisObject ) {
725 if ( arguments.length === 2 ) {
726 if ( typeof proxy === "string" ) {
728 fn = thisObject[ proxy ];
731 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
737 if ( !proxy && fn ) {
739 return fn.apply( thisObject || this, arguments );
743 // Set the guid of unique handler to the same of original handler, so it can be removed
745 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
748 // So proxy can be declared as an argument
752 // Mutifunctional method to get and set values to a collection
753 // The value/s can be optionally by executed if its a function
754 access: function( elems, key, value, exec, fn, pass ) {
755 var length = elems.length;
757 // Setting many attributes
758 if ( typeof key === "object" ) {
759 for ( var k in key ) {
760 jQuery.access( elems, k, key[k], exec, fn, value );
765 // Setting one attribute
766 if ( value !== undefined ) {
767 // Optionally, function values get executed if exec is true
768 exec = !pass && exec && jQuery.isFunction(value);
770 for ( var i = 0; i < length; i++ ) {
771 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
777 // Getting an attribute
778 return length ? fn( elems[0], key ) : undefined;
782 return (new Date()).getTime();
785 // Create a simple deferred (one callbacks list)
786 _deferred: function( cancellable ) {
788 // cancellable by default
789 cancellable = cancellable !== false;
791 var // callbacks list
793 // stored [ context , args ]
795 // to avoid firing when already doing so
797 // flag to know if the deferred has been cancelled
799 // the deferred itself
802 // then( f1, f2, ...)
807 var args = arguments,
819 for ( i = 0, length = args.length ; i < length ; i++ ) {
821 type = jQuery.type( elem );
822 if ( type === "array" ) {
823 deferred.then.apply( deferred , elem );
824 } else if ( type === "function" ) {
825 callbacks.push( elem );
830 deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
836 // resolve with given context and args
837 // (i is used internally)
838 fire: function( context , args , i ) {
839 if ( ! cancelled && ! fired && ! firing ) {
842 for( i = 0 ; ! cancelled && callbacks[ i ] ; i++ ) {
843 cancelled = ( callbacks[ i ].apply( context , args ) === false ) && cancellable;
846 cancelled = cancellable;
849 fired = [ context , args ];
850 callbacks = cancelled ? [] : callbacks.slice( i + 1 );
857 // resolve with this as context and given arguments
858 resolve: function() {
859 deferred.fire( this , arguments );
863 // cancelling further callbacks
874 // Add the deferred marker
875 deferred.then._ = deferredMarker;
880 // Full fledged deferred (two callbacks list)
881 // Typical success/error system
882 deferred: function( func , cancellable ) {
885 if ( arguments.length === 1 ) {
887 if ( typeof func === "boolean" ) {
893 var errorDeferred = jQuery._deferred( cancellable ),
894 deferred = jQuery._deferred( cancellable ),
895 // Keep reference of the cancel method since we'll redefine it
896 cancelThen = deferred.cancel;
898 // Add errorDeferred methods and redefine cancel
899 jQuery.extend( deferred , {
901 fail: errorDeferred.then,
902 fireReject: errorDeferred.fire,
903 reject: errorDeferred.resolve,
906 errorDeferred.cancel();
912 // Make sure only one callback list will be used
913 deferred.then( errorDeferred.cancel ).fail( cancelThen );
915 // Call given func if any
917 func.call( deferred , deferred );
923 // Check if an object is a deferred
924 isDeferred: function( object , method ) {
925 method = method || "then";
926 return !!( object && object[ method ] && object[ method ]._ === deferredMarker );
930 when: function( object , method ) {
931 method = method || "then";
932 object = jQuery.isDeferred( object , method ) ?
934 jQuery.deferred().resolve( object );
935 object.fail = object.fail || function() { return this; };
936 object[ method ] = object[ method ] || object.then;
937 object.then = object.then || object[ method ];
941 // Use of jQuery.browser is frowned upon.
942 // More details: http://docs.jquery.com/Utilities/jQuery.browser
943 uaMatch: function( ua ) {
944 ua = ua.toLowerCase();
946 var match = rwebkit.exec( ua ) ||
949 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
952 return { browser: match[1] || "", version: match[2] || "0" };
958 // Create readyList deferred
959 // also force $.fn.ready to be recognized as a defer
960 readyList = jQuery._deferred( false );
961 jQuery.fn.ready._ = deferredMarker;
963 // Populate the class2type map
964 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
965 class2type[ "[object " + name + "]" ] = name.toLowerCase();
968 browserMatch = jQuery.uaMatch( userAgent );
969 if ( browserMatch.browser ) {
970 jQuery.browser[ browserMatch.browser ] = true;
971 jQuery.browser.version = browserMatch.version;
974 // Deprecated, use jQuery.browser.webkit instead
975 if ( jQuery.browser.webkit ) {
976 jQuery.browser.safari = true;
980 jQuery.inArray = function( elem, array ) {
981 return indexOf.call( array, elem );
985 // Verify that \s matches non-breaking spaces
986 // (IE fails on this test)
987 if ( !rwhite.test( "\xA0" ) ) {
988 trimLeft = /^[\s\xA0]+/;
989 trimRight = /[\s\xA0]+$/;
992 // All jQuery objects should point back to these
993 rootjQuery = jQuery(document);
995 // Cleanup functions for the document ready method
996 if ( document.addEventListener ) {
997 DOMContentLoaded = function() {
998 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1002 } else if ( document.attachEvent ) {
1003 DOMContentLoaded = function() {
1004 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1005 if ( document.readyState === "complete" ) {
1006 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1012 // The DOM ready check for Internet Explorer
1013 function doScrollCheck() {
1014 if ( jQuery.isReady ) {
1019 // If IE is used, use the trick by Diego Perini
1020 // http://javascript.nwbox.com/IEContentLoaded/
1021 document.documentElement.doScroll("left");
1023 setTimeout( doScrollCheck, 1 );
1027 // and execute any waiting functions
1031 // Expose jQuery to the global object
1032 return (window.jQuery = window.$ = jQuery);