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