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