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