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