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 // Handle when the DOM is ready
366 // Make sure that the DOM is not already loaded
367 if ( !jQuery.isReady ) {
368 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
369 if ( !document.body ) {
370 return setTimeout( jQuery.ready, 13 );
373 // Remember that the DOM is ready
374 jQuery.isReady = true;
376 // If there are functions bound, to execute
378 // Execute all of them
380 while ( (fn = readyList[ i++ ]) ) {
381 fn.call( document, jQuery );
384 // Reset the list of functions
388 // Trigger any bound ready events
389 if ( jQuery.fn.triggerHandler ) {
390 jQuery( document ).triggerHandler( "ready" );
395 bindReady: function() {
402 // Catch cases where $(document).ready() is called after the
403 // browser event has already occurred.
404 if ( document.readyState === "complete" ) {
405 return jQuery.ready();
408 // Mozilla, Opera and webkit nightlies currently support this event
409 if ( document.addEventListener ) {
410 // Use the handy event callback
411 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
413 // A fallback to window.onload, that will always work
414 window.addEventListener( "load", jQuery.ready, false );
416 // If IE event model is used
417 } else if ( document.attachEvent ) {
418 // ensure firing before onload,
419 // maybe late but safe also for iframes
420 document.attachEvent("onreadystatechange", DOMContentLoaded);
422 // A fallback to window.onload, that will always work
423 window.attachEvent( "onload", jQuery.ready );
425 // If IE and not a frame
426 // continually check to see if the document is ready
427 var toplevel = false;
430 toplevel = window.frameElement == null;
433 if ( document.documentElement.doScroll && toplevel ) {
439 // See test/unit/core.js for details concerning isFunction.
440 // Since version 1.3, DOM methods and functions like alert
441 // aren't supported. They return false on IE (#2968).
442 isFunction: function( obj ) {
443 return jQuery.type(obj) === "function";
446 isArray: Array.isArray || function( obj ) {
447 return jQuery.type(obj) === "array";
450 type: function( obj ) {
453 toString.call(obj).slice(8, -1).toLowerCase();
456 isPlainObject: function( obj ) {
457 // Must be an Object.
458 // Because of IE, we also have to check the presence of the constructor property.
459 // Make sure that DOM nodes and window objects don't pass through, as well
460 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) {
464 // Not own constructor property must be Object
465 if ( obj.constructor &&
466 !hasOwn.call(obj, "constructor") &&
467 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
471 // Own properties are enumerated firstly, so to speed up,
472 // if last one is own, then all properties are own.
475 for ( key in obj ) {}
477 return key === undefined || hasOwn.call( obj, key );
480 isEmptyObject: function( obj ) {
481 for ( var name in obj ) {
487 error: function( msg ) {
491 parseJSON: function( data ) {
492 if ( typeof data !== "string" || !data ) {
496 // Make sure leading/trailing whitespace is removed (IE can't handle it)
497 data = jQuery.trim( data );
499 // Make sure the incoming data is actual JSON
500 // Logic borrowed from http://json.org/json2.js
501 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
502 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
503 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
505 // Try to use the native JSON parser first
506 return window.JSON && window.JSON.parse ?
507 window.JSON.parse( data ) :
508 (new Function("return " + data))();
511 jQuery.error( "Invalid JSON: " + data );
517 // Evalulates a script in a global context
518 globalEval: function( data ) {
519 if ( data && rnotwhite.test(data) ) {
520 // Inspired by code by Andrea Giammarchi
521 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
522 var head = document.getElementsByTagName("head")[0] || document.documentElement,
523 script = document.createElement("script");
525 script.type = "text/javascript";
527 if ( jQuery.support.scriptEval ) {
528 script.appendChild( document.createTextNode( data ) );
533 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
534 // This arises when a base node is used (#2709).
535 head.insertBefore( script, head.firstChild );
536 head.removeChild( script );
540 nodeName: function( elem, name ) {
541 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
544 // args is for internal usage only
545 each: function( object, callback, args ) {
547 length = object.length,
548 isObj = length === undefined || jQuery.isFunction(object);
552 for ( name in object ) {
553 if ( callback.apply( object[ name ], args ) === false ) {
558 for ( ; i < length; ) {
559 if ( callback.apply( object[ i++ ], args ) === false ) {
565 // A special, fast, case for the most common use of each
568 for ( name in object ) {
569 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
574 for ( var value = object[0];
575 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
582 // Use native String.trim function wherever possible
585 return text == null ?
590 // Otherwise use our own trimming functionality
592 return text == null ?
594 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
597 // results is for internal usage only
598 makeArray: function( array, results ) {
599 var ret = results || [];
601 if ( array != null ) {
602 // The window, strings (and functions) also have 'length'
603 // The extra typeof function check is to prevent crashes
604 // in Safari 2 (See: #3039)
605 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
606 var type = jQuery.type(array);
608 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || "setInterval" in array ) {
609 push.call( ret, array );
611 jQuery.merge( ret, array );
618 inArray: function( elem, array ) {
619 if ( array.indexOf ) {
620 return array.indexOf( elem );
623 for ( var i = 0, length = array.length; i < length; i++ ) {
624 if ( array[ i ] === elem ) {
632 merge: function( first, second ) {
633 var i = first.length, j = 0;
635 if ( typeof second.length === "number" ) {
636 for ( var l = second.length; j < l; j++ ) {
637 first[ i++ ] = second[ j ];
641 while ( second[j] !== undefined ) {
642 first[ i++ ] = second[ j++ ];
651 grep: function( elems, callback, inv ) {
652 var ret = [], retVal;
655 // Go through the array, only saving the items
656 // that pass the validator function
657 for ( var i = 0, length = elems.length; i < length; i++ ) {
658 retVal = !!callback( elems[ i ], i );
659 if ( inv !== retVal ) {
660 ret.push( elems[ i ] );
667 // arg is for internal usage only
668 map: function( elems, callback, arg ) {
671 // Go through the array, translating each of the items to their
672 // new value (or values).
673 for ( var i = 0, length = elems.length; i < length; i++ ) {
674 value = callback( elems[ i ], i, arg );
676 if ( value != null ) {
677 ret[ ret.length ] = value;
681 return ret.concat.apply( [], ret );
684 // A global GUID counter for objects
687 proxy: function( fn, proxy, thisObject ) {
688 if ( arguments.length === 2 ) {
689 if ( typeof proxy === "string" ) {
691 fn = thisObject[ proxy ];
694 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
700 if ( !proxy && fn ) {
702 return fn.apply( thisObject || this, arguments );
706 // Set the guid of unique handler to the same of original handler, so it can be removed
708 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
711 // So proxy can be declared as an argument
715 // Mutifunctional method to get and set values to a collection
716 // The value/s can be optionally by executed if its a function
717 access: function( elems, key, value, exec, fn, pass ) {
718 var length = elems.length;
720 // Setting many attributes
721 if ( typeof key === "object" ) {
722 for ( var k in key ) {
723 jQuery.access( elems, k, key[k], exec, fn, value );
728 // Setting one attribute
729 if ( value !== undefined ) {
730 // Optionally, function values get executed if exec is true
731 exec = !pass && exec && jQuery.isFunction(value);
733 for ( var i = 0; i < length; i++ ) {
734 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
740 // Getting an attribute
741 return length ? fn( elems[0], key ) : undefined;
745 return (new Date()).getTime();
748 // Use of jQuery.browser is frowned upon.
749 // More details: http://docs.jquery.com/Utilities/jQuery.browser
750 uaMatch: function( ua ) {
751 ua = ua.toLowerCase();
753 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
754 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
755 /(msie) ([\w.]+)/.exec( ua ) ||
756 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
759 return { browser: match[1] || "", version: match[2] || "0" };
765 browserMatch = jQuery.uaMatch( userAgent );
766 if ( browserMatch.browser ) {
767 jQuery.browser[ browserMatch.browser ] = true;
768 jQuery.browser.version = browserMatch.version;
771 // Deprecated, use jQuery.browser.webkit instead
772 if ( jQuery.browser.webkit ) {
773 jQuery.browser.safari = true;
777 jQuery.inArray = function( elem, array ) {
778 return indexOf.call( array, elem );
782 // Verify that \s matches non-breaking spaces
783 // (IE fails on this test)
784 if ( !/\s/.test( "\xA0" ) ) {
785 trimLeft = /^[\s\xA0]+/;
786 trimRight = /[\s\xA0]+$/;
789 // All jQuery objects should point back to these
790 rootjQuery = jQuery(document);
792 // Cleanup functions for the document ready method
793 if ( document.addEventListener ) {
794 DOMContentLoaded = function() {
795 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
799 } else if ( document.attachEvent ) {
800 DOMContentLoaded = function() {
801 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
802 if ( document.readyState === "complete" ) {
803 document.detachEvent( "onreadystatechange", DOMContentLoaded );
809 // The DOM ready check for Internet Explorer
810 function doScrollCheck() {
811 if ( jQuery.isReady ) {
816 // If IE is used, use the trick by Diego Perini
817 // http://javascript.nwbox.com/IEContentLoaded/
818 document.documentElement.doScroll("left");
820 setTimeout( doScrollCheck, 1 );
824 // and execute any waiting functions
828 // Expose jQuery to the global object
829 return window.jQuery = window.$ = jQuery;