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 // Handle it asynchronously to allow scripts the opportunity to delay ready
433 return setTimeout( jQuery.ready, 13 );
436 // Mozilla, Opera and webkit nightlies currently support this event
437 if ( document.addEventListener ) {
438 // Use the handy event callback
439 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
441 // A fallback to window.onload, that will always work
442 window.addEventListener( "load", jQuery.ready, false );
444 // If IE event model is used
445 } else if ( document.attachEvent ) {
446 // ensure firing before onload,
447 // maybe late but safe also for iframes
448 document.attachEvent("onreadystatechange", DOMContentLoaded);
450 // A fallback to window.onload, that will always work
451 window.attachEvent( "onload", jQuery.ready );
453 // If IE and not a frame
454 // continually check to see if the document is ready
455 var toplevel = false;
458 toplevel = window.frameElement == null;
461 if ( document.documentElement.doScroll && toplevel ) {
467 // See test/unit/core.js for details concerning isFunction.
468 // Since version 1.3, DOM methods and functions like alert
469 // aren't supported. They return false on IE (#2968).
470 isFunction: function( obj ) {
471 return jQuery.type(obj) === "function";
474 isArray: Array.isArray || function( obj ) {
475 return jQuery.type(obj) === "array";
478 // A crude way of determining if an object is a window
479 isWindow: function( obj ) {
480 return obj && typeof obj === "object" && "setInterval" in obj;
483 type: function( obj ) {
486 toString.call(obj).slice(8, -1).toLowerCase();
489 isPlainObject: function( obj ) {
490 // Must be an Object.
491 // Because of IE, we also have to check the presence of the constructor property.
492 // Make sure that DOM nodes and window objects don't pass through, as well
493 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
497 // Not own constructor property must be Object
498 if ( obj.constructor &&
499 !hasOwn.call(obj, "constructor") &&
500 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
504 // Own properties are enumerated firstly, so to speed up,
505 // if last one is own, then all properties are own.
508 for ( key in obj ) {}
510 return key === undefined || hasOwn.call( obj, key );
513 isEmptyObject: function( obj ) {
514 for ( var name in obj ) {
520 error: function( msg ) {
524 parseJSON: function( data ) {
525 if ( typeof data !== "string" || !data ) {
529 // Make sure leading/trailing whitespace is removed (IE can't handle it)
530 data = jQuery.trim( 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 // Try to use the native JSON parser first
539 return window.JSON && window.JSON.parse ?
540 window.JSON.parse( data ) :
541 (new Function("return " + data))();
544 jQuery.error( "Invalid JSON: " + data );
550 // Evalulates a script in a global context
551 globalEval: function( data ) {
552 if ( data && rnotwhite.test(data) ) {
553 // Inspired by code by Andrea Giammarchi
554 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
555 var head = document.getElementsByTagName("head")[0] || document.documentElement,
556 script = document.createElement("script");
558 script.type = "text/javascript";
560 if ( jQuery.support.scriptEval ) {
561 script.appendChild( document.createTextNode( data ) );
566 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
567 // This arises when a base node is used (#2709).
568 head.insertBefore( script, head.firstChild );
569 head.removeChild( script );
573 nodeName: function( elem, name ) {
574 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
577 // args is for internal usage only
578 each: function( object, callback, args ) {
580 length = object.length,
581 isObj = length === undefined || jQuery.isFunction(object);
585 for ( name in object ) {
586 if ( callback.apply( object[ name ], args ) === false ) {
591 for ( ; i < length; ) {
592 if ( callback.apply( object[ i++ ], args ) === false ) {
598 // A special, fast, case for the most common use of each
601 for ( name in object ) {
602 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
607 for ( var value = object[0];
608 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
615 // Use native String.trim function wherever possible
618 return text == null ?
623 // Otherwise use our own trimming functionality
625 return text == null ?
627 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
630 // results is for internal usage only
631 makeArray: function( array, results ) {
632 var ret = results || [];
634 if ( array != null ) {
635 // The window, strings (and functions) also have 'length'
636 // The extra typeof function check is to prevent crashes
637 // in Safari 2 (See: #3039)
638 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
639 var type = jQuery.type(array);
641 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
642 push.call( ret, array );
644 jQuery.merge( ret, array );
651 inArray: function( elem, array ) {
652 if ( array.indexOf ) {
653 return array.indexOf( elem );
656 for ( var i = 0, length = array.length; i < length; i++ ) {
657 if ( array[ i ] === elem ) {
665 merge: function( first, second ) {
666 var i = first.length, j = 0;
668 if ( typeof second.length === "number" ) {
669 for ( var l = second.length; j < l; j++ ) {
670 first[ i++ ] = second[ j ];
674 while ( second[j] !== undefined ) {
675 first[ i++ ] = second[ j++ ];
684 grep: function( elems, callback, inv ) {
685 var ret = [], retVal;
688 // Go through the array, only saving the items
689 // that pass the validator function
690 for ( var i = 0, length = elems.length; i < length; i++ ) {
691 retVal = !!callback( elems[ i ], i );
692 if ( inv !== retVal ) {
693 ret.push( elems[ i ] );
700 // arg is for internal usage only
701 map: function( elems, callback, arg ) {
704 // Go through the array, translating each of the items to their
705 // new value (or values).
706 for ( var i = 0, length = elems.length; i < length; i++ ) {
707 value = callback( elems[ i ], i, arg );
709 if ( value != null ) {
710 ret[ ret.length ] = value;
714 return ret.concat.apply( [], ret );
717 // A global GUID counter for objects
720 proxy: function( fn, proxy, thisObject ) {
721 if ( arguments.length === 2 ) {
722 if ( typeof proxy === "string" ) {
724 fn = thisObject[ proxy ];
727 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
733 if ( !proxy && fn ) {
735 return fn.apply( thisObject || this, arguments );
739 // Set the guid of unique handler to the same of original handler, so it can be removed
741 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
744 // So proxy can be declared as an argument
748 // Mutifunctional method to get and set values to a collection
749 // The value/s can be optionally by executed if its a function
750 access: function( elems, key, value, exec, fn, pass ) {
751 var length = elems.length;
753 // Setting many attributes
754 if ( typeof key === "object" ) {
755 for ( var k in key ) {
756 jQuery.access( elems, k, key[k], exec, fn, value );
761 // Setting one attribute
762 if ( value !== undefined ) {
763 // Optionally, function values get executed if exec is true
764 exec = !pass && exec && jQuery.isFunction(value);
766 for ( var i = 0; i < length; i++ ) {
767 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
773 // Getting an attribute
774 return length ? fn( elems[0], key ) : undefined;
778 return (new Date()).getTime();
781 // Use of jQuery.browser is frowned upon.
782 // More details: http://docs.jquery.com/Utilities/jQuery.browser
783 uaMatch: function( ua ) {
784 ua = ua.toLowerCase();
786 var match = rwebkit.exec( ua ) ||
789 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
792 return { browser: match[1] || "", version: match[2] || "0" };
798 browserMatch = jQuery.uaMatch( userAgent );
799 if ( browserMatch.browser ) {
800 jQuery.browser[ browserMatch.browser ] = true;
801 jQuery.browser.version = browserMatch.version;
804 // Deprecated, use jQuery.browser.webkit instead
805 if ( jQuery.browser.webkit ) {
806 jQuery.browser.safari = true;
810 jQuery.inArray = function( elem, array ) {
811 return indexOf.call( array, elem );
815 // Verify that \s matches non-breaking spaces
816 // (IE fails on this test)
817 if ( !rwhite.test( "\xA0" ) ) {
818 trimLeft = /^[\s\xA0]+/;
819 trimRight = /[\s\xA0]+$/;
822 // All jQuery objects should point back to these
823 rootjQuery = jQuery(document);
825 // Cleanup functions for the document ready method
826 if ( document.addEventListener ) {
827 DOMContentLoaded = function() {
828 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
832 } else if ( document.attachEvent ) {
833 DOMContentLoaded = function() {
834 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
835 if ( document.readyState === "complete" ) {
836 document.detachEvent( "onreadystatechange", DOMContentLoaded );
842 // The DOM ready check for Internet Explorer
843 function doScrollCheck() {
844 if ( jQuery.isReady ) {
849 // If IE is used, use the trick by Diego Perini
850 // http://javascript.nwbox.com/IEContentLoaded/
851 document.documentElement.doScroll("left");
853 setTimeout( doScrollCheck, 1 );
857 // and execute any waiting functions
861 // Expose jQuery to the global object
862 return (window.jQuery = window.$ = jQuery);