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