fix clone(true) for IE
[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 ( typeof jQuery != "undefined" )
14         var _jQuery = jQuery;
15
16 var jQuery = window.jQuery = function(a,c) {
17         // If the context is global, return a new object
18         if ( window == this || !this.init )
19                 return new jQuery(a,c);
20         
21         return this.init(a,c);
22 };
23
24 // Map over the $ in case of overwrite
25 if ( typeof $ != "undefined" )
26         var _$ = $;
27         
28 // Map the jQuery namespace to the '$' one
29 window.$ = jQuery;
30
31 var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/;
32
33 jQuery.fn = jQuery.prototype = {
34         init: function(a,c) {
35                 // Make sure that a selection was provided
36                 a = a || document;
37
38                 // Handle HTML strings
39                 if ( typeof a  == "string" ) {
40                         var m = quickExpr.exec(a);
41                         if ( m && (m[1] || !c) ) {
42                                 // HANDLE: $(html) -> $(array)
43                                 if ( m[1] )
44                                         a = jQuery.clean( [ m[1] ], c );
45
46                                 // HANDLE: $("#id")
47                                 else {
48                                         var tmp = document.getElementById( m[3] );
49                                         if ( tmp )
50                                                 // Handle the case where IE and Opera return items
51                                                 // by name instead of ID
52                                                 if ( tmp.id != m[3] )
53                                                         return jQuery().find( a );
54                                                 else {
55                                                         this[0] = tmp;
56                                                         this.length = 1;
57                                                         return this;
58                                                 }
59                                         else
60                                                 a = [];
61                                 }
62
63                         // HANDLE: $(expr)
64                         } else
65                                 return new jQuery( c ).find( a );
66
67                 // HANDLE: $(function)
68                 // Shortcut for document ready
69                 } else if ( jQuery.isFunction(a) )
70                         return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a );
71
72                 return this.setArray(
73                         // HANDLE: $(array)
74                         a.constructor == Array && a ||
75
76                         // HANDLE: $(arraylike)
77                         // Watch for when an array-like object is passed as the selector
78                         (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a ) ||
79
80                         // HANDLE: $(*)
81                         [ a ] );
82         },
83         
84         jquery: "@VERSION",
85
86         size: function() {
87                 return this.length;
88         },
89         
90         length: 0,
91
92         get: function( num ) {
93                 return num == undefined ?
94
95                         // Return a 'clean' array
96                         jQuery.makeArray( this ) :
97
98                         // Return just the object
99                         this[num];
100         },
101         
102         pushStack: function( a ) {
103                 var ret = jQuery(a);
104                 ret.prevObject = this;
105                 return ret;
106         },
107         
108         setArray: function( a ) {
109                 this.length = 0;
110                 Array.prototype.push.apply( this, a );
111                 return this;
112         },
113
114         each: function( fn, args ) {
115                 return jQuery.each( this, fn, args );
116         },
117
118         index: function( obj ) {
119                 var pos = -1;
120                 this.each(function(i){
121                         if ( this == obj ) pos = i;
122                 });
123                 return pos;
124         },
125
126         attr: function( key, value, type ) {
127                 var obj = key;
128                 
129                 // Look for the case where we're accessing a style value
130                 if ( key.constructor == String )
131                         if ( value == undefined )
132                                 return this.length && jQuery[ type || "attr" ]( this[0], key ) || undefined;
133                         else {
134                                 obj = {};
135                                 obj[ key ] = value;
136                         }
137                 
138                 // Check to see if we're setting style values
139                 return this.each(function(index){
140                         // Set all the styles
141                         for ( var prop in obj )
142                                 jQuery.attr(
143                                         type ? this.style : this,
144                                         prop, jQuery.prop(this, obj[prop], type, index, prop)
145                                 );
146                 });
147         },
148
149         css: function( key, value ) {
150                 return this.attr( key, value, "curCSS" );
151         },
152
153         text: function(e) {
154                 if ( typeof e != "object" && e != null )
155                         return this.empty().append( document.createTextNode( e ) );
156
157                 var t = "";
158                 jQuery.each( e || this, function(){
159                         jQuery.each( this.childNodes, function(){
160                                 if ( this.nodeType != 8 )
161                                         t += this.nodeType != 1 ?
162                                                 this.nodeValue : jQuery.fn.text([ this ]);
163                         });
164                 });
165                 return t;
166         },
167
168         wrapAll: function(html) {
169                 if ( this[0] )
170                         // The elements to wrap the target around
171                         jQuery(html, this[0].ownerDocument)
172                                 .clone()
173                                 .insertBefore(this[0])
174                                 .map(function(){
175                                         var elem = this;
176                                         while ( elem.firstChild )
177                                                 elem = elem.firstChild;
178                                         return elem;
179                                 })
180                                 .append(this);
181
182                 return this;
183         },
184
185         wrapInner: function(html) {
186                 return this.each(function(){
187                         jQuery(this).contents().wrapAll(html);
188                 });
189         },
190
191         wrap: function(html) {
192                 return this.each(function(){
193                         jQuery(this).wrapAll(html);
194                 });
195         },
196
197         append: function() {
198                 return this.domManip(arguments, true, 1, function(a){
199                         this.appendChild( a );
200                 });
201         },
202
203         prepend: function() {
204                 return this.domManip(arguments, true, -1, function(a){
205                         this.insertBefore( a, this.firstChild );
206                 });
207         },
208         
209         before: function() {
210                 return this.domManip(arguments, false, 1, function(a){
211                         this.parentNode.insertBefore( a, this );
212                 });
213         },
214
215         after: function() {
216                 return this.domManip(arguments, false, -1, function(a){
217                         this.parentNode.insertBefore( a, this.nextSibling );
218                 });
219         },
220
221         end: function() {
222                 return this.prevObject || jQuery([]);
223         },
224
225         find: function(t) {
226                 var data = jQuery.map(this, function(a){ return jQuery.find(t,a); });
227                 return this.pushStack( /[^+>] [^+>]/.test( t ) || t.indexOf("..") > -1 ?
228                         jQuery.unique( data ) : data );
229         },
230
231         clone: function(events) {
232                 // Do the clone
233                 var ret = this.map(function(){
234                         return this.outerHTML ? jQuery(this.outerHTML)[0] : this.cloneNode(true);
235                 });
236
237                 // Need to set the expando to null on the cloned set if it exists
238                 // removeData doesn't work here, IE removes it from the original as well
239                 // this is primarily for IE but the data expando shouldn't be copied over in any browser
240                 var clone = ret.find("*").andSelf().each(function(){
241                         if ( this[ expando ] != undefined )
242                                 this[ expando ] = null;
243                 });
244                 
245                 // Copy the events from the original to the clone
246                 if (events === true)
247                         this.find("*").andSelf().each(function(i) {
248                                 var events = jQuery.data(this, "events");
249                                 for ( var type in events )
250                                         for ( var handler in events[type] )
251                                                 jQuery.event.add(clone[i], type, events[type][handler], events[type][handler].data);
252                         });
253
254                 // Return the cloned set
255                 return ret;
256         },
257
258         filter: function(t) {
259                 return this.pushStack(
260                         jQuery.isFunction( t ) &&
261                         jQuery.grep(this, function(el, index){
262                                 return t.apply(el, [index]);
263                         }) ||
264
265                         jQuery.multiFilter(t,this) );
266         },
267
268         not: function(t) {
269                 return this.pushStack(
270                         t.constructor == String &&
271                         jQuery.multiFilter(t, this, true) ||
272
273                         jQuery.grep(this, function(a) {
274                                 return ( t.constructor == Array || t.jquery )
275                                         ? jQuery.inArray( a, t ) < 0
276                                         : a != t;
277                         })
278                 );
279         },
280
281         add: function(t) {
282                 return this.pushStack( jQuery.merge(
283                         this.get(),
284                         t.constructor == String ?
285                                 jQuery(t).get() :
286                                 t.length != undefined && (!t.nodeName || t.nodeName == "FORM") ?
287                                         t : [t] )
288                 );
289         },
290
291         is: function(expr) {
292                 return expr ? jQuery.multiFilter(expr,this).length > 0 : false;
293         },
294
295         hasClass: function(expr) {
296                 return this.is("." + expr);
297         },
298         
299         val: function( val ) {
300                 if ( val == undefined ) {
301                         if ( this.length ) {
302                                 var elem = this[0];
303                         
304                                 // We need to handle select boxes special
305                                 if ( jQuery.nodeName(elem, "select") ) {
306                                         var index = elem.selectedIndex,
307                                                 a = [],
308                                                 options = elem.options,
309                                                 one = elem.type == "select-one";
310                                         
311                                         // Nothing was selected
312                                         if ( index < 0 )
313                                                 return null;
314
315                                         // Loop through all the selected options
316                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
317                                                 var option = options[i];
318                                                 if ( option.selected ) {
319                                                         // Get the specifc value for the option
320                                                         var val = jQuery.browser.msie && !option.attributes["value"].specified ? option.text : option.value;
321                                                         
322                                                         // We don't need an array for one selects
323                                                         if ( one )
324                                                                 return val;
325                                                         
326                                                         // Multi-Selects return an array
327                                                         a.push(val);
328                                                 }
329                                         }
330                                         
331                                         return a;
332                                         
333                                 // Everything else, we just grab the value
334                                 } else
335                                         return this[0].value.replace(/\r/g, "");
336                         }
337                 } else
338                         return this.each(function(){
339                                 if ( val.constructor == Array && /radio|checkbox/.test(this.type) )
340                                         this.checked = (jQuery.inArray(this.value, val) >= 0 ||
341                                                 jQuery.inArray(this.name, val) >= 0);
342                                 else if ( jQuery.nodeName(this, "select") ) {
343                                         var tmp = val.constructor == Array ? val : [val];
344
345                                         jQuery("option", this).each(function(){
346                                                 this.selected = (jQuery.inArray(this.value, tmp) >= 0 ||
347                                                 jQuery.inArray(this.text, tmp) >= 0);
348                                         });
349
350                                         if ( !tmp.length )
351                                                 this.selectedIndex = -1;
352                                 } else
353                                         this.value = val;
354                         });
355         },
356         
357         html: function( val ) {
358                 return val == undefined ?
359                         ( this.length ? this[0].innerHTML : null ) :
360                         this.empty().append( val );
361         },
362
363         replaceWith: function( val ) {
364                 return this.after( val ).remove();
365         },
366
367         slice: function() {
368                 return this.pushStack( Array.prototype.slice.apply( this, arguments ) );
369         },
370
371         map: function(fn) {
372                 return this.pushStack(jQuery.map( this, function(elem,i){
373                         return fn.call( elem, i, elem );
374                 }));
375         },
376
377         andSelf: function() {
378                 return this.add( this.prevObject );
379         },
380         
381         domManip: function(args, table, dir, fn) {
382                 var clone = this.length > 1, a; 
383
384                 return this.each(function(){
385                         if ( !a ) {
386                                 a = jQuery.clean(args, this.ownerDocument);
387                                 if ( dir < 0 )
388                                         a.reverse();
389                         }
390
391                         var obj = this;
392
393                         if ( table && jQuery.nodeName(this, "table") && jQuery.nodeName(a[0], "tr") )
394                                 obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));
395
396                         jQuery.each( a, function(){
397                                 if ( jQuery.nodeName(this, "script") ) {
398                                         if ( this.src )
399                                                 jQuery.ajax({ url: this.src, async: false, dataType: "script" });
400                                         else
401                                                 jQuery.globalEval( this.text || this.textContent || this.innerHTML || "" );
402                                 } else
403                                         fn.apply( obj, [ clone ? this.cloneNode(true) : this ] );
404                         });
405                 });
406         }
407 };
408
409 jQuery.extend = jQuery.fn.extend = function() {
410         // copy reference to target object
411         var target = arguments[0] || {}, a = 1, al = arguments.length, deep = false;
412
413         // Handle a deep copy situation
414         if ( target.constructor == Boolean ) {
415                 deep = target;
416                 target = arguments[1] || {};
417         }
418
419         // extend jQuery itself if only one argument is passed
420         if ( al == 1 ) {
421                 target = this;
422                 a = 0;
423         }
424
425         var prop;
426
427         for ( ; a < al; a++ )
428                 // Only deal with non-null/undefined values
429                 if ( (prop = arguments[a]) != null )
430                         // Extend the base object
431                         for ( var i in prop ) {
432                                 // Prevent never-ending loop
433                                 if ( target == prop[i] )
434                                         continue;
435
436                                 // Recurse if we're merging object values
437                                 if ( deep && typeof prop[i] == 'object' && target[i] )
438                                         jQuery.extend( target[i], prop[i] );
439
440                                 // Don't bring in undefined values
441                                 else if ( prop[i] != undefined )
442                                         target[i] = prop[i];
443                         }
444
445         // Return the modified object
446         return target;
447 };
448
449 var expando = "jQuery" + (new Date()).getTime(), uuid = 0, win = {};
450
451 jQuery.extend({
452         noConflict: function(deep) {
453                 window.$ = _$;
454                 if ( deep )
455                         window.jQuery = _jQuery;
456                 return jQuery;
457         },
458
459         // This may seem like some crazy code, but trust me when I say that this
460         // is the only cross-browser way to do this. --John
461         isFunction: function( fn ) {
462                 return !!fn && typeof fn != "string" && !fn.nodeName && 
463                         fn.constructor != Array && /function/i.test( fn + "" );
464         },
465         
466         // check if an element is in a XML document
467         isXMLDoc: function(elem) {
468                 return elem.documentElement && !elem.body ||
469                         elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
470         },
471
472         // Evalulates a script in a global context
473         // Evaluates Async. in Safari 2 :-(
474         globalEval: function( data ) {
475                 data = jQuery.trim( data );
476                 if ( data ) {
477                         if ( window.execScript )
478                                 window.execScript( data );
479                         else if ( jQuery.browser.safari )
480                                 // safari doesn't provide a synchronous global eval
481                                 window.setTimeout( data, 0 );
482                         else
483                                 eval.call( window, data );
484                 }
485         },
486
487         nodeName: function( elem, name ) {
488                 return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
489         },
490         
491         cache: {},
492         
493         data: function( elem, name, data ) {
494                 elem = elem == window ? win : elem;
495
496                 var id = elem[ expando ];
497
498                 // Compute a unique ID for the element
499                 if ( !id ) 
500                         id = elem[ expando ] = ++uuid;
501
502                 // Only generate the data cache if we're
503                 // trying to access or manipulate it
504                 if ( name && !jQuery.cache[ id ] )
505                         jQuery.cache[ id ] = {};
506                 
507                 // Prevent overriding the named cache with undefined values
508                 if ( data != undefined )
509                         jQuery.cache[ id ][ name ] = data;
510                 
511                 // Return the named cache data, or the ID for the element       
512                 return name ? jQuery.cache[ id ][ name ] : id;
513         },
514         
515         removeData: function( elem, name ) {
516                 elem = elem == window ? win : elem;
517
518                 var id = elem[ expando ];
519
520                 // If we want to remove a specific section of the element's data
521                 if ( name ) {
522                         if ( jQuery.cache[ id ] ) {
523                                 // Remove the section of cache data
524                                 delete jQuery.cache[ id ][ name ];
525
526                                 // If we've removed all the data, remove the element's cache
527                                 name = "";
528                                 for ( name in jQuery.cache[ id ] ) break;
529                                 if ( !name )
530                                         jQuery.removeData( elem );
531                         }
532
533                 // Otherwise, we want to remove all of the element's data
534                 } else {
535                         // Clean up the element expando
536                         try {
537                                 delete elem[ expando ];
538                         } catch(e){
539                                 // IE has trouble directly removing the expando
540                                 // but it's ok with using removeAttribute
541                                 if ( elem.removeAttribute )
542                                         elem.removeAttribute( expando );
543                         }
544
545                         // Completely remove the data cache
546                         delete jQuery.cache[ id ];
547                 }
548         },
549
550         // args is for internal usage only
551         each: function( obj, fn, args ) {
552                 if ( args ) {
553                         if ( obj.length == undefined )
554                                 for ( var i in obj )
555                                         fn.apply( obj[i], args );
556                         else
557                                 for ( var i = 0, ol = obj.length; i < ol; i++ )
558                                         if ( fn.apply( obj[i], args ) === false ) break;
559
560                 // A special, fast, case for the most common use of each
561                 } else {
562                         if ( obj.length == undefined )
563                                 for ( var i in obj )
564                                         fn.call( obj[i], i, obj[i] );
565                         else
566                                 for ( var i = 0, ol = obj.length, val = obj[0]; 
567                                         i < ol && fn.call(val,i,val) !== false; val = obj[++i] ){}
568                 }
569
570                 return obj;
571         },
572         
573         prop: function(elem, value, type, index, prop){
574                         // Handle executable functions
575                         if ( jQuery.isFunction( value ) )
576                                 value = value.call( elem, [index] );
577                                 
578                         // exclude the following css properties to add px
579                         var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i;
580
581                         // Handle passing in a number to a CSS property
582                         return value && value.constructor == Number && type == "curCSS" && !exclude.test(prop) ?
583                                 value + "px" :
584                                 value;
585         },
586
587         className: {
588                 // internal only, use addClass("class")
589                 add: function( elem, c ){
590                         jQuery.each( (c || "").split(/\s+/), function(i, cur){
591                                 if ( !jQuery.className.has( elem.className, cur ) )
592                                         elem.className += ( elem.className ? " " : "" ) + cur;
593                         });
594                 },
595
596                 // internal only, use removeClass("class")
597                 remove: function( elem, c ){
598                         elem.className = c != undefined ?
599                                 jQuery.grep( elem.className.split(/\s+/), function(cur){
600                                         return !jQuery.className.has( c, cur ); 
601                                 }).join(" ") : "";
602                 },
603
604                 // internal only, use is(".class")
605                 has: function( t, c ) {
606                         return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1;
607                 }
608         },
609
610         swap: function(e,o,f) {
611                 for ( var i in o ) {
612                         e.style["old"+i] = e.style[i];
613                         e.style[i] = o[i];
614                 }
615                 f.apply( e, [] );
616                 for ( var i in o )
617                         e.style[i] = e.style["old"+i];
618         },
619
620         css: function(e,p) {
621                 if ( p == "height" || p == "width" ) {
622                         var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
623
624                         jQuery.each( d, function(){
625                                 old["padding" + this] = 0;
626                                 old["border" + this + "Width"] = 0;
627                         });
628
629                         jQuery.swap( e, old, function() {
630                                 if ( jQuery(e).is(':visible') ) {
631                                         oHeight = e.offsetHeight;
632                                         oWidth = e.offsetWidth;
633                                 } else {
634                                         e = jQuery(e.cloneNode(true))
635                                                 .find(":radio").removeAttr("checked").end()
636                                                 .css({
637                                                         visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
638                                                 }).appendTo(e.parentNode)[0];
639
640                                         var parPos = jQuery.css(e.parentNode,"position") || "static";
641                                         if ( parPos == "static" )
642                                                 e.parentNode.style.position = "relative";
643
644                                         oHeight = e.clientHeight;
645                                         oWidth = e.clientWidth;
646
647                                         if ( parPos == "static" )
648                                                 e.parentNode.style.position = "static";
649
650                                         e.parentNode.removeChild(e);
651                                 }
652                         });
653
654                         return p == "height" ? oHeight : oWidth;
655                 }
656
657                 return jQuery.curCSS( e, p );
658         },
659
660         curCSS: function(elem, prop, force) {
661                 var ret, stack = [], swap = [];
662
663                 // A helper method for determining if an element's values are broken
664                 function color(a){
665                         if ( !jQuery.browser.safari )
666                                 return false;
667
668                         var ret = document.defaultView.getComputedStyle(a,null);
669                         return !ret || ret.getPropertyValue("color") == "";
670                 }
671
672                 if (prop == "opacity" && jQuery.browser.msie) {
673                         ret = jQuery.attr(elem.style, "opacity");
674                         return ret == "" ? "1" : ret;
675                 }
676                 
677                 if (prop.match(/float/i))
678                         prop = styleFloat;
679
680                 if (!force && elem.style[prop])
681                         ret = elem.style[prop];
682
683                 else if (document.defaultView && document.defaultView.getComputedStyle) {
684
685                         if (prop.match(/float/i))
686                                 prop = "float";
687
688                         prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
689                         var cur = document.defaultView.getComputedStyle(elem, null);
690
691                         if ( cur && !color(elem) )
692                                 ret = cur.getPropertyValue(prop);
693
694                         // If the element isn't reporting its values properly in Safari
695                         // then some display: none elements are involved
696                         else {
697                                 // Locate all of the parent display: none elements
698                                 for ( var a = elem; a && color(a); a = a.parentNode )
699                                         stack.unshift(a);
700
701                                 // Go through and make them visible, but in reverse
702                                 // (It would be better if we knew the exact display type that they had)
703                                 for ( a = 0; a < stack.length; a++ )
704                                         if ( color(stack[a]) ) {
705                                                 swap[a] = stack[a].style.display;
706                                                 stack[a].style.display = "block";
707                                         }
708
709                                 // Since we flip the display style, we have to handle that
710                                 // one special, otherwise get the value
711                                 ret = prop == "display" && swap[stack.length-1] != null ?
712                                         "none" :
713                                         document.defaultView.getComputedStyle(elem,null).getPropertyValue(prop) || "";
714
715                                 // Finally, revert the display styles back
716                                 for ( a = 0; a < swap.length; a++ )
717                                         if ( swap[a] != null )
718                                                 stack[a].style.display = swap[a];
719                         }
720
721                         if ( prop == "opacity" && ret == "" )
722                                 ret = "1";
723
724                 } else if (elem.currentStyle) {
725                         var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});
726                         ret = elem.currentStyle[prop] || elem.currentStyle[newProp];
727
728                         // From the awesome hack by Dean Edwards
729                         // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
730
731                         // If we're not dealing with a regular pixel number
732                         // but a number that has a weird ending, we need to convert it to pixels
733                         if ( !/^\d+(px)?$/i.test(ret) && /^\d/.test(ret) ) {
734                                 var style = elem.style.left;
735                                 var runtimeStyle = elem.runtimeStyle.left;
736                                 elem.runtimeStyle.left = elem.currentStyle.left;
737                                 elem.style.left = ret || 0;
738                                 ret = elem.style.pixelLeft + "px";
739                                 elem.style.left = style;
740                                 elem.runtimeStyle.left = runtimeStyle;
741                         }
742                 }
743
744                 return ret;
745         },
746         
747         clean: function(a, doc) {
748                 var r = [];
749                 doc = doc || document;
750
751                 jQuery.each( a, function(i,arg){
752                         if ( !arg ) return;
753
754                         if ( arg.constructor == Number )
755                                 arg = arg.toString();
756                         
757                         // Convert html string into DOM nodes
758                         if ( typeof arg == "string" ) {
759                                 // Fix "XHTML"-style tags in all browsers
760                                 arg = arg.replace(/(<(\w+)[^>]*?)\/>/g, function(m, all, tag){
761                                         return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area)$/i)? m : all+"></"+tag+">";
762                                 });
763
764                                 // Trim whitespace, otherwise indexOf won't work as expected
765                                 var s = jQuery.trim(arg).toLowerCase(), div = doc.createElement("div"), tb = [];
766
767                                 var wrap =
768                                         // option or optgroup
769                                         !s.indexOf("<opt") &&
770                                         [1, "<select>", "</select>"] ||
771                                         
772                                         !s.indexOf("<leg") &&
773                                         [1, "<fieldset>", "</fieldset>"] ||
774                                         
775                                         s.match(/^<(thead|tbody|tfoot|colg|cap)/) &&
776                                         [1, "<table>", "</table>"] ||
777                                         
778                                         !s.indexOf("<tr") &&
779                                         [2, "<table><tbody>", "</tbody></table>"] ||
780                                         
781                                         // <thead> matched above
782                                         (!s.indexOf("<td") || !s.indexOf("<th")) &&
783                                         [3, "<table><tbody><tr>", "</tr></tbody></table>"] ||
784                                         
785                                         !s.indexOf("<col") &&
786                                         [2, "<table><tbody></tbody><colgroup>", "</colgroup></table>"] ||
787
788                                         // IE can't serialize <link> and <script> tags normally
789                                         jQuery.browser.msie &&
790                                         [1, "div<div>", "</div>"] ||
791                                         
792                                         [0,"",""];
793
794                                 // Go to html and back, then peel off extra wrappers
795                                 div.innerHTML = wrap[1] + arg + wrap[2];
796                                 
797                                 // Move to the right depth
798                                 while ( wrap[0]-- )
799                                         div = div.lastChild;
800                                 
801                                 // Remove IE's autoinserted <tbody> from table fragments
802                                 if ( jQuery.browser.msie ) {
803                                         
804                                         // String was a <table>, *may* have spurious <tbody>
805                                         if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 ) 
806                                                 tb = div.firstChild && div.firstChild.childNodes;
807                                                 
808                                         // String was a bare <thead> or <tfoot>
809                                         else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )
810                                                 tb = div.childNodes;
811
812                                         for ( var n = tb.length-1; n >= 0 ; --n )
813                                                 if ( jQuery.nodeName(tb[n], "tbody") && !tb[n].childNodes.length )
814                                                         tb[n].parentNode.removeChild(tb[n]);
815         
816                                         // IE completely kills leading whitespace when innerHTML is used        
817                                         if ( /^\s/.test(arg) )  
818                                                 div.insertBefore( doc.createTextNode( arg.match(/^\s*/)[0] ), div.firstChild );
819
820                                 }
821                                 
822                                 arg = jQuery.makeArray( div.childNodes );
823                         }
824
825                         if ( 0 === arg.length && (!jQuery.nodeName(arg, "form") && !jQuery.nodeName(arg, "select")) )
826                                 return;
827
828                         if ( arg[0] == undefined || jQuery.nodeName(arg, "form") || arg.options )
829                                 r.push( arg );
830                         else
831                                 r = jQuery.merge( r, arg );
832
833                 });
834
835                 return r;
836         },
837         
838         attr: function(elem, name, value){
839                 var fix = jQuery.isXMLDoc(elem) ? {} : jQuery.props;
840
841                 // Safari mis-reports the default selected property of a hidden option
842                 // Accessing the parent's selectedIndex property fixes it
843                 if ( name == "selected" && jQuery.browser.safari )
844                         elem.parentNode.selectedIndex;
845                 
846                 // Certain attributes only work when accessed via the old DOM 0 way
847                 if ( fix[name] ) {
848                         if ( value != undefined ) elem[fix[name]] = value;
849                         return elem[fix[name]];
850                 } else if ( jQuery.browser.msie && name == "style" )
851                         return jQuery.attr( elem.style, "cssText", value );
852
853                 else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName(elem, "form") && (name == "action" || name == "method") )
854                         return elem.getAttributeNode(name).nodeValue;
855
856                 // IE elem.getAttribute passes even for style
857                 else if ( elem.tagName ) {
858
859                         if ( value != undefined ) {
860                                 if ( name == "type" && jQuery.nodeName(elem,"input") && elem.parentNode )
861                                         throw "type property can't be changed";
862                                 elem.setAttribute( name, value );
863                         }
864
865                         if ( jQuery.browser.msie && /href|src/.test(name) && !jQuery.isXMLDoc(elem) ) 
866                                 return elem.getAttribute( name, 2 );
867
868                         return elem.getAttribute( name );
869
870                 // elem is actually elem.style ... set the style
871                 } else {
872                         // IE actually uses filters for opacity
873                         if ( name == "opacity" && jQuery.browser.msie ) {
874                                 if ( value != undefined ) {
875                                         // IE has trouble with opacity if it does not have layout
876                                         // Force it by setting the zoom level
877                                         elem.zoom = 1; 
878         
879                                         // Set the alpha filter to set the opacity
880                                         elem.filter = (elem.filter || "").replace(/alpha\([^)]*\)/,"") +
881                                                 (parseFloat(value).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
882                                 }
883         
884                                 return elem.filter ? 
885                                         (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() : "";
886                         }
887                         name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});
888                         if ( value != undefined ) elem[name] = value;
889                         return elem[name];
890                 }
891         },
892         
893         trim: function(t){
894                 return (t||"").replace(/^\s+|\s+$/g, "");
895         },
896
897         makeArray: function( a ) {
898                 var r = [];
899
900                 // Need to use typeof to fight Safari childNodes crashes
901                 if ( typeof a != "array" )
902                         for ( var i = 0, al = a.length; i < al; i++ )
903                                 r.push( a[i] );
904                 else
905                         r = a.slice( 0 );
906
907                 return r;
908         },
909
910         inArray: function( b, a ) {
911                 for ( var i = 0, al = a.length; i < al; i++ )
912                         if ( a[i] == b )
913                                 return i;
914                 return -1;
915         },
916
917         merge: function(first, second) {
918                 // We have to loop this way because IE & Opera overwrite the length
919                 // expando of getElementsByTagName
920
921                 // Also, we need to make sure that the correct elements are being returned
922                 // (IE returns comment nodes in a '*' query)
923                 if ( jQuery.browser.msie ) {
924                         for ( var i = 0; second[i]; i++ )
925                                 if ( second[i].nodeType != 8 )
926                                         first.push(second[i]);
927                 } else
928                         for ( var i = 0; second[i]; i++ )
929                                 first.push(second[i]);
930
931                 return first;
932         },
933
934         unique: function(first) {
935                 var r = [], done = {};
936
937                 try {
938                         for ( var i = 0, fl = first.length; i < fl; i++ ) {
939                                 var id = jQuery.data(first[i]);
940                                 if ( !done[id] ) {
941                                         done[id] = true;
942                                         r.push(first[i]);
943                                 }
944                         }
945                 } catch(e) {
946                         r = first;
947                 }
948
949                 return r;
950         },
951
952         grep: function(elems, fn, inv) {
953                 // If a string is passed in for the function, make a function
954                 // for it (a handy shortcut)
955                 if ( typeof fn == "string" )
956                         fn = eval("false||function(a,i){return " + fn + "}");
957
958                 var result = [];
959
960                 // Go through the array, only saving the items
961                 // that pass the validator function
962                 for ( var i = 0, el = elems.length; i < el; i++ )
963                         if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
964                                 result.push( elems[i] );
965
966                 return result;
967         },
968
969         map: function(elems, fn) {
970                 // If a string is passed in for the function, make a function
971                 // for it (a handy shortcut)
972                 if ( typeof fn == "string" )
973                         fn = eval("false||function(a){return " + fn + "}");
974
975                 var result = [];
976
977                 // Go through the array, translating each of the items to their
978                 // new value (or values).
979                 for ( var i = 0, el = elems.length; i < el; i++ ) {
980                         var val = fn(elems[i],i);
981
982                         if ( val !== null && val != undefined ) {
983                                 if ( val.constructor != Array ) val = [val];
984                                 result = result.concat( val );
985                         }
986                 }
987
988                 return result;
989         }
990 });
991
992 var userAgent = navigator.userAgent.toLowerCase();
993
994 // Figure out what browser is being used
995 jQuery.browser = {
996         version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
997         safari: /webkit/.test(userAgent),
998         opera: /opera/.test(userAgent),
999         msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
1000         mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
1001 };
1002
1003 var styleFloat = jQuery.browser.msie ? "styleFloat" : "cssFloat";
1004         
1005 jQuery.extend({
1006         // Check to see if the W3C box model is being used
1007         boxModel: !jQuery.browser.msie || document.compatMode == "CSS1Compat",
1008         
1009         styleFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",
1010         
1011         props: {
1012                 "for": "htmlFor",
1013                 "class": "className",
1014                 "float": styleFloat,
1015                 cssFloat: styleFloat,
1016                 styleFloat: styleFloat,
1017                 innerHTML: "innerHTML",
1018                 className: "className",
1019                 value: "value",
1020                 disabled: "disabled",
1021                 checked: "checked",
1022                 readonly: "readOnly",
1023                 selected: "selected",
1024                 maxlength: "maxLength"
1025         }
1026 });
1027
1028 jQuery.each({
1029         parent: "a.parentNode",
1030         parents: "jQuery.dir(a,'parentNode')",
1031         next: "jQuery.nth(a,2,'nextSibling')",
1032         prev: "jQuery.nth(a,2,'previousSibling')",
1033         nextAll: "jQuery.dir(a,'nextSibling')",
1034         prevAll: "jQuery.dir(a,'previousSibling')",
1035         siblings: "jQuery.sibling(a.parentNode.firstChild,a)",
1036         children: "jQuery.sibling(a.firstChild)",
1037         contents: "jQuery.nodeName(a,'iframe')?a.contentDocument||a.contentWindow.document:jQuery.makeArray(a.childNodes)"
1038 }, function(i,n){
1039         jQuery.fn[ i ] = function(a) {
1040                 var ret = jQuery.map(this,n);
1041                 if ( a && typeof a == "string" )
1042                         ret = jQuery.multiFilter(a,ret);
1043                 return this.pushStack( jQuery.unique(ret) );
1044         };
1045 });
1046
1047 jQuery.each({
1048         appendTo: "append",
1049         prependTo: "prepend",
1050         insertBefore: "before",
1051         insertAfter: "after",
1052         replaceAll: "replaceWith"
1053 }, function(i,n){
1054         jQuery.fn[ i ] = function(){
1055                 var a = arguments;
1056                 return this.each(function(){
1057                         for ( var j = 0, al = a.length; j < al; j++ )
1058                                 jQuery(a[j])[n]( this );
1059                 });
1060         };
1061 });
1062
1063 jQuery.each( {
1064         removeAttr: function( key ) {
1065                 jQuery.attr( this, key, "" );
1066                 this.removeAttribute( key );
1067         },
1068         addClass: function(c){
1069                 jQuery.className.add(this,c);
1070         },
1071         removeClass: function(c){
1072                 jQuery.className.remove(this,c);
1073         },
1074         toggleClass: function( c ){
1075                 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);
1076         },
1077         remove: function(a){
1078                 if ( !a || jQuery.filter( a, [this] ).r.length ) {
1079                         jQuery.removeData( this );
1080                         this.parentNode.removeChild( this );
1081                 }
1082         },
1083         empty: function() {
1084                 // Clean up the cache
1085                 jQuery("*", this).each(function(){ jQuery.removeData(this); });
1086
1087                 while ( this.firstChild )
1088                         this.removeChild( this.firstChild );
1089         }
1090 }, function(i,n){
1091         jQuery.fn[ i ] = function() {
1092                 return this.each( n, arguments );
1093         };
1094 });
1095
1096 jQuery.each( [ "Height", "Width" ], function(i,name){
1097         var n = name.toLowerCase();
1098         
1099         jQuery.fn[ n ] = function(h) {
1100                 return this[0] == window ?
1101                         jQuery.browser.safari && self["inner" + name] ||
1102                         jQuery.boxModel && Math.max(document.documentElement["client" + name], document.body["client" + name]) ||
1103                         document.body["client" + name] :
1104                 
1105                         this[0] == document ?
1106                                 Math.max( document.body["scroll" + name], document.body["offset" + name] ) :
1107         
1108                                 h == undefined ?
1109                                         ( this.length ? jQuery.css( this[0], n ) : null ) :
1110                                         this.css( n, h.constructor == String ? h : h + "px" );
1111         };
1112 });