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