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