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