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
36 // Match a standalone tag
37 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
40 rvalidchars = /^[\],:{}\s]*$/,
41 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
42 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
43 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
46 rwebkit = /(webkit)[ \/]([\w.]+)/,
47 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
48 rmsie = /(msie) ([\w.]+)/,
49 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
51 // Keep a UserAgent string for use with jQuery.browser
52 userAgent = navigator.userAgent,
54 // For matching the engine and version of the browser
57 // Has the ready events already been bound?
60 // The functions to execute on DOM ready
63 // The ready event handler
66 // Save a reference to some core methods
67 toString = Object.prototype.toString,
68 hasOwn = Object.prototype.hasOwnProperty,
69 push = Array.prototype.push,
70 slice = Array.prototype.slice,
71 trim = String.prototype.trim,
72 indexOf = Array.prototype.indexOf;
74 jQuery.fn = jQuery.prototype = {
75 init: function( selector, context ) {
76 var match, elem, ret, doc;
78 // Handle $(""), $(null), or $(undefined)
83 // Handle $(DOMElement)
84 if ( selector.nodeType ) {
85 this.context = this[0] = selector;
90 // The body element only exists once, optimize finding it
91 if ( selector === "body" && !context ) {
92 this.context = document;
93 this[0] = document.body;
94 this.selector = "body";
99 // Handle HTML strings
100 if ( typeof selector === "string" ) {
101 // Are we dealing with HTML string or an ID?
102 match = quickExpr.exec( selector );
104 // Verify a match, and that no context was specified for #id
105 if ( match && (match[1] || !context) ) {
107 // HANDLE: $(html) -> $(array)
109 doc = (context ? context.ownerDocument || context : document);
111 // If a single string is passed in and it's a single tag
112 // just do a createElement and skip the rest
113 ret = rsingleTag.exec( selector );
116 if ( jQuery.isPlainObject( context ) ) {
117 selector = [ document.createElement( ret[1] ) ];
118 jQuery.fn.attr.call( selector, context, true );
121 selector = [ doc.createElement( ret[1] ) ];
125 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
126 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
129 return jQuery.merge( this, selector );
133 elem = document.getElementById( match[2] );
135 // Check parentNode to catch when Blackberry 4.6 returns
136 // nodes that are no longer in the document #6963
137 if ( elem && elem.parentNode ) {
138 // Handle the case where IE and Opera return items
139 // by name instead of ID
140 if ( elem.id !== match[2] ) {
141 return rootjQuery.find( selector );
144 // Otherwise, we inject the element directly into the jQuery object
149 this.context = document;
150 this.selector = selector;
155 } else if ( !context && !rnonword.test( selector ) ) {
156 this.selector = selector;
157 this.context = document;
158 selector = document.getElementsByTagName( selector );
159 return jQuery.merge( this, 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 jQuery( 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.slice(num)[ 0 ] : 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
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
254 // If the DOM is already ready
255 if ( jQuery.isReady ) {
256 // Execute the function immediately
257 fn.call( document, jQuery );
259 // Otherwise, remember the function for later
260 } else if ( readyList ) {
261 // Add the function to the wait list
262 readyList.push( fn );
271 this.slice( i, +i + 1 );
279 return this.eq( -1 );
283 return this.pushStack( slice.apply( this, arguments ),
284 "slice", slice.call(arguments).join(",") );
287 map: function( callback ) {
288 return this.pushStack( jQuery.map(this, function( elem, i ) {
289 return callback.call( elem, i, elem );
294 return this.prevObject || jQuery(null);
297 // For internal use only.
298 // Behaves like an Array's method, not like a jQuery method.
304 // Give the init function the jQuery prototype for later instantiation
305 jQuery.fn.init.prototype = jQuery.fn;
307 jQuery.extend = jQuery.fn.extend = function() {
308 // copy reference to target object
309 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
311 // Handle a deep copy situation
312 if ( typeof target === "boolean" ) {
314 target = arguments[1] || {};
315 // skip the boolean and the target
319 // Handle case when target is a string or something (possible in deep copy)
320 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
324 // extend jQuery itself if only one argument is passed
325 if ( length === i ) {
330 for ( ; i < length; i++ ) {
331 // Only deal with non-null/undefined values
332 if ( (options = arguments[ i ]) != null ) {
333 // Extend the base object
334 for ( name in options ) {
335 src = target[ name ];
336 copy = options[ name ];
338 // Prevent never-ending loop
339 if ( target === copy ) {
343 // Recurse if we're merging object literal values or arrays
344 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
345 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
346 : jQuery.isArray(copy) ? [] : {};
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, 13 );
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
405 // Execute all of them
407 while ( (fn = readyList[ i++ ]) ) {
408 fn.call( document, jQuery );
411 // Reset the list of functions
415 // Trigger any bound ready events
416 if ( jQuery.fn.triggerHandler ) {
417 jQuery( document ).triggerHandler( "ready" );
422 bindReady: function() {
429 // Catch cases where $(document).ready() is called after the
430 // browser event has already occurred.
431 if ( document.readyState === "complete" ) {
432 return jQuery.ready();
435 // Mozilla, Opera and webkit nightlies currently support this event
436 if ( document.addEventListener ) {
437 // Use the handy event callback
438 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
440 // A fallback to window.onload, that will always work
441 window.addEventListener( "load", jQuery.ready, false );
443 // If IE event model is used
444 } else if ( document.attachEvent ) {
445 // ensure firing before onload,
446 // maybe late but safe also for iframes
447 document.attachEvent("onreadystatechange", DOMContentLoaded);
449 // A fallback to window.onload, that will always work
450 window.attachEvent( "onload", jQuery.ready );
452 // If IE and not a frame
453 // continually check to see if the document is ready
454 var toplevel = false;
457 toplevel = window.frameElement == null;
460 if ( document.documentElement.doScroll && toplevel ) {
466 // See test/unit/core.js for details concerning isFunction.
467 // Since version 1.3, DOM methods and functions like alert
468 // aren't supported. They return false on IE (#2968).
469 isFunction: function( obj ) {
470 return jQuery.type(obj) === "function";
473 isArray: Array.isArray || function( obj ) {
474 return jQuery.type(obj) === "array";
477 type: function( obj ) {
480 toString.call(obj).slice(8, -1).toLowerCase();
483 isPlainObject: function( obj ) {
484 // Must be an Object.
485 // Because of IE, we also have to check the presence of the constructor property.
486 // Make sure that DOM nodes and window objects don't pass through, as well
487 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) {
491 // Not own constructor property must be Object
492 if ( obj.constructor &&
493 !hasOwn.call(obj, "constructor") &&
494 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
498 // Own properties are enumerated firstly, so to speed up,
499 // if last one is own, then all properties are own.
502 for ( key in obj ) {}
504 return key === undefined || hasOwn.call( obj, key );
507 isEmptyObject: function( obj ) {
508 for ( var name in obj ) {
514 error: function( msg ) {
518 parseJSON: function( data ) {
519 if ( typeof data !== "string" || !data ) {
523 // Make sure leading/trailing whitespace is removed (IE can't handle it)
524 data = jQuery.trim( data );
526 // Make sure the incoming data is actual JSON
527 // Logic borrowed from http://json.org/json2.js
528 if ( rvalidchars.test(data.replace(rvalidescape, "@")
529 .replace(rvalidtokens, "]")
530 .replace(rvalidbraces, "")) ) {
532 // Try to use the native JSON parser first
533 return window.JSON && window.JSON.parse ?
534 window.JSON.parse( data ) :
535 (new Function("return " + data))();
538 jQuery.error( "Invalid JSON: " + data );
544 // Evalulates a script in a global context
545 globalEval: function( data ) {
546 if ( data && rnotwhite.test(data) ) {
547 // Inspired by code by Andrea Giammarchi
548 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
549 var head = document.getElementsByTagName("head")[0] || document.documentElement,
550 script = document.createElement("script");
552 script.type = "text/javascript";
554 if ( jQuery.support.scriptEval ) {
555 script.appendChild( document.createTextNode( data ) );
560 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
561 // This arises when a base node is used (#2709).
562 head.insertBefore( script, head.firstChild );
563 head.removeChild( script );
567 nodeName: function( elem, name ) {
568 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
571 // args is for internal usage only
572 each: function( object, callback, args ) {
574 length = object.length,
575 isObj = length === undefined || jQuery.isFunction(object);
579 for ( name in object ) {
580 if ( callback.apply( object[ name ], args ) === false ) {
585 for ( ; i < length; ) {
586 if ( callback.apply( object[ i++ ], args ) === false ) {
592 // A special, fast, case for the most common use of each
595 for ( name in object ) {
596 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
601 for ( var value = object[0];
602 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
609 // Use native String.trim function wherever possible
612 return text == null ?
617 // Otherwise use our own trimming functionality
619 return text == null ?
621 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
624 // results is for internal usage only
625 makeArray: function( array, results ) {
626 var ret = results || [];
628 if ( array != null ) {
629 // The window, strings (and functions) also have 'length'
630 // The extra typeof function check is to prevent crashes
631 // in Safari 2 (See: #3039)
632 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
633 var type = jQuery.type(array);
635 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || "setInterval" in array ) {
636 push.call( ret, array );
638 jQuery.merge( ret, array );
645 inArray: function( elem, array ) {
646 if ( array.indexOf ) {
647 return array.indexOf( elem );
650 for ( var i = 0, length = array.length; i < length; i++ ) {
651 if ( array[ i ] === elem ) {
659 merge: function( first, second ) {
660 var i = first.length, j = 0;
662 if ( typeof second.length === "number" ) {
663 for ( var l = second.length; j < l; j++ ) {
664 first[ i++ ] = second[ j ];
668 while ( second[j] !== undefined ) {
669 first[ i++ ] = second[ j++ ];
678 grep: function( elems, callback, inv ) {
679 var ret = [], retVal;
682 // Go through the array, only saving the items
683 // that pass the validator function
684 for ( var i = 0, length = elems.length; i < length; i++ ) {
685 retVal = !!callback( elems[ i ], i );
686 if ( inv !== retVal ) {
687 ret.push( elems[ i ] );
694 // arg is for internal usage only
695 map: function( elems, callback, arg ) {
698 // Go through the array, translating each of the items to their
699 // new value (or values).
700 for ( var i = 0, length = elems.length; i < length; i++ ) {
701 value = callback( elems[ i ], i, arg );
703 if ( value != null ) {
704 ret[ ret.length ] = value;
708 return ret.concat.apply( [], ret );
711 // A global GUID counter for objects
714 proxy: function( fn, proxy, thisObject ) {
715 if ( arguments.length === 2 ) {
716 if ( typeof proxy === "string" ) {
718 fn = thisObject[ proxy ];
721 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
727 if ( !proxy && fn ) {
729 return fn.apply( thisObject || this, arguments );
733 // Set the guid of unique handler to the same of original handler, so it can be removed
735 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
738 // So proxy can be declared as an argument
742 // Mutifunctional method to get and set values to a collection
743 // The value/s can be optionally by executed if its a function
744 access: function( elems, key, value, exec, fn, pass ) {
745 var length = elems.length;
747 // Setting many attributes
748 if ( typeof key === "object" ) {
749 for ( var k in key ) {
750 jQuery.access( elems, k, key[k], exec, fn, value );
755 // Setting one attribute
756 if ( value !== undefined ) {
757 // Optionally, function values get executed if exec is true
758 exec = !pass && exec && jQuery.isFunction(value);
760 for ( var i = 0; i < length; i++ ) {
761 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
767 // Getting an attribute
768 return length ? fn( elems[0], key ) : undefined;
772 return (new Date()).getTime();
775 // Use of jQuery.browser is frowned upon.
776 // More details: http://docs.jquery.com/Utilities/jQuery.browser
777 uaMatch: function( ua ) {
778 ua = ua.toLowerCase();
780 var match = rwebkit.exec( ua ) ||
783 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
786 return { browser: match[1] || "", version: match[2] || "0" };
792 browserMatch = jQuery.uaMatch( userAgent );
793 if ( browserMatch.browser ) {
794 jQuery.browser[ browserMatch.browser ] = true;
795 jQuery.browser.version = browserMatch.version;
798 // Deprecated, use jQuery.browser.webkit instead
799 if ( jQuery.browser.webkit ) {
800 jQuery.browser.safari = true;
804 jQuery.inArray = function( elem, array ) {
805 return indexOf.call( array, elem );
809 // Verify that \s matches non-breaking spaces
810 // (IE fails on this test)
811 if ( !rwhite.test( "\xA0" ) ) {
812 trimLeft = /^[\s\xA0]+/;
813 trimRight = /[\s\xA0]+$/;
816 // All jQuery objects should point back to these
817 rootjQuery = jQuery(document);
819 // Cleanup functions for the document ready method
820 if ( document.addEventListener ) {
821 DOMContentLoaded = function() {
822 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
826 } else if ( document.attachEvent ) {
827 DOMContentLoaded = function() {
828 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
829 if ( document.readyState === "complete" ) {
830 document.detachEvent( "onreadystatechange", DOMContentLoaded );
836 // The DOM ready check for Internet Explorer
837 function doScrollCheck() {
838 if ( jQuery.isReady ) {
843 // If IE is used, use the trick by Diego Perini
844 // http://javascript.nwbox.com/IEContentLoaded/
845 document.documentElement.doScroll("left");
847 setTimeout( doScrollCheck, 1 );
851 // and execute any waiting functions
855 // Expose jQuery to the global object
856 return (window.jQuery = window.$ = jQuery);