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 match = quickExpr.exec( selector );
101 // Verify a match, and that no context was specified for #id
102 if ( match && (match[1] || !context) ) {
104 // HANDLE: $(html) -> $(array)
106 context = context instanceof jQuery ? context[0] : context;
107 doc = (context ? context.ownerDocument || context : document);
109 // If a single string is passed in and it's a single tag
110 // just do a createElement and skip the rest
111 ret = rsingleTag.exec( selector );
114 if ( jQuery.isPlainObject( context ) ) {
115 selector = [ document.createElement( ret[1] ) ];
116 jQuery.fn.attr.call( selector, context, true );
119 selector = [ doc.createElement( ret[1] ) ];
123 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
124 selector = (ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment).childNodes;
127 return jQuery.merge( this, selector );
131 elem = document.getElementById( match[2] );
133 // Check parentNode to catch when Blackberry 4.6 returns
134 // nodes that are no longer in the document #6963
135 if ( elem && elem.parentNode ) {
136 // Handle the case where IE and Opera return items
137 // by name instead of ID
138 if ( elem.id !== match[2] ) {
139 return rootjQuery.find( selector );
142 // Otherwise, we inject the element directly into the jQuery object
147 this.context = document;
148 this.selector = selector;
152 // HANDLE: $(expr, $(...))
153 } else if ( !context || context.jquery ) {
154 return (context || rootjQuery).find( selector );
156 // HANDLE: $(expr, context)
157 // (which is just equivalent to: $(context).find(expr)
159 return this.constructor( context ).find( selector );
162 // HANDLE: $(function)
163 // Shortcut for document ready
164 } else if ( jQuery.isFunction( selector ) ) {
165 return rootjQuery.ready( selector );
168 if (selector.selector !== undefined) {
169 this.selector = selector.selector;
170 this.context = selector.context;
173 return jQuery.makeArray( selector, this );
176 // Start with an empty selector
179 // The current version of jQuery being used
182 // The default length of a jQuery object is 0
185 // The number of elements contained in the matched element set
190 toArray: function() {
191 return slice.call( this, 0 );
194 // Get the Nth element in the matched element set OR
195 // Get the whole matched element set as a clean array
196 get: function( num ) {
199 // Return a 'clean' array
202 // Return just the object
203 ( num < 0 ? this[ this.length + num ] : this[ num ] );
206 // Take an array of elements and push it onto the stack
207 // (returning the new matched element set)
208 pushStack: function( elems, name, selector ) {
209 // Build a new jQuery matched element set
210 var ret = this.constructor();
212 if ( jQuery.isArray( elems ) ) {
213 push.apply( ret, elems );
216 jQuery.merge( ret, elems );
219 // Add the old object onto the stack (as a reference)
220 ret.prevObject = this;
222 ret.context = this.context;
224 if ( name === "find" ) {
225 ret.selector = this.selector + (this.selector ? " " : "") + selector;
227 ret.selector = this.selector + "." + name + "(" + selector + ")";
230 // Return the newly-formed element set
234 // Execute a callback for every element in the matched set.
235 // (You can seed the arguments with an array of args, but this is
236 // only used internally.)
237 each: function( callback, args ) {
238 return jQuery.each( this, callback, args );
241 ready: function( fn ) {
242 // Attach the listeners
246 readyList.done( fn );
254 this.slice( i, +i + 1 );
262 return this.eq( -1 );
266 return this.pushStack( slice.apply( this, arguments ),
267 "slice", slice.call(arguments).join(",") );
270 map: function( callback ) {
271 return this.pushStack( jQuery.map(this, function( elem, i ) {
272 return callback.call( elem, i, elem );
277 return this.prevObject || this.constructor(null);
280 // For internal use only.
281 // Behaves like an Array's method, not like a jQuery method.
287 // Give the init function the jQuery prototype for later instantiation
288 jQuery.fn.init.prototype = jQuery.fn;
290 jQuery.extend = jQuery.fn.extend = function() {
291 var options, name, src, copy, copyIsArray, clone,
292 target = arguments[0] || {},
294 length = arguments.length,
297 // Handle a deep copy situation
298 if ( typeof target === "boolean" ) {
300 target = arguments[1] || {};
301 // skip the boolean and the target
305 // Handle case when target is a string or something (possible in deep copy)
306 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
310 // extend jQuery itself if only one argument is passed
311 if ( length === i ) {
316 for ( ; i < length; i++ ) {
317 // Only deal with non-null/undefined values
318 if ( (options = arguments[ i ]) != null ) {
319 // Extend the base object
320 for ( name in options ) {
321 src = target[ name ];
322 copy = options[ name ];
324 // Prevent never-ending loop
325 if ( target === copy ) {
329 // Recurse if we're merging plain objects or arrays
330 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
333 clone = src && jQuery.isArray(src) ? src : [];
336 clone = src && jQuery.isPlainObject(src) ? src : {};
339 // Never move original objects, clone them
340 target[ name ] = jQuery.extend( deep, clone, copy );
342 // Don't bring in undefined values
343 } else if ( copy !== undefined ) {
344 target[ name ] = copy;
350 // Return the modified object
355 noConflict: function( deep ) {
359 window.jQuery = _jQuery;
365 // Is the DOM ready to be used? Set to true once it occurs.
368 // A counter to track how many items to wait for before
369 // the ready event fires. See #6781
372 // Handle when the DOM is ready
373 ready: function( wait ) {
374 // A third-party is pushing the ready event forwards
375 if ( wait === true ) {
379 // Make sure that the DOM is not already loaded
380 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
381 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
382 if ( !document.body ) {
383 return setTimeout( jQuery.ready, 1 );
386 // Remember that the DOM is ready
387 jQuery.isReady = true;
389 // If a normal DOM Ready event fired, decrement, and wait if need be
390 if ( wait !== true && --jQuery.readyWait > 0 ) {
394 // If there are functions bound, to execute
395 readyList.resolveWith( document, [ jQuery ] );
397 // Trigger any bound ready events
398 if ( jQuery.fn.trigger ) {
399 jQuery( document ).trigger( "ready" ).unbind( "ready" );
404 bindReady: function() {
409 readyList = jQuery._Deferred();
411 // Catch cases where $(document).ready() is called after the
412 // browser event has already occurred.
413 if ( document.readyState === "complete" ) {
414 // Handle it asynchronously to allow scripts the opportunity to delay ready
415 return setTimeout( jQuery.ready, 1 );
418 // Mozilla, Opera and webkit nightlies currently support this event
419 if ( document.addEventListener ) {
420 // Use the handy event callback
421 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
423 // A fallback to window.onload, that will always work
424 window.addEventListener( "load", jQuery.ready, false );
426 // If IE event model is used
427 } else if ( document.attachEvent ) {
428 // ensure firing before onload,
429 // maybe late but safe also for iframes
430 document.attachEvent("onreadystatechange", DOMContentLoaded);
432 // A fallback to window.onload, that will always work
433 window.attachEvent( "onload", jQuery.ready );
435 // If IE and not a frame
436 // continually check to see if the document is ready
437 var toplevel = false;
440 toplevel = window.frameElement == null;
443 if ( document.documentElement.doScroll && toplevel ) {
449 // See test/unit/core.js for details concerning isFunction.
450 // Since version 1.3, DOM methods and functions like alert
451 // aren't supported. They return false on IE (#2968).
452 isFunction: function( obj ) {
453 return jQuery.type(obj) === "function";
456 isArray: Array.isArray || function( obj ) {
457 return jQuery.type(obj) === "array";
460 // A crude way of determining if an object is a window
461 isWindow: function( obj ) {
462 return obj && typeof obj === "object" && "setInterval" in obj;
465 isNaN: function( obj ) {
466 return obj == null || !rdigit.test( obj ) || isNaN( obj );
469 type: function( obj ) {
472 class2type[ toString.call(obj) ] || "object";
475 isPlainObject: function( obj ) {
476 // Must be an Object.
477 // Because of IE, we also have to check the presence of the constructor property.
478 // Make sure that DOM nodes and window objects don't pass through, as well
479 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
483 // Not own constructor property must be Object
484 if ( obj.constructor &&
485 !hasOwn.call(obj, "constructor") &&
486 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
490 // Own properties are enumerated firstly, so to speed up,
491 // if last one is own, then all properties are own.
494 for ( key in obj ) {}
496 return key === undefined || hasOwn.call( obj, key );
499 isEmptyObject: function( obj ) {
500 for ( var name in obj ) {
506 error: function( msg ) {
510 parseJSON: function( data ) {
511 if ( typeof data !== "string" || !data ) {
515 // Make sure leading/trailing whitespace is removed (IE can't handle it)
516 data = jQuery.trim( data );
518 // Make sure the incoming data is actual JSON
519 // Logic borrowed from http://json.org/json2.js
520 if ( rvalidchars.test(data.replace(rvalidescape, "@")
521 .replace(rvalidtokens, "]")
522 .replace(rvalidbraces, "")) ) {
524 // Try to use the native JSON parser first
525 return window.JSON && window.JSON.parse ?
526 window.JSON.parse( data ) :
527 (new Function("return " + data))();
530 jQuery.error( "Invalid JSON: " + data );
534 // Cross-browser xml parsing
535 // (xml & tmp used internally)
536 parseXML: function( data , xml , tmp ) {
538 if ( window.DOMParser ) { // Standard
539 tmp = new DOMParser();
540 xml = tmp.parseFromString( data , "text/xml" );
542 xml = new ActiveXObject( "Microsoft.XMLDOM" );
547 tmp = xml.documentElement;
549 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
550 jQuery.error( "Invalid XML: " + data );
558 // Evalulates a script in a global context
559 globalEval: function( data ) {
560 if ( data && rnotwhite.test(data) ) {
561 // Inspired by code by Andrea Giammarchi
562 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
563 var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
564 script = document.createElement( "script" );
566 if ( jQuery.support.scriptEval() ) {
567 script.appendChild( document.createTextNode( data ) );
572 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
573 // This arises when a base node is used (#2709).
574 head.insertBefore( script, head.firstChild );
575 head.removeChild( script );
579 nodeName: function( elem, name ) {
580 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
583 // args is for internal usage only
584 each: function( object, callback, args ) {
586 length = object.length,
587 isObj = length === undefined || jQuery.isFunction(object);
591 for ( name in object ) {
592 if ( callback.apply( object[ name ], args ) === false ) {
597 for ( ; i < length; ) {
598 if ( callback.apply( object[ i++ ], args ) === false ) {
604 // A special, fast, case for the most common use of each
607 for ( name in object ) {
608 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
613 for ( var value = object[0];
614 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
621 // Use native String.trim function wherever possible
624 return text == null ?
629 // Otherwise use our own trimming functionality
631 return text == null ?
633 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
636 // results is for internal usage only
637 makeArray: function( array, results ) {
638 var ret = results || [];
640 if ( array != null ) {
641 // The window, strings (and functions) also have 'length'
642 // The extra typeof function check is to prevent crashes
643 // in Safari 2 (See: #3039)
644 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
645 var type = jQuery.type(array);
647 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
648 push.call( ret, array );
650 jQuery.merge( ret, array );
657 inArray: function( elem, array ) {
658 if ( array.indexOf ) {
659 return array.indexOf( elem );
662 for ( var i = 0, length = array.length; i < length; i++ ) {
663 if ( array[ i ] === elem ) {
671 merge: function( first, second ) {
672 var i = first.length,
675 if ( typeof second.length === "number" ) {
676 for ( var l = second.length; j < l; j++ ) {
677 first[ i++ ] = second[ j ];
681 while ( second[j] !== undefined ) {
682 first[ i++ ] = second[ j++ ];
691 grep: function( elems, callback, inv ) {
692 var ret = [], retVal;
695 // Go through the array, only saving the items
696 // that pass the validator function
697 for ( var i = 0, length = elems.length; i < length; i++ ) {
698 retVal = !!callback( elems[ i ], i );
699 if ( inv !== retVal ) {
700 ret.push( elems[ i ] );
707 // arg is for internal usage only
708 map: function( elems, callback, arg ) {
711 // Go through the array, translating each of the items to their
712 // new value (or values).
713 for ( var i = 0, length = elems.length; i < length; i++ ) {
714 value = callback( elems[ i ], i, arg );
716 if ( value != null ) {
717 ret[ ret.length ] = value;
721 // Flatten any nested arrays
722 return ret.concat.apply( [], ret );
725 // A global GUID counter for objects
728 proxy: function( fn, proxy, thisObject ) {
729 if ( arguments.length === 2 ) {
730 if ( typeof proxy === "string" ) {
732 fn = thisObject[ proxy ];
735 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
741 if ( !proxy && fn ) {
743 return fn.apply( thisObject || this, arguments );
747 // Set the guid of unique handler to the same of original handler, so it can be removed
749 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
752 // So proxy can be declared as an argument
756 // Mutifunctional method to get and set values to a collection
757 // The value/s can be optionally by executed if its a function
758 access: function( elems, key, value, exec, fn, pass ) {
759 var length = elems.length;
761 // Setting many attributes
762 if ( typeof key === "object" ) {
763 for ( var k in key ) {
764 jQuery.access( elems, k, key[k], exec, fn, value );
769 // Setting one attribute
770 if ( value !== undefined ) {
771 // Optionally, function values get executed if exec is true
772 exec = !pass && exec && jQuery.isFunction(value);
774 for ( var i = 0; i < length; i++ ) {
775 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
781 // Getting an attribute
782 return length ? fn( elems[0], key ) : undefined;
786 return (new Date()).getTime();
789 // Use of jQuery.browser is frowned upon.
790 // More details: http://docs.jquery.com/Utilities/jQuery.browser
791 uaMatch: function( ua ) {
792 ua = ua.toLowerCase();
794 var match = rwebkit.exec( ua ) ||
797 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
800 return { browser: match[1] || "", version: match[2] || "0" };
804 function jQuerySubclass( selector, context ) {
805 return new jQuerySubclass.fn.init( selector, context );
807 jQuery.extend( true, jQuerySubclass, this );
808 jQuerySubclass.superclass = this;
809 jQuerySubclass.fn = jQuerySubclass.prototype = this();
810 jQuerySubclass.fn.constructor = jQuerySubclass;
811 jQuerySubclass.subclass = this.subclass;
812 jQuerySubclass.fn.init = function init( selector, context ) {
813 if ( context && context instanceof jQuery && !(context instanceof jQuerySubclass) ) {
814 context = jQuerySubclass(context);
817 return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
819 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
820 var rootjQuerySubclass = jQuerySubclass(document);
821 return jQuerySubclass;
827 // Populate the class2type map
828 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
829 class2type[ "[object " + name + "]" ] = name.toLowerCase();
832 browserMatch = jQuery.uaMatch( userAgent );
833 if ( browserMatch.browser ) {
834 jQuery.browser[ browserMatch.browser ] = true;
835 jQuery.browser.version = browserMatch.version;
838 // Deprecated, use jQuery.browser.webkit instead
839 if ( jQuery.browser.webkit ) {
840 jQuery.browser.safari = true;
844 jQuery.inArray = function( elem, array ) {
845 return indexOf.call( array, elem );
849 // IE doesn't match non-breaking spaces with \s
850 if ( rnotwhite.test( "\xA0" ) ) {
851 trimLeft = /^[\s\xA0]+/;
852 trimRight = /[\s\xA0]+$/;
855 // All jQuery objects should point back to these
856 rootjQuery = jQuery(document);
858 // Cleanup functions for the document ready method
859 if ( document.addEventListener ) {
860 DOMContentLoaded = function() {
861 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
865 } else if ( document.attachEvent ) {
866 DOMContentLoaded = function() {
867 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
868 if ( document.readyState === "complete" ) {
869 document.detachEvent( "onreadystatechange", DOMContentLoaded );
875 // The DOM ready check for Internet Explorer
876 function doScrollCheck() {
877 if ( jQuery.isReady ) {
882 // If IE is used, use the trick by Diego Perini
883 // http://javascript.nwbox.com/IEContentLoaded/
884 document.documentElement.doScroll("left");
886 setTimeout( doScrollCheck, 1 );
890 // and execute any waiting functions
894 // Expose jQuery to the global object