Fixed the ajax test regarding the jsonp option set to false and added a test to contr...
[jquery.git] / src / core.js
1 var jQuery = (function() {
2
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, rootjQuery );
7         },
8
9         // Map over jQuery in case of overwrite
10         _jQuery = window.jQuery,
11
12         // Map over the $ in case of overwrite
13         _$ = window.$,
14
15         // A central reference to the root jQuery(document)
16         rootjQuery,
17
18         // A simple way to check for HTML strings or ID strings
19         // (both of which we optimize for)
20         quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
21
22         // Is it a simple selector
23         isSimple = /^.[^:#\[\.,]*$/,
24
25         // Check if a string has a non-whitespace character in it
26         rnotwhite = /\S/,
27         rwhite = /\s/,
28
29         // Used for trimming whitespace
30         trimLeft = /^\s+/,
31         trimRight = /\s+$/,
32
33         // Check for non-word characters
34         rnonword = /\W/,
35
36         // Check for digits
37         rdigit = /\d/,
38
39         // Match a standalone tag
40         rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
41
42         // JSON RegExp
43         rvalidchars = /^[\],:{}\s]*$/,
44         rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
45         rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
46         rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
47
48         // Useragent RegExp
49         rwebkit = /(webkit)[ \/]([\w.]+)/,
50         ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
51         rmsie = /(msie) ([\w.]+)/,
52         rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
53
54         // Keep a UserAgent string for use with jQuery.browser
55         userAgent = navigator.userAgent,
56
57         // For matching the engine and version of the browser
58         browserMatch,
59
60         // Has the ready events already been bound?
61         readyBound = false,
62
63         // The deferred used on DOM ready
64         readyList,
65
66         // The ready event handler
67         DOMContentLoaded,
68
69         // Save a reference to some core methods
70         toString = Object.prototype.toString,
71         hasOwn = Object.prototype.hasOwnProperty,
72         push = Array.prototype.push,
73         slice = Array.prototype.slice,
74         trim = String.prototype.trim,
75         indexOf = Array.prototype.indexOf,
76
77         // [[Class]] -> type pairs
78         class2type = {};
79
80 jQuery.fn = jQuery.prototype = {
81         constructor: jQuery,
82         init: function( selector, context, rootjQuery ) {
83                 var match, elem, ret, doc;
84
85                 // Handle $(""), $(null), or $(undefined)
86                 if ( !selector ) {
87                         return this;
88                 }
89
90                 // Handle $(DOMElement)
91                 if ( selector.nodeType ) {
92                         this.context = this[0] = selector;
93                         this.length = 1;
94                         return this;
95                 }
96
97                 // The body element only exists once, optimize finding it
98                 if ( selector === "body" && !context && document.body ) {
99                         this.context = document;
100                         this[0] = document.body;
101                         this.selector = "body";
102                         this.length = 1;
103                         return this;
104                 }
105
106                 // Handle HTML strings
107                 if ( typeof selector === "string" ) {
108                         // Are we dealing with HTML string or an ID?
109                         match = quickExpr.exec( selector );
110
111                         // Verify a match, and that no context was specified for #id
112                         if ( match && (match[1] || !context) ) {
113
114                                 // HANDLE: $(html) -> $(array)
115                                 if ( match[1] ) {
116                                         context = context instanceof jQuery ? context[0] : context;
117                                         doc = (context ? context.ownerDocument || context : document);
118
119                                         // If a single string is passed in and it's a single tag
120                                         // just do a createElement and skip the rest
121                                         ret = rsingleTag.exec( selector );
122
123                                         if ( ret ) {
124                                                 if ( jQuery.isPlainObject( context ) ) {
125                                                         selector = [ document.createElement( ret[1] ) ];
126                                                         jQuery.fn.attr.call( selector, context, true );
127
128                                                 } else {
129                                                         selector = [ doc.createElement( ret[1] ) ];
130                                                 }
131
132                                         } else {
133                                                 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
134                                                 selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
135                                         }
136
137                                         return jQuery.merge( this, selector );
138
139                                 // HANDLE: $("#id")
140                                 } else {
141                                         elem = document.getElementById( match[2] );
142
143                                         // Check parentNode to catch when Blackberry 4.6 returns
144                                         // nodes that are no longer in the document #6963
145                                         if ( elem && elem.parentNode ) {
146                                                 // Handle the case where IE and Opera return items
147                                                 // by name instead of ID
148                                                 if ( elem.id !== match[2] ) {
149                                                         return rootjQuery.find( selector );
150                                                 }
151
152                                                 // Otherwise, we inject the element directly into the jQuery object
153                                                 this.length = 1;
154                                                 this[0] = elem;
155                                         }
156
157                                         this.context = document;
158                                         this.selector = selector;
159                                         return this;
160                                 }
161
162                         // HANDLE: $("TAG")
163                         } else if ( !context && !rnonword.test( selector ) ) {
164                                 this.selector = selector;
165                                 this.context = document;
166                                 selector = document.getElementsByTagName( selector );
167                                 return jQuery.merge( this, selector );
168
169                         // HANDLE: $(expr, $(...))
170                         } else if ( !context || context.jquery ) {
171                                 return (context || rootjQuery).find( selector );
172
173                         // HANDLE: $(expr, context)
174                         // (which is just equivalent to: $(context).find(expr)
175                         } else {
176                                 return this.constructor( context ).find( selector );
177                         }
178
179                 // HANDLE: $(function)
180                 // Shortcut for document ready
181                 } else if ( jQuery.isFunction( selector ) ) {
182                         return rootjQuery.ready( selector );
183                 }
184
185                 if (selector.selector !== undefined) {
186                         this.selector = selector.selector;
187                         this.context = selector.context;
188                 }
189
190                 return jQuery.makeArray( selector, this );
191         },
192
193         // Start with an empty selector
194         selector: "",
195
196         // The current version of jQuery being used
197         jquery: "@VERSION",
198
199         // The default length of a jQuery object is 0
200         length: 0,
201
202         // The number of elements contained in the matched element set
203         size: function() {
204                 return this.length;
205         },
206
207         toArray: function() {
208                 return slice.call( this, 0 );
209         },
210
211         // Get the Nth element in the matched element set OR
212         // Get the whole matched element set as a clean array
213         get: function( num ) {
214                 return num == null ?
215
216                         // Return a 'clean' array
217                         this.toArray() :
218
219                         // Return just the object
220                         ( num < 0 ? this[ this.length + num ] : this[ num ] );
221         },
222
223         // Take an array of elements and push it onto the stack
224         // (returning the new matched element set)
225         pushStack: function( elems, name, selector ) {
226                 // Build a new jQuery matched element set
227                 var ret = this.constructor();
228
229                 if ( jQuery.isArray( elems ) ) {
230                         push.apply( ret, elems );
231
232                 } else {
233                         jQuery.merge( ret, elems );
234                 }
235
236                 // Add the old object onto the stack (as a reference)
237                 ret.prevObject = this;
238
239                 ret.context = this.context;
240
241                 if ( name === "find" ) {
242                         ret.selector = this.selector + (this.selector ? " " : "") + selector;
243                 } else if ( name ) {
244                         ret.selector = this.selector + "." + name + "(" + selector + ")";
245                 }
246
247                 // Return the newly-formed element set
248                 return ret;
249         },
250
251         // Execute a callback for every element in the matched set.
252         // (You can seed the arguments with an array of args, but this is
253         // only used internally.)
254         each: function( callback, args ) {
255                 return jQuery.each( this, callback, args );
256         },
257
258         ready: function() {
259                 // Attach the listeners
260                 jQuery.bindReady();
261
262                 // Change ready & apply
263                 return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
264         },
265
266         eq: function( i ) {
267                 return i === -1 ?
268                         this.slice( i ) :
269                         this.slice( i, +i + 1 );
270         },
271
272         first: function() {
273                 return this.eq( 0 );
274         },
275
276         last: function() {
277                 return this.eq( -1 );
278         },
279
280         slice: function() {
281                 return this.pushStack( slice.apply( this, arguments ),
282                         "slice", slice.call(arguments).join(",") );
283         },
284
285         map: function( callback ) {
286                 return this.pushStack( jQuery.map(this, function( elem, i ) {
287                         return callback.call( elem, i, elem );
288                 }));
289         },
290
291         end: function() {
292                 return this.prevObject || this.constructor(null);
293         },
294
295         // For internal use only.
296         // Behaves like an Array's method, not like a jQuery method.
297         push: push,
298         sort: [].sort,
299         splice: [].splice
300 };
301
302 // Give the init function the jQuery prototype for later instantiation
303 jQuery.fn.init.prototype = jQuery.fn;
304
305 jQuery.extend = jQuery.fn.extend = function() {
306          var options, name, src, copy, copyIsArray, clone,
307                 target = arguments[0] || {},
308                 i = 1,
309                 length = arguments.length,
310                 deep = false;
311
312         // Handle a deep copy situation
313         if ( typeof target === "boolean" ) {
314                 deep = target;
315                 target = arguments[1] || {};
316                 // skip the boolean and the target
317                 i = 2;
318         }
319
320         // Handle case when target is a string or something (possible in deep copy)
321         if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
322                 target = {};
323         }
324
325         // extend jQuery itself if only one argument is passed
326         if ( length === i ) {
327                 target = this;
328                 --i;
329         }
330
331         for ( ; i < length; i++ ) {
332                 // Only deal with non-null/undefined values
333                 if ( (options = arguments[ i ]) != null ) {
334                         // Extend the base object
335                         for ( name in options ) {
336                                 src = target[ name ];
337                                 copy = options[ name ];
338
339                                 // Prevent never-ending loop
340                                 if ( target === copy ) {
341                                         continue;
342                                 }
343
344                                 // Recurse if we're merging plain objects or arrays
345                                 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
346                                         if ( copyIsArray ) {
347                                                 copyIsArray = false;
348                                                 clone = src && jQuery.isArray(src) ? src : [];
349
350                                         } else {
351                                                 clone = src && jQuery.isPlainObject(src) ? src : {};
352                                         }
353
354                                         // Never move original objects, clone them
355                                         target[ name ] = jQuery.extend( deep, clone, copy );
356
357                                 // Don't bring in undefined values
358                                 } else if ( copy !== undefined ) {
359                                         target[ name ] = copy;
360                                 }
361                         }
362                 }
363         }
364
365         // Return the modified object
366         return target;
367 };
368
369 jQuery.extend({
370         noConflict: function( deep ) {
371                 window.$ = _$;
372
373                 if ( deep ) {
374                         window.jQuery = _jQuery;
375                 }
376
377                 return jQuery;
378         },
379
380         // Is the DOM ready to be used? Set to true once it occurs.
381         isReady: false,
382
383         // A counter to track how many items to wait for before
384         // the ready event fires. See #6781
385         readyWait: 1,
386
387         // Handle when the DOM is ready
388         ready: function( wait ) {
389                 // A third-party is pushing the ready event forwards
390                 if ( wait === true ) {
391                         jQuery.readyWait--;
392                 }
393
394                 // Make sure that the DOM is not already loaded
395                 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
396                         // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
397                         if ( !document.body ) {
398                                 return setTimeout( jQuery.ready, 1 );
399                         }
400
401                         // Remember that the DOM is ready
402                         jQuery.isReady = true;
403
404                         // If a normal DOM Ready event fired, decrement, and wait if need be
405                         if ( wait !== true && --jQuery.readyWait > 0 ) {
406                                 return;
407                         }
408
409                         // If there are functions bound, to execute
410                         readyList.fire( document , [ jQuery ] );
411
412                         // Trigger any bound ready events
413                         if ( jQuery.fn.trigger ) {
414                                 jQuery( document ).trigger( "ready" ).unbind( "ready" );
415                         }
416                 }
417         },
418
419         bindReady: function() {
420                 if ( readyBound ) {
421                         return;
422                 }
423
424                 readyBound = true;
425
426                 // Catch cases where $(document).ready() is called after the
427                 // browser event has already occurred.
428                 if ( document.readyState === "complete" ) {
429                         // Handle it asynchronously to allow scripts the opportunity to delay ready
430                         return setTimeout( jQuery.ready, 1 );
431                 }
432
433                 // Mozilla, Opera and webkit nightlies currently support this event
434                 if ( document.addEventListener ) {
435                         // Use the handy event callback
436                         document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
437
438                         // A fallback to window.onload, that will always work
439                         window.addEventListener( "load", jQuery.ready, false );
440
441                 // If IE event model is used
442                 } else if ( document.attachEvent ) {
443                         // ensure firing before onload,
444                         // maybe late but safe also for iframes
445                         document.attachEvent("onreadystatechange", DOMContentLoaded);
446
447                         // A fallback to window.onload, that will always work
448                         window.attachEvent( "onload", jQuery.ready );
449
450                         // If IE and not a frame
451                         // continually check to see if the document is ready
452                         var toplevel = false;
453
454                         try {
455                                 toplevel = window.frameElement == null;
456                         } catch(e) {}
457
458                         if ( document.documentElement.doScroll && toplevel ) {
459                                 doScrollCheck();
460                         }
461                 }
462         },
463
464         // See test/unit/core.js for details concerning isFunction.
465         // Since version 1.3, DOM methods and functions like alert
466         // aren't supported. They return false on IE (#2968).
467         isFunction: function( obj ) {
468                 return jQuery.type(obj) === "function";
469         },
470
471         isArray: Array.isArray || function( obj ) {
472                 return jQuery.type(obj) === "array";
473         },
474
475         // A crude way of determining if an object is a window
476         isWindow: function( obj ) {
477                 return obj && typeof obj === "object" && "setInterval" in obj;
478         },
479
480         isNaN: function( obj ) {
481                 return obj == null || !rdigit.test( obj ) || isNaN( obj );
482         },
483
484         type: function( obj ) {
485                 return obj == null ?
486                         String( obj ) :
487                         class2type[ toString.call(obj) ] || "object";
488         },
489
490         isPlainObject: function( obj ) {
491                 // Must be an Object.
492                 // Because of IE, we also have to check the presence of the constructor property.
493                 // Make sure that DOM nodes and window objects don't pass through, as well
494                 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
495                         return false;
496                 }
497
498                 // Not own constructor property must be Object
499                 if ( obj.constructor &&
500                         !hasOwn.call(obj, "constructor") &&
501                         !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
502                         return false;
503                 }
504
505                 // Own properties are enumerated firstly, so to speed up,
506                 // if last one is own, then all properties are own.
507
508                 var key;
509                 for ( key in obj ) {}
510
511                 return key === undefined || hasOwn.call( obj, key );
512         },
513
514         isEmptyObject: function( obj ) {
515                 for ( var name in obj ) {
516                         return false;
517                 }
518                 return true;
519         },
520
521         error: function( msg ) {
522                 throw msg;
523         },
524
525         parseJSON: function( data ) {
526                 if ( typeof data !== "string" || !data ) {
527                         return null;
528                 }
529
530                 // Make sure leading/trailing whitespace is removed (IE can't handle it)
531                 data = jQuery.trim( data );
532
533                 // Make sure the incoming data is actual JSON
534                 // Logic borrowed from http://json.org/json2.js
535                 if ( rvalidchars.test(data.replace(rvalidescape, "@")
536                         .replace(rvalidtokens, "]")
537                         .replace(rvalidbraces, "")) ) {
538
539                         // Try to use the native JSON parser first
540                         return window.JSON && window.JSON.parse ?
541                                 window.JSON.parse( data ) :
542                                 (new Function("return " + data))();
543
544                 } else {
545                         jQuery.error( "Invalid JSON: " + data );
546                 }
547         },
548
549         // Cross-browser xml parsing
550         // (xml & tmp used internally)
551         parseXML: function( data , xml , tmp ) {
552
553                 if ( window.DOMParser ) { // Standard
554                         tmp = new DOMParser();
555                         xml = tmp.parseFromString( data , "text/xml" );
556                 } else { // IE
557                         xml = new ActiveXObject( "Microsoft.XMLDOM" );
558                         xml.async = "false";
559                         xml.loadXML( data );
560                 }
561
562                 tmp = xml.documentElement;
563
564                 if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
565                         jQuery.error( "Invalid XML: " + data );
566                 }
567
568                 return xml;
569         },
570
571         noop: function() {},
572
573         // Evalulates a script in a global context
574         globalEval: function( data ) {
575                 if ( data && rnotwhite.test(data) ) {
576                         // Inspired by code by Andrea Giammarchi
577                         // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
578                         var head = document.getElementsByTagName("head")[0] || document.documentElement,
579                                 script = document.createElement("script");
580
581                         script.type = "text/javascript";
582
583                         if ( jQuery.support.scriptEval ) {
584                                 script.appendChild( document.createTextNode( data ) );
585                         } else {
586                                 script.text = data;
587                         }
588
589                         // Use insertBefore instead of appendChild to circumvent an IE6 bug.
590                         // This arises when a base node is used (#2709).
591                         head.insertBefore( script, head.firstChild );
592                         head.removeChild( script );
593                 }
594         },
595
596         nodeName: function( elem, name ) {
597                 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
598         },
599
600         // args is for internal usage only
601         each: function( object, callback, args ) {
602                 var name, i = 0,
603                         length = object.length,
604                         isObj = length === undefined || jQuery.isFunction(object);
605
606                 if ( args ) {
607                         if ( isObj ) {
608                                 for ( name in object ) {
609                                         if ( callback.apply( object[ name ], args ) === false ) {
610                                                 break;
611                                         }
612                                 }
613                         } else {
614                                 for ( ; i < length; ) {
615                                         if ( callback.apply( object[ i++ ], args ) === false ) {
616                                                 break;
617                                         }
618                                 }
619                         }
620
621                 // A special, fast, case for the most common use of each
622                 } else {
623                         if ( isObj ) {
624                                 for ( name in object ) {
625                                         if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
626                                                 break;
627                                         }
628                                 }
629                         } else {
630                                 for ( var value = object[0];
631                                         i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
632                         }
633                 }
634
635                 return object;
636         },
637
638         // Use native String.trim function wherever possible
639         trim: trim ?
640                 function( text ) {
641                         return text == null ?
642                                 "" :
643                                 trim.call( text );
644                 } :
645
646                 // Otherwise use our own trimming functionality
647                 function( text ) {
648                         return text == null ?
649                                 "" :
650                                 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
651                 },
652
653         // results is for internal usage only
654         makeArray: function( array, results ) {
655                 var ret = results || [];
656
657                 if ( array != null ) {
658                         // The window, strings (and functions) also have 'length'
659                         // The extra typeof function check is to prevent crashes
660                         // in Safari 2 (See: #3039)
661                         // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
662                         var type = jQuery.type(array);
663
664                         if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
665                                 push.call( ret, array );
666                         } else {
667                                 jQuery.merge( ret, array );
668                         }
669                 }
670
671                 return ret;
672         },
673
674         inArray: function( elem, array ) {
675                 if ( array.indexOf ) {
676                         return array.indexOf( elem );
677                 }
678
679                 for ( var i = 0, length = array.length; i < length; i++ ) {
680                         if ( array[ i ] === elem ) {
681                                 return i;
682                         }
683                 }
684
685                 return -1;
686         },
687
688         merge: function( first, second ) {
689                 var i = first.length,
690                         j = 0;
691
692                 if ( typeof second.length === "number" ) {
693                         for ( var l = second.length; j < l; j++ ) {
694                                 first[ i++ ] = second[ j ];
695                         }
696
697                 } else {
698                         while ( second[j] !== undefined ) {
699                                 first[ i++ ] = second[ j++ ];
700                         }
701                 }
702
703                 first.length = i;
704
705                 return first;
706         },
707
708         grep: function( elems, callback, inv ) {
709                 var ret = [], retVal;
710                 inv = !!inv;
711
712                 // Go through the array, only saving the items
713                 // that pass the validator function
714                 for ( var i = 0, length = elems.length; i < length; i++ ) {
715                         retVal = !!callback( elems[ i ], i );
716                         if ( inv !== retVal ) {
717                                 ret.push( elems[ i ] );
718                         }
719                 }
720
721                 return ret;
722         },
723
724         // arg is for internal usage only
725         map: function( elems, callback, arg ) {
726                 var ret = [], value;
727
728                 // Go through the array, translating each of the items to their
729                 // new value (or values).
730                 for ( var i = 0, length = elems.length; i < length; i++ ) {
731                         value = callback( elems[ i ], i, arg );
732
733                         if ( value != null ) {
734                                 ret[ ret.length ] = value;
735                         }
736                 }
737
738                 // Flatten any nested arrays
739                 return ret.concat.apply( [], ret );
740         },
741
742         // A global GUID counter for objects
743         guid: 1,
744
745         proxy: function( fn, proxy, thisObject ) {
746                 if ( arguments.length === 2 ) {
747                         if ( typeof proxy === "string" ) {
748                                 thisObject = fn;
749                                 fn = thisObject[ proxy ];
750                                 proxy = undefined;
751
752                         } else if ( proxy && !jQuery.isFunction( proxy ) ) {
753                                 thisObject = proxy;
754                                 proxy = undefined;
755                         }
756                 }
757
758                 if ( !proxy && fn ) {
759                         proxy = function() {
760                                 return fn.apply( thisObject || this, arguments );
761                         };
762                 }
763
764                 // Set the guid of unique handler to the same of original handler, so it can be removed
765                 if ( fn ) {
766                         proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
767                 }
768
769                 // So proxy can be declared as an argument
770                 return proxy;
771         },
772
773         // Mutifunctional method to get and set values to a collection
774         // The value/s can be optionally by executed if its a function
775         access: function( elems, key, value, exec, fn, pass ) {
776                 var length = elems.length;
777
778                 // Setting many attributes
779                 if ( typeof key === "object" ) {
780                         for ( var k in key ) {
781                                 jQuery.access( elems, k, key[k], exec, fn, value );
782                         }
783                         return elems;
784                 }
785
786                 // Setting one attribute
787                 if ( value !== undefined ) {
788                         // Optionally, function values get executed if exec is true
789                         exec = !pass && exec && jQuery.isFunction(value);
790
791                         for ( var i = 0; i < length; i++ ) {
792                                 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
793                         }
794
795                         return elems;
796                 }
797
798                 // Getting an attribute
799                 return length ? fn( elems[0], key ) : undefined;
800         },
801
802         now: function() {
803                 return (new Date()).getTime();
804         },
805
806         // Create a simple deferred (one callbacks list)
807         _Deferred: function() {
808
809                 var // callbacks list
810                         callbacks = [],
811                         // stored [ context , args ]
812                         fired,
813                         // to avoid firing when already doing so
814                         firing,
815                         // flag to know if the deferred has been cancelled
816                         cancelled,
817                         // the deferred itself
818                         deferred  = {
819
820                                 // done( f1, f2, ...)
821                                 done: function () {
822
823                                         if ( ! cancelled ) {
824
825                                                 var args = arguments,
826                                                         i,
827                                                         length,
828                                                         elem,
829                                                         type,
830                                                         _fired;
831
832                                                 if ( fired ) {
833                                                         _fired = fired;
834                                                         fired = 0;
835                                                 }
836
837                                                 for ( i = 0, length = args.length ; i < length ; i++ ) {
838                                                         elem = args[ i ];
839                                                         type = jQuery.type( elem );
840                                                         if ( type === "array" ) {
841                                                                 deferred.done.apply( deferred , elem );
842                                                         } else if ( type === "function" ) {
843                                                                 callbacks.push( elem );
844                                                         }
845                                                 }
846
847                                                 if ( _fired ) {
848                                                         deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
849                                                 }
850                                         }
851
852                                         return this;
853                                 },
854
855                                 // resolve with given context and args
856                                 fire: function( context , args ) {
857                                         if ( ! cancelled && ! fired && ! firing ) {
858
859                                                 firing = 1;
860
861                                                 try {
862                                                         while( callbacks[ 0 ] ) {
863                                                                 callbacks.shift().apply( context , args );
864                                                         }
865                                                 }
866                                                 finally {
867                                                         fired = [ context , args ];
868                                                         firing = 0;
869                                                 }
870                                         }
871                                         return this;
872                                 },
873
874                                 // resolve with this as context and given arguments
875                                 resolve: function() {
876                                         deferred.fire( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments );
877                                         return this;
878                                 },
879
880                                 // Has this deferred been resolved?
881                                 isResolved: function() {
882                                         return !!( firing || fired );
883                                 },
884
885                                 // Cancel
886                                 cancel: function() {
887                                         cancelled = 1;
888                                         callbacks = [];
889                                         return this;
890                                 }
891                         };
892
893                 return deferred;
894         },
895
896         // Full fledged deferred (two callbacks list)
897         // Typical success/error system
898         Deferred: function( func ) {
899
900                 var deferred = jQuery._Deferred(),
901                         failDeferred = jQuery._Deferred();
902
903                 // Add errorDeferred methods and redefine cancel
904                 jQuery.extend( deferred , {
905
906                         then: function( doneCallbacks , failCallbacks ) {
907                                 deferred.done( doneCallbacks ).fail( failCallbacks );
908                                 return this;
909                         },
910                         fail: failDeferred.done,
911                         fireReject: failDeferred.fire,
912                         reject: failDeferred.resolve,
913                         isRejected: failDeferred.isResolved,
914                         // Get a promise for this deferred
915                         // If obj is provided, the promise aspect is added to the object
916                         promise: function( obj ) {
917                                 obj = obj || {};
918                                 jQuery.each( "then done fail isResolved isRejected".split( " " ) , function( _ , method ) {
919                                         obj[ method ] = deferred[ method ];
920                                 });
921                                 obj.promise = function() {
922                                         return obj;
923                                 };
924                                 return obj;
925                         }
926
927                 } );
928
929                 // Make sure only one callback list will be used
930                 deferred.then( failDeferred.cancel , deferred.cancel );
931
932                 // Unexpose cancel
933                 delete deferred.cancel;
934
935                 // Call given func if any
936                 if ( func ) {
937                         func.call( deferred , deferred );
938                 }
939
940                 return deferred;
941         },
942
943         // Deferred helper
944         when: function( object ) {
945                 object = object && jQuery.isFunction( object.promise ) ?
946                         object :
947                         jQuery.Deferred().resolve( object );
948                 return object.promise();
949         },
950
951         // Use of jQuery.browser is frowned upon.
952         // More details: http://docs.jquery.com/Utilities/jQuery.browser
953         uaMatch: function( ua ) {
954                 ua = ua.toLowerCase();
955
956                 var match = rwebkit.exec( ua ) ||
957                         ropera.exec( ua ) ||
958                         rmsie.exec( ua ) ||
959                         ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
960                         [];
961
962                 return { browser: match[1] || "", version: match[2] || "0" };
963         },
964
965         subclass: function(){
966                 function jQuerySubclass( selector, context ) {
967                         return new jQuerySubclass.fn.init( selector, context );
968                 }
969                 jQuerySubclass.superclass = this;
970                 jQuerySubclass.fn = jQuerySubclass.prototype = this();
971                 jQuerySubclass.fn.constructor = jQuerySubclass;
972                 jQuerySubclass.subclass = this.subclass;
973                 jQuerySubclass.fn.init = function init( selector, context ) {
974                         if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){
975                                 context = jQuerySubclass(context);
976                         }
977                         return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );
978                 };
979                 jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;
980                 var rootjQuerySubclass = jQuerySubclass(document);
981                 return jQuerySubclass;
982         },
983
984         browser: {}
985 });
986
987 // Create readyList deferred
988 readyList = jQuery._Deferred();
989
990 // Populate the class2type map
991 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
992         class2type[ "[object " + name + "]" ] = name.toLowerCase();
993 });
994
995 browserMatch = jQuery.uaMatch( userAgent );
996 if ( browserMatch.browser ) {
997         jQuery.browser[ browserMatch.browser ] = true;
998         jQuery.browser.version = browserMatch.version;
999 }
1000
1001 // Deprecated, use jQuery.browser.webkit instead
1002 if ( jQuery.browser.webkit ) {
1003         jQuery.browser.safari = true;
1004 }
1005
1006 if ( indexOf ) {
1007         jQuery.inArray = function( elem, array ) {
1008                 return indexOf.call( array, elem );
1009         };
1010 }
1011
1012 // Verify that \s matches non-breaking spaces
1013 // (IE fails on this test)
1014 if ( !rwhite.test( "\xA0" ) ) {
1015         trimLeft = /^[\s\xA0]+/;
1016         trimRight = /[\s\xA0]+$/;
1017 }
1018
1019 // All jQuery objects should point back to these
1020 rootjQuery = jQuery(document);
1021
1022 // Cleanup functions for the document ready method
1023 if ( document.addEventListener ) {
1024         DOMContentLoaded = function() {
1025                 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
1026                 jQuery.ready();
1027         };
1028
1029 } else if ( document.attachEvent ) {
1030         DOMContentLoaded = function() {
1031                 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
1032                 if ( document.readyState === "complete" ) {
1033                         document.detachEvent( "onreadystatechange", DOMContentLoaded );
1034                         jQuery.ready();
1035                 }
1036         };
1037 }
1038
1039 // The DOM ready check for Internet Explorer
1040 function doScrollCheck() {
1041         if ( jQuery.isReady ) {
1042                 return;
1043         }
1044
1045         try {
1046                 // If IE is used, use the trick by Diego Perini
1047                 // http://javascript.nwbox.com/IEContentLoaded/
1048                 document.documentElement.doScroll("left");
1049         } catch(e) {
1050                 setTimeout( doScrollCheck, 1 );
1051                 return;
1052         }
1053
1054         // and execute any waiting functions
1055         jQuery.ready();
1056 }
1057
1058 // Expose jQuery to the global object
1059 return (window.jQuery = window.$ = jQuery);
1060
1061 })();