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