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