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 // Use the correct document accordingly with window argument (sandbox)
16 //document = window.document,
18 // A central reference to the root jQuery(document)
21 // A simple way to check for HTML strings or ID strings
22 // (both of which we optimize for)
23 quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w\-]+)$/,
25 // Is it a simple selector
26 isSimple = /^.[^:#\[\.,]*$/,
28 // Check if a string has a non-whitespace character in it
31 // Used for trimming whitespace
35 // Match a standalone tag
36 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
38 // Keep a UserAgent string for use with jQuery.browser
39 userAgent = navigator.userAgent,
41 // For matching the engine and version of the browser
44 // Has the ready events already been bound?
47 // The functions to execute on DOM ready
50 // The ready event handler
53 // Save a reference to some core methods
54 toString = Object.prototype.toString,
55 hasOwn = Object.prototype.hasOwnProperty,
56 push = Array.prototype.push,
57 slice = Array.prototype.slice,
58 trim = String.prototype.trim,
59 indexOf = Array.prototype.indexOf;
61 jQuery.fn = jQuery.prototype = {
62 init: function( selector, context ) {
63 var match, elem, ret, doc;
65 // Handle $(""), $(null), or $(undefined)
70 // Handle $(DOMElement)
71 if ( selector.nodeType ) {
72 this.context = this[0] = selector;
77 // The body element only exists once, optimize finding it
78 if ( selector === "body" && !context ) {
79 this.context = document;
80 this[0] = document.body;
81 this.selector = "body";
86 // Handle HTML strings
87 if ( typeof selector === "string" ) {
88 // Are we dealing with HTML string or an ID?
89 match = quickExpr.exec( selector );
91 // Verify a match, and that no context was specified for #id
92 if ( match && (match[1] || !context) ) {
94 // HANDLE: $(html) -> $(array)
96 doc = (context ? context.ownerDocument || context : document);
98 // If a single string is passed in and it's a single tag
99 // just do a createElement and skip the rest
100 ret = rsingleTag.exec( selector );
103 if ( jQuery.isPlainObject( context ) ) {
104 selector = [ document.createElement( ret[1] ) ];
105 jQuery.fn.attr.call( selector, context, true );
108 selector = [ doc.createElement( ret[1] ) ];
112 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
113 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
116 return jQuery.merge( this, selector );
120 elem = document.getElementById( match[2] );
122 // Check parentNode to catch when Blackberry 4.6 returns
123 // nodes that are no longer in the document #6963
124 if ( elem && elem.parentNode ) {
125 // Handle the case where IE and Opera return items
126 // by name instead of ID
127 if ( elem.id !== match[2] ) {
128 return rootjQuery.find( selector );
131 // Otherwise, we inject the element directly into the jQuery object
136 this.context = document;
137 this.selector = selector;
142 } else if ( !context && /^\w+$/.test( selector ) ) {
143 this.selector = selector;
144 this.context = document;
145 selector = document.getElementsByTagName( selector );
146 return jQuery.merge( this, selector );
148 // HANDLE: $(expr, $(...))
149 } else if ( !context || context.jquery ) {
150 return (context || rootjQuery).find( selector );
152 // HANDLE: $(expr, context)
153 // (which is just equivalent to: $(context).find(expr)
155 return jQuery( context ).find( selector );
158 // HANDLE: $(function)
159 // Shortcut for document ready
160 } else if ( jQuery.isFunction( selector ) ) {
161 return rootjQuery.ready( selector );
164 if (selector.selector !== undefined) {
165 this.selector = selector.selector;
166 this.context = selector.context;
169 return jQuery.makeArray( selector, this );
172 // Start with an empty selector
175 // The current version of jQuery being used
178 // The default length of a jQuery object is 0
181 // The number of elements contained in the matched element set
186 toArray: function() {
187 return slice.call( this, 0 );
190 // Get the Nth element in the matched element set OR
191 // Get the whole matched element set as a clean array
192 get: function( num ) {
195 // Return a 'clean' array
198 // Return just the object
199 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
202 // Take an array of elements and push it onto the stack
203 // (returning the new matched element set)
204 pushStack: function( elems, name, selector ) {
205 // Build a new jQuery matched element set
208 if ( jQuery.isArray( elems ) ) {
209 push.apply( ret, elems );
212 jQuery.merge( ret, elems );
215 // Add the old object onto the stack (as a reference)
216 ret.prevObject = this;
218 ret.context = this.context;
220 if ( name === "find" ) {
221 ret.selector = this.selector + (this.selector ? " " : "") + selector;
223 ret.selector = this.selector + "." + name + "(" + selector + ")";
226 // Return the newly-formed element set
230 // Execute a callback for every element in the matched set.
231 // (You can seed the arguments with an array of args, but this is
232 // only used internally.)
233 each: function( callback, args ) {
234 return jQuery.each( this, callback, args );
237 ready: function( fn ) {
238 // Attach the listeners
241 // If the DOM is already ready
242 if ( jQuery.isReady ) {
243 // Execute the function immediately
244 fn.call( document, jQuery );
246 // Otherwise, remember the function for later
247 } else if ( readyList ) {
248 // Add the function to the wait list
249 readyList.push( fn );
258 this.slice( i, +i + 1 );
266 return this.eq( -1 );
270 return this.pushStack( slice.apply( this, arguments ),
271 "slice", slice.call(arguments).join(",") );
274 map: function( callback ) {
275 return this.pushStack( jQuery.map(this, function( elem, i ) {
276 return callback.call( elem, i, elem );
281 return this.prevObject || jQuery(null);
284 // For internal use only.
285 // Behaves like an Array's method, not like a jQuery method.
291 // Give the init function the jQuery prototype for later instantiation
292 jQuery.fn.init.prototype = jQuery.fn;
294 jQuery.extend = jQuery.fn.extend = function() {
295 // copy reference to target object
296 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
298 // Handle a deep copy situation
299 if ( typeof target === "boolean" ) {
301 target = arguments[1] || {};
302 // skip the boolean and the target
306 // Handle case when target is a string or something (possible in deep copy)
307 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
311 // extend jQuery itself if only one argument is passed
312 if ( length === i ) {
317 for ( ; i < length; i++ ) {
318 // Only deal with non-null/undefined values
319 if ( (options = arguments[ i ]) != null ) {
320 // Extend the base object
321 for ( name in options ) {
322 src = target[ name ];
323 copy = options[ name ];
325 // Prevent never-ending loop
326 if ( target === copy ) {
330 // Recurse if we're merging object literal values or arrays
331 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
332 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
333 : jQuery.isArray(copy) ? [] : {};
335 // Never move original objects, clone them
336 target[ name ] = jQuery.extend( deep, clone, copy );
338 // Don't bring in undefined values
339 } else if ( copy !== undefined ) {
340 target[ name ] = copy;
346 // Return the modified object
351 noConflict: function( deep ) {
355 window.jQuery = _jQuery;
361 // Is the DOM ready to be used? Set to true once it occurs.
364 // A counter to track how many items to wait for before
365 // the ready event fires. See #6781
368 // Handle when the DOM is ready
369 ready: function( wait ) {
370 // A third-party is pushing the ready event forwards
371 if ( wait === true ) {
375 // Make sure that the DOM is not already loaded
376 if ( !jQuery.readyWait && !jQuery.isReady ) {
377 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
378 if ( !document.body ) {
379 return setTimeout( jQuery.ready, 13 );
382 // Remember that the DOM is ready
383 jQuery.isReady = true;
385 // If a normal DOM Ready event fired, decrement, and wait if need be
386 if ( wait !== true && --jQuery.readyWait > 0 ) {
390 // If there are functions bound, to execute
392 // Execute all of them
394 while ( (fn = readyList[ i++ ]) ) {
395 fn.call( document, jQuery );
398 // Reset the list of functions
402 // Trigger any bound ready events
403 if ( jQuery.fn.triggerHandler ) {
404 jQuery( document ).triggerHandler( "ready" );
409 bindReady: function() {
416 // Catch cases where $(document).ready() is called after the
417 // browser event has already occurred.
418 if ( document.readyState === "complete" ) {
419 return jQuery.ready();
422 // Mozilla, Opera and webkit nightlies currently support this event
423 if ( document.addEventListener ) {
424 // Use the handy event callback
425 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
427 // A fallback to window.onload, that will always work
428 window.addEventListener( "load", jQuery.ready, false );
430 // If IE event model is used
431 } else if ( document.attachEvent ) {
432 // ensure firing before onload,
433 // maybe late but safe also for iframes
434 document.attachEvent("onreadystatechange", DOMContentLoaded);
436 // A fallback to window.onload, that will always work
437 window.attachEvent( "onload", jQuery.ready );
439 // If IE and not a frame
440 // continually check to see if the document is ready
441 var toplevel = false;
444 toplevel = window.frameElement == null;
447 if ( document.documentElement.doScroll && toplevel ) {
453 // See test/unit/core.js for details concerning isFunction.
454 // Since version 1.3, DOM methods and functions like alert
455 // aren't supported. They return false on IE (#2968).
456 isFunction: function( obj ) {
457 return jQuery.type(obj) === "function";
460 isArray: Array.isArray || function( obj ) {
461 return jQuery.type(obj) === "array";
464 type: function( obj ) {
467 toString.call(obj).slice(8, -1).toLowerCase();
470 isPlainObject: function( obj ) {
471 // Must be an Object.
472 // Because of IE, we also have to check the presence of the constructor property.
473 // Make sure that DOM nodes and window objects don't pass through, as well
474 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) {
478 // Not own constructor property must be Object
479 if ( obj.constructor &&
480 !hasOwn.call(obj, "constructor") &&
481 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
485 // Own properties are enumerated firstly, so to speed up,
486 // if last one is own, then all properties are own.
489 for ( key in obj ) {}
491 return key === undefined || hasOwn.call( obj, key );
494 isEmptyObject: function( obj ) {
495 for ( var name in obj ) {
501 error: function( msg ) {
505 parseJSON: function( data ) {
506 if ( typeof data !== "string" || !data ) {
510 // Make sure leading/trailing whitespace is removed (IE can't handle it)
511 data = jQuery.trim( data );
513 // Make sure the incoming data is actual JSON
514 // Logic borrowed from http://json.org/json2.js
515 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
516 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
517 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
519 // Try to use the native JSON parser first
520 return window.JSON && window.JSON.parse ?
521 window.JSON.parse( data ) :
522 (new Function("return " + data))();
525 jQuery.error( "Invalid JSON: " + data );
531 // Evalulates a script in a global context
532 globalEval: function( data ) {
533 if ( data && rnotwhite.test(data) ) {
534 // Inspired by code by Andrea Giammarchi
535 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
536 var head = document.getElementsByTagName("head")[0] || document.documentElement,
537 script = document.createElement("script");
539 script.type = "text/javascript";
541 if ( jQuery.support.scriptEval ) {
542 script.appendChild( document.createTextNode( data ) );
547 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
548 // This arises when a base node is used (#2709).
549 head.insertBefore( script, head.firstChild );
550 head.removeChild( script );
554 nodeName: function( elem, name ) {
555 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
558 // args is for internal usage only
559 each: function( object, callback, args ) {
561 length = object.length,
562 isObj = length === undefined || jQuery.isFunction(object);
566 for ( name in object ) {
567 if ( callback.apply( object[ name ], args ) === false ) {
572 for ( ; i < length; ) {
573 if ( callback.apply( object[ i++ ], args ) === false ) {
579 // A special, fast, case for the most common use of each
582 for ( name in object ) {
583 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
588 for ( var value = object[0];
589 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
596 // Use native String.trim function wherever possible
599 return text == null ?
604 // Otherwise use our own trimming functionality
606 return text == null ?
608 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
611 // results is for internal usage only
612 makeArray: function( array, results ) {
613 var ret = results || [];
615 if ( array != null ) {
616 // The window, strings (and functions) also have 'length'
617 // The extra typeof function check is to prevent crashes
618 // in Safari 2 (See: #3039)
619 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
620 var type = jQuery.type(array);
622 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || "setInterval" in array ) {
623 push.call( ret, array );
625 jQuery.merge( ret, array );
632 inArray: function( elem, array ) {
633 if ( array.indexOf ) {
634 return array.indexOf( elem );
637 for ( var i = 0, length = array.length; i < length; i++ ) {
638 if ( array[ i ] === elem ) {
646 merge: function( first, second ) {
647 var i = first.length, j = 0;
649 if ( typeof second.length === "number" ) {
650 for ( var l = second.length; j < l; j++ ) {
651 first[ i++ ] = second[ j ];
655 while ( second[j] !== undefined ) {
656 first[ i++ ] = second[ j++ ];
665 grep: function( elems, callback, inv ) {
666 var ret = [], retVal;
669 // Go through the array, only saving the items
670 // that pass the validator function
671 for ( var i = 0, length = elems.length; i < length; i++ ) {
672 retVal = !!callback( elems[ i ], i );
673 if ( inv !== retVal ) {
674 ret.push( elems[ i ] );
681 // arg is for internal usage only
682 map: function( elems, callback, arg ) {
685 // Go through the array, translating each of the items to their
686 // new value (or values).
687 for ( var i = 0, length = elems.length; i < length; i++ ) {
688 value = callback( elems[ i ], i, arg );
690 if ( value != null ) {
691 ret[ ret.length ] = value;
695 return ret.concat.apply( [], ret );
698 // A global GUID counter for objects
701 proxy: function( fn, proxy, thisObject ) {
702 if ( arguments.length === 2 ) {
703 if ( typeof proxy === "string" ) {
705 fn = thisObject[ proxy ];
708 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
714 if ( !proxy && fn ) {
716 return fn.apply( thisObject || this, arguments );
720 // Set the guid of unique handler to the same of original handler, so it can be removed
722 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
725 // So proxy can be declared as an argument
729 // Mutifunctional method to get and set values to a collection
730 // The value/s can be optionally by executed if its a function
731 access: function( elems, key, value, exec, fn, pass ) {
732 var length = elems.length;
734 // Setting many attributes
735 if ( typeof key === "object" ) {
736 for ( var k in key ) {
737 jQuery.access( elems, k, key[k], exec, fn, value );
742 // Setting one attribute
743 if ( value !== undefined ) {
744 // Optionally, function values get executed if exec is true
745 exec = !pass && exec && jQuery.isFunction(value);
747 for ( var i = 0; i < length; i++ ) {
748 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
754 // Getting an attribute
755 return length ? fn( elems[0], key ) : undefined;
759 return (new Date()).getTime();
762 // Use of jQuery.browser is frowned upon.
763 // More details: http://docs.jquery.com/Utilities/jQuery.browser
764 uaMatch: function( ua ) {
765 ua = ua.toLowerCase();
767 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
768 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
769 /(msie) ([\w.]+)/.exec( ua ) ||
770 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
773 return { browser: match[1] || "", version: match[2] || "0" };
779 browserMatch = jQuery.uaMatch( userAgent );
780 if ( browserMatch.browser ) {
781 jQuery.browser[ browserMatch.browser ] = true;
782 jQuery.browser.version = browserMatch.version;
785 // Deprecated, use jQuery.browser.webkit instead
786 if ( jQuery.browser.webkit ) {
787 jQuery.browser.safari = true;
791 jQuery.inArray = function( elem, array ) {
792 return indexOf.call( array, elem );
796 // Verify that \s matches non-breaking spaces
797 // (IE fails on this test)
798 if ( !/\s/.test( "\xA0" ) ) {
799 trimLeft = /^[\s\xA0]+/;
800 trimRight = /[\s\xA0]+$/;
803 // All jQuery objects should point back to these
804 rootjQuery = jQuery(document);
806 // Cleanup functions for the document ready method
807 if ( document.addEventListener ) {
808 DOMContentLoaded = function() {
809 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
813 } else if ( document.attachEvent ) {
814 DOMContentLoaded = function() {
815 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
816 if ( document.readyState === "complete" ) {
817 document.detachEvent( "onreadystatechange", DOMContentLoaded );
823 // The DOM ready check for Internet Explorer
824 function doScrollCheck() {
825 if ( jQuery.isReady ) {
830 // If IE is used, use the trick by Diego Perini
831 // http://javascript.nwbox.com/IEContentLoaded/
832 document.documentElement.doScroll("left");
834 setTimeout( doScrollCheck, 1 );
838 // and execute any waiting functions
842 // Expose jQuery to the global object
843 return (window.jQuery = window.$ = jQuery);