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
32 // Match a standalone tag
33 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
36 rvalidchars = /^[\],:{}\s]*$/,
37 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
38 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
39 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
42 rwebkit = /(webkit)[ \/]([\w.]+)/,
43 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
44 rmsie = /(msie) ([\w.]+)/,
45 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
47 // Keep a UserAgent string for use with jQuery.browser
48 userAgent = navigator.userAgent,
50 // For matching the engine and version of the browser
53 // The deferred used on DOM ready
56 // The ready event handler
59 // Save a reference to some core methods
60 toString = Object.prototype.toString,
61 hasOwn = Object.prototype.hasOwnProperty,
62 push = Array.prototype.push,
63 slice = Array.prototype.slice,
64 trim = String.prototype.trim,
65 indexOf = Array.prototype.indexOf,
67 // [[Class]] -> type pairs
70 jQuery.fn = jQuery.prototype = {
72 init: function( selector, context, rootjQuery ) {
73 var match, elem, ret, doc;
75 // Handle $(""), $(null), or $(undefined)
80 // Handle $(DOMElement)
81 if ( selector.nodeType ) {
82 this.context = this[0] = selector;
87 // The body element only exists once, optimize finding it
88 if ( selector === "body" && !context && document.body ) {
89 this.context = document;
90 this[0] = document.body;
91 this.selector = "body";
96 // Handle HTML strings
97 if ( typeof selector === "string" ) {
98 // Are we dealing with HTML string or an ID?
99 if ( selector.length > 1024 ) {
100 // Assume very large strings are HTML and skip the regex check
101 match = [ null, selector, null ];
103 match = quickExpr.exec( selector );
106 // Verify a match, and that no context was specified for #id
107 if ( match && (match[1] || !context) ) {
109 // HANDLE: $(html) -> $(array)
111 context = context instanceof jQuery ? context[0] : context;
112 doc = (context ? context.ownerDocument || context : document);
114 // If a single string is passed in and it's a single tag
115 // just do a createElement and skip the rest
116 ret = rsingleTag.exec( selector );
119 if ( jQuery.isPlainObject( context ) ) {
120 selector = [ document.createElement( ret[1] ) ];
121 jQuery.fn.attr.call( selector, context, true );
124 selector = [ doc.createElement( ret[1] ) ];
128 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
129 selector = (ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment).childNodes;
132 return jQuery.merge( this, selector );
136 elem = document.getElementById( match[2] );
138 // Check parentNode to catch when Blackberry 4.6 returns
139 // nodes that are no longer in the document #6963
140 if ( elem && elem.parentNode ) {
141 // Handle the case where IE and Opera return items
142 // by name instead of ID
143 if ( elem.id !== match[2] ) {
144 return rootjQuery.find( selector );
147 // Otherwise, we inject the element directly into the jQuery object
152 this.context = document;
153 this.selector = selector;
157 // HANDLE: $(expr, $(...))
158 } else if ( !context || context.jquery ) {
159 return (context || rootjQuery).find( selector );
161 // HANDLE: $(expr, context)
162 // (which is just equivalent to: $(context).find(expr)
164 return this.constructor( context ).find( selector );
167 // HANDLE: $(function)
168 // Shortcut for document ready
169 } else if ( jQuery.isFunction( selector ) ) {
170 return rootjQuery.ready( selector );
173 if (selector.selector !== undefined) {
174 this.selector = selector.selector;
175 this.context = selector.context;
178 return jQuery.makeArray( selector, this );
181 // Start with an empty selector
184 // The current version of jQuery being used
187 // The default length of a jQuery object is 0
190 // The number of elements contained in the matched element set
195 toArray: function() {
196 return slice.call( this, 0 );
199 // Get the Nth element in the matched element set OR
200 // Get the whole matched element set as a clean array
201 get: function( num ) {
204 // Return a 'clean' array
207 // Return just the object
208 ( num < 0 ? this[ this.length + num ] : this[ num ] );
211 // Take an array of elements and push it onto the stack
212 // (returning the new matched element set)
213 pushStack: function( elems, name, selector ) {
214 // Build a new jQuery matched element set
215 var ret = this.constructor();
217 if ( jQuery.isArray( elems ) ) {
218 push.apply( ret, elems );
221 jQuery.merge( ret, elems );
224 // Add the old object onto the stack (as a reference)
225 ret.prevObject = this;
227 ret.context = this.context;
229 if ( name === "find" ) {
230 ret.selector = this.selector + (this.selector ? " " : "") + selector;
232 ret.selector = this.selector + "." + name + "(" + selector + ")";
235 // Return the newly-formed element set
239 // Execute a callback for every element in the matched set.
240 // (You can seed the arguments with an array of args, but this is
241 // only used internally.)
242 each: function( callback, args ) {
243 return jQuery.each( this, callback, args );
246 ready: function( fn ) {
247 // Attach the listeners
251 readyList.done( fn );
259 this.slice( i, +i + 1 );
267 return this.eq( -1 );
271 return this.pushStack( slice.apply( this, arguments ),
272 "slice", slice.call(arguments).join(",") );
275 map: function( callback ) {
276 return this.pushStack( jQuery.map(this, function( elem, i ) {
277 return callback.call( elem, i, elem );
282 return this.prevObject || this.constructor(null);
285 // For internal use only.
286 // Behaves like an Array's method, not like a jQuery method.
292 // Give the init function the jQuery prototype for later instantiation
293 jQuery.fn.init.prototype = jQuery.fn;
295 jQuery.extend = jQuery.fn.extend = function() {
296 var options, name, src, copy, copyIsArray, clone,
297 target = arguments[0] || {},
299 length = arguments.length,
302 // Handle a deep copy situation
303 if ( typeof target === "boolean" ) {
305 target = arguments[1] || {};
306 // skip the boolean and the target
310 // Handle case when target is a string or something (possible in deep copy)
311 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
315 // extend jQuery itself if only one argument is passed
316 if ( length === i ) {
321 for ( ; i < length; i++ ) {
322 // Only deal with non-null/undefined values
323 if ( (options = arguments[ i ]) != null ) {
324 // Extend the base object
325 for ( name in options ) {
326 src = target[ name ];
327 copy = options[ name ];
329 // Prevent never-ending loop
330 if ( target === copy ) {
334 // Recurse if we're merging plain objects or arrays
335 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
338 clone = src && jQuery.isArray(src) ? src : [];
341 clone = src && jQuery.isPlainObject(src) ? src : {};
344 // Never move original objects, clone them
345 target[ name ] = jQuery.extend( deep, clone, copy );
347 // Don't bring in undefined values
348 } else if ( copy !== undefined ) {
349 target[ name ] = copy;
355 // Return the modified object
360 noConflict: function( deep ) {
364 window.jQuery = _jQuery;
370 // Is the DOM ready to be used? Set to true once it occurs.
373 // A counter to track how many items to wait for before
374 // the ready event fires. See #6781
377 // Hold (or release) the ready event
378 holdReady: function( hold ) {
382 jQuery.ready( true );
386 // Handle when the DOM is ready
387 ready: function( wait ) {
388 // Either a released hold or an DOMready/load event and not yet ready
389 if ( (wait === true && !--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() {
418 readyList = jQuery._Deferred();
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 // Attempt to parse using the native JSON parser first
528 if ( window.JSON && window.JSON.parse ) {
529 return window.JSON.parse( data );
532 // Make sure the incoming data is actual JSON
533 // Logic borrowed from http://json.org/json2.js
534 if ( rvalidchars.test( data.replace( rvalidescape, "@" )
535 .replace( rvalidtokens, "]" )
536 .replace( rvalidbraces, "")) ) {
538 return (new Function( "return " + data ))();
541 jQuery.error( "Invalid JSON: " + data );
544 // Cross-browser xml parsing
545 // (xml & tmp used internally)
546 parseXML: function( data , xml , tmp ) {
548 if ( window.DOMParser ) { // Standard
549 tmp = new DOMParser();
550 xml = tmp.parseFromString( data , "text/xml" );
552 xml = new ActiveXObject( "Microsoft.XMLDOM" );
557 tmp = xml.documentElement;
559 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
560 jQuery.error( "Invalid XML: " + data );
568 // Evalulates a script in a global context
569 globalEval: function( data ) {
570 if ( data && rnotwhite.test(data) ) {
571 // Inspired by code by Andrea Giammarchi
572 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
573 var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
574 script = document.createElement( "script" );
576 if ( jQuery.support.scriptEval() ) {
577 script.appendChild( document.createTextNode( data ) );
582 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
583 // This arises when a base node is used (#2709).
584 head.insertBefore( script, head.firstChild );
585 head.removeChild( script );
589 nodeName: function( elem, name ) {
590 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
593 // args is for internal usage only
594 each: function( object, callback, args ) {
596 length = object.length,
597 isObj = length === undefined || jQuery.isFunction(object);
601 for ( name in object ) {
602 if ( callback.apply( object[ name ], args ) === false ) {
607 for ( ; i < length; ) {
608 if ( callback.apply( object[ i++ ], args ) === false ) {
614 // A special, fast, case for the most common use of each
617 for ( name in object ) {
618 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
623 for ( var value = object[0];
624 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
631 // Use native String.trim function wherever possible
634 return text == null ?
639 // Otherwise use our own trimming functionality
641 return text == null ?
643 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
646 // results is for internal usage only
647 makeArray: function( array, results ) {
648 var ret = results || [];
650 if ( array != null ) {
651 // The window, strings (and functions) also have 'length'
652 // The extra typeof function check is to prevent crashes
653 // in Safari 2 (See: #3039)
654 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
655 var type = jQuery.type(array);
657 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
658 push.call( ret, array );
660 jQuery.merge( ret, array );
667 inArray: function( elem, array ) {
668 if ( array.indexOf ) {
669 return array.indexOf( elem );
672 for ( var i = 0, length = array.length; i < length; i++ ) {
673 if ( array[ i ] === elem ) {
681 merge: function( first, second ) {
682 var i = first.length,
685 if ( typeof second.length === "number" ) {
686 for ( var l = second.length; j < l; j++ ) {
687 first[ i++ ] = second[ j ];
691 while ( second[j] !== undefined ) {
692 first[ i++ ] = second[ j++ ];
701 grep: function( elems, callback, inv ) {
702 var ret = [], retVal;
705 // Go through the array, only saving the items
706 // that pass the validator function
707 for ( var i = 0, length = elems.length; i < length; i++ ) {
708 retVal = !!callback( elems[ i ], i );
709 if ( inv !== retVal ) {
710 ret.push( elems[ i ] );
717 // arg is for internal usage only
718 map: function( elems, callback, arg ) {
721 // Go through the array, translating each of the items to their
722 // new value (or values).
723 for ( var i = 0, length = elems.length; i < length; i++ ) {
724 value = callback( elems[ i ], i, arg );
726 if ( value != null ) {
727 ret[ ret.length ] = value;
731 // Flatten any nested arrays
732 return ret.concat.apply( [], ret );
735 // A global GUID counter for objects
738 proxy: function( fn, proxy, thisObject ) {
739 if ( arguments.length === 2 ) {
740 if ( typeof proxy === "string" ) {
742 fn = thisObject[ proxy ];
745 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
751 if ( !proxy && fn ) {
753 return fn.apply( thisObject || this, arguments );
757 // Set the guid of unique handler to the same of original handler, so it can be removed
759 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
762 // So proxy can be declared as an argument
766 // Mutifunctional method to get and set values to a collection
767 // The value/s can be optionally by executed if its a function
768 access: function( elems, key, value, exec, fn, pass ) {
769 var length = elems.length;
771 // Setting many attributes
772 if ( typeof key === "object" ) {
773 for ( var k in key ) {
774 jQuery.access( elems, k, key[k], exec, fn, value );
779 // Setting one attribute
780 if ( value !== undefined ) {
781 // Optionally, function values get executed if exec is true
782 exec = !pass && exec && jQuery.isFunction(value);
784 for ( var i = 0; i < length; i++ ) {
785 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
791 // Getting an attribute
792 return length ? fn( elems[0], key ) : undefined;
796 return (new Date()).getTime();
799 // Use of jQuery.browser is frowned upon.
800 // More details: http://docs.jquery.com/Utilities/jQuery.browser
801 uaMatch: function( ua ) {
802 ua = ua.toLowerCase();
804 var match = rwebkit.exec( ua ) ||
807 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
810 return { browser: match[1] || "", version: match[2] || "0" };
814 function jQuerySubclass( selector, context ) {
815 return new jQuerySubclass.fn.init( selector, context );
817 jQuery.extend( true, jQuerySubclass, this );
818 jQuerySubclass.superclass = this;
819 jQuerySubclass.fn = jQuerySubclass.prototype = this();
820 jQuerySubclass.fn.constructor = jQuerySubclass;
821 jQuerySubclass.subclass = this.subclass;
822 jQuerySubclass.fn.init = function init( selector, context ) {
823 if ( context && context instanceof jQuery && !(context instanceof jQuerySubclass) ) {
824 context = jQuerySubclass(context);
827 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
829 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
830 var rootjQuerySubclass = jQuerySubclass(document);
831 return jQuerySubclass;
837 // Populate the class2type map
838 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
839 class2type[ "[object " + name + "]" ] = name.toLowerCase();
842 browserMatch = jQuery.uaMatch( userAgent );
843 if ( browserMatch.browser ) {
844 jQuery.browser[ browserMatch.browser ] = true;
845 jQuery.browser.version = browserMatch.version;
848 // Deprecated, use jQuery.browser.webkit instead
849 if ( jQuery.browser.webkit ) {
850 jQuery.browser.safari = true;
854 jQuery.inArray = function( elem, array ) {
855 return indexOf.call( array, elem );
859 // IE doesn't match non-breaking spaces with \s
860 if ( rnotwhite.test( "\xA0" ) ) {
861 trimLeft = /^[\s\xA0]+/;
862 trimRight = /[\s\xA0]+$/;
865 // All jQuery objects should point back to these
866 rootjQuery = jQuery(document);
868 // Cleanup functions for the document ready method
869 if ( document.addEventListener ) {
870 DOMContentLoaded = function() {
871 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
875 } else if ( document.attachEvent ) {
876 DOMContentLoaded = function() {
877 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
878 if ( document.readyState === "complete" ) {
879 document.detachEvent( "onreadystatechange", DOMContentLoaded );
885 // The DOM ready check for Internet Explorer
886 function doScrollCheck() {
887 if ( jQuery.isReady ) {
892 // If IE is used, use the trick by Diego Perini
893 // http://javascript.nwbox.com/IEContentLoaded/
894 document.documentElement.doScroll("left");
896 setTimeout( doScrollCheck, 1 );
900 // and execute any waiting functions
904 // Expose jQuery to the global object