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
29 // Check for non-word characters
35 // Match a standalone tag
36 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
39 rvalidchars = /^[\],:{}\s]*$/,
40 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
41 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
42 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
45 rwebkit = /(webkit)[ \/]([\w.]+)/,
46 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
47 rmsie = /(msie) ([\w.]+)/,
48 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
50 // Keep a UserAgent string for use with jQuery.browser
51 userAgent = navigator.userAgent,
53 // For matching the engine and version of the browser
56 // Has the ready events already been bound?
59 // The deferred used on DOM ready
63 promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
65 // The ready event handler
68 // Save a reference to some core methods
69 toString = Object.prototype.toString,
70 hasOwn = Object.prototype.hasOwnProperty,
71 push = Array.prototype.push,
72 slice = Array.prototype.slice,
73 trim = String.prototype.trim,
74 indexOf = Array.prototype.indexOf,
76 // [[Class]] -> type pairs
79 jQuery.fn = jQuery.prototype = {
81 init: function( selector, context, rootjQuery ) {
82 var match, elem, ret, doc;
84 // Handle $(""), $(null), or $(undefined)
89 // Handle $(DOMElement)
90 if ( selector.nodeType ) {
91 this.context = this[0] = selector;
96 // The body element only exists once, optimize finding it
97 if ( selector === "body" && !context && document.body ) {
98 this.context = document;
99 this[0] = document.body;
100 this.selector = "body";
105 // Handle HTML strings
106 if ( typeof selector === "string" ) {
107 // Are we dealing with HTML string or an ID?
108 match = quickExpr.exec( selector );
110 // Verify a match, and that no context was specified for #id
111 if ( match && (match[1] || !context) ) {
113 // HANDLE: $(html) -> $(array)
115 context = context instanceof jQuery ? context[0] : context;
116 doc = (context ? context.ownerDocument || context : document);
118 // If a single string is passed in and it's a single tag
119 // just do a createElement and skip the rest
120 ret = rsingleTag.exec( selector );
123 if ( jQuery.isPlainObject( context ) ) {
124 selector = [ document.createElement( ret[1] ) ];
125 jQuery.fn.attr.call( selector, context, true );
128 selector = [ doc.createElement( ret[1] ) ];
132 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
133 selector = (ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment).childNodes;
136 return jQuery.merge( this, selector );
140 elem = document.getElementById( match[2] );
142 // Check parentNode to catch when Blackberry 4.6 returns
143 // nodes that are no longer in the document #6963
144 if ( elem && elem.parentNode ) {
145 // Handle the case where IE and Opera return items
146 // by name instead of ID
147 if ( elem.id !== match[2] ) {
148 return rootjQuery.find( selector );
151 // Otherwise, we inject the element directly into the jQuery object
156 this.context = document;
157 this.selector = selector;
161 // HANDLE: $(expr, $(...))
162 } else if ( !context || context.jquery ) {
163 return (context || rootjQuery).find( selector );
165 // HANDLE: $(expr, context)
166 // (which is just equivalent to: $(context).find(expr)
168 return this.constructor( context ).find( selector );
171 // HANDLE: $(function)
172 // Shortcut for document ready
173 } else if ( jQuery.isFunction( selector ) ) {
174 return rootjQuery.ready( selector );
177 if (selector.selector !== undefined) {
178 this.selector = selector.selector;
179 this.context = selector.context;
182 return jQuery.makeArray( selector, this );
185 // Start with an empty selector
188 // The current version of jQuery being used
191 // The default length of a jQuery object is 0
194 // The number of elements contained in the matched element set
199 toArray: function() {
200 return slice.call( this, 0 );
203 // Get the Nth element in the matched element set OR
204 // Get the whole matched element set as a clean array
205 get: function( num ) {
208 // Return a 'clean' array
211 // Return just the object
212 ( num < 0 ? this[ this.length + num ] : this[ num ] );
215 // Take an array of elements and push it onto the stack
216 // (returning the new matched element set)
217 pushStack: function( elems, name, selector ) {
218 // Build a new jQuery matched element set
219 var ret = this.constructor();
221 if ( jQuery.isArray( elems ) ) {
222 push.apply( ret, elems );
225 jQuery.merge( ret, elems );
228 // Add the old object onto the stack (as a reference)
229 ret.prevObject = this;
231 ret.context = this.context;
233 if ( name === "find" ) {
234 ret.selector = this.selector + (this.selector ? " " : "") + selector;
236 ret.selector = this.selector + "." + name + "(" + selector + ")";
239 // Return the newly-formed element set
243 // Execute a callback for every element in the matched set.
244 // (You can seed the arguments with an array of args, but this is
245 // only used internally.)
246 each: function( callback, args ) {
247 return jQuery.each( this, callback, args );
250 ready: function( fn ) {
251 // Attach the listeners
255 readyList.done( fn );
263 this.slice( i, +i + 1 );
271 return this.eq( -1 );
275 return this.pushStack( slice.apply( this, arguments ),
276 "slice", slice.call(arguments).join(",") );
279 map: function( callback ) {
280 return this.pushStack( jQuery.map(this, function( elem, i ) {
281 return callback.call( elem, i, elem );
286 return this.prevObject || this.constructor(null);
289 // For internal use only.
290 // Behaves like an Array's method, not like a jQuery method.
296 // Give the init function the jQuery prototype for later instantiation
297 jQuery.fn.init.prototype = jQuery.fn;
299 jQuery.extend = jQuery.fn.extend = function() {
300 var options, name, src, copy, copyIsArray, clone,
301 target = arguments[0] || {},
303 length = arguments.length,
306 // Handle a deep copy situation
307 if ( typeof target === "boolean" ) {
309 target = arguments[1] || {};
310 // skip the boolean and the target
314 // Handle case when target is a string or something (possible in deep copy)
315 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
319 // extend jQuery itself if only one argument is passed
320 if ( length === i ) {
325 for ( ; i < length; i++ ) {
326 // Only deal with non-null/undefined values
327 if ( (options = arguments[ i ]) != null ) {
328 // Extend the base object
329 for ( name in options ) {
330 src = target[ name ];
331 copy = options[ name ];
333 // Prevent never-ending loop
334 if ( target === copy ) {
338 // Recurse if we're merging plain objects or arrays
339 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
342 clone = src && jQuery.isArray(src) ? src : [];
345 clone = src && jQuery.isPlainObject(src) ? src : {};
348 // Never move original objects, clone them
349 target[ name ] = jQuery.extend( deep, clone, copy );
351 // Don't bring in undefined values
352 } else if ( copy !== undefined ) {
353 target[ name ] = copy;
359 // Return the modified object
364 noConflict: function( deep ) {
368 window.jQuery = _jQuery;
374 // Is the DOM ready to be used? Set to true once it occurs.
377 // A counter to track how many items to wait for before
378 // the ready event fires. See #6781
381 // Handle when the DOM is ready
382 ready: function( wait ) {
383 // A third-party is pushing the ready event forwards
384 if ( wait === true ) {
388 // Make sure that the DOM is not already loaded
389 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
390 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
391 if ( !document.body ) {
392 return setTimeout( jQuery.ready, 1 );
395 // Remember that the DOM is ready
396 jQuery.isReady = true;
398 // If a normal DOM Ready event fired, decrement, and wait if need be
399 if ( wait !== true && --jQuery.readyWait > 0 ) {
403 // If there are functions bound, to execute
404 readyList.resolveWith( document, [ jQuery ] );
406 // Trigger any bound ready events
407 if ( jQuery.fn.trigger ) {
408 jQuery( document ).trigger( "ready" ).unbind( "ready" );
413 bindReady: function() {
420 // Catch cases where $(document).ready() is called after the
421 // browser event has already occurred.
422 if ( document.readyState === "complete" ) {
423 // Handle it asynchronously to allow scripts the opportunity to delay ready
424 return setTimeout( jQuery.ready, 1 );
427 // Mozilla, Opera and webkit nightlies currently support this event
428 if ( document.addEventListener ) {
429 // Use the handy event callback
430 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
432 // A fallback to window.onload, that will always work
433 window.addEventListener( "load", jQuery.ready, false );
435 // If IE event model is used
436 } else if ( document.attachEvent ) {
437 // ensure firing before onload,
438 // maybe late but safe also for iframes
439 document.attachEvent("onreadystatechange", DOMContentLoaded);
441 // A fallback to window.onload, that will always work
442 window.attachEvent( "onload", jQuery.ready );
444 // If IE and not a frame
445 // continually check to see if the document is ready
446 var toplevel = false;
449 toplevel = window.frameElement == null;
452 if ( document.documentElement.doScroll && toplevel ) {
458 // See test/unit/core.js for details concerning isFunction.
459 // Since version 1.3, DOM methods and functions like alert
460 // aren't supported. They return false on IE (#2968).
461 isFunction: function( obj ) {
462 return jQuery.type(obj) === "function";
465 isArray: Array.isArray || function( obj ) {
466 return jQuery.type(obj) === "array";
469 // A crude way of determining if an object is a window
470 isWindow: function( obj ) {
471 return obj && typeof obj === "object" && "setInterval" in obj;
474 isNaN: function( obj ) {
475 return obj == null || !rdigit.test( obj ) || isNaN( obj );
478 type: function( obj ) {
481 class2type[ toString.call(obj) ] || "object";
484 isPlainObject: function( obj ) {
485 // Must be an Object.
486 // Because of IE, we also have to check the presence of the constructor property.
487 // Make sure that DOM nodes and window objects don't pass through, as well
488 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
492 // Not own constructor property must be Object
493 if ( obj.constructor &&
494 !hasOwn.call(obj, "constructor") &&
495 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
499 // Own properties are enumerated firstly, so to speed up,
500 // if last one is own, then all properties are own.
503 for ( key in obj ) {}
505 return key === undefined || hasOwn.call( obj, key );
508 isEmptyObject: function( obj ) {
509 for ( var name in obj ) {
515 error: function( msg ) {
519 parseJSON: function( data ) {
520 if ( typeof data !== "string" || !data ) {
524 // Make sure leading/trailing whitespace is removed (IE can't handle it)
525 data = jQuery.trim( data );
527 // Make sure the incoming data is actual JSON
528 // Logic borrowed from http://json.org/json2.js
529 if ( rvalidchars.test(data.replace(rvalidescape, "@")
530 .replace(rvalidtokens, "]")
531 .replace(rvalidbraces, "")) ) {
533 // Try to use the native JSON parser first
534 return window.JSON && window.JSON.parse ?
535 window.JSON.parse( data ) :
536 (new Function("return " + data))();
539 jQuery.error( "Invalid JSON: " + data );
543 // Cross-browser xml parsing
544 // (xml & tmp used internally)
545 parseXML: function( data , xml , tmp ) {
547 if ( window.DOMParser ) { // Standard
548 tmp = new DOMParser();
549 xml = tmp.parseFromString( data , "text/xml" );
551 xml = new ActiveXObject( "Microsoft.XMLDOM" );
556 tmp = xml.documentElement;
558 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
559 jQuery.error( "Invalid XML: " + data );
567 // Evalulates a script in a global context
568 globalEval: function( data ) {
569 if ( data && rnotwhite.test(data) ) {
570 // Inspired by code by Andrea Giammarchi
571 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
572 var head = document.getElementsByTagName("head")[0] || document.documentElement,
573 script = document.createElement("script");
575 script.type = "text/javascript";
577 if ( jQuery.support.scriptEval() ) {
578 script.appendChild( document.createTextNode( data ) );
583 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
584 // This arises when a base node is used (#2709).
585 head.insertBefore( script, head.firstChild );
586 head.removeChild( script );
590 nodeName: function( elem, name ) {
591 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
594 // args is for internal usage only
595 each: function( object, callback, args ) {
597 length = object.length,
598 isObj = length === undefined || jQuery.isFunction(object);
602 for ( name in object ) {
603 if ( callback.apply( object[ name ], args ) === false ) {
608 for ( ; i < length; ) {
609 if ( callback.apply( object[ i++ ], args ) === false ) {
615 // A special, fast, case for the most common use of each
618 for ( name in object ) {
619 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
624 for ( var value = object[0];
625 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
632 // Use native String.trim function wherever possible
635 return text == null ?
640 // Otherwise use our own trimming functionality
642 return text == null ?
644 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
647 // results is for internal usage only
648 makeArray: function( array, results ) {
649 var ret = results || [];
651 if ( array != null ) {
652 // The window, strings (and functions) also have 'length'
653 // The extra typeof function check is to prevent crashes
654 // in Safari 2 (See: #3039)
655 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
656 var type = jQuery.type(array);
658 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
659 push.call( ret, array );
661 jQuery.merge( ret, array );
668 inArray: function( elem, array ) {
669 if ( array.indexOf ) {
670 return array.indexOf( elem );
673 for ( var i = 0, length = array.length; i < length; i++ ) {
674 if ( array[ i ] === elem ) {
682 merge: function( first, second ) {
683 var i = first.length,
686 if ( typeof second.length === "number" ) {
687 for ( var l = second.length; j < l; j++ ) {
688 first[ i++ ] = second[ j ];
692 while ( second[j] !== undefined ) {
693 first[ i++ ] = second[ j++ ];
702 grep: function( elems, callback, inv ) {
703 var ret = [], retVal;
706 // Go through the array, only saving the items
707 // that pass the validator function
708 for ( var i = 0, length = elems.length; i < length; i++ ) {
709 retVal = !!callback( elems[ i ], i );
710 if ( inv !== retVal ) {
711 ret.push( elems[ i ] );
718 // arg is for internal usage only
719 map: function( elems, callback, arg ) {
722 // Go through the array, translating each of the items to their
723 // new value (or values).
724 for ( var i = 0, length = elems.length; i < length; i++ ) {
725 value = callback( elems[ i ], i, arg );
727 if ( value != null ) {
728 ret[ ret.length ] = value;
732 // Flatten any nested arrays
733 return ret.concat.apply( [], ret );
736 // A global GUID counter for objects
739 proxy: function( fn, proxy, thisObject ) {
740 if ( arguments.length === 2 ) {
741 if ( typeof proxy === "string" ) {
743 fn = thisObject[ proxy ];
746 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
752 if ( !proxy && fn ) {
754 return fn.apply( thisObject || this, arguments );
758 // Set the guid of unique handler to the same of original handler, so it can be removed
760 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
763 // So proxy can be declared as an argument
767 // Mutifunctional method to get and set values to a collection
768 // The value/s can be optionally by executed if its a function
769 access: function( elems, key, value, exec, fn, pass ) {
770 var length = elems.length;
772 // Setting many attributes
773 if ( typeof key === "object" ) {
774 for ( var k in key ) {
775 jQuery.access( elems, k, key[k], exec, fn, value );
780 // Setting one attribute
781 if ( value !== undefined ) {
782 // Optionally, function values get executed if exec is true
783 exec = !pass && exec && jQuery.isFunction(value);
785 for ( var i = 0; i < length; i++ ) {
786 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
792 // Getting an attribute
793 return length ? fn( elems[0], key ) : undefined;
797 return (new Date()).getTime();
800 // Create a simple deferred (one callbacks list)
801 _Deferred: function() {
802 var // callbacks list
804 // stored [ context , args ]
806 // to avoid firing when already doing so
808 // flag to know if the deferred has been cancelled
810 // the deferred itself
813 // done( f1, f2, ...)
816 var args = arguments,
826 for ( i = 0, length = args.length; i < length; i++ ) {
828 type = jQuery.type( elem );
829 if ( type === "array" ) {
830 deferred.done.apply( deferred, elem );
831 } else if ( type === "function" ) {
832 callbacks.push( elem );
836 deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
842 // resolve with given context and args
843 resolveWith: function( context, args ) {
844 if ( !cancelled && !fired && !firing ) {
847 while( callbacks[ 0 ] ) {
848 callbacks.shift().apply( context, args );
852 fired = [ context, args ];
859 // resolve with this as context and given arguments
860 resolve: function() {
861 deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this, arguments );
865 // Has this deferred been resolved?
866 isResolved: function() {
867 return !!( firing || fired );
881 // Full fledged deferred (two callbacks list)
882 Deferred: function( func ) {
883 var deferred = jQuery._Deferred(),
884 failDeferred = jQuery._Deferred(),
886 // Add errorDeferred methods, then and promise
887 jQuery.extend( deferred, {
888 then: function( doneCallbacks, failCallbacks ) {
889 deferred.done( doneCallbacks ).fail( failCallbacks );
892 fail: failDeferred.done,
893 rejectWith: failDeferred.resolveWith,
894 reject: failDeferred.resolve,
895 isRejected: failDeferred.isResolved,
896 // Get a promise for this deferred
897 // If obj is provided, the promise aspect is added to the object
898 promise: function( obj , i /* internal */ ) {
905 i = promiseMethods.length;
907 obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
912 // Make sure only one callback list will be used
913 deferred.then( failDeferred.cancel, deferred.cancel );
915 delete deferred.cancel;
916 // Call given func if any
918 func.call( deferred, deferred );
924 when: function( object ) {
925 var args = arguments,
926 length = args.length,
927 deferred = length <= 1 && object && jQuery.isFunction( object.promise ) ?
930 promise = deferred.promise(),
934 resolveArray = new Array( length );
935 jQuery.each( args, function( index, element, args ) {
936 jQuery.when( element ).then( function( value ) {
938 resolveArray[ index ] = args.length > 1 ? slice.call( args, 0 ) : value;
940 deferred.resolveWith( promise, resolveArray );
942 }, deferred.reject );
944 } else if ( deferred !== object ) {
945 deferred.resolve( object );
950 // Use of jQuery.browser is frowned upon.
951 // More details: http://docs.jquery.com/Utilities/jQuery.browser
952 uaMatch: function( ua ) {
953 ua = ua.toLowerCase();
955 var match = rwebkit.exec( ua ) ||
958 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
961 return { browser: match[1] || "", version: match[2] || "0" };
964 subclass: function(){
965 function jQuerySubclass( selector, context ) {
966 return new jQuerySubclass.fn.init( selector, context );
968 jQuerySubclass.superclass = this;
969 jQuerySubclass.fn = jQuerySubclass.prototype = this();
970 jQuerySubclass.fn.constructor = jQuerySubclass;
971 jQuerySubclass.subclass = this.subclass;
972 jQuerySubclass.fn.init = function init( selector, context ) {
973 if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){
974 context = jQuerySubclass(context);
976 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
978 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
979 var rootjQuerySubclass = jQuerySubclass(document);
980 return jQuerySubclass;
986 // Create readyList deferred
987 readyList = jQuery._Deferred();
989 // Populate the class2type map
990 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
991 class2type[ "[object " + name + "]" ] = name.toLowerCase();
994 browserMatch = jQuery.uaMatch( userAgent );
995 if ( browserMatch.browser ) {
996 jQuery.browser[ browserMatch.browser ] = true;
997 jQuery.browser.version = browserMatch.version;
1000 // Deprecated, use jQuery.browser.webkit instead
1001 if ( jQuery.browser.webkit ) {
1002 jQuery.browser.safari = true;
1006 jQuery.inArray = function( elem, array ) {
1007 return indexOf.call( array, elem );
1011 // IE doesn't match non-breaking spaces with \s
1012 if ( rnotwhite.test( "\xA0" ) ) {
1013 trimLeft = /^[\s\xA0]+/;
1014 trimRight = /[\s\xA0]+$/;
1017 // All jQuery objects should point back to these
1018 rootjQuery = jQuery(document);
1020 // Cleanup functions for the document ready method
1021 if ( document.addEventListener ) {
1022 DOMContentLoaded = function() {
1023 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1027 } else if ( document.attachEvent ) {
1028 DOMContentLoaded = function() {
1029 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1030 if ( document.readyState === "complete" ) {
1031 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1037 // The DOM ready check for Internet Explorer
1038 function doScrollCheck() {
1039 if ( jQuery.isReady ) {
1044 // If IE is used, use the trick by Diego Perini
1045 // http://javascript.nwbox.com/IEContentLoaded/
1046 document.documentElement.doScroll("left");
1048 setTimeout( doScrollCheck, 1 );
1052 // and execute any waiting functions
1056 // Expose jQuery to the global object
1057 return (window.jQuery = window.$ = jQuery);