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