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