Added support for the new .closest() method (very useful for event delegation).
[jquery.git] / src / core.js
1 /*
2  * jQuery @VERSION - New Wave Javascript
3  *
4  * Copyright (c) 2008 John Resig (jquery.com)
5  * Dual licensed under the MIT (MIT-LICENSE.txt)
6  * and GPL (GPL-LICENSE.txt) licenses.
7  *
8  * $Date$
9  * $Rev$
10  */
11
12 // Map over jQuery in case of overwrite
13 var _jQuery = window.jQuery,
14 // Map over the $ in case of overwrite
15         _$ = window.$;
16
17 var jQuery = window.jQuery = window.$ = function( selector, context ) {
18         // The jQuery object is actually just the init constructor 'enhanced'
19         return new jQuery.fn.init( selector, context );
20 };
21
22 // A simple way to check for HTML strings or ID strings
23 // (both of which we optimize for)
24 var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,
25
26 // Is it a simple selector
27         isSimple = /^.[^:#\[\.]*$/,
28
29 // Will speed up references to undefined, and allows munging its name.
30         undefined;
31
32 jQuery.fn = jQuery.prototype = {
33         init: function( selector, context ) {
34                 // Make sure that a selection was provided
35                 selector = selector || document;
36
37                 // Handle $(DOMElement)
38                 if ( selector.nodeType ) {
39                         this[0] = selector;
40                         this.length = 1;
41                         this.context = selector;
42                         return this;
43                 }
44                 // Handle HTML strings
45                 if ( typeof selector === "string" ) {
46                         // Are we dealing with HTML string or an ID?
47                         var match = quickExpr.exec( selector );
48
49                         // Verify a match, and that no context was specified for #id
50                         if ( match && (match[1] || !context) ) {
51
52                                 // HANDLE: $(html) -> $(array)
53                                 if ( match[1] )
54                                         selector = jQuery.clean( [ match[1] ], context );
55
56                                 // HANDLE: $("#id")
57                                 else {
58                                         var elem = document.getElementById( match[3] );
59
60                                         // Make sure an element was located
61                                         if ( elem ){
62                                                 // Handle the case where IE and Opera return items
63                                                 // by name instead of ID
64                                                 if ( elem.id != match[3] )
65                                                         return jQuery().find( selector );
66
67                                                 // Otherwise, we inject the element directly into the jQuery object
68                                                 var ret = jQuery( elem );
69                                                 ret.context = document;
70                                                 ret.selector = selector;
71                                                 return ret;
72                                         }
73                                         selector = [];
74                                 }
75
76                         // HANDLE: $(expr, [context])
77                         // (which is just equivalent to: $(content).find(expr)
78                         } else
79                                 return jQuery( context ).find( selector );
80
81                 // HANDLE: $(function)
82                 // Shortcut for document ready
83                 } else if ( jQuery.isFunction( selector ) )
84                         return jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector );
85
86                 return this.setArray(jQuery.makeArray(selector));
87         },
88
89         // Start with an empty selector
90         selector: "",
91
92         // The current version of jQuery being used
93         jquery: "@VERSION",
94
95         // The number of elements contained in the matched element set
96         size: function() {
97                 return this.length;
98         },
99
100         // Get the Nth element in the matched element set OR
101         // Get the whole matched element set as a clean array
102         get: function( num ) {
103                 return num === undefined ?
104
105                         // Return a 'clean' array
106                         jQuery.makeArray( this ) :
107
108                         // Return just the object
109                         this[ num ];
110         },
111
112         // Take an array of elements and push it onto the stack
113         // (returning the new matched element set)
114         pushStack: function( elems, name, selector ) {
115                 // Build a new jQuery matched element set
116                 var ret = jQuery( elems );
117
118                 // Add the old object onto the stack (as a reference)
119                 ret.prevObject = this;
120
121                 ret.context = this.context;
122
123                 if ( name === "find" )
124                         ret.selector = this.selector + (this.selector ? " " : "") + selector;
125                 else if ( name )
126                         ret.selector = this.selector + "." + name + "(" + selector + ")";
127
128                 // Return the newly-formed element set
129                 return ret;
130         },
131
132         // Force the current matched set of elements to become
133         // the specified array of elements (destroying the stack in the process)
134         // You should use pushStack() in order to do this, but maintain the stack
135         setArray: function( elems ) {
136                 // Resetting the length to 0, then using the native Array push
137                 // is a super-fast way to populate an object with array-like properties
138                 this.length = 0;
139                 Array.prototype.push.apply( this, elems );
140
141                 return this;
142         },
143
144         // Execute a callback for every element in the matched set.
145         // (You can seed the arguments with an array of args, but this is
146         // only used internally.)
147         each: function( callback, args ) {
148                 return jQuery.each( this, callback, args );
149         },
150
151         // Determine the position of an element within
152         // the matched set of elements
153         index: function( elem ) {
154                 var ret = -1;
155
156                 // Locate the position of the desired element
157                 return jQuery.inArray(
158                         // If it receives a jQuery object, the first element is used
159                         elem && elem.jquery ? elem[0] : elem
160                 , this );
161         },
162
163         attr: function( name, value, type ) {
164                 var options = name;
165
166                 // Look for the case where we're accessing a style value
167                 if ( typeof name === "string" )
168                         if ( value === undefined )
169                                 return this[0] && jQuery[ type || "attr" ]( this[0], name );
170
171                         else {
172                                 options = {};
173                                 options[ name ] = value;
174                         }
175
176                 // Check to see if we're setting style values
177                 return this.each(function(i){
178                         // Set all the styles
179                         for ( name in options )
180                                 jQuery.attr(
181                                         type ?
182                                                 this.style :
183                                                 this,
184                                         name, jQuery.prop( this, options[ name ], type, i, name )
185                                 );
186                 });
187         },
188
189         css: function( key, value ) {
190                 // ignore negative width and height values
191                 if ( (key == 'width' || key == 'height') && parseFloat(value) < 0 )
192                         value = undefined;
193                 return this.attr( key, value, "curCSS" );
194         },
195
196         text: function( text ) {
197                 if ( typeof text !== "object" && text != null )
198                         return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
199
200                 var ret = "";
201
202                 jQuery.each( text || this, function(){
203                         jQuery.each( this.childNodes, function(){
204                                 if ( this.nodeType != 8 )
205                                         ret += this.nodeType != 1 ?
206                                                 this.nodeValue :
207                                                 jQuery.fn.text( [ this ] );
208                         });
209                 });
210
211                 return ret;
212         },
213
214         wrapAll: function( html ) {
215                 if ( this[0] )
216                         // The elements to wrap the target around
217                         jQuery( html, this[0].ownerDocument )
218                                 .clone()
219                                 .insertBefore( this[0] )
220                                 .map(function(){
221                                         var elem = this;
222
223                                         while ( elem.firstChild )
224                                                 elem = elem.firstChild;
225
226                                         return elem;
227                                 })
228                                 .append(this);
229
230                 return this;
231         },
232
233         wrapInner: function( html ) {
234                 return this.each(function(){
235                         jQuery( this ).contents().wrapAll( html );
236                 });
237         },
238
239         wrap: function( html ) {
240                 return this.each(function(){
241                         jQuery( this ).wrapAll( html );
242                 });
243         },
244
245         append: function() {
246                 return this.domManip(arguments, true, function(elem){
247                         if (this.nodeType == 1)
248                                 this.appendChild( elem );
249                 });
250         },
251
252         prepend: function() {
253                 return this.domManip(arguments, true, function(elem){
254                         if (this.nodeType == 1)
255                                 this.insertBefore( elem, this.firstChild );
256                 });
257         },
258
259         before: function() {
260                 return this.domManip(arguments, false, function(elem){
261                         this.parentNode.insertBefore( elem, this );
262                 });
263         },
264
265         after: function() {
266                 return this.domManip(arguments, false, function(elem){
267                         this.parentNode.insertBefore( elem, this.nextSibling );
268                 });
269         },
270
271         end: function() {
272                 return this.prevObject || jQuery( [] );
273         },
274
275         find: function( selector ) {
276                 var elems = jQuery.map(this, function(elem){
277                         return jQuery.find( selector, elem );
278                 });
279
280                 return this.pushStack( /[^+>] [^+>]/.test( selector ) ?
281                         jQuery.unique( elems ) :
282                         elems, "find", selector );
283         },
284
285         clone: function( events ) {
286                 // Do the clone
287                 var ret = this.map(function(){
288                         if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
289                                 // IE copies events bound via attachEvent when
290                                 // using cloneNode. Calling detachEvent on the
291                                 // clone will also remove the events from the orignal
292                                 // In order to get around this, we use innerHTML.
293                                 // Unfortunately, this means some modifications to
294                                 // attributes in IE that are actually only stored
295                                 // as properties will not be copied (such as the
296                                 // the name attribute on an input).
297                                 var clone = this.cloneNode(true),
298                                         container = document.createElement("div");
299                                 container.appendChild(clone);
300                                 return jQuery.clean([container.innerHTML])[0];
301                         } else
302                                 return this.cloneNode(true);
303                 });
304
305                 // Need to set the expando to null on the cloned set if it exists
306                 // removeData doesn't work here, IE removes it from the original as well
307                 // this is primarily for IE but the data expando shouldn't be copied over in any browser
308                 var clone = ret.find("*").andSelf().each(function(){
309                         if ( this[ expando ] !== undefined )
310                                 this[ expando ] = null;
311                 });
312
313                 // Copy the events from the original to the clone
314                 if ( events === true )
315                         this.find("*").andSelf().each(function(i){
316                                 if (this.nodeType == 3)
317                                         return;
318                                 var events = jQuery.data( this, "events" );
319
320                                 for ( var type in events )
321                                         for ( var handler in events[ type ] )
322                                                 jQuery.event.add( clone[ i ], type, events[ type ][ handler ], events[ type ][ handler ].data );
323                         });
324
325                 // Return the cloned set
326                 return ret;
327         },
328
329         filter: function( selector ) {
330                 return this.pushStack(
331                         jQuery.isFunction( selector ) &&
332                         jQuery.grep(this, function(elem, i){
333                                 return selector.call( elem, i );
334                         }) ||
335
336                         jQuery.multiFilter( selector, jQuery.grep(this, function(elem){
337                                 return elem.nodeType === 1;
338                         }) ), "filter", selector );
339         },
340
341         closest: function( selector ) {
342                 return this.map(function(){
343                         var cur = this;
344                         while ( cur && cur.ownerDocument ) {
345                                 if ( jQuery(cur).is(selector) )
346                                         return cur;
347                                 cur = cur.parentNode;
348                         }
349                 });
350         },
351
352         not: function( selector ) {
353                 if ( typeof selector === "string" )
354                         // test special case where just one selector is passed in
355                         if ( isSimple.test( selector ) )
356                                 return this.pushStack( jQuery.multiFilter( selector, this, true ), "not", selector );
357                         else
358                                 selector = jQuery.multiFilter( selector, this );
359
360                 var isArrayLike = selector.length && selector[selector.length - 1] !== undefined && !selector.nodeType;
361                 return this.filter(function() {
362                         return isArrayLike ? jQuery.inArray( this, selector ) < 0 : this != selector;
363                 });
364         },
365
366         add: function( selector ) {
367                 return this.pushStack( jQuery.unique( jQuery.merge(
368                         this.get(),
369                         typeof selector === "string" ?
370                                 jQuery( selector ) :
371                                 jQuery.makeArray( selector )
372                 )));
373         },
374
375         is: function( selector ) {
376                 return !!selector && jQuery.multiFilter( selector, this ).length > 0;
377         },
378
379         hasClass: function( selector ) {
380                 return !!selector && this.is( "." + selector );
381         },
382
383         val: function( value ) {
384                 if ( value === undefined ) {                    
385                         var elem = this[0];
386
387                         if ( elem ) {
388                                 if( jQuery.nodeName( elem, 'option' ) )
389                                         return (elem.attributes.value || {}).specified ? elem.value : elem.text;
390                                 
391                                 // We need to handle select boxes special
392                                 if ( jQuery.nodeName( elem, "select" ) ) {
393                                         var index = elem.selectedIndex,
394                                                 values = [],
395                                                 options = elem.options,
396                                                 one = elem.type == "select-one";
397
398                                         // Nothing was selected
399                                         if ( index < 0 )
400                                                 return null;
401
402                                         // Loop through all the selected options
403                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
404                                                 var option = options[ i ];
405
406                                                 if ( option.selected ) {
407                                                         // Get the specifc value for the option
408                                                         value = jQuery(option).val();
409
410                                                         // We don't need an array for one selects
411                                                         if ( one )
412                                                                 return value;
413
414                                                         // Multi-Selects return an array
415                                                         values.push( value );
416                                                 }
417                                         }
418
419                                         return values;                          
420                                 }
421
422                                 // Everything else, we just grab the value
423                                 return (elem.value || "").replace(/\r/g, "");
424
425                         }
426
427                         return undefined;
428                 }
429
430                 if ( typeof value === "number" )
431                         value += '';
432
433                 return this.each(function(){
434                         if ( this.nodeType != 1 )
435                                 return;
436
437                         if ( jQuery.isArray(value) && /radio|checkbox/.test( this.type ) )
438                                 this.checked = (jQuery.inArray(this.value, value) >= 0 ||
439                                         jQuery.inArray(this.name, value) >= 0);
440
441                         else if ( jQuery.nodeName( this, "select" ) ) {
442                                 var values = jQuery.makeArray(value);
443
444                                 jQuery( "option", this ).each(function(){
445                                         this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
446                                                 jQuery.inArray( this.text, values ) >= 0);
447                                 });
448
449                                 if ( !values.length )
450                                         this.selectedIndex = -1;
451
452                         } else
453                                 this.value = value;
454                 });
455         },
456
457         html: function( value ) {
458                 return value === undefined ?
459                         (this[0] ?
460                                 this[0].innerHTML :
461                                 null) :
462                         this.empty().append( value );
463         },
464
465         replaceWith: function( value ) {
466                 return this.after( value ).remove();
467         },
468
469         eq: function( i ) {
470                 return this.slice( i, +i + 1 );
471         },
472
473         slice: function() {
474                 return this.pushStack( Array.prototype.slice.apply( this, arguments ),
475                         "slice", Array.prototype.slice.call(arguments).join(",") );
476         },
477
478         map: function( callback ) {
479                 return this.pushStack( jQuery.map(this, function(elem, i){
480                         return callback.call( elem, i, elem );
481                 }));
482         },
483
484         andSelf: function() {
485                 return this.add( this.prevObject );
486         },
487
488         data: function( key, value ){
489                 var parts = key.split(".");
490                 parts[1] = parts[1] ? "." + parts[1] : "";
491
492                 if ( value === undefined ) {
493                         var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
494
495                         if ( data === undefined && this.length )
496                                 data = jQuery.data( this[0], key );
497
498                         return data == null && parts[1] ?
499                                 this.data( parts[0] ) :
500                                 data;
501                 } else
502                         return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){
503                                 jQuery.data( this, key, value );
504                         });
505         },
506
507         removeData: function( key ){
508                 return this.each(function(){
509                         jQuery.removeData( this, key );
510                 });
511         },
512
513         domManip: function( args, table, callback ) {
514                 if ( this[0] ) {
515                         var fragment = this[0].ownerDocument.createDocumentFragment(),
516                                 scripts = jQuery.clean( args, this[0].ownerDocument, fragment ),
517                                 first = fragment.firstChild,
518                                 extra = this.length > 1 ? fragment.cloneNode(true) : fragment;
519                         
520                         if ( first )
521                                 for ( var i = 0, l = this.length; i < l; i++ )
522                                         callback.call( root(this[i], first), i > 0 ? extra.cloneNode(true) : fragment );
523                         
524                         if ( scripts )
525                                 jQuery.each( scripts, evalScript );
526                 }
527
528                 return this;
529                 
530                 function root( elem, cur ) {
531                         return table && jQuery.nodeName(elem, "table") && jQuery.nodeName(cur, "tr") ?
532                                 (elem.getElementsByTagName("tbody")[0] ||
533                                 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
534                                 elem;
535                 }
536         }
537 };
538
539 // Give the init function the jQuery prototype for later instantiation
540 jQuery.fn.init.prototype = jQuery.fn;
541
542 function evalScript( i, elem ) {
543         if ( elem.src )
544                 jQuery.ajax({
545                         url: elem.src,
546                         async: false,
547                         dataType: "script"
548                 });
549
550         else
551                 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
552
553         if ( elem.parentNode )
554                 elem.parentNode.removeChild( elem );
555 }
556
557 function now(){
558         return +new Date;
559 }
560
561 jQuery.extend = jQuery.fn.extend = function() {
562         // copy reference to target object
563         var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;
564
565         // Handle a deep copy situation
566         if ( typeof target === "boolean" ) {
567                 deep = target;
568                 target = arguments[1] || {};
569                 // skip the boolean and the target
570                 i = 2;
571         }
572
573         // Handle case when target is a string or something (possible in deep copy)
574         if ( typeof target !== "object" && !jQuery.isFunction(target) )
575                 target = {};
576
577         // extend jQuery itself if only one argument is passed
578         if ( length == i ) {
579                 target = this;
580                 --i;
581         }
582
583         for ( ; i < length; i++ )
584                 // Only deal with non-null/undefined values
585                 if ( (options = arguments[ i ]) != null )
586                         // Extend the base object
587                         for ( var name in options ) {
588                                 var src = target[ name ], copy = options[ name ];
589
590                                 // Prevent never-ending loop
591                                 if ( target === copy )
592                                         continue;
593
594                                 // Recurse if we're merging object values
595                                 if ( deep && copy && typeof copy === "object" && !copy.nodeType )
596                                         target[ name ] = jQuery.extend( deep, 
597                                                 // Never move original objects, clone them
598                                                 src || ( copy.length != null ? [ ] : { } )
599                                         , copy );
600
601                                 // Don't bring in undefined values
602                                 else if ( copy !== undefined )
603                                         target[ name ] = copy;
604
605                         }
606
607         // Return the modified object
608         return target;
609 };
610
611 var expando = "jQuery" + now(), uuid = 0, windowData = {},
612         // exclude the following css properties to add px
613         exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
614         // cache defaultView
615         defaultView = document.defaultView || {},
616         toString = Object.prototype.toString;
617
618 jQuery.extend({
619         noConflict: function( deep ) {
620                 window.$ = _$;
621
622                 if ( deep )
623                         window.jQuery = _jQuery;
624
625                 return jQuery;
626         },
627
628         // See test/unit/core.js for details concerning isFunction.
629         // Since version 1.3, DOM methods and functions like alert
630         // aren't supported. They return false on IE (#2968).
631         isFunction: function( obj ) {
632                 return toString.call(obj) === "[object Function]";
633         },
634
635         isArray: function( obj ) {
636                 return toString.call(obj) === "[object Array]";
637         },
638
639         // check if an element is in a (or is an) XML document
640         isXMLDoc: function( elem ) {
641                 return elem.documentElement && !elem.body ||
642                         elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
643         },
644
645         // Evalulates a script in a global context
646         globalEval: function( data ) {
647                 data = jQuery.trim( data );
648
649                 if ( data ) {
650                         // Inspired by code by Andrea Giammarchi
651                         // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
652                         var head = document.getElementsByTagName("head")[0] || document.documentElement,
653                                 script = document.createElement("script");
654
655                         script.type = "text/javascript";
656                         if ( jQuery.support.scriptEval )
657                                 script.appendChild( document.createTextNode( data ) );
658                         else
659                                 script.text = data;
660
661                         // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
662                         // This arises when a base node is used (#2709).
663                         head.insertBefore( script, head.firstChild );
664                         head.removeChild( script );
665                 }
666         },
667
668         nodeName: function( elem, name ) {
669                 return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
670         },
671
672         cache: {},
673
674         data: function( elem, name, data ) {
675                 elem = elem == window ?
676                         windowData :
677                         elem;
678
679                 var id = elem[ expando ];
680
681                 // Compute a unique ID for the element
682                 if ( !id )
683                         id = elem[ expando ] = ++uuid;
684
685                 // Only generate the data cache if we're
686                 // trying to access or manipulate it
687                 if ( name && !jQuery.cache[ id ] )
688                         jQuery.cache[ id ] = {};
689
690                 // Prevent overriding the named cache with undefined values
691                 if ( data !== undefined )
692                         jQuery.cache[ id ][ name ] = data;
693
694                 // Return the named cache data, or the ID for the element
695                 return name ?
696                         jQuery.cache[ id ][ name ] || null :
697                         id;
698         },
699
700         removeData: function( elem, name ) {
701                 elem = elem == window ?
702                         windowData :
703                         elem;
704
705                 var id = elem[ expando ];
706
707                 // If we want to remove a specific section of the element's data
708                 if ( name ) {
709                         if ( jQuery.cache[ id ] ) {
710                                 // Remove the section of cache data
711                                 delete jQuery.cache[ id ][ name ];
712
713                                 // If we've removed all the data, remove the element's cache
714                                 name = "";
715
716                                 for ( name in jQuery.cache[ id ] )
717                                         break;
718
719                                 if ( !name )
720                                         jQuery.removeData( elem );
721                         }
722
723                 // Otherwise, we want to remove all of the element's data
724                 } else {
725                         // Clean up the element expando
726                         try {
727                                 delete elem[ expando ];
728                         } catch(e){
729                                 // IE has trouble directly removing the expando
730                                 // but it's ok with using removeAttribute
731                                 if ( elem.removeAttribute )
732                                         elem.removeAttribute( expando );
733                         }
734
735                         // Completely remove the data cache
736                         delete jQuery.cache[ id ];
737                 }
738         },
739
740         // args is for internal usage only
741         each: function( object, callback, args ) {
742                 var name, i = 0, length = object.length;
743
744                 if ( args ) {
745                         if ( length === undefined ) {
746                                 for ( name in object )
747                                         if ( callback.apply( object[ name ], args ) === false )
748                                                 break;
749                         } else
750                                 for ( ; i < length; )
751                                         if ( callback.apply( object[ i++ ], args ) === false )
752                                                 break;
753
754                 // A special, fast, case for the most common use of each
755                 } else {
756                         if ( length === undefined ) {
757                                 for ( name in object )
758                                         if ( callback.call( object[ name ], name, object[ name ] ) === false )
759                                                 break;
760                         } else
761                                 for ( var value = object[0];
762                                         i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}
763                 }
764
765                 return object;
766         },
767
768         prop: function( elem, value, type, i, name ) {
769                 // Handle executable functions
770                 if ( jQuery.isFunction( value ) )
771                         value = value.call( elem, i );
772
773                 // Handle passing in a number to a CSS property
774                 return typeof value === "number" && type == "curCSS" && !exclude.test( name ) ?
775                         value + "px" :
776                         value;
777         },
778
779         className: {
780                 // internal only, use addClass("class")
781                 add: function( elem, classNames ) {
782                         jQuery.each((classNames || "").split(/\s+/), function(i, className){
783                                 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
784                                         elem.className += (elem.className ? " " : "") + className;
785                         });
786                 },
787
788                 // internal only, use removeClass("class")
789                 remove: function( elem, classNames ) {
790                         if (elem.nodeType == 1)
791                                 elem.className = classNames !== undefined ?
792                                         jQuery.grep(elem.className.split(/\s+/), function(className){
793                                                 return !jQuery.className.has( classNames, className );
794                                         }).join(" ") :
795                                         "";
796                 },
797
798                 // internal only, use hasClass("class")
799                 has: function( elem, className ) {
800                         return jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
801                 }
802         },
803
804         // A method for quickly swapping in/out CSS properties to get correct calculations
805         swap: function( elem, options, callback ) {
806                 var old = {};
807                 // Remember the old values, and insert the new ones
808                 for ( var name in options ) {
809                         old[ name ] = elem.style[ name ];
810                         elem.style[ name ] = options[ name ];
811                 }
812
813                 callback.call( elem );
814
815                 // Revert the old values
816                 for ( var name in options )
817                         elem.style[ name ] = old[ name ];
818         },
819
820         css: function( elem, name, force ) {
821                 if ( name == "width" || name == "height" ) {
822                         var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
823
824                         function getWH() {
825                                 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
826                                 var padding = 0, border = 0;
827                                 jQuery.each( which, function() {
828                                         padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
829                                         border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
830                                 });
831                                 val -= Math.round(padding + border);
832                         }
833
834                         if ( jQuery(elem).is(":visible") )
835                                 getWH();
836                         else
837                                 jQuery.swap( elem, props, getWH );
838
839                         return Math.max(0, val);
840                 }
841
842                 return jQuery.curCSS( elem, name, force );
843         },
844
845         curCSS: function( elem, name, force ) {
846                 var ret, style = elem.style;
847
848                 // We need to handle opacity special in IE
849                 if ( name == "opacity" && !jQuery.support.opacity ) {
850                         ret = jQuery.attr( style, "opacity" );
851
852                         return ret == "" ?
853                                 "1" :
854                                 ret;
855                 }
856
857                 // Make sure we're using the right name for getting the float value
858                 if ( name.match( /float/i ) )
859                         name = styleFloat;
860
861                 if ( !force && style && style[ name ] )
862                         ret = style[ name ];
863
864                 else if ( defaultView.getComputedStyle ) {
865
866                         // Only "float" is needed here
867                         if ( name.match( /float/i ) )
868                                 name = "float";
869
870                         name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
871
872                         var computedStyle = defaultView.getComputedStyle( elem, null );
873
874                         if ( computedStyle )
875                                 ret = computedStyle.getPropertyValue( name );
876
877                         // We should always get a number back from opacity
878                         if ( name == "opacity" && ret == "" )
879                                 ret = "1";
880
881                 } else if ( elem.currentStyle ) {
882                         var camelCase = name.replace(/\-(\w)/g, function(all, letter){
883                                 return letter.toUpperCase();
884                         });
885
886                         ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
887
888                         // From the awesome hack by Dean Edwards
889                         // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
890
891                         // If we're not dealing with a regular pixel number
892                         // but a number that has a weird ending, we need to convert it to pixels
893                         if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
894                                 // Remember the original values
895                                 var left = style.left, rsLeft = elem.runtimeStyle.left;
896
897                                 // Put in the new values to get a computed value out
898                                 elem.runtimeStyle.left = elem.currentStyle.left;
899                                 style.left = ret || 0;
900                                 ret = style.pixelLeft + "px";
901
902                                 // Revert the changed values
903                                 style.left = left;
904                                 elem.runtimeStyle.left = rsLeft;
905                         }
906                 }
907
908                 return ret;
909         },
910
911         clean: function( elems, context, fragment ) {
912                 var ret = [], scripts = [];
913                 context = context || document;
914
915                 // !context.createElement fails in IE with an error but returns typeof 'object'
916                 if ( typeof context.createElement === "undefined" )
917                         context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
918
919                 jQuery.each(elems, function(i, elem){
920                         if ( typeof elem === "number" )
921                                 elem += '';
922
923                         if ( !elem )
924                                 return;
925
926                         // Convert html string into DOM nodes
927                         if ( typeof elem === "string" ) {
928                                 // Fix "XHTML"-style tags in all browsers
929                                 elem = elem.replace(/(<(\w+)[^>]*?)\/>/g, function(all, front, tag){
930                                         return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ?
931                                                 all :
932                                                 front + "></" + tag + ">";
933                                 });
934
935                                 // Trim whitespace, otherwise indexOf won't work as expected
936                                 var tags = jQuery.trim( elem ).toLowerCase(), div = context.createElement("div");
937
938                                 var wrap =
939                                         // option or optgroup
940                                         !tags.indexOf("<opt") &&
941                                         [ 1, "<select multiple='multiple'>", "</select>" ] ||
942
943                                         !tags.indexOf("<leg") &&
944                                         [ 1, "<fieldset>", "</fieldset>" ] ||
945
946                                         tags.match(/^<(thead|tbody|tfoot|colg|cap)/) &&
947                                         [ 1, "<table>", "</table>" ] ||
948
949                                         !tags.indexOf("<tr") &&
950                                         [ 2, "<table><tbody>", "</tbody></table>" ] ||
951
952                                         // <thead> matched above
953                                         (!tags.indexOf("<td") || !tags.indexOf("<th")) &&
954                                         [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ] ||
955
956                                         !tags.indexOf("<col") &&
957                                         [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ] ||
958
959                                         // IE can't serialize <link> and <script> tags normally
960                                         !jQuery.support.htmlSerialize &&
961                                         [ 1, "div<div>", "</div>" ] ||
962
963                                         [ 0, "", "" ];
964
965                                 // Go to html and back, then peel off extra wrappers
966                                 div.innerHTML = wrap[1] + elem + wrap[2];
967
968                                 // Move to the right depth
969                                 while ( wrap[0]-- )
970                                         div = div.lastChild;
971
972                                 // Remove IE's autoinserted <tbody> from table fragments
973                                 if ( !jQuery.support.tbody ) {
974
975                                         // String was a <table>, *may* have spurious <tbody>
976                                         var tbody = !tags.indexOf("<table") && tags.indexOf("<tbody") < 0 ?
977                                                 div.firstChild && div.firstChild.childNodes :
978
979                                                 // String was a bare <thead> or <tfoot>
980                                                 wrap[1] == "<table>" && tags.indexOf("<tbody") < 0 ?
981                                                         div.childNodes :
982                                                         [];
983
984                                         for ( var j = tbody.length - 1; j >= 0 ; --j )
985                                                 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length )
986                                                         tbody[ j ].parentNode.removeChild( tbody[ j ] );
987
988                                         }
989
990                                 // IE completely kills leading whitespace when innerHTML is used
991                                 if ( !jQuery.support.leadingWhitespace && /^\s/.test( elem ) )
992                                         div.insertBefore( context.createTextNode( elem.match(/^\s*/)[0] ), div.firstChild );
993                                 
994                                 if ( fragment ) {
995                                         var found = div.getElementsByTagName("script");
996                         
997                                         while ( found.length ) {
998                                                 scripts.push( found[0] );
999                                                 found[0].parentNode.removeChild( found[0] );
1000                                         }
1001                                 }
1002
1003                                 elem = jQuery.makeArray( div.childNodes );
1004                         }
1005
1006                         if ( elem.nodeType )
1007                                 ret.push( elem );
1008                         else
1009                                 ret = jQuery.merge( ret, elem );
1010
1011                 });
1012                 
1013                 if ( fragment ) {
1014                         for ( var i = 0; ret[i]; i++ ) {
1015                                 if ( jQuery.nodeName( ret[i], "script" ) ) {
1016                                         ret[i].parentNode.removeChild( ret[i] );
1017                                 } else {
1018                                         if ( ret[i].nodeType === 1 )
1019                                                 ret = jQuery.merge( ret, ret[i].getElementsByTagName("script"));
1020                                         fragment.appendChild( ret[i] );
1021                                 }
1022                         }
1023                         
1024                         return scripts;
1025                 }
1026
1027                 return ret;
1028         },
1029
1030         attr: function( elem, name, value ) {
1031                 // don't set attributes on text and comment nodes
1032                 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
1033                         return undefined;
1034
1035                 var notxml = !jQuery.isXMLDoc( elem ),
1036                         // Whether we are setting (or getting)
1037                         set = value !== undefined;
1038
1039                 // Try to normalize/fix the name
1040                 name = notxml && jQuery.props[ name ] || name;
1041
1042                 // Only do all the following if this is a node (faster for style)
1043                 // IE elem.getAttribute passes even for style
1044                 if ( elem.tagName ) {
1045
1046                         // These attributes require special treatment
1047                         var special = /href|src|style/.test( name );
1048
1049                         // Safari mis-reports the default selected property of a hidden option
1050                         // Accessing the parent's selectedIndex property fixes it
1051                         if ( name == "selected" )
1052                                 elem.parentNode.selectedIndex;
1053
1054                         // If applicable, access the attribute via the DOM 0 way
1055                         if ( name in elem && notxml && !special ) {
1056                                 if ( set ){
1057                                         // We can't allow the type property to be changed (since it causes problems in IE)
1058                                         if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
1059                                                 throw "type property can't be changed";
1060
1061                                         elem[ name ] = value;
1062                                 }
1063
1064                                 // browsers index elements by id/name on forms, give priority to attributes.
1065                                 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
1066                                         return elem.getAttributeNode( name ).nodeValue;
1067
1068                                 return elem[ name ];
1069                         }
1070
1071                         if ( !jQuery.support.style && notxml &&  name == "style" )
1072                                 return jQuery.attr( elem.style, "cssText", value );
1073
1074                         if ( set )
1075                                 // convert the value to a string (all browsers do this but IE) see #1070
1076                                 elem.setAttribute( name, "" + value );
1077
1078                         var attr = !jQuery.support.hrefNormalized && notxml && special
1079                                         // Some attributes require a special call on IE
1080                                         ? elem.getAttribute( name, 2 )
1081                                         : elem.getAttribute( name );
1082
1083                         // Non-existent attributes return null, we normalize to undefined
1084                         return attr === null ? undefined : attr;
1085                 }
1086
1087                 // elem is actually elem.style ... set the style
1088
1089                 // IE uses filters for opacity
1090                 if ( !jQuery.support.opacity && name == "opacity" ) {
1091                         if ( set ) {
1092                                 // IE has trouble with opacity if it does not have layout
1093                                 // Force it by setting the zoom level
1094                                 elem.zoom = 1;
1095
1096                                 // Set the alpha filter to set the opacity
1097                                 elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
1098                                         (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
1099                         }
1100
1101                         return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
1102                                 (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
1103                                 "";
1104                 }
1105
1106                 name = name.replace(/-([a-z])/ig, function(all, letter){
1107                         return letter.toUpperCase();
1108                 });
1109
1110                 if ( set )
1111                         elem[ name ] = value;
1112
1113                 return elem[ name ];
1114         },
1115
1116         trim: function( text ) {
1117                 return (text || "").replace( /^\s+|\s+$/g, "" );
1118         },
1119
1120         makeArray: function( array ) {
1121                 var ret = [];
1122
1123                 if( array != null ){
1124                         var i = array.length;
1125                         // The window, strings (and functions) also have 'length'
1126                         if( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval )
1127                                 ret[0] = array;
1128                         else
1129                                 while( i )
1130                                         ret[--i] = array[i];
1131                 }
1132
1133                 return ret;
1134         },
1135
1136         inArray: function( elem, array ) {
1137                 for ( var i = 0, length = array.length; i < length; i++ )
1138                 // Use === because on IE, window == document
1139                         if ( array[ i ] === elem )
1140                                 return i;
1141
1142                 return -1;
1143         },
1144
1145         merge: function( first, second ) {
1146                 // We have to loop this way because IE & Opera overwrite the length
1147                 // expando of getElementsByTagName
1148                 var i = 0, elem, pos = first.length;
1149                 // Also, we need to make sure that the correct elements are being returned
1150                 // (IE returns comment nodes in a '*' query)
1151                 if ( !jQuery.support.getAll ) {
1152                         while ( (elem = second[ i++ ]) )
1153                                 if ( elem.nodeType != 8 )
1154                                         first[ pos++ ] = elem;
1155
1156                 } else
1157                         while ( (elem = second[ i++ ]) )
1158                                 first[ pos++ ] = elem;
1159
1160                 return first;
1161         },
1162
1163         unique: function( array ) {
1164                 var ret = [], done = {};
1165
1166                 try {
1167
1168                         for ( var i = 0, length = array.length; i < length; i++ ) {
1169                                 var id = jQuery.data( array[ i ] );
1170
1171                                 if ( !done[ id ] ) {
1172                                         done[ id ] = true;
1173                                         ret.push( array[ i ] );
1174                                 }
1175                         }
1176
1177                 } catch( e ) {
1178                         ret = array;
1179                 }
1180
1181                 return ret;
1182         },
1183
1184         grep: function( elems, callback, inv ) {
1185                 var ret = [];
1186
1187                 // Go through the array, only saving the items
1188                 // that pass the validator function
1189                 for ( var i = 0, length = elems.length; i < length; i++ )
1190                         if ( !inv != !callback( elems[ i ], i ) )
1191                                 ret.push( elems[ i ] );
1192
1193                 return ret;
1194         },
1195
1196         map: function( elems, callback ) {
1197                 var ret = [];
1198
1199                 // Go through the array, translating each of the items to their
1200                 // new value (or values).
1201                 for ( var i = 0, length = elems.length; i < length; i++ ) {
1202                         var value = callback( elems[ i ], i );
1203
1204                         if ( value != null )
1205                                 ret[ ret.length ] = value;
1206                 }
1207
1208                 return ret.concat.apply( [], ret );
1209         }
1210 });
1211
1212 // Use of jQuery.browser is deprecated.
1213 // It's included for backwards compatibility and plugins,
1214 // although they should work to migrate away.
1215
1216 var userAgent = navigator.userAgent.toLowerCase();
1217
1218 // Figure out what browser is being used
1219 jQuery.browser = {
1220         version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
1221         safari: /webkit/.test( userAgent ),
1222         opera: /opera/.test( userAgent ),
1223         msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
1224         mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
1225 };
1226
1227 // Check to see if the W3C box model is being used
1228 jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
1229
1230 jQuery.each({
1231         parent: function(elem){return elem.parentNode;},
1232         parents: function(elem){return jQuery.dir(elem,"parentNode");},
1233         next: function(elem){return jQuery.nth(elem,2,"nextSibling");},
1234         prev: function(elem){return jQuery.nth(elem,2,"previousSibling");},
1235         nextAll: function(elem){return jQuery.dir(elem,"nextSibling");},
1236         prevAll: function(elem){return jQuery.dir(elem,"previousSibling");},
1237         siblings: function(elem){return jQuery.sibling(elem.parentNode.firstChild,elem);},
1238         children: function(elem){return jQuery.sibling(elem.firstChild);},
1239         contents: function(elem){return jQuery.nodeName(elem,"iframe")?elem.contentDocument||elem.contentWindow.document:jQuery.makeArray(elem.childNodes);}
1240 }, function(name, fn){
1241         jQuery.fn[ name ] = function( selector ) {
1242                 var ret = jQuery.map( this, fn );
1243
1244                 if ( selector && typeof selector == "string" )
1245                         ret = jQuery.multiFilter( selector, ret );
1246
1247                 return this.pushStack( jQuery.unique( ret ), name, selector );
1248         };
1249 });
1250
1251 jQuery.each({
1252         appendTo: "append",
1253         prependTo: "prepend",
1254         insertBefore: "before",
1255         insertAfter: "after",
1256         replaceAll: "replaceWith"
1257 }, function(name, original){
1258         jQuery.fn[ name ] = function() {
1259                 var args = arguments;
1260
1261                 return this.each(function(){
1262                         for ( var i = 0, length = args.length; i < length; i++ )
1263                                 jQuery( args[ i ] )[ original ]( this );
1264                 });
1265         };
1266 });
1267
1268 jQuery.each({
1269         removeAttr: function( name ) {
1270                 jQuery.attr( this, name, "" );
1271                 if (this.nodeType == 1)
1272                         this.removeAttribute( name );
1273         },
1274
1275         addClass: function( classNames ) {
1276                 jQuery.className.add( this, classNames );
1277         },
1278
1279         removeClass: function( classNames ) {
1280                 jQuery.className.remove( this, classNames );
1281         },
1282
1283         toggleClass: function( classNames ) {
1284                 jQuery.className[ jQuery.className.has( this, classNames ) ? "remove" : "add" ]( this, classNames );
1285         },
1286
1287         remove: function( selector ) {
1288                 if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
1289                         // Prevent memory leaks
1290                         jQuery( "*", this ).add([this]).each(function(){
1291                                 jQuery.event.remove(this);
1292                                 jQuery.removeData(this);
1293                         });
1294                         if (this.parentNode)
1295                                 this.parentNode.removeChild( this );
1296                 }
1297         },
1298
1299         empty: function() {
1300                 // Remove element nodes and prevent memory leaks
1301                 jQuery( ">*", this ).remove();
1302
1303                 // Remove any remaining nodes
1304                 while ( this.firstChild )
1305                         this.removeChild( this.firstChild );
1306         }
1307 }, function(name, fn){
1308         jQuery.fn[ name ] = function(){
1309                 return this.each( fn, arguments );
1310         };
1311 });
1312
1313 // Helper function used by the dimensions and offset modules
1314 function num(elem, prop) {
1315         return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;
1316 }