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 // [[Class]] -> type pairs
77 jQuery.fn = jQuery.prototype = {
78 init: function( selector, context ) {
79 var match, elem, ret, doc;
81 // Handle $(""), $(null), or $(undefined)
86 // Handle $(DOMElement)
87 if ( selector.nodeType ) {
88 this.context = this[0] = selector;
93 // The body element only exists once, optimize finding it
94 if ( selector === "body" && !context ) {
95 this.context = document;
96 this[0] = document.body;
97 this.selector = "body";
102 // Handle HTML strings
103 if ( typeof selector === "string" ) {
104 // Are we dealing with HTML string or an ID?
105 match = quickExpr.exec( selector );
107 // Verify a match, and that no context was specified for #id
108 if ( match && (match[1] || !context) ) {
110 // HANDLE: $(html) -> $(array)
112 doc = (context ? context.ownerDocument || context : document);
114 // If a single string is passed in and it's a single tag
115 // just do a createElement and skip the rest
116 ret = rsingleTag.exec( selector );
119 if ( jQuery.isPlainObject( context ) ) {
120 selector = [ document.createElement( ret[1] ) ];
121 jQuery.fn.attr.call( selector, context, true );
124 selector = [ doc.createElement( ret[1] ) ];
128 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
129 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
132 return jQuery.merge( this, selector );
136 elem = document.getElementById( match[2] );
138 // Check parentNode to catch when Blackberry 4.6 returns
139 // nodes that are no longer in the document #6963
140 if ( elem && elem.parentNode ) {
141 // Handle the case where IE and Opera return items
142 // by name instead of ID
143 if ( elem.id !== match[2] ) {
144 return rootjQuery.find( selector );
147 // Otherwise, we inject the element directly into the jQuery object
152 this.context = document;
153 this.selector = selector;
158 } else if ( !context && !rnonword.test( selector ) ) {
159 this.selector = selector;
160 this.context = document;
161 selector = document.getElementsByTagName( selector );
162 return jQuery.merge( this, selector );
164 // HANDLE: $(expr, $(...))
165 } else if ( !context || context.jquery ) {
166 return (context || rootjQuery).find( selector );
168 // HANDLE: $(expr, context)
169 // (which is just equivalent to: $(context).find(expr)
171 return jQuery( context ).find( selector );
174 // HANDLE: $(function)
175 // Shortcut for document ready
176 } else if ( jQuery.isFunction( selector ) ) {
177 return rootjQuery.ready( selector );
180 if (selector.selector !== undefined) {
181 this.selector = selector.selector;
182 this.context = selector.context;
185 return jQuery.makeArray( selector, this );
188 // Start with an empty selector
191 // The current version of jQuery being used
194 // The default length of a jQuery object is 0
197 // The number of elements contained in the matched element set
202 toArray: function() {
203 return slice.call( this, 0 );
206 // Get the Nth element in the matched element set OR
207 // Get the whole matched element set as a clean array
208 get: function( num ) {
211 // Return a 'clean' array
214 // Return just the object
215 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
218 // Take an array of elements and push it onto the stack
219 // (returning the new matched element set)
220 pushStack: function( elems, name, selector ) {
221 // Build a new jQuery matched element set
224 if ( jQuery.isArray( elems ) ) {
225 push.apply( ret, elems );
228 jQuery.merge( ret, elems );
231 // Add the old object onto the stack (as a reference)
232 ret.prevObject = this;
234 ret.context = this.context;
236 if ( name === "find" ) {
237 ret.selector = this.selector + (this.selector ? " " : "") + selector;
239 ret.selector = this.selector + "." + name + "(" + selector + ")";
242 // Return the newly-formed element set
246 // Execute a callback for every element in the matched set.
247 // (You can seed the arguments with an array of args, but this is
248 // only used internally.)
249 each: function( callback, args ) {
250 return jQuery.each( this, callback, args );
253 ready: function( fn ) {
254 // Attach the listeners
257 // If the DOM is already ready
258 if ( jQuery.isReady ) {
259 // Execute the function immediately
260 fn.call( document, jQuery );
262 // Otherwise, remember the function for later
263 } else if ( readyList ) {
264 // Add the function to the wait list
265 readyList.push( fn );
274 this.slice( i, +i + 1 );
282 return this.eq( -1 );
286 return this.pushStack( slice.apply( this, arguments ),
287 "slice", slice.call(arguments).join(",") );
290 map: function( callback ) {
291 return this.pushStack( jQuery.map(this, function( elem, i ) {
292 return callback.call( elem, i, elem );
297 return this.prevObject || jQuery(null);
300 // For internal use only.
301 // Behaves like an Array's method, not like a jQuery method.
307 // Give the init function the jQuery prototype for later instantiation
308 jQuery.fn.init.prototype = jQuery.fn;
310 jQuery.extend = jQuery.fn.extend = function() {
311 // copy reference to target object
312 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy, copyIsArray;
314 // Handle a deep copy situation
315 if ( typeof target === "boolean" ) {
317 target = arguments[1] || {};
318 // skip the boolean and the target
322 // Handle case when target is a string or something (possible in deep copy)
323 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
327 // extend jQuery itself if only one argument is passed
328 if ( length === i ) {
333 for ( ; i < length; i++ ) {
334 // Only deal with non-null/undefined values
335 if ( (options = arguments[ i ]) != null ) {
336 // Extend the base object
337 for ( name in options ) {
338 src = target[ name ];
339 copy = options[ name ];
341 // Prevent never-ending loop
342 if ( target === copy ) {
346 // Recurse if we're merging plain objects or arrays
347 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
350 clone = src && jQuery.isArray(src) ? src : [];
353 clone = src && jQuery.isPlainObject(src) ? src : {};
356 // Never move original objects, clone them
357 target[ name ] = jQuery.extend( deep, clone, copy );
359 // Don't bring in undefined values
360 } else if ( copy !== undefined ) {
361 target[ name ] = copy;
367 // Return the modified object
372 noConflict: function( deep ) {
376 window.jQuery = _jQuery;
382 // Is the DOM ready to be used? Set to true once it occurs.
385 // A counter to track how many items to wait for before
386 // the ready event fires. See #6781
389 // Handle when the DOM is ready
390 ready: function( wait ) {
391 // A third-party is pushing the ready event forwards
392 if ( wait === true ) {
396 // Make sure that the DOM is not already loaded
397 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
398 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
399 if ( !document.body ) {
400 return setTimeout( jQuery.ready, 1 );
403 // Remember that the DOM is ready
404 jQuery.isReady = true;
406 // If a normal DOM Ready event fired, decrement, and wait if need be
407 if ( wait !== true && --jQuery.readyWait > 0 ) {
411 // If there are functions bound, to execute
413 // Execute all of them
415 while ( (fn = readyList[ i++ ]) ) {
416 fn.call( document, jQuery );
419 // Reset the list of functions
423 // Trigger any bound ready events
424 if ( jQuery.fn.triggerHandler ) {
425 jQuery( document ).triggerHandler( "ready" );
430 bindReady: function() {
437 // Catch cases where $(document).ready() is called after the
438 // browser event has already occurred.
439 if ( document.readyState === "complete" ) {
440 // Handle it asynchronously to allow scripts the opportunity to delay ready
441 return setTimeout( jQuery.ready, 1 );
444 // Mozilla, Opera and webkit nightlies currently support this event
445 if ( document.addEventListener ) {
446 // Use the handy event callback
447 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
449 // A fallback to window.onload, that will always work
450 window.addEventListener( "load", jQuery.ready, false );
452 // If IE event model is used
453 } else if ( document.attachEvent ) {
454 // ensure firing before onload,
455 // maybe late but safe also for iframes
456 document.attachEvent("onreadystatechange", DOMContentLoaded);
458 // A fallback to window.onload, that will always work
459 window.attachEvent( "onload", jQuery.ready );
461 // If IE and not a frame
462 // continually check to see if the document is ready
463 var toplevel = false;
466 toplevel = window.frameElement == null;
469 if ( document.documentElement.doScroll && toplevel ) {
475 // See test/unit/core.js for details concerning isFunction.
476 // Since version 1.3, DOM methods and functions like alert
477 // aren't supported. They return false on IE (#2968).
478 isFunction: function( obj ) {
479 return jQuery.type(obj) === "function";
482 isArray: Array.isArray || function( obj ) {
483 return jQuery.type(obj) === "array";
486 // A crude way of determining if an object is a window
487 isWindow: function( obj ) {
488 return obj && typeof obj === "object" && "setInterval" in obj;
491 type: function( obj ) {
494 class2type[ toString.call(obj) ] || "object";
497 isPlainObject: function( obj ) {
498 // Must be an Object.
499 // Because of IE, we also have to check the presence of the constructor property.
500 // Make sure that DOM nodes and window objects don't pass through, as well
501 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
505 // Not own constructor property must be Object
506 if ( obj.constructor &&
507 !hasOwn.call(obj, "constructor") &&
508 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
512 // Own properties are enumerated firstly, so to speed up,
513 // if last one is own, then all properties are own.
516 for ( key in obj ) {}
518 return key === undefined || hasOwn.call( obj, key );
521 isEmptyObject: function( obj ) {
522 for ( var name in obj ) {
528 error: function( msg ) {
532 parseJSON: function( data ) {
533 if ( typeof data !== "string" || !data ) {
537 // Make sure leading/trailing whitespace is removed (IE can't handle it)
538 data = jQuery.trim( data );
540 // Make sure the incoming data is actual JSON
541 // Logic borrowed from http://json.org/json2.js
542 if ( rvalidchars.test(data.replace(rvalidescape, "@")
543 .replace(rvalidtokens, "]")
544 .replace(rvalidbraces, "")) ) {
546 // Try to use the native JSON parser first
547 return window.JSON && window.JSON.parse ?
548 window.JSON.parse( data ) :
549 (new Function("return " + data))();
552 jQuery.error( "Invalid JSON: " + data );
558 // Evalulates a script in a global context
559 globalEval: function( data ) {
560 if ( data && rnotwhite.test(data) ) {
561 // Inspired by code by Andrea Giammarchi
562 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
563 var head = document.getElementsByTagName("head")[0] || document.documentElement,
564 script = document.createElement("script");
566 script.type = "text/javascript";
568 if ( jQuery.support.scriptEval ) {
569 script.appendChild( document.createTextNode( data ) );
574 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
575 // This arises when a base node is used (#2709).
576 head.insertBefore( script, head.firstChild );
577 head.removeChild( script );
581 nodeName: function( elem, name ) {
582 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
585 // args is for internal usage only
586 each: function( object, callback, args ) {
588 length = object.length,
589 isObj = length === undefined || jQuery.isFunction(object);
593 for ( name in object ) {
594 if ( callback.apply( object[ name ], args ) === false ) {
599 for ( ; i < length; ) {
600 if ( callback.apply( object[ i++ ], args ) === false ) {
606 // A special, fast, case for the most common use of each
609 for ( name in object ) {
610 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
615 for ( var value = object[0];
616 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
623 // Use native String.trim function wherever possible
626 return text == null ?
631 // Otherwise use our own trimming functionality
633 return text == null ?
635 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
638 // results is for internal usage only
639 makeArray: function( array, results ) {
640 var ret = results || [];
642 if ( array != null ) {
643 // The window, strings (and functions) also have 'length'
644 // The extra typeof function check is to prevent crashes
645 // in Safari 2 (See: #3039)
646 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
647 var type = jQuery.type(array);
649 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
650 push.call( ret, array );
652 jQuery.merge( ret, array );
659 inArray: function( elem, array ) {
660 if ( array.indexOf ) {
661 return array.indexOf( elem );
664 for ( var i = 0, length = array.length; i < length; i++ ) {
665 if ( array[ i ] === elem ) {
673 merge: function( first, second ) {
674 var i = first.length, j = 0;
676 if ( typeof second.length === "number" ) {
677 for ( var l = second.length; j < l; j++ ) {
678 first[ i++ ] = second[ j ];
682 while ( second[j] !== undefined ) {
683 first[ i++ ] = second[ j++ ];
692 grep: function( elems, callback, inv ) {
693 var ret = [], retVal;
696 // Go through the array, only saving the items
697 // that pass the validator function
698 for ( var i = 0, length = elems.length; i < length; i++ ) {
699 retVal = !!callback( elems[ i ], i );
700 if ( inv !== retVal ) {
701 ret.push( elems[ i ] );
708 // arg is for internal usage only
709 map: function( elems, callback, arg ) {
712 // Go through the array, translating each of the items to their
713 // new value (or values).
714 for ( var i = 0, length = elems.length; i < length; i++ ) {
715 value = callback( elems[ i ], i, arg );
717 if ( value != null ) {
718 ret[ ret.length ] = value;
722 return ret.concat.apply( [], ret );
725 // A global GUID counter for objects
728 proxy: function( fn, proxy, thisObject ) {
729 if ( arguments.length === 2 ) {
730 if ( typeof proxy === "string" ) {
732 fn = thisObject[ proxy ];
735 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
741 if ( !proxy && fn ) {
743 return fn.apply( thisObject || this, arguments );
747 // Set the guid of unique handler to the same of original handler, so it can be removed
749 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
752 // So proxy can be declared as an argument
756 // Mutifunctional method to get and set values to a collection
757 // The value/s can be optionally by executed if its a function
758 access: function( elems, key, value, exec, fn, pass ) {
759 var length = elems.length;
761 // Setting many attributes
762 if ( typeof key === "object" ) {
763 for ( var k in key ) {
764 jQuery.access( elems, k, key[k], exec, fn, value );
769 // Setting one attribute
770 if ( value !== undefined ) {
771 // Optionally, function values get executed if exec is true
772 exec = !pass && exec && jQuery.isFunction(value);
774 for ( var i = 0; i < length; i++ ) {
775 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
781 // Getting an attribute
782 return length ? fn( elems[0], key ) : undefined;
786 return (new Date()).getTime();
789 // Use of jQuery.browser is frowned upon.
790 // More details: http://docs.jquery.com/Utilities/jQuery.browser
791 uaMatch: function( ua ) {
792 ua = ua.toLowerCase();
794 var match = rwebkit.exec( ua ) ||
797 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
800 return { browser: match[1] || "", version: match[2] || "0" };
806 // Populate the class2type map
807 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
808 class2type[ "[object " + name + "]" ] = name.toLowerCase();
811 browserMatch = jQuery.uaMatch( userAgent );
812 if ( browserMatch.browser ) {
813 jQuery.browser[ browserMatch.browser ] = true;
814 jQuery.browser.version = browserMatch.version;
817 // Deprecated, use jQuery.browser.webkit instead
818 if ( jQuery.browser.webkit ) {
819 jQuery.browser.safari = true;
823 jQuery.inArray = function( elem, array ) {
824 return indexOf.call( array, elem );
828 // Verify that \s matches non-breaking spaces
829 // (IE fails on this test)
830 if ( !rwhite.test( "\xA0" ) ) {
831 trimLeft = /^[\s\xA0]+/;
832 trimRight = /[\s\xA0]+$/;
835 // All jQuery objects should point back to these
836 rootjQuery = jQuery(document);
838 // Cleanup functions for the document ready method
839 if ( document.addEventListener ) {
840 DOMContentLoaded = function() {
841 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
845 } else if ( document.attachEvent ) {
846 DOMContentLoaded = function() {
847 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
848 if ( document.readyState === "complete" ) {
849 document.detachEvent( "onreadystatechange", DOMContentLoaded );
855 // The DOM ready check for Internet Explorer
856 function doScrollCheck() {
857 if ( jQuery.isReady ) {
862 // If IE is used, use the trick by Diego Perini
863 // http://javascript.nwbox.com/IEContentLoaded/
864 document.documentElement.doScroll("left");
866 setTimeout( doScrollCheck, 1 );
870 // and execute any waiting functions
874 // Expose jQuery to the global object
875 return (window.jQuery = window.$ = jQuery);