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