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, copyIsArray;
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 plain objects or arrays
344 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
347 clone = src && jQuery.isArray(src) ? src : [];
350 clone = src && jQuery.isPlainObject(src) ? src : {};
353 // Never move original objects, clone them
354 target[ name ] = jQuery.extend( deep, clone, copy );
356 // Don't bring in undefined values
357 } else if ( copy !== undefined ) {
358 target[ name ] = copy;
364 // Return the modified object
369 noConflict: function( deep ) {
373 window.jQuery = _jQuery;
379 // Is the DOM ready to be used? Set to true once it occurs.
382 // A counter to track how many items to wait for before
383 // the ready event fires. See #6781
386 // Handle when the DOM is ready
387 ready: function( wait ) {
388 // A third-party is pushing the ready event forwards
389 if ( wait === true ) {
393 // Make sure that the DOM is not already loaded
394 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
395 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
396 if ( !document.body ) {
397 return setTimeout( jQuery.ready, 13 );
400 // Remember that the DOM is ready
401 jQuery.isReady = true;
403 // If a normal DOM Ready event fired, decrement, and wait if need be
404 if ( wait !== true && --jQuery.readyWait > 0 ) {
408 // If there are functions bound, to execute
410 // Execute all of them
412 while ( (fn = readyList[ i++ ]) ) {
413 fn.call( document, jQuery );
416 // Reset the list of functions
420 // Trigger any bound ready events
421 if ( jQuery.fn.triggerHandler ) {
422 jQuery( document ).triggerHandler( "ready" );
427 bindReady: function() {
434 // Catch cases where $(document).ready() is called after the
435 // browser event has already occurred.
436 if ( document.readyState === "complete" ) {
437 return jQuery.ready();
440 // Mozilla, Opera and webkit nightlies currently support this event
441 if ( document.addEventListener ) {
442 // Use the handy event callback
443 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
445 // A fallback to window.onload, that will always work
446 window.addEventListener( "load", jQuery.ready, false );
448 // If IE event model is used
449 } else if ( document.attachEvent ) {
450 // ensure firing before onload,
451 // maybe late but safe also for iframes
452 document.attachEvent("onreadystatechange", DOMContentLoaded);
454 // A fallback to window.onload, that will always work
455 window.attachEvent( "onload", jQuery.ready );
457 // If IE and not a frame
458 // continually check to see if the document is ready
459 var toplevel = false;
462 toplevel = window.frameElement == null;
465 if ( document.documentElement.doScroll && toplevel ) {
471 // See test/unit/core.js for details concerning isFunction.
472 // Since version 1.3, DOM methods and functions like alert
473 // aren't supported. They return false on IE (#2968).
474 isFunction: function( obj ) {
475 return jQuery.type(obj) === "function";
478 isArray: Array.isArray || function( obj ) {
479 return jQuery.type(obj) === "array";
482 // A crude way of determining if an object is a window
483 isWindow: function( obj ) {
484 return obj && typeof obj === "object" && "setInterval" in obj;
487 type: function( obj ) {
490 toString.call(obj).slice(8, -1).toLowerCase();
493 isPlainObject: function( obj ) {
494 // Must be an Object.
495 // Because of IE, we also have to check the presence of the constructor property.
496 // Make sure that DOM nodes and window objects don't pass through, as well
497 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
501 // Not own constructor property must be Object
502 if ( obj.constructor &&
503 !hasOwn.call(obj, "constructor") &&
504 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
508 // Own properties are enumerated firstly, so to speed up,
509 // if last one is own, then all properties are own.
512 for ( key in obj ) {}
514 return key === undefined || hasOwn.call( obj, key );
517 isEmptyObject: function( obj ) {
518 for ( var name in obj ) {
524 error: function( msg ) {
528 parseJSON: function( data ) {
529 if ( typeof data !== "string" || !data ) {
533 // Make sure leading/trailing whitespace is removed (IE can't handle it)
534 data = jQuery.trim( data );
536 // Make sure the incoming data is actual JSON
537 // Logic borrowed from http://json.org/json2.js
538 if ( rvalidchars.test(data.replace(rvalidescape, "@")
539 .replace(rvalidtokens, "]")
540 .replace(rvalidbraces, "")) ) {
542 // Try to use the native JSON parser first
543 return window.JSON && window.JSON.parse ?
544 window.JSON.parse( data ) :
545 (new Function("return " + data))();
548 jQuery.error( "Invalid JSON: " + data );
554 // Evalulates a script in a global context
555 globalEval: function( data ) {
556 if ( data && rnotwhite.test(data) ) {
557 // Inspired by code by Andrea Giammarchi
558 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
559 var head = document.getElementsByTagName("head")[0] || document.documentElement,
560 script = document.createElement("script");
562 script.type = "text/javascript";
564 if ( jQuery.support.scriptEval ) {
565 script.appendChild( document.createTextNode( data ) );
570 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
571 // This arises when a base node is used (#2709).
572 head.insertBefore( script, head.firstChild );
573 head.removeChild( script );
577 nodeName: function( elem, name ) {
578 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
581 // args is for internal usage only
582 each: function( object, callback, args ) {
584 length = object.length,
585 isObj = length === undefined || jQuery.isFunction(object);
589 for ( name in object ) {
590 if ( callback.apply( object[ name ], args ) === false ) {
595 for ( ; i < length; ) {
596 if ( callback.apply( object[ i++ ], args ) === false ) {
602 // A special, fast, case for the most common use of each
605 for ( name in object ) {
606 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
611 for ( var value = object[0];
612 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
619 // Use native String.trim function wherever possible
622 return text == null ?
627 // Otherwise use our own trimming functionality
629 return text == null ?
631 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
634 // results is for internal usage only
635 makeArray: function( array, results ) {
636 var ret = results || [];
638 if ( array != null ) {
639 // The window, strings (and functions) also have 'length'
640 // The extra typeof function check is to prevent crashes
641 // in Safari 2 (See: #3039)
642 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
643 var type = jQuery.type(array);
645 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
646 push.call( ret, array );
648 jQuery.merge( ret, array );
655 inArray: function( elem, array ) {
656 if ( array.indexOf ) {
657 return array.indexOf( elem );
660 for ( var i = 0, length = array.length; i < length; i++ ) {
661 if ( array[ i ] === elem ) {
669 merge: function( first, second ) {
670 var i = first.length, j = 0;
672 if ( typeof second.length === "number" ) {
673 for ( var l = second.length; j < l; j++ ) {
674 first[ i++ ] = second[ j ];
678 while ( second[j] !== undefined ) {
679 first[ i++ ] = second[ j++ ];
688 grep: function( elems, callback, inv ) {
689 var ret = [], retVal;
692 // Go through the array, only saving the items
693 // that pass the validator function
694 for ( var i = 0, length = elems.length; i < length; i++ ) {
695 retVal = !!callback( elems[ i ], i );
696 if ( inv !== retVal ) {
697 ret.push( elems[ i ] );
704 // arg is for internal usage only
705 map: function( elems, callback, arg ) {
708 // Go through the array, translating each of the items to their
709 // new value (or values).
710 for ( var i = 0, length = elems.length; i < length; i++ ) {
711 value = callback( elems[ i ], i, arg );
713 if ( value != null ) {
714 ret[ ret.length ] = value;
718 return ret.concat.apply( [], ret );
721 // A global GUID counter for objects
724 proxy: function( fn, proxy, thisObject ) {
725 if ( arguments.length === 2 ) {
726 if ( typeof proxy === "string" ) {
728 fn = thisObject[ proxy ];
731 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
737 if ( !proxy && fn ) {
739 return fn.apply( thisObject || this, arguments );
743 // Set the guid of unique handler to the same of original handler, so it can be removed
745 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
748 // So proxy can be declared as an argument
752 // Mutifunctional method to get and set values to a collection
753 // The value/s can be optionally by executed if its a function
754 access: function( elems, key, value, exec, fn, pass ) {
755 var length = elems.length;
757 // Setting many attributes
758 if ( typeof key === "object" ) {
759 for ( var k in key ) {
760 jQuery.access( elems, k, key[k], exec, fn, value );
765 // Setting one attribute
766 if ( value !== undefined ) {
767 // Optionally, function values get executed if exec is true
768 exec = !pass && exec && jQuery.isFunction(value);
770 for ( var i = 0; i < length; i++ ) {
771 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
777 // Getting an attribute
778 return length ? fn( elems[0], key ) : undefined;
782 return (new Date()).getTime();
785 // Use of jQuery.browser is frowned upon.
786 // More details: http://docs.jquery.com/Utilities/jQuery.browser
787 uaMatch: function( ua ) {
788 ua = ua.toLowerCase();
790 var match = rwebkit.exec( ua ) ||
793 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
796 return { browser: match[1] || "", version: match[2] || "0" };
802 browserMatch = jQuery.uaMatch( userAgent );
803 if ( browserMatch.browser ) {
804 jQuery.browser[ browserMatch.browser ] = true;
805 jQuery.browser.version = browserMatch.version;
808 // Deprecated, use jQuery.browser.webkit instead
809 if ( jQuery.browser.webkit ) {
810 jQuery.browser.safari = true;
814 jQuery.inArray = function( elem, array ) {
815 return indexOf.call( array, elem );
819 // Verify that \s matches non-breaking spaces
820 // (IE fails on this test)
821 if ( !rwhite.test( "\xA0" ) ) {
822 trimLeft = /^[\s\xA0]+/;
823 trimRight = /[\s\xA0]+$/;
826 // All jQuery objects should point back to these
827 rootjQuery = jQuery(document);
829 // Cleanup functions for the document ready method
830 if ( document.addEventListener ) {
831 DOMContentLoaded = function() {
832 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
836 } else if ( document.attachEvent ) {
837 DOMContentLoaded = function() {
838 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
839 if ( document.readyState === "complete" ) {
840 document.detachEvent( "onreadystatechange", DOMContentLoaded );
846 // The DOM ready check for Internet Explorer
847 function doScrollCheck() {
848 if ( jQuery.isReady ) {
853 // If IE is used, use the trick by Diego Perini
854 // http://javascript.nwbox.com/IEContentLoaded/
855 document.documentElement.doScroll("left");
857 setTimeout( doScrollCheck, 1 );
861 // and execute any waiting functions
865 // Expose jQuery to the global object
866 return (window.jQuery = window.$ = jQuery);