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 functions to execute 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 jQuery.fn = jQuery.prototype = {
81 init: function( selector, context ) {
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 doc = (context ? context.ownerDocument || context : document);
117 // If a single string is passed in and it's a single tag
118 // just do a createElement and skip the rest
119 ret = rsingleTag.exec( selector );
122 if ( jQuery.isPlainObject( context ) ) {
123 selector = [ document.createElement( ret[1] ) ];
124 jQuery.fn.attr.call( selector, context, true );
127 selector = [ doc.createElement( ret[1] ) ];
131 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
132 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
135 return jQuery.merge( this, selector );
139 elem = document.getElementById( match[2] );
141 // Check parentNode to catch when Blackberry 4.6 returns
142 // nodes that are no longer in the document #6963
143 if ( elem && elem.parentNode ) {
144 // Handle the case where IE and Opera return items
145 // by name instead of ID
146 if ( elem.id !== match[2] ) {
147 return rootjQuery.find( selector );
150 // Otherwise, we inject the element directly into the jQuery object
155 this.context = document;
156 this.selector = selector;
161 } else if ( !context && !rnonword.test( selector ) ) {
162 this.selector = selector;
163 this.context = document;
164 selector = document.getElementsByTagName( selector );
165 return jQuery.merge( this, selector );
167 // HANDLE: $(expr, $(...))
168 } else if ( !context || context.jquery ) {
169 return (context || rootjQuery).find( selector );
171 // HANDLE: $(expr, context)
172 // (which is just equivalent to: $(context).find(expr)
174 return jQuery( context ).find( selector );
177 // HANDLE: $(function)
178 // Shortcut for document ready
179 } else if ( jQuery.isFunction( selector ) ) {
180 return rootjQuery.ready( selector );
183 if (selector.selector !== undefined) {
184 this.selector = selector.selector;
185 this.context = selector.context;
188 return jQuery.makeArray( selector, this );
191 // Start with an empty selector
194 // The current version of jQuery being used
197 // The default length of a jQuery object is 0
200 // The number of elements contained in the matched element set
205 toArray: function() {
206 return slice.call( this, 0 );
209 // Get the Nth element in the matched element set OR
210 // Get the whole matched element set as a clean array
211 get: function( num ) {
214 // Return a 'clean' array
217 // Return just the object
218 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
221 // Take an array of elements and push it onto the stack
222 // (returning the new matched element set)
223 pushStack: function( elems, name, selector ) {
224 // Build a new jQuery matched element set
227 if ( jQuery.isArray( elems ) ) {
228 push.apply( ret, elems );
231 jQuery.merge( ret, elems );
234 // Add the old object onto the stack (as a reference)
235 ret.prevObject = this;
237 ret.context = this.context;
239 if ( name === "find" ) {
240 ret.selector = this.selector + (this.selector ? " " : "") + selector;
242 ret.selector = this.selector + "." + name + "(" + selector + ")";
245 // Return the newly-formed element set
249 // Execute a callback for every element in the matched set.
250 // (You can seed the arguments with an array of args, but this is
251 // only used internally.)
252 each: function( callback, args ) {
253 return jQuery.each( this, callback, args );
256 ready: function( fn ) {
257 // Attach the listeners
260 // If the DOM is already ready
261 if ( jQuery.isReady ) {
262 // Execute the function immediately
263 fn.call( document, jQuery );
265 // Otherwise, remember the function for later
266 } else if ( readyList ) {
267 // Add the function to the wait list
268 readyList.push( fn );
277 this.slice( i, +i + 1 );
285 return this.eq( -1 );
289 return this.pushStack( slice.apply( this, arguments ),
290 "slice", slice.call(arguments).join(",") );
293 map: function( callback ) {
294 return this.pushStack( jQuery.map(this, function( elem, i ) {
295 return callback.call( elem, i, elem );
300 return this.prevObject || jQuery(null);
303 // For internal use only.
304 // Behaves like an Array's method, not like a jQuery method.
310 // Give the init function the jQuery prototype for later instantiation
311 jQuery.fn.init.prototype = jQuery.fn;
313 jQuery.extend = jQuery.fn.extend = function() {
314 // copy reference to target object
315 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy, copyIsArray, clone;
317 // Handle a deep copy situation
318 if ( typeof target === "boolean" ) {
320 target = arguments[1] || {};
321 // skip the boolean and the target
325 // Handle case when target is a string or something (possible in deep copy)
326 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
330 // extend jQuery itself if only one argument is passed
331 if ( length === i ) {
336 for ( ; i < length; i++ ) {
337 // Only deal with non-null/undefined values
338 if ( (options = arguments[ i ]) != null ) {
339 // Extend the base object
340 for ( name in options ) {
341 src = target[ name ];
342 copy = options[ name ];
344 // Prevent never-ending loop
345 if ( target === copy ) {
349 // Recurse if we're merging plain objects or arrays
350 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
353 clone = src && jQuery.isArray(src) ? src : [];
356 clone = src && jQuery.isPlainObject(src) ? src : {};
359 // Never move original objects, clone them
360 target[ name ] = jQuery.extend( deep, clone, copy );
362 // Don't bring in undefined values
363 } else if ( copy !== undefined ) {
364 target[ name ] = copy;
370 // Return the modified object
375 noConflict: function( deep ) {
379 window.jQuery = _jQuery;
385 // Is the DOM ready to be used? Set to true once it occurs.
388 // A counter to track how many items to wait for before
389 // the ready event fires. See #6781
392 // Handle when the DOM is ready
393 ready: function( wait ) {
394 // A third-party is pushing the ready event forwards
395 if ( wait === true ) {
399 // Make sure that the DOM is not already loaded
400 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
401 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
402 if ( !document.body ) {
403 return setTimeout( jQuery.ready, 1 );
406 // Remember that the DOM is ready
407 jQuery.isReady = true;
409 // If a normal DOM Ready event fired, decrement, and wait if need be
410 if ( wait !== true && --jQuery.readyWait > 0 ) {
414 // If there are functions bound, to execute
416 // Execute all of them
418 while ( (fn = readyList[ i++ ]) ) {
419 fn.call( document, jQuery );
422 // Reset the list of functions
426 // Trigger any bound ready events
427 if ( jQuery.fn.triggerHandler ) {
428 jQuery( document ).triggerHandler( "ready" );
433 bindReady: function() {
440 // Catch cases where $(document).ready() is called after the
441 // browser event has already occurred.
442 if ( document.readyState === "complete" ) {
443 // Handle it asynchronously to allow scripts the opportunity to delay ready
444 return setTimeout( jQuery.ready, 1 );
447 // Mozilla, Opera and webkit nightlies currently support this event
448 if ( document.addEventListener ) {
449 // Use the handy event callback
450 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
452 // A fallback to window.onload, that will always work
453 window.addEventListener( "load", jQuery.ready, false );
455 // If IE event model is used
456 } else if ( document.attachEvent ) {
457 // ensure firing before onload,
458 // maybe late but safe also for iframes
459 document.attachEvent("onreadystatechange", DOMContentLoaded);
461 // A fallback to window.onload, that will always work
462 window.attachEvent( "onload", jQuery.ready );
464 // If IE and not a frame
465 // continually check to see if the document is ready
466 var toplevel = false;
469 toplevel = window.frameElement == null;
472 if ( document.documentElement.doScroll && toplevel ) {
478 // See test/unit/core.js for details concerning isFunction.
479 // Since version 1.3, DOM methods and functions like alert
480 // aren't supported. They return false on IE (#2968).
481 isFunction: function( obj ) {
482 return jQuery.type(obj) === "function";
485 isArray: Array.isArray || function( obj ) {
486 return jQuery.type(obj) === "array";
489 // A crude way of determining if an object is a window
490 isWindow: function( obj ) {
491 return obj && typeof obj === "object" && "setInterval" in obj;
494 isNaN: function( obj ) {
495 return obj == null || !rdigit.test( obj ) || isNaN( obj );
498 type: function( obj ) {
501 class2type[ toString.call(obj) ] || "object";
504 isPlainObject: function( obj ) {
505 // Must be an Object.
506 // Because of IE, we also have to check the presence of the constructor property.
507 // Make sure that DOM nodes and window objects don't pass through, as well
508 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
512 // Not own constructor property must be Object
513 if ( obj.constructor &&
514 !hasOwn.call(obj, "constructor") &&
515 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
519 // Own properties are enumerated firstly, so to speed up,
520 // if last one is own, then all properties are own.
523 for ( key in obj ) {}
525 return key === undefined || hasOwn.call( obj, key );
528 isEmptyObject: function( obj ) {
529 for ( var name in obj ) {
535 error: function( msg ) {
539 parseJSON: function( data ) {
540 if ( typeof data !== "string" || !data ) {
544 // Make sure leading/trailing whitespace is removed (IE can't handle it)
545 data = jQuery.trim( data );
547 // Make sure the incoming data is actual JSON
548 // Logic borrowed from http://json.org/json2.js
549 if ( rvalidchars.test(data.replace(rvalidescape, "@")
550 .replace(rvalidtokens, "]")
551 .replace(rvalidbraces, "")) ) {
553 // Try to use the native JSON parser first
554 return window.JSON && window.JSON.parse ?
555 window.JSON.parse( data ) :
556 (new Function("return " + data))();
559 jQuery.error( "Invalid JSON: " + 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, j = 0;
683 if ( typeof second.length === "number" ) {
684 for ( var l = second.length; j < l; j++ ) {
685 first[ i++ ] = second[ j ];
689 while ( second[j] !== undefined ) {
690 first[ i++ ] = second[ j++ ];
699 grep: function( elems, callback, inv ) {
700 var ret = [], retVal;
703 // Go through the array, only saving the items
704 // that pass the validator function
705 for ( var i = 0, length = elems.length; i < length; i++ ) {
706 retVal = !!callback( elems[ i ], i );
707 if ( inv !== retVal ) {
708 ret.push( elems[ i ] );
715 // arg is for internal usage only
716 map: function( elems, callback, arg ) {
719 // Go through the array, translating each of the items to their
720 // new value (or values).
721 for ( var i = 0, length = elems.length; i < length; i++ ) {
722 value = callback( elems[ i ], i, arg );
724 if ( value != null ) {
725 ret[ ret.length ] = value;
729 return ret.concat.apply( [], ret );
732 // A global GUID counter for objects
735 proxy: function( fn, proxy, thisObject ) {
736 if ( arguments.length === 2 ) {
737 if ( typeof proxy === "string" ) {
739 fn = thisObject[ proxy ];
742 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
748 if ( !proxy && fn ) {
750 return fn.apply( thisObject || this, arguments );
754 // Set the guid of unique handler to the same of original handler, so it can be removed
756 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
759 // So proxy can be declared as an argument
763 // Mutifunctional method to get and set values to a collection
764 // The value/s can be optionally by executed if its a function
765 access: function( elems, key, value, exec, fn, pass ) {
766 var length = elems.length;
768 // Setting many attributes
769 if ( typeof key === "object" ) {
770 for ( var k in key ) {
771 jQuery.access( elems, k, key[k], exec, fn, value );
776 // Setting one attribute
777 if ( value !== undefined ) {
778 // Optionally, function values get executed if exec is true
779 exec = !pass && exec && jQuery.isFunction(value);
781 for ( var i = 0; i < length; i++ ) {
782 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
788 // Getting an attribute
789 return length ? fn( elems[0], key ) : undefined;
793 return (new Date()).getTime();
796 // Use of jQuery.browser is frowned upon.
797 // More details: http://docs.jquery.com/Utilities/jQuery.browser
798 uaMatch: function( ua ) {
799 ua = ua.toLowerCase();
801 var match = rwebkit.exec( ua ) ||
804 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
807 return { browser: match[1] || "", version: match[2] || "0" };
813 // Populate the class2type map
814 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
815 class2type[ "[object " + name + "]" ] = name.toLowerCase();
818 browserMatch = jQuery.uaMatch( userAgent );
819 if ( browserMatch.browser ) {
820 jQuery.browser[ browserMatch.browser ] = true;
821 jQuery.browser.version = browserMatch.version;
824 // Deprecated, use jQuery.browser.webkit instead
825 if ( jQuery.browser.webkit ) {
826 jQuery.browser.safari = true;
830 jQuery.inArray = function( elem, array ) {
831 return indexOf.call( array, elem );
835 // Verify that \s matches non-breaking spaces
836 // (IE fails on this test)
837 if ( !rwhite.test( "\xA0" ) ) {
838 trimLeft = /^[\s\xA0]+/;
839 trimRight = /[\s\xA0]+$/;
842 // All jQuery objects should point back to these
843 rootjQuery = jQuery(document);
845 // Cleanup functions for the document ready method
846 if ( document.addEventListener ) {
847 DOMContentLoaded = function() {
848 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
852 } else if ( document.attachEvent ) {
853 DOMContentLoaded = function() {
854 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
855 if ( document.readyState === "complete" ) {
856 document.detachEvent( "onreadystatechange", DOMContentLoaded );
862 // The DOM ready check for Internet Explorer
863 function doScrollCheck() {
864 if ( jQuery.isReady ) {
869 // If IE is used, use the trick by Diego Perini
870 // http://javascript.nwbox.com/IEContentLoaded/
871 document.documentElement.doScroll("left");
873 setTimeout( doScrollCheck, 1 );
877 // and execute any waiting functions
881 // Expose jQuery to the global object
882 return (window.jQuery = window.$ = jQuery);