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 );
251 // Attach the listeners
254 // Change ready & apply
255 return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
261 this.slice( i, +i + 1 );
269 return this.eq( -1 );
273 return this.pushStack( slice.apply( this, arguments ),
274 "slice", slice.call(arguments).join(",") );
277 map: function( callback ) {
278 return this.pushStack( jQuery.map(this, function( elem, i ) {
279 return callback.call( elem, i, elem );
284 return this.prevObject || this.constructor(null);
287 // For internal use only.
288 // Behaves like an Array's method, not like a jQuery method.
294 // Give the init function the jQuery prototype for later instantiation
295 jQuery.fn.init.prototype = jQuery.fn;
297 jQuery.extend = jQuery.fn.extend = function() {
298 var options, name, src, copy, copyIsArray, clone,
299 target = arguments[0] || {},
301 length = arguments.length,
304 // Handle a deep copy situation
305 if ( typeof target === "boolean" ) {
307 target = arguments[1] || {};
308 // skip the boolean and the target
312 // Handle case when target is a string or something (possible in deep copy)
313 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
317 // extend jQuery itself if only one argument is passed
318 if ( length === i ) {
323 for ( ; i < length; i++ ) {
324 // Only deal with non-null/undefined values
325 if ( (options = arguments[ i ]) != null ) {
326 // Extend the base object
327 for ( name in options ) {
328 src = target[ name ];
329 copy = options[ name ];
331 // Prevent never-ending loop
332 if ( target === copy ) {
336 // Recurse if we're merging plain objects or arrays
337 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
340 clone = src && jQuery.isArray(src) ? src : [];
343 clone = src && jQuery.isPlainObject(src) ? src : {};
346 // Never move original objects, clone them
347 target[ name ] = jQuery.extend( deep, clone, copy );
349 // Don't bring in undefined values
350 } else if ( copy !== undefined ) {
351 target[ name ] = copy;
357 // Return the modified object
362 noConflict: function( deep ) {
366 window.jQuery = _jQuery;
372 // Is the DOM ready to be used? Set to true once it occurs.
375 // A counter to track how many items to wait for before
376 // the ready event fires. See #6781
379 // Handle when the DOM is ready
380 ready: function( wait ) {
381 // A third-party is pushing the ready event forwards
382 if ( wait === true ) {
386 // Make sure that the DOM is not already loaded
387 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
388 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
389 if ( !document.body ) {
390 return setTimeout( jQuery.ready, 1 );
393 // Remember that the DOM is ready
394 jQuery.isReady = true;
396 // If a normal DOM Ready event fired, decrement, and wait if need be
397 if ( wait !== true && --jQuery.readyWait > 0 ) {
401 // If there are functions bound, to execute
402 readyList.resolveWith( document , [ jQuery ] );
404 // Trigger any bound ready events
405 if ( jQuery.fn.trigger ) {
406 jQuery( document ).trigger( "ready" ).unbind( "ready" );
411 bindReady: function() {
418 // Catch cases where $(document).ready() is called after the
419 // browser event has already occurred.
420 if ( document.readyState === "complete" ) {
421 // Handle it asynchronously to allow scripts the opportunity to delay ready
422 return setTimeout( jQuery.ready, 1 );
425 // Mozilla, Opera and webkit nightlies currently support this event
426 if ( document.addEventListener ) {
427 // Use the handy event callback
428 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
430 // A fallback to window.onload, that will always work
431 window.addEventListener( "load", jQuery.ready, false );
433 // If IE event model is used
434 } else if ( document.attachEvent ) {
435 // ensure firing before onload,
436 // maybe late but safe also for iframes
437 document.attachEvent("onreadystatechange", DOMContentLoaded);
439 // A fallback to window.onload, that will always work
440 window.attachEvent( "onload", jQuery.ready );
442 // If IE and not a frame
443 // continually check to see if the document is ready
444 var toplevel = false;
447 toplevel = window.frameElement == null;
450 if ( document.documentElement.doScroll && toplevel ) {
456 // See test/unit/core.js for details concerning isFunction.
457 // Since version 1.3, DOM methods and functions like alert
458 // aren't supported. They return false on IE (#2968).
459 isFunction: function( obj ) {
460 return jQuery.type(obj) === "function";
463 isArray: Array.isArray || function( obj ) {
464 return jQuery.type(obj) === "array";
467 // A crude way of determining if an object is a window
468 isWindow: function( obj ) {
469 return obj && typeof obj === "object" && "setInterval" in obj;
472 isNaN: function( obj ) {
473 return obj == null || !rdigit.test( obj ) || isNaN( obj );
476 type: function( obj ) {
479 class2type[ toString.call(obj) ] || "object";
482 isPlainObject: function( obj ) {
483 // Must be an Object.
484 // Because of IE, we also have to check the presence of the constructor property.
485 // Make sure that DOM nodes and window objects don't pass through, as well
486 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
490 // Not own constructor property must be Object
491 if ( obj.constructor &&
492 !hasOwn.call(obj, "constructor") &&
493 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
497 // Own properties are enumerated firstly, so to speed up,
498 // if last one is own, then all properties are own.
501 for ( key in obj ) {}
503 return key === undefined || hasOwn.call( obj, key );
506 isEmptyObject: function( obj ) {
507 for ( var name in obj ) {
513 error: function( msg ) {
517 parseJSON: function( data ) {
518 if ( typeof data !== "string" || !data ) {
522 // Make sure leading/trailing whitespace is removed (IE can't handle it)
523 data = jQuery.trim( data );
525 // Make sure the incoming data is actual JSON
526 // Logic borrowed from http://json.org/json2.js
527 if ( rvalidchars.test(data.replace(rvalidescape, "@")
528 .replace(rvalidtokens, "]")
529 .replace(rvalidbraces, "")) ) {
531 // Try to use the native JSON parser first
532 return window.JSON && window.JSON.parse ?
533 window.JSON.parse( data ) :
534 (new Function("return " + data))();
537 jQuery.error( "Invalid JSON: " + data );
541 // Cross-browser xml parsing
542 // (xml & tmp used internally)
543 parseXML: function( data , xml , tmp ) {
545 if ( window.DOMParser ) { // Standard
546 tmp = new DOMParser();
547 xml = tmp.parseFromString( data , "text/xml" );
549 xml = new ActiveXObject( "Microsoft.XMLDOM" );
554 tmp = xml.documentElement;
556 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
557 jQuery.error( "Invalid XML: " + data );
565 // Evalulates a script in a global context
566 globalEval: function( data ) {
567 if ( data && rnotwhite.test(data) ) {
568 // Inspired by code by Andrea Giammarchi
569 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
570 var head = document.getElementsByTagName("head")[0] || document.documentElement,
571 script = document.createElement("script");
573 script.type = "text/javascript";
575 if ( jQuery.support.scriptEval() ) {
576 script.appendChild( document.createTextNode( data ) );
581 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
582 // This arises when a base node is used (#2709).
583 head.insertBefore( script, head.firstChild );
584 head.removeChild( script );
588 nodeName: function( elem, name ) {
589 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
592 // args is for internal usage only
593 each: function( object, callback, args ) {
595 length = object.length,
596 isObj = length === undefined || jQuery.isFunction(object);
600 for ( name in object ) {
601 if ( callback.apply( object[ name ], args ) === false ) {
606 for ( ; i < length; ) {
607 if ( callback.apply( object[ i++ ], args ) === false ) {
613 // A special, fast, case for the most common use of each
616 for ( name in object ) {
617 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
622 for ( var value = object[0];
623 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
630 // Use native String.trim function wherever possible
633 return text == null ?
638 // Otherwise use our own trimming functionality
640 return text == null ?
642 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
645 // results is for internal usage only
646 makeArray: function( array, results ) {
647 var ret = results || [];
649 if ( array != null ) {
650 // The window, strings (and functions) also have 'length'
651 // The extra typeof function check is to prevent crashes
652 // in Safari 2 (See: #3039)
653 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
654 var type = jQuery.type(array);
656 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
657 push.call( ret, array );
659 jQuery.merge( ret, array );
666 inArray: function( elem, array ) {
667 if ( array.indexOf ) {
668 return array.indexOf( elem );
671 for ( var i = 0, length = array.length; i < length; i++ ) {
672 if ( array[ i ] === elem ) {
680 merge: function( first, second ) {
681 var i = first.length,
684 if ( typeof second.length === "number" ) {
685 for ( var l = second.length; j < l; j++ ) {
686 first[ i++ ] = second[ j ];
690 while ( second[j] !== undefined ) {
691 first[ i++ ] = second[ j++ ];
700 grep: function( elems, callback, inv ) {
701 var ret = [], retVal;
704 // Go through the array, only saving the items
705 // that pass the validator function
706 for ( var i = 0, length = elems.length; i < length; i++ ) {
707 retVal = !!callback( elems[ i ], i );
708 if ( inv !== retVal ) {
709 ret.push( elems[ i ] );
716 // arg is for internal usage only
717 map: function( elems, callback, arg ) {
720 // Go through the array, translating each of the items to their
721 // new value (or values).
722 for ( var i = 0, length = elems.length; i < length; i++ ) {
723 value = callback( elems[ i ], i, arg );
725 if ( value != null ) {
726 ret[ ret.length ] = value;
730 // Flatten any nested arrays
731 return ret.concat.apply( [], ret );
734 // A global GUID counter for objects
737 proxy: function( fn, proxy, thisObject ) {
738 if ( arguments.length === 2 ) {
739 if ( typeof proxy === "string" ) {
741 fn = thisObject[ proxy ];
744 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
750 if ( !proxy && fn ) {
752 return fn.apply( thisObject || this, arguments );
756 // Set the guid of unique handler to the same of original handler, so it can be removed
758 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
761 // So proxy can be declared as an argument
765 // Mutifunctional method to get and set values to a collection
766 // The value/s can be optionally by executed if its a function
767 access: function( elems, key, value, exec, fn, pass ) {
768 var length = elems.length;
770 // Setting many attributes
771 if ( typeof key === "object" ) {
772 for ( var k in key ) {
773 jQuery.access( elems, k, key[k], exec, fn, value );
778 // Setting one attribute
779 if ( value !== undefined ) {
780 // Optionally, function values get executed if exec is true
781 exec = !pass && exec && jQuery.isFunction(value);
783 for ( var i = 0; i < length; i++ ) {
784 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
790 // Getting an attribute
791 return length ? fn( elems[0], key ) : undefined;
795 return (new Date()).getTime();
798 // Create a simple deferred (one callbacks list)
799 _Deferred: function() {
801 var // callbacks list
803 // stored [ context , args ]
805 // to avoid firing when already doing so
807 // flag to know if the deferred has been cancelled
809 // the deferred itself
812 // done( f1, f2, ...)
817 var args = arguments,
829 for ( i = 0, length = args.length ; i < length ; i++ ) {
831 type = jQuery.type( elem );
832 if ( type === "array" ) {
833 deferred.done.apply( deferred , elem );
834 } else if ( type === "function" ) {
835 callbacks.push( elem );
840 deferred.resolveWith( _fired[ 0 ] , _fired[ 1 ] );
847 // resolve with given context and args
848 resolveWith: function( context , args ) {
849 if ( ! cancelled && ! fired && ! firing ) {
854 while( callbacks[ 0 ] ) {
855 callbacks.shift().apply( context , args );
859 fired = [ context , args ];
866 // resolve with this as context and given arguments
867 resolve: function() {
868 deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments );
872 // Has this deferred been resolved?
873 isResolved: function() {
874 return !!( firing || fired );
888 // Full fledged deferred (two callbacks list)
889 // Typical success/error system
890 Deferred: function( func ) {
892 var deferred = jQuery._Deferred(),
893 failDeferred = jQuery._Deferred(),
896 // Add errorDeferred methods, then and promise
897 jQuery.extend( deferred , {
899 then: function( doneCallbacks , failCallbacks ) {
900 deferred.done( doneCallbacks ).fail( failCallbacks );
903 fail: failDeferred.done,
904 rejectWith: failDeferred.resolveWith,
905 reject: failDeferred.resolve,
906 isRejected: failDeferred.isResolved,
907 // Get a promise for this deferred
908 // If obj is provided, the promise aspect is added to the object
909 // (i is used internally)
910 promise: function( obj , i ) {
917 i = promiseMethods.length;
919 obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
926 // Make sure only one callback list will be used
927 deferred.then( failDeferred.cancel , deferred.cancel );
930 delete deferred.cancel;
932 // Call given func if any
934 func.call( deferred , deferred );
941 when: function( object ) {
942 var args = arguments,
943 length = args.length,
944 deferred = length <= 1 && object && jQuery.isFunction( object.promise ) ?
947 promise = deferred.promise(),
951 resolveArray = new Array( length );
952 jQuery.each( args, function( index, element, args ) {
953 jQuery.when( element ).done( function( value ) {
955 resolveArray[ index ] = args.length > 1 ? slice.call( args , 0 ) : value;
957 deferred.resolveWith( promise, resolveArray );
959 }).fail( function() {
960 deferred.rejectWith( promise, arguments );
962 return !deferred.isRejected();
964 } else if ( deferred !== object ) {
965 deferred.resolve( object );
970 // Use of jQuery.browser is frowned upon.
971 // More details: http://docs.jquery.com/Utilities/jQuery.browser
972 uaMatch: function( ua ) {
973 ua = ua.toLowerCase();
975 var match = rwebkit.exec( ua ) ||
978 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
981 return { browser: match[1] || "", version: match[2] || "0" };
984 subclass: function(){
985 function jQuerySubclass( selector, context ) {
986 return new jQuerySubclass.fn.init( selector, context );
988 jQuerySubclass.superclass = this;
989 jQuerySubclass.fn = jQuerySubclass.prototype = this();
990 jQuerySubclass.fn.constructor = jQuerySubclass;
991 jQuerySubclass.subclass = this.subclass;
992 jQuerySubclass.fn.init = function init( selector, context ) {
993 if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){
994 context = jQuerySubclass(context);
996 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
998 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
999 var rootjQuerySubclass = jQuerySubclass(document);
1000 return jQuerySubclass;
1006 // Create readyList deferred
1007 readyList = jQuery._Deferred();
1009 // Populate the class2type map
1010 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
1011 class2type[ "[object " + name + "]" ] = name.toLowerCase();
1014 browserMatch = jQuery.uaMatch( userAgent );
1015 if ( browserMatch.browser ) {
1016 jQuery.browser[ browserMatch.browser ] = true;
1017 jQuery.browser.version = browserMatch.version;
1020 // Deprecated, use jQuery.browser.webkit instead
1021 if ( jQuery.browser.webkit ) {
1022 jQuery.browser.safari = true;
1026 jQuery.inArray = function( elem, array ) {
1027 return indexOf.call( array, elem );
1031 // IE doesn't match non-breaking spaces with \s
1032 if ( rnotwhite.test( "\xA0" ) ) {
1033 trimLeft = /^[\s\xA0]+/;
1034 trimRight = /[\s\xA0]+$/;
1037 // All jQuery objects should point back to these
1038 rootjQuery = jQuery(document);
1040 // Cleanup functions for the document ready method
1041 if ( document.addEventListener ) {
1042 DOMContentLoaded = function() {
1043 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1047 } else if ( document.attachEvent ) {
1048 DOMContentLoaded = function() {
1049 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1050 if ( document.readyState === "complete" ) {
1051 document.detachEvent( "onreadystatechange", DOMContentLoaded );
1057 // The DOM ready check for Internet Explorer
1058 function doScrollCheck() {
1059 if ( jQuery.isReady ) {
1064 // If IE is used, use the trick by Diego Perini
1065 // http://javascript.nwbox.com/IEContentLoaded/
1066 document.documentElement.doScroll("left");
1068 setTimeout( doScrollCheck, 1 );
1072 // and execute any waiting functions
1076 // Expose jQuery to the global object
1077 return (window.jQuery = window.$ = jQuery);