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