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
28 // Used for trimming whitespace
32 // Match a standalone tag
33 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
35 // Keep a UserAgent string for use with jQuery.browser
36 userAgent = navigator.userAgent,
38 // For matching the engine and version of the browser
41 // Has the ready events already been bound?
44 // The functions to execute on DOM ready
47 // The ready event handler
50 // Save a reference to some core methods
51 toString = Object.prototype.toString,
52 hasOwn = Object.prototype.hasOwnProperty,
53 push = Array.prototype.push,
54 slice = Array.prototype.slice,
55 trim = String.prototype.trim,
56 indexOf = Array.prototype.indexOf;
58 jQuery.fn = jQuery.prototype = {
59 init: function( selector, context ) {
60 var match, elem, ret, doc;
62 // Handle $(""), $(null), or $(undefined)
67 // Handle $(DOMElement)
68 if ( selector.nodeType ) {
69 this.context = this[0] = selector;
74 // The body element only exists once, optimize finding it
75 if ( selector === "body" && !context ) {
76 this.context = document;
77 this[0] = document.body;
78 this.selector = "body";
83 // Handle HTML strings
84 if ( typeof selector === "string" ) {
85 // Are we dealing with HTML string or an ID?
86 match = quickExpr.exec( selector );
88 // Verify a match, and that no context was specified for #id
89 if ( match && (match[1] || !context) ) {
91 // HANDLE: $(html) -> $(array)
93 doc = (context ? context.ownerDocument || context : document);
95 // If a single string is passed in and it's a single tag
96 // just do a createElement and skip the rest
97 ret = rsingleTag.exec( selector );
100 if ( jQuery.isPlainObject( context ) ) {
101 selector = [ document.createElement( ret[1] ) ];
102 jQuery.fn.attr.call( selector, context, true );
105 selector = [ doc.createElement( ret[1] ) ];
109 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
110 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
113 return jQuery.merge( this, selector );
117 elem = document.getElementById( match[2] );
119 // Check parentNode to catch when Blackberry 4.6 returns
120 // nodes that are no longer in the document #6963
121 if ( elem && elem.parentNode ) {
122 // Handle the case where IE and Opera return items
123 // by name instead of ID
124 if ( elem.id !== match[2] ) {
125 return rootjQuery.find( selector );
128 // Otherwise, we inject the element directly into the jQuery object
133 this.context = document;
134 this.selector = selector;
139 } else if ( !context && /^\w+$/.test( selector ) ) {
140 this.selector = selector;
141 this.context = document;
142 selector = document.getElementsByTagName( selector );
143 return jQuery.merge( this, selector );
145 // HANDLE: $(expr, $(...))
146 } else if ( !context || context.jquery ) {
147 return (context || rootjQuery).find( selector );
149 // HANDLE: $(expr, context)
150 // (which is just equivalent to: $(context).find(expr)
152 return jQuery( context ).find( selector );
155 // HANDLE: $(function)
156 // Shortcut for document ready
157 } else if ( jQuery.isFunction( selector ) ) {
158 return rootjQuery.ready( selector );
161 if (selector.selector !== undefined) {
162 this.selector = selector.selector;
163 this.context = selector.context;
166 return jQuery.makeArray( selector, this );
169 // Start with an empty selector
172 // The current version of jQuery being used
175 // The default length of a jQuery object is 0
178 // The number of elements contained in the matched element set
183 toArray: function() {
184 return slice.call( this, 0 );
187 // Get the Nth element in the matched element set OR
188 // Get the whole matched element set as a clean array
189 get: function( num ) {
192 // Return a 'clean' array
195 // Return just the object
196 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
199 // Take an array of elements and push it onto the stack
200 // (returning the new matched element set)
201 pushStack: function( elems, name, selector ) {
202 // Build a new jQuery matched element set
205 if ( jQuery.isArray( elems ) ) {
206 push.apply( ret, elems );
209 jQuery.merge( ret, elems );
212 // Add the old object onto the stack (as a reference)
213 ret.prevObject = this;
215 ret.context = this.context;
217 if ( name === "find" ) {
218 ret.selector = this.selector + (this.selector ? " " : "") + selector;
220 ret.selector = this.selector + "." + name + "(" + selector + ")";
223 // Return the newly-formed element set
227 // Execute a callback for every element in the matched set.
228 // (You can seed the arguments with an array of args, but this is
229 // only used internally.)
230 each: function( callback, args ) {
231 return jQuery.each( this, callback, args );
234 ready: function( fn ) {
235 // Attach the listeners
238 // If the DOM is already ready
239 if ( jQuery.isReady ) {
240 // Execute the function immediately
241 fn.call( document, jQuery );
243 // Otherwise, remember the function for later
244 } else if ( readyList ) {
245 // Add the function to the wait list
246 readyList.push( fn );
255 this.slice( i, +i + 1 );
263 return this.eq( -1 );
267 return this.pushStack( slice.apply( this, arguments ),
268 "slice", slice.call(arguments).join(",") );
271 map: function( callback ) {
272 return this.pushStack( jQuery.map(this, function( elem, i ) {
273 return callback.call( elem, i, elem );
278 return this.prevObject || jQuery(null);
281 // For internal use only.
282 // Behaves like an Array's method, not like a jQuery method.
288 // Give the init function the jQuery prototype for later instantiation
289 jQuery.fn.init.prototype = jQuery.fn;
291 jQuery.extend = jQuery.fn.extend = function() {
292 // copy reference to target object
293 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
295 // Handle a deep copy situation
296 if ( typeof target === "boolean" ) {
298 target = arguments[1] || {};
299 // skip the boolean and the target
303 // Handle case when target is a string or something (possible in deep copy)
304 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
308 // extend jQuery itself if only one argument is passed
309 if ( length === i ) {
314 for ( ; i < length; i++ ) {
315 // Only deal with non-null/undefined values
316 if ( (options = arguments[ i ]) != null ) {
317 // Extend the base object
318 for ( name in options ) {
319 src = target[ name ];
320 copy = options[ name ];
322 // Prevent never-ending loop
323 if ( target === copy ) {
327 // Recurse if we're merging object literal values or arrays
328 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
329 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
330 : jQuery.isArray(copy) ? [] : {};
332 // Never move original objects, clone them
333 target[ name ] = jQuery.extend( deep, clone, copy );
335 // Don't bring in undefined values
336 } else if ( copy !== undefined ) {
337 target[ name ] = copy;
343 // Return the modified object
348 noConflict: function( deep ) {
352 window.jQuery = _jQuery;
358 // Is the DOM ready to be used? Set to true once it occurs.
361 // A counter to track how many items to wait for before
362 // the ready event fires. See #6781
365 // Handle when the DOM is ready
366 ready: function( wait ) {
367 // A third-party is pushing the ready event forwards
368 if ( wait === true ) {
372 // Make sure that the DOM is not already loaded
373 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
374 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
375 if ( !document.body ) {
376 return setTimeout( jQuery.ready, 13 );
379 // Remember that the DOM is ready
380 jQuery.isReady = true;
382 // If a normal DOM Ready event fired, decrement, and wait if need be
383 if ( wait !== true && --jQuery.readyWait > 0 ) {
387 // If there are functions bound, to execute
389 // Execute all of them
391 while ( (fn = readyList[ i++ ]) ) {
392 fn.call( document, jQuery );
395 // Reset the list of functions
399 // Trigger any bound ready events
400 if ( jQuery.fn.triggerHandler ) {
401 jQuery( document ).triggerHandler( "ready" );
406 bindReady: function() {
413 // Catch cases where $(document).ready() is called after the
414 // browser event has already occurred.
415 if ( document.readyState === "complete" ) {
416 return jQuery.ready();
419 // Mozilla, Opera and webkit nightlies currently support this event
420 if ( document.addEventListener ) {
421 // Use the handy event callback
422 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
424 // A fallback to window.onload, that will always work
425 window.addEventListener( "load", jQuery.ready, false );
427 // If IE event model is used
428 } else if ( document.attachEvent ) {
429 // ensure firing before onload,
430 // maybe late but safe also for iframes
431 document.attachEvent("onreadystatechange", DOMContentLoaded);
433 // A fallback to window.onload, that will always work
434 window.attachEvent( "onload", jQuery.ready );
436 // If IE and not a frame
437 // continually check to see if the document is ready
438 var toplevel = false;
441 toplevel = window.frameElement == null;
444 if ( document.documentElement.doScroll && toplevel ) {
450 // See test/unit/core.js for details concerning isFunction.
451 // Since version 1.3, DOM methods and functions like alert
452 // aren't supported. They return false on IE (#2968).
453 isFunction: function( obj ) {
454 return jQuery.type(obj) === "function";
457 isArray: Array.isArray || function( obj ) {
458 return jQuery.type(obj) === "array";
461 type: function( obj ) {
464 toString.call(obj).slice(8, -1).toLowerCase();
467 isPlainObject: function( obj ) {
468 // Must be an Object.
469 // Because of IE, we also have to check the presence of the constructor property.
470 // Make sure that DOM nodes and window objects don't pass through, as well
471 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) {
475 // Not own constructor property must be Object
476 if ( obj.constructor &&
477 !hasOwn.call(obj, "constructor") &&
478 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
482 // Own properties are enumerated firstly, so to speed up,
483 // if last one is own, then all properties are own.
486 for ( key in obj ) {}
488 return key === undefined || hasOwn.call( obj, key );
491 isEmptyObject: function( obj ) {
492 for ( var name in obj ) {
498 error: function( msg ) {
502 parseJSON: function( data ) {
503 if ( typeof data !== "string" || !data ) {
507 // Make sure leading/trailing whitespace is removed (IE can't handle it)
508 data = jQuery.trim( data );
510 // Make sure the incoming data is actual JSON
511 // Logic borrowed from http://json.org/json2.js
512 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
513 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
514 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
516 // Try to use the native JSON parser first
517 return window.JSON && window.JSON.parse ?
518 window.JSON.parse( data ) :
519 (new Function("return " + data))();
522 jQuery.error( "Invalid JSON: " + data );
528 // Evalulates a script in a global context
529 globalEval: function( data ) {
530 if ( data && rnotwhite.test(data) ) {
531 // Inspired by code by Andrea Giammarchi
532 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
533 var head = document.getElementsByTagName("head")[0] || document.documentElement,
534 script = document.createElement("script");
536 script.type = "text/javascript";
538 if ( jQuery.support.scriptEval ) {
539 script.appendChild( document.createTextNode( data ) );
544 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
545 // This arises when a base node is used (#2709).
546 head.insertBefore( script, head.firstChild );
547 head.removeChild( script );
551 nodeName: function( elem, name ) {
552 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
555 // args is for internal usage only
556 each: function( object, callback, args ) {
558 length = object.length,
559 isObj = length === undefined || jQuery.isFunction(object);
563 for ( name in object ) {
564 if ( callback.apply( object[ name ], args ) === false ) {
569 for ( ; i < length; ) {
570 if ( callback.apply( object[ i++ ], args ) === false ) {
576 // A special, fast, case for the most common use of each
579 for ( name in object ) {
580 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
585 for ( var value = object[0];
586 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
593 // Use native String.trim function wherever possible
596 return text == null ?
601 // Otherwise use our own trimming functionality
603 return text == null ?
605 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
608 // results is for internal usage only
609 makeArray: function( array, results ) {
610 var ret = results || [];
612 if ( array != null ) {
613 // The window, strings (and functions) also have 'length'
614 // The extra typeof function check is to prevent crashes
615 // in Safari 2 (See: #3039)
616 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
617 var type = jQuery.type(array);
619 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || "setInterval" in array ) {
620 push.call( ret, array );
622 jQuery.merge( ret, array );
629 inArray: function( elem, array ) {
630 if ( array.indexOf ) {
631 return array.indexOf( elem );
634 for ( var i = 0, length = array.length; i < length; i++ ) {
635 if ( array[ i ] === elem ) {
643 merge: function( first, second ) {
644 var i = first.length, j = 0;
646 if ( typeof second.length === "number" ) {
647 for ( var l = second.length; j < l; j++ ) {
648 first[ i++ ] = second[ j ];
652 while ( second[j] !== undefined ) {
653 first[ i++ ] = second[ j++ ];
662 grep: function( elems, callback, inv ) {
663 var ret = [], retVal;
666 // Go through the array, only saving the items
667 // that pass the validator function
668 for ( var i = 0, length = elems.length; i < length; i++ ) {
669 retVal = !!callback( elems[ i ], i );
670 if ( inv !== retVal ) {
671 ret.push( elems[ i ] );
678 // arg is for internal usage only
679 map: function( elems, callback, arg ) {
682 // Go through the array, translating each of the items to their
683 // new value (or values).
684 for ( var i = 0, length = elems.length; i < length; i++ ) {
685 value = callback( elems[ i ], i, arg );
687 if ( value != null ) {
688 ret[ ret.length ] = value;
692 return ret.concat.apply( [], ret );
695 // A global GUID counter for objects
698 proxy: function( fn, proxy, thisObject ) {
699 if ( arguments.length === 2 ) {
700 if ( typeof proxy === "string" ) {
702 fn = thisObject[ proxy ];
705 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
711 if ( !proxy && fn ) {
713 return fn.apply( thisObject || this, arguments );
717 // Set the guid of unique handler to the same of original handler, so it can be removed
719 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
722 // So proxy can be declared as an argument
726 // Mutifunctional method to get and set values to a collection
727 // The value/s can be optionally by executed if its a function
728 access: function( elems, key, value, exec, fn, pass ) {
729 var length = elems.length;
731 // Setting many attributes
732 if ( typeof key === "object" ) {
733 for ( var k in key ) {
734 jQuery.access( elems, k, key[k], exec, fn, value );
739 // Setting one attribute
740 if ( value !== undefined ) {
741 // Optionally, function values get executed if exec is true
742 exec = !pass && exec && jQuery.isFunction(value);
744 for ( var i = 0; i < length; i++ ) {
745 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
751 // Getting an attribute
752 return length ? fn( elems[0], key ) : undefined;
756 return (new Date()).getTime();
759 // Use of jQuery.browser is frowned upon.
760 // More details: http://docs.jquery.com/Utilities/jQuery.browser
761 uaMatch: function( ua ) {
762 ua = ua.toLowerCase();
764 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
765 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
766 /(msie) ([\w.]+)/.exec( ua ) ||
767 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
770 return { browser: match[1] || "", version: match[2] || "0" };
776 browserMatch = jQuery.uaMatch( userAgent );
777 if ( browserMatch.browser ) {
778 jQuery.browser[ browserMatch.browser ] = true;
779 jQuery.browser.version = browserMatch.version;
782 // Deprecated, use jQuery.browser.webkit instead
783 if ( jQuery.browser.webkit ) {
784 jQuery.browser.safari = true;
788 jQuery.inArray = function( elem, array ) {
789 return indexOf.call( array, elem );
793 // Verify that \s matches non-breaking spaces
794 // (IE fails on this test)
795 if ( !/\s/.test( "\xA0" ) ) {
796 trimLeft = /^[\s\xA0]+/;
797 trimRight = /[\s\xA0]+$/;
800 // All jQuery objects should point back to these
801 rootjQuery = jQuery(document);
803 // Cleanup functions for the document ready method
804 if ( document.addEventListener ) {
805 DOMContentLoaded = function() {
806 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
810 } else if ( document.attachEvent ) {
811 DOMContentLoaded = function() {
812 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
813 if ( document.readyState === "complete" ) {
814 document.detachEvent( "onreadystatechange", DOMContentLoaded );
820 // The DOM ready check for Internet Explorer
821 function doScrollCheck() {
822 if ( jQuery.isReady ) {
827 // If IE is used, use the trick by Diego Perini
828 // http://javascript.nwbox.com/IEContentLoaded/
829 document.documentElement.doScroll("left");
831 setTimeout( doScrollCheck, 1 );
835 // and execute any waiting functions
839 // Expose jQuery to the global object
840 return (window.jQuery = window.$ = jQuery);