Fixed add to also create HTML on-the-fly by using jQuery() instead of jQuery.find()
[jquery.git] / src / jquery / jquery.js
1 /*
2  * jQuery @VERSION - New Wave Javascript
3  *
4  * Copyright (c) 2006 John Resig (jquery.com)
5  * Dual licensed under the MIT (MIT-LICENSE.txt)
6  * and GPL (GPL-LICENSE.txt) licenses.
7  *
8  * $Date$
9  * $Rev$
10  */
11
12 // Global undefined variable
13 window.undefined = window.undefined;
14
15 /**
16  * Create a new jQuery Object
17  *
18  * @constructor
19  * @private
20  * @name jQuery
21  * @param String|Function|Element|Array<Element>|jQuery a selector
22  * @param jQuery|Element|Array<Element> c context
23  * @cat Core
24  */
25 var jQuery = function(a,c) {
26         // If the context is global, return a new object
27         if ( window == this )
28                 return new jQuery(a,c);
29
30         // Make sure that a selection was provided
31         a = a || document;
32         
33         // HANDLE: $(function)
34         // Shortcut for document ready
35         // Safari reports typeof on DOM NodeLists as a function
36         if ( typeof a == "function" && !a.nodeType && a[0] == undefined )
37                 return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a );
38         
39         // Handle HTML strings
40         if ( typeof a  == "string" ) {
41                 // HANDLE: $(html) -> $(array)
42                 var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
43                 if ( m )
44                         a = jQuery.clean( [ m[1] ] );
45                 
46                 // HANDLE: $(expr)
47                 else
48                         return new jQuery( c ).find( a );
49         }
50         
51         return this.setArray(
52                 // HANDLE: $(array)
53                 a.constructor == Array && a ||
54
55                 // HANDLE: $(arraylike)
56                 // Watch for when an array-like object is passed as the selector
57                 (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a ) ||
58
59                 // HANDLE: $(*)
60                 [ a ] );
61 };
62
63 // Map over the $ in case of overwrite
64 if ( typeof $ != "undefined" )
65         jQuery._$ = $;
66         
67 // Map the jQuery namespace to the '$' one
68 var $ = jQuery;
69
70 /**
71  * This function accepts a string containing a CSS or
72  * basic XPath selector which is then used to match a set of elements.
73  *
74  * The core functionality of jQuery centers around this function.
75  * Everything in jQuery is based upon this, or uses this in some way.
76  * The most basic use of this function is to pass in an expression
77  * (usually consisting of CSS or XPath), which then finds all matching
78  * elements.
79  *
80  * By default, $() looks for DOM elements within the context of the
81  * current HTML document.
82  *
83  * @example $("div > p")
84  * @desc Finds all p elements that are children of a div element.
85  * @before <p>one</p> <div><p>two</p></div> <p>three</p>
86  * @result [ <p>two</p> ]
87  *
88  * @example $("input:radio", document.forms[0])
89  * @desc Searches for all inputs of type radio within the first form in the document
90  *
91  * @example $("div", xml.responseXML)
92  * @desc This finds all div elements within the specified XML document.
93  *
94  * @name $
95  * @param String expr An expression to search with
96  * @param Element|jQuery context (optional) A DOM Element, Document or jQuery to use as context
97  * @cat Core
98  * @type jQuery
99  * @see $(Element)
100  * @see $(Element<Array>)
101  */
102  
103 /**
104  * Create DOM elements on-the-fly from the provided String of raw HTML.
105  *
106  * @example $("<div><p>Hello</p></div>").appendTo("#body")
107  * @desc Creates a div element (and all of its contents) dynamically, 
108  * and appends it to the element with the ID of body. Internally, an
109  * element is created and it's innerHTML property set to the given markup.
110  * It is therefore both quite flexible and limited. 
111  *
112  * @name $
113  * @param String html A string of HTML to create on the fly.
114  * @cat Core
115  * @type jQuery
116  * @see appendTo(String)
117  */
118
119 /**
120  * Wrap jQuery functionality around a single or multiple DOM Element(s).
121  *
122  * This function also accepts XML Documents and Window objects
123  * as valid arguments (even though they are not DOM Elements).
124  *
125  * @example $(document).find("div > p")
126  * @before <p>one</p> <div><p>two</p></div> <p>three</p>
127  * @result [ <p>two</p> ]
128  * @desc Same as $("div > p") because the document
129  *
130  * @example $(document.body).background( "black" );
131  * @desc Sets the background color of the page to black.
132  *
133  * @example $( myForm.elements ).hide()
134  * @desc Hides all the input elements within a form
135  *
136  * @name $
137  * @param Element|Array<Element> elems DOM element(s) to be encapsulated by a jQuery object.
138  * @cat Core
139  * @type jQuery
140  */
141
142 /**
143  * A shorthand for $(document).ready(), allowing you to bind a function
144  * to be executed when the DOM document has finished loading. This function
145  * behaves just like $(document).ready(), in that it should be used to wrap
146  * all of the other $() operations on your page. While this function is,
147  * technically, chainable - there really isn't much use for chaining against it.
148  * You can have as many $(document).ready events on your page as you like.
149  *
150  * See ready(Function) for details about the ready event. 
151  * 
152  * @example $(function(){
153  *   // Document is ready
154  * });
155  * @desc Executes the function when the DOM is ready to be used.
156  *
157  * @name $
158  * @param Function fn The function to execute when the DOM is ready.
159  * @cat Core
160  * @type jQuery
161  */
162
163 /**
164  * A means of creating a cloned copy of a jQuery object. This function
165  * copies the set of matched elements from one jQuery object and creates
166  * another, new, jQuery object containing the same elements.
167  *
168  * @example var div = $("div");
169  * $( div ).find("p");
170  * @desc Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div' (as would normally be the case if a simple div.find("p") was done).
171  *
172  * @name $
173  * @param jQuery obj The jQuery object to be cloned.
174  * @cat Core
175  * @type jQuery
176  */
177
178 jQuery.fn = jQuery.prototype = {
179         /**
180          * The current version of jQuery.
181          *
182          * @private
183          * @property
184          * @name jquery
185          * @type String
186          * @cat Core
187          */
188         jquery: "@VERSION",
189
190         /**
191          * The number of elements currently matched.
192          *
193          * @example $("img").length;
194          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
195          * @result 2
196          *
197          * @property
198          * @name length
199          * @type Number
200          * @cat Core
201          */
202
203         /**
204          * The number of elements currently matched.
205          *
206          * @example $("img").size();
207          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
208          * @result 2
209          *
210          * @name size
211          * @type Number
212          * @cat Core
213          */
214         size: function() {
215                 return this.length;
216         },
217         
218         length: 0,
219
220         /**
221          * Access all matched elements. This serves as a backwards-compatible
222          * way of accessing all matched elements (other than the jQuery object
223          * itself, which is, in fact, an array of elements).
224          *
225          * @example $("img").get();
226          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
227          * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
228          * @desc Selects all images in the document and returns the DOM Elements as an Array
229          *
230          * @name get
231          * @type Array<Element>
232          * @cat Core
233          */
234
235         /**
236          * Access a single matched element. num is used to access the
237          * Nth element matched.
238          *
239          * @example $("img").get(0);
240          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
241          * @result [ <img src="test1.jpg"/> ]
242          * @desc Selects all images in the document and returns the first one
243          *
244          * @name get
245          * @type Element
246          * @param Number num Access the element in the Nth position.
247          * @cat Core
248          */
249         get: function( num ) {
250                 return num == undefined ?
251
252                         // Return a 'clean' array
253                         jQuery.makeArray( this ) :
254
255                         // Return just the object
256                         this[num];
257         },
258         
259         /**
260          * Set the jQuery object to an array of elements, while maintaining
261          * the stack.
262          *
263          * @example $("img").set([ document.body ]);
264          * @result $("img").set() == [ document.body ]
265          *
266          * @private
267          * @name set
268          * @type jQuery
269          * @param Elements elems An array of elements
270          * @cat Core
271          */
272         set: function( a ) {
273                 var ret = jQuery(this);
274                 ret.prevObject = this;
275                 return ret.setArray( a );
276         },
277         
278         /**
279          * Set the jQuery object to an array of elements. This operation is
280          * completely destructive - be sure to use .set() if you wish to maintain
281          * the jQuery stack.
282          *
283          * @example $("img").setArray([ document.body ]);
284          * @result $("img").setArray() == [ document.body ]
285          *
286          * @private
287          * @name setArray
288          * @type jQuery
289          * @param Elements elems An array of elements
290          * @cat Core
291          */
292         setArray: function( a ) {
293                 this.length = 0;
294                 [].push.apply( this, a );
295                 return this;
296         },
297
298         /**
299          * Execute a function within the context of every matched element.
300          * This means that every time the passed-in function is executed
301          * (which is once for every element matched) the 'this' keyword
302          * points to the specific element.
303          *
304          * Additionally, the function, when executed, is passed a single
305          * argument representing the position of the element in the matched
306          * set.
307          *
308          * @example $("img").each(function(i){
309          *   this.src = "test" + i + ".jpg";
310          * });
311          * @before <img/><img/>
312          * @result <img src="test0.jpg"/><img src="test1.jpg"/>
313          * @desc Iterates over two images and sets their src property
314          *
315          * @name each
316          * @type jQuery
317          * @param Function fn A function to execute
318          * @cat Core
319          */
320         each: function( fn, args ) {
321                 return jQuery.each( this, fn, args );
322         },
323
324         /**
325          * Searches every matched element for the object and returns
326          * the index of the element, if found, starting with zero. 
327          * Returns -1 if the object wasn't found.
328          *
329          * @example $("*").index( $('#foobar')[0] ) 
330          * @before <div id="foobar"></div><b></b><span id="foo"></span>
331          * @result 0
332          * @desc Returns the index for the element with ID foobar
333          *
334          * @example $("*").index( $('#foo')) 
335          * @before <div id="foobar"></div><b></b><span id="foo"></span>
336          * @result 2
337          * @desc Returns the index for the element with ID foo
338          *
339          * @example $("*").index( $('#bar')) 
340          * @before <div id="foobar"></div><b></b><span id="foo"></span>
341          * @result -1
342          * @desc Returns -1, as there is no element with ID bar
343          *
344          * @name index
345          * @type Number
346          * @param Element subject Object to search for
347          * @cat Core
348          */
349         index: function( obj ) {
350                 var pos = -1;
351                 this.each(function(i){
352                         if ( this == obj ) pos = i;
353                 });
354                 return pos;
355         },
356
357         /**
358          * Access a property on the first matched element.
359          * This method makes it easy to retrieve a property value
360          * from the first matched element.
361          *
362          * @example $("img").attr("src");
363          * @before <img src="test.jpg"/>
364          * @result test.jpg
365          * @desc Returns the src attribute from the first image in the document.
366          *
367          * @name attr
368          * @type Object
369          * @param String name The name of the property to access.
370          * @cat DOM/Attributes
371          */
372
373         /**
374          * Set a key/value object as properties to all matched elements.
375          *
376          * This serves as the best way to set a large number of properties
377          * on all matched elements.
378          *
379          * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
380          * @before <img/>
381          * @result <img src="test.jpg" alt="Test Image"/>
382          * @desc Sets src and alt attributes to all images.
383          *
384          * @name attr
385          * @type jQuery
386          * @param Map properties Key/value pairs to set as object properties.
387          * @cat DOM/Attributes
388          */
389
390         /**
391          * Set a single property to a value, on all matched elements.
392          *
393          * Can compute values provided as ${formula}, see second example.
394          *
395          * Note that you can't set the name property of input elements in IE.
396          * Use $(html) or .append(html) or .html(html) to create elements
397          * on the fly including the name property.
398          *
399          * @example $("img").attr("src","test.jpg");
400          * @before <img/>
401          * @result <img src="test.jpg"/>
402          * @desc Sets src attribute to all images.
403          *
404          * @example $("img").attr("title", "${this.src}");
405          * @before <img src="test.jpg" />
406          * @result <img src="test.jpg" title="test.jpg" />
407          * @desc Sets title attribute from src attribute, a shortcut for attr(String,Function)
408          *
409          * @name attr
410          * @type jQuery
411          * @param String key The name of the property to set.
412          * @param Object value The value to set the property to.
413          * @cat DOM/Attributes
414          */
415          
416         /**
417          * Set a single property to a computed value, on all matched elements.
418          *
419          * Instead of a value, a function is provided, that computes the value.
420          *
421          * @example $("img").attr("title", function() { return this.src });
422          * @before <img src="test.jpg" />
423          * @result <img src="test.jpg" title="test.jpg" />
424          * @desc Sets title attribute from src attribute.
425          *
426          * @name attr
427          * @type jQuery
428          * @param String key The name of the property to set.
429          * @param Function value A function returning the value to set.
430          * @cat DOM/Attributes
431          */
432         attr: function( key, value, type ) {
433                 var obj = key;
434                 
435                 // Look for the case where we're accessing a style value
436                 if ( key.constructor == String )
437                         if ( value == undefined )
438                                 return jQuery[ type || "attr" ]( this[0], key );
439                         else {
440                                 obj = {};
441                                 obj[ key ] = value;
442                         }
443                 
444                 // Check to see if we're setting style values
445                 return this.each(function(){
446                         // Set all the styles
447                         for ( var prop in obj )
448                                 jQuery.attr(
449                                         type ? this.style : this,
450                                         prop, jQuery.prop(this, prop, obj[prop], type)
451                                 );
452                 });
453         },
454
455         /**
456          * Access a style property on the first matched element.
457          * This method makes it easy to retrieve a style property value
458          * from the first matched element.
459          *
460          * @example $("p").css("color");
461          * @before <p style="color:red;">Test Paragraph.</p>
462          * @result "red"
463          * @desc Retrieves the color style of the first paragraph
464          *
465          * @example $("p").css("font-weight");
466          * @before <p style="font-weight: bold;">Test Paragraph.</p>
467          * @result "bold"
468          * @desc Retrieves the font-weight style of the first paragraph.
469          *
470          * @name css
471          * @type String
472          * @param String name The name of the property to access.
473          * @cat CSS
474          */
475
476         /**
477          * Set a key/value object as style properties to all matched elements.
478          *
479          * This serves as the best way to set a large number of style properties
480          * on all matched elements.
481          *
482          * @example $("p").css({ color: "red", background: "blue" });
483          * @before <p>Test Paragraph.</p>
484          * @result <p style="color:red; background:blue;">Test Paragraph.</p>
485          * @desc Sets color and background styles to all p elements.
486          *
487          * @name css
488          * @type jQuery
489          * @param Map properties Key/value pairs to set as style properties.
490          * @cat CSS
491          */
492
493         /**
494          * Set a single style property to a value, on all matched elements.
495          *
496          * @example $("p").css("color","red");
497          * @before <p>Test Paragraph.</p>
498          * @result <p style="color:red;">Test Paragraph.</p>
499          * @desc Changes the color of all paragraphs to red
500          *
501          * @name css
502          * @type jQuery
503          * @param String key The name of the property to set.
504          * @param Object value The value to set the property to.
505          * @cat CSS
506          */
507         css: function( key, value ) {
508                 return this.attr( key, value, "curCSS" );
509         },
510
511         /**
512          * Get the text contents of all matched elements. The result is
513          * a string that contains the combined text contents of all matched
514          * elements. This method works on both HTML and XML documents.
515          *
516          * @example $("p").text();
517          * @before <p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>
518          * @result Test Paragraph.Paraparagraph
519          * @desc Gets the concatenated text of all paragraphs
520          *
521          * @name text
522          * @type String
523          * @cat DOM/Attributes
524          */
525
526         /**
527          * Set the text contents of all matched elements.
528          *
529          * Similar to html(), but escapes HTML (replace "<" and ">" with their
530          * HTML entities).
531          *
532          * @example $("p").text("<b>Some</b> new text.");
533          * @before <p>Test Paragraph.</p>
534          * @result <p>&lt;b&gt;Some&lt;/b&gt; new text.</p>
535          * @desc Sets the text of all paragraphs.
536          *
537          * @example $("p").text("<b>Some</b> new text.", true);
538          * @before <p>Test Paragraph.</p>
539          * @result <p>Some new text.</p>
540          * @desc Sets the text of all paragraphs.
541          *
542          * @name text
543          * @type String
544          * @param String val The text value to set the contents of the element to.
545          * @cat DOM/Attributes
546          */
547         text: function(e) {
548                 var type = this.length && this[0].innerText == undefined ?
549                         "textContent" : "innerText";
550                         
551                 return e == undefined ?
552                         this.length && this[0][ type ] :
553                         this.each(function(){ this[ type ] = e; });
554         },
555
556         /**
557          * Wrap all matched elements with a structure of other elements.
558          * This wrapping process is most useful for injecting additional
559          * stucture into a document, without ruining the original semantic
560          * qualities of a document.
561          *
562          * This works by going through the first element
563          * provided (which is generated, on the fly, from the provided HTML)
564          * and finds the deepest ancestor element within its
565          * structure - it is that element that will en-wrap everything else.
566          *
567          * This does not work with elements that contain text. Any necessary text
568          * must be added after the wrapping is done.
569          *
570          * @example $("p").wrap("<div class='wrap'></div>");
571          * @before <p>Test Paragraph.</p>
572          * @result <div class='wrap'><p>Test Paragraph.</p></div>
573          * 
574          * @name wrap
575          * @type jQuery
576          * @param String html A string of HTML, that will be created on the fly and wrapped around the target.
577          * @cat DOM/Manipulation
578          */
579
580         /**
581          * Wrap all matched elements with a structure of other elements.
582          * This wrapping process is most useful for injecting additional
583          * stucture into a document, without ruining the original semantic
584          * qualities of a document.
585          *
586          * This works by going through the first element
587          * provided and finding the deepest ancestor element within its
588          * structure - it is that element that will en-wrap everything else.
589          *
590          * This does not work with elements that contain text. Any necessary text
591          * must be added after the wrapping is done.
592          *
593          * @example $("p").wrap( document.getElementById('content') );
594          * @before <p>Test Paragraph.</p><div id="content"></div>
595          * @result <div id="content"><p>Test Paragraph.</p></div>
596          *
597          * @name wrap
598          * @type jQuery
599          * @param Element elem A DOM element that will be wrapped around the target.
600          * @cat DOM/Manipulation
601          */
602         wrap: function() {
603                 // The elements to wrap the target around
604                 var a = jQuery.clean(arguments);
605
606                 // Wrap each of the matched elements individually
607                 return this.each(function(){
608                         // Clone the structure that we're using to wrap
609                         var b = a[0].cloneNode(true);
610
611                         // Insert it before the element to be wrapped
612                         this.parentNode.insertBefore( b, this );
613
614                         // Find the deepest point in the wrap structure
615                         while ( b.firstChild )
616                                 b = b.firstChild;
617
618                         // Move the matched element to within the wrap structure
619                         b.appendChild( this );
620                 });
621         },
622
623         /**
624          * Append content to the inside of every matched element.
625          *
626          * This operation is similar to doing an appendChild to all the
627          * specified elements, adding them into the document.
628          *
629          * @example $("p").append("<b>Hello</b>");
630          * @before <p>I would like to say: </p>
631          * @result <p>I would like to say: <b>Hello</b></p>
632          * @desc Appends some HTML to all paragraphs.
633          *
634          * @example $("p").append( $("#foo")[0] );
635          * @before <p>I would like to say: </p><b id="foo">Hello</b>
636          * @result <p>I would like to say: <b id="foo">Hello</b></p>
637          * @desc Appends an Element to all paragraphs.
638          *
639          * @example $("p").append( $("b") );
640          * @before <p>I would like to say: </p><b>Hello</b>
641          * @result <p>I would like to say: <b>Hello</b></p>
642          * @desc Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
643          *
644          * @name append
645          * @type jQuery
646          * @param <Content> content Content to append to the target
647          * @cat DOM/Manipulation
648          * @see prepend(<Content>)
649          * @see before(<Content>)
650          * @see after(<Content>)
651          */
652         append: function() {
653                 return this.domManip(arguments, true, 1, function(a){
654                         this.appendChild( a );
655                 });
656         },
657
658         /**
659          * Prepend content to the inside of every matched element.
660          *
661          * This operation is the best way to insert elements
662          * inside, at the beginning, of all matched elements.
663          *
664          * @example $("p").prepend("<b>Hello</b>");
665          * @before <p>I would like to say: </p>
666          * @result <p><b>Hello</b>I would like to say: </p>
667          * @desc Prepends some HTML to all paragraphs.
668          *
669          * @example $("p").prepend( $("#foo")[0] );
670          * @before <p>I would like to say: </p><b id="foo">Hello</b>
671          * @result <p><b id="foo">Hello</b>I would like to say: </p>
672          * @desc Prepends an Element to all paragraphs.
673          *      
674          * @example $("p").prepend( $("b") );
675          * @before <p>I would like to say: </p><b>Hello</b>
676          * @result <p><b>Hello</b>I would like to say: </p>
677          * @desc Prepends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
678          *
679          * @name prepend
680          * @type jQuery
681          * @param <Content> content Content to prepend to the target.
682          * @cat DOM/Manipulation
683          * @see append(<Content>)
684          * @see before(<Content>)
685          * @see after(<Content>)
686          */
687         prepend: function() {
688                 return this.domManip(arguments, true, -1, function(a){
689                         this.insertBefore( a, this.firstChild );
690                 });
691         },
692         
693         /**
694          * Insert content before each of the matched elements.
695          *
696          * @example $("p").before("<b>Hello</b>");
697          * @before <p>I would like to say: </p>
698          * @result <b>Hello</b><p>I would like to say: </p>
699          * @desc Inserts some HTML before all paragraphs.
700          *
701          * @example $("p").before( $("#foo")[0] );
702          * @before <p>I would like to say: </p><b id="foo">Hello</b>
703          * @result <b id="foo">Hello</b><p>I would like to say: </p>
704          * @desc Inserts an Element before all paragraphs.
705          *
706          * @example $("p").before( $("b") );
707          * @before <p>I would like to say: </p><b>Hello</b>
708          * @result <b>Hello</b><p>I would like to say: </p>
709          * @desc Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs.
710          *
711          * @name before
712          * @type jQuery
713          * @param <Content> content Content to insert before each target.
714          * @cat DOM/Manipulation
715          * @see append(<Content>)
716          * @see prepend(<Content>)
717          * @see after(<Content>)
718          */
719         before: function() {
720                 return this.domManip(arguments, false, 1, function(a){
721                         this.parentNode.insertBefore( a, this );
722                 });
723         },
724
725         /**
726          * Insert content after each of the matched elements.
727          *
728          * @example $("p").after("<b>Hello</b>");
729          * @before <p>I would like to say: </p>
730          * @result <p>I would like to say: </p><b>Hello</b>
731          * @desc Inserts some HTML after all paragraphs.
732          *
733          * @example $("p").after( $("#foo")[0] );
734          * @before <b id="foo">Hello</b><p>I would like to say: </p>
735          * @result <p>I would like to say: </p><b id="foo">Hello</b>
736          * @desc Inserts an Element after all paragraphs.
737          *
738          * @example $("p").after( $("b") );
739          * @before <b>Hello</b><p>I would like to say: </p>
740          * @result <p>I would like to say: </p><b>Hello</b>
741          * @desc Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs.
742          *
743          * @name after
744          * @type jQuery
745          * @param <Content> content Content to insert after each target.
746          * @cat DOM/Manipulation
747          * @see append(<Content>)
748          * @see prepend(<Content>)
749          * @see before(<Content>)
750          */
751         after: function() {
752                 return this.domManip(arguments, false, -1, function(a){
753                         this.parentNode.insertBefore( a, this.nextSibling );
754                 });
755         },
756
757         /**
758          * End the most recent 'destructive' operation, reverting the list of matched elements
759          * back to its previous state. After an end operation, the list of matched elements will
760          * revert to the last state of matched elements.
761          *
762          * If there was no destructive operation before, an empty set is returned.
763          *
764          * @example $("p").find("span").end();
765          * @before <p><span>Hello</span>, how are you?</p>
766          * @result [ <p>...</p> ]
767          * @desc Selects all paragraphs, finds span elements inside these, and reverts the
768          * selection back to the paragraphs.
769          *
770          * @name end
771          * @type jQuery
772          * @cat DOM/Traversing
773          */
774         end: function() {
775                 return this.prevObject || jQuery([]);
776         },
777
778         /**
779          * Searches for all elements that match the specified expression.
780          
781          * This method is a good way to find additional descendant
782          * elements with which to process.
783          *
784          * All searching is done using a jQuery expression. The expression can be
785          * written using CSS 1-3 Selector syntax, or basic XPath.
786          *
787          * @example $("p").find("span");
788          * @before <p><span>Hello</span>, how are you?</p>
789          * @result [ <span>Hello</span> ]
790          * @desc Starts with all paragraphs and searches for descendant span
791          * elements, same as $("p span")
792          *
793          * @name find
794          * @type jQuery
795          * @param String expr An expression to search with.
796          * @cat DOM/Traversing
797          */
798         find: function(t) {
799                 return this.set( jQuery.map( this, function(a){
800                         return jQuery.find(t,a);
801                 }) );
802         },
803
804         /**
805          * Clone matched DOM Elements and select the clones. 
806          *
807          * This is useful for moving copies of the elements to another
808          * location in the DOM.
809          *
810          * @example $("b").clone().prependTo("p");
811          * @before <b>Hello</b><p>, how are you?</p>
812          * @result <b>Hello</b><p><b>Hello</b>, how are you?</p>
813          * @desc Clones all b elements (and selects the clones) and prepends them to all paragraphs.
814          *
815          * @name clone
816          * @type jQuery
817          * @cat DOM/Manipulation
818          */
819         clone: function(deep) {
820                 return this.set( jQuery.map( this, function(a){
821                         return a.cloneNode( deep != undefined ? deep : true );
822                 }) );
823         },
824
825         /**
826          * Removes all elements from the set of matched elements that do not
827          * match the specified expression(s). This method is used to narrow down
828          * the results of a search.
829          *
830          * Provide a String array of expressions to apply multiple filters at once.
831          *
832          * @example $("p").filter(".selected")
833          * @before <p class="selected">Hello</p><p>How are you?</p>
834          * @result [ <p class="selected">Hello</p> ]
835          * @desc Selects all paragraphs and removes those without a class "selected".
836          *
837          * @example $("p").filter([".selected", ":first"])
838          * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
839          * @result [ <p>Hello</p>, <p class="selected">And Again</p> ]
840          * @desc Selects all paragraphs and removes those without class "selected" and being the first one.
841          *
842          * @name filter
843          * @type jQuery
844          * @param String|Array<String> expression Expression(s) to search with.
845          * @cat DOM/Traversing
846          */
847          
848         /**
849          * Removes all elements from the set of matched elements that do not
850          * pass the specified filter. This method is used to narrow down
851          * the results of a search.
852          *
853          * @example $("p").filter(function(index) {
854          *   return $("ol", this).length == 0;
855          * })
856          * @before <p><ol><li>Hello</li></ol></p><p>How are you?</p>
857          * @result [ <p>How are you?</p> ]
858          * @desc Remove all elements that have a child ol element
859          *
860          * @name filter
861          * @type jQuery
862          * @param Function filter A function to use for filtering
863          * @cat DOM/Traversing
864          */
865         filter: function(t) {
866                 return this.set(
867                         t.constructor == Array &&
868                         jQuery.map(this,function(a){
869                                 for ( var i = 0, tl = t.length; i < tl; i++ )
870                                         if ( jQuery.filter(t[i],[a]).r.length )
871                                                 return a;
872                                 return null;
873                         }) ||
874
875                         t.constructor == Boolean &&
876                         ( t ? this.get() : [] ) ||
877
878                         typeof t == "function" &&
879                         jQuery.grep( this, function(el, index) { return t.apply(el, [index]) }) ||
880
881                         jQuery.filter(t,this).r );
882         },
883
884         /**
885          * Removes the specified Element from the set of matched elements. This
886          * method is used to remove a single Element from a jQuery object.
887          *
888          * @example $("p").not( $("#selected")[0] )
889          * @before <p>Hello</p><p id="selected">Hello Again</p>
890          * @result [ <p>Hello</p> ]
891          * @desc Removes the element with the ID "selected" from the set of all paragraphs.
892          *
893          * @name not
894          * @type jQuery
895          * @param Element el An element to remove from the set
896          * @cat DOM/Traversing
897          */
898
899         /**
900          * Removes elements matching the specified expression from the set
901          * of matched elements. This method is used to remove one or more
902          * elements from a jQuery object.
903          *
904          * @example $("p").not("#selected")
905          * @before <p>Hello</p><p id="selected">Hello Again</p>
906          * @result [ <p>Hello</p> ]
907          * @desc Removes the element with the ID "selected" from the set of all paragraphs.
908          *
909          * @name not
910          * @type jQuery
911          * @param String expr An expression with which to remove matching elements
912          * @cat DOM/Traversing
913          */
914         not: function(t) {
915                 return this.set( typeof t == "string" ?
916                         jQuery.filter(t,this,true).r :
917                         jQuery.grep(this,function(a){ return a != t; }) );
918         },
919
920         /**
921          * Adds the elements matched by the expression to the jQuery object. This
922          * can be used to concatenate the result sets of two expressions.
923          *
924          * @example $("p").add("span")
925          * @before <p>Hello</p><p><span>Hello Again</span></p>
926          * @result [ <p>Hello</p>, <span>Hello Again</span> ]
927          *
928          * @name add
929          * @type jQuery
930          * @param String expr An expression whose matched elements are added
931          * @cat DOM/Traversing
932          */
933          
934         /**
935          * Adds the on the fly created elements to the jQuery object.
936          *
937          * @example $("p").add("<span>Again</span>")
938          * @before <p>Hello</p>
939          * @result [ <p>Hello</p>, <span>Again</span> ]
940          *
941          * @name add
942          * @type jQuery
943          * @param String html A string of HTML to create on the fly.
944          * @cat DOM/Traversing
945          */
946
947         /**
948          * Adds one or more Elements to the set of matched elements.
949          *
950          * This is used to add a set of Elements to a jQuery object.
951          *
952          * @example $("p").add( document.getElementById("a") )
953          * @before <p>Hello</p><p><span id="a">Hello Again</span></p>
954          * @result [ <p>Hello</p>, <span id="a">Hello Again</span> ]
955          *
956          * @example $("p").add([document.getElementById("a"), document.getElementById("b")])
957          * @before <p>Hello</p><p><span id="a">Hello Again</span><span id="b">And Again</span></p>
958          * @result [ <p>Hello</p>, <span id="a">Hello Again</span>, <span id="b">And Again</span> ]
959          *
960          * @name add
961          * @type jQuery
962          * @param Element|Array<Element> elements One or more Elements to add
963          * @cat DOM/Traversing
964          */
965         add: function(t) {
966                 return this.set( jQuery.merge(
967                         this.get(),
968                         typeof t == "string" ? jQuery(t).get() : t )
969                 );
970         },
971
972         /**
973          * Checks the current selection against an expression and returns true,
974          * if at least one element of the selection fits the given expression.
975          *
976          * Does return false, if no element fits or the expression is not valid.
977          *
978          * filter(String) is used internally, therefore all rules that apply there
979          * apply here, too.
980          *
981          * @example $("input[@type='checkbox']").parent().is("form")
982          * @before <form><input type="checkbox" /></form>
983          * @result true
984          * @desc Returns true, because the parent of the input is a form element
985          * 
986          * @example $("input[@type='checkbox']").parent().is("form")
987          * @before <form><p><input type="checkbox" /></p></form>
988          * @result false
989          * @desc Returns false, because the parent of the input is a p element
990          *
991          * @name is
992          * @type Boolean
993          * @param String expr The expression with which to filter
994          * @cat DOM/Traversing
995          */
996         is: function(expr) {
997                 return expr ? jQuery.filter(expr,this).r.length > 0 : false;
998         },
999         
1000         /**
1001          * Get the current value of the first matched element.
1002          *
1003          * @example $("input").val();
1004          * @before <input type="text" value="some text"/>
1005          * @result "some text"
1006          *
1007          * @name val
1008          * @type String
1009          * @cat DOM/Attributes
1010          */
1011         
1012         /**
1013          * Set the value of every matched element.
1014          *
1015          * @example $("input").val("test");
1016          * @before <input type="text" value="some text"/>
1017          * @result <input type="text" value="test"/>
1018          *
1019          * @name val
1020          * @type jQuery
1021          * @param String val Set the property to the specified value.
1022          * @cat DOM/Attributes
1023          */
1024         val: function( val ) {
1025                 return val == undefined ?\r                      ( this.length ? this[0].value : null ) :\r                       this.attr( "value", val );
1026         },
1027         
1028         /**
1029          * Get the html contents of the first matched element.
1030          * This property is not available on XML documents.
1031          *
1032          * @example $("div").html();
1033          * @before <div><input/></div>
1034          * @result <input/>
1035          *
1036          * @name html
1037          * @type String
1038          * @cat DOM/Attributes
1039          */
1040         
1041         /**
1042          * Set the html contents of every matched element.
1043          * This property is not available on XML documents.
1044          *
1045          * @example $("div").html("<b>new stuff</b>");
1046          * @before <div><input/></div>
1047          * @result <div><b>new stuff</b></div>
1048          *
1049          * @name html
1050          * @type jQuery
1051          * @param String val Set the html contents to the specified value.
1052          * @cat DOM/Attributes
1053          */
1054         html: function( val ) {
1055                 return val == undefined ?\r                      ( this.length ? this[0].innerHTML : null ) :\r                   this.empty().append( val );
1056         },
1057         
1058         /**
1059          * @private
1060          * @name domManip
1061          * @param Array args
1062          * @param Boolean table Insert TBODY in TABLEs if one is not found.
1063          * @param Number dir If dir<0, process args in reverse order.
1064          * @param Function fn The function doing the DOM manipulation.
1065          * @type jQuery
1066          * @cat Core
1067          */
1068         domManip: function(args, table, dir, fn){
1069                 var clone = this.length > 1; 
1070                 var a = jQuery.clean(args);
1071                 if ( dir < 0 )
1072                         a.reverse();
1073
1074                 return this.each(function(){
1075                         var obj = this;
1076
1077                         if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" )
1078                                 obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));
1079
1080                         for ( var i = 0, al = a.length; i < al; i++ )
1081                                 fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );
1082
1083                 });
1084         }
1085 };
1086
1087 /**
1088  * Extends the jQuery object itself. Can be used to add functions into
1089  * the jQuery namespace and to add plugin methods (plugins).
1090  * 
1091  * @example jQuery.fn.extend({
1092  *   check: function() {
1093  *     return this.each(function() { this.checked = true; });
1094  *   },
1095  *   uncheck: function() {
1096  *     return this.each(function() { this.checked = false; });
1097  *   }
1098  * });
1099  * $("input[@type=checkbox]").check();
1100  * $("input[@type=radio]").uncheck();
1101  * @desc Adds two plugin methods.
1102  *
1103  * @example jQuery.extend({
1104  *   min: function(a, b) { return a < b ? a : b; },
1105  *   max: function(a, b) { return a > b ? a : b; }
1106  * });
1107  * @desc Adds two functions into the jQuery namespace
1108  *
1109  * @name $.extend
1110  * @param Object prop The object that will be merged into the jQuery object
1111  * @type Object
1112  * @cat Core
1113  */
1114
1115 /**
1116  * Extend one object with one or more others, returning the original,
1117  * modified, object. This is a great utility for simple inheritance.
1118  * 
1119  * @example var settings = { validate: false, limit: 5, name: "foo" };
1120  * var options = { validate: true, name: "bar" };
1121  * jQuery.extend(settings, options);
1122  * @result settings == { validate: true, limit: 5, name: "bar" }
1123  * @desc Merge settings and options, modifying settings
1124  *
1125  * @example var defaults = { validate: false, limit: 5, name: "foo" };
1126  * var options = { validate: true, name: "bar" };
1127  * var settings = jQuery.extend({}, defaults, options);
1128  * @result settings == { validate: true, limit: 5, name: "bar" }
1129  * @desc Merge defaults and options, without modifying the defaults
1130  *
1131  * @name $.extend
1132  * @param Object target The object to extend
1133  * @param Object prop1 The object that will be merged into the first.
1134  * @param Object propN (optional) More objects to merge into the first
1135  * @type Object
1136  * @cat JavaScript
1137  */
1138 jQuery.extend = jQuery.fn.extend = function() {
1139         // copy reference to target object
1140         var target = arguments[0],
1141                 a = 1;
1142
1143         // extend jQuery itself if only one argument is passed
1144         if ( arguments.length == 1 ) {
1145                 target = this;
1146                 a = 0;
1147         }
1148         var prop;
1149         while (prop = arguments[a++])
1150                 // Extend the base object
1151                 for ( var i in prop ) target[i] = prop[i];
1152
1153         // Return the modified object
1154         return target;
1155 };
1156
1157 jQuery.extend({
1158         /**
1159          * Run this function to give control of the $ variable back
1160          * to whichever library first implemented it. This helps to make 
1161          * sure that jQuery doesn't conflict with the $ object
1162          * of other libraries.
1163          *
1164          * By using this function, you will only be able to access jQuery
1165          * using the 'jQuery' variable. For example, where you used to do
1166          * $("div p"), you now must do jQuery("div p").
1167          *
1168          * @example jQuery.noConflict();
1169          * // Do something with jQuery
1170          * jQuery("div p").hide();
1171          * // Do something with another library's $()
1172          * $("content").style.display = 'none';
1173          * @desc Maps the original object that was referenced by $ back to $
1174          *
1175          * @example jQuery.noConflict();
1176          * (function($) { 
1177          *   $(function() {
1178          *     // more code using $ as alias to jQuery
1179          *   });
1180          * })(jQuery);
1181          * // other code using $ as an alias to the other library
1182          * @desc Reverts the $ alias and then creates and executes a
1183          * function to provide the $ as a jQuery alias inside the functions
1184          * scope. Inside the function the original $ object is not available.
1185          * This works well for most plugins that don't rely on any other library.
1186          * 
1187          *
1188          * @name $.noConflict
1189          * @type undefined
1190          * @cat Core 
1191          */
1192         noConflict: function() {
1193                 if ( jQuery._$ )
1194                         $ = jQuery._$;
1195         },
1196
1197         /**
1198          * A generic iterator function, which can be used to seemlessly
1199          * iterate over both objects and arrays. This function is not the same
1200          * as $().each() - which is used to iterate, exclusively, over a jQuery
1201          * object. This function can be used to iterate over anything.
1202          *
1203          * The callback has two arguments:the key (objects) or index (arrays) as first
1204          * the first, and the value as the second.
1205          *
1206          * @example $.each( [0,1,2], function(i, n){
1207          *   alert( "Item #" + i + ": " + n );
1208          * });
1209          * @desc This is an example of iterating over the items in an array,
1210          * accessing both the current item and its index.
1211          *
1212          * @example $.each( { name: "John", lang: "JS" }, function(i, n){
1213          *   alert( "Name: " + i + ", Value: " + n );
1214          * });
1215          *
1216          * @desc This is an example of iterating over the properties in an
1217          * Object, accessing both the current item and its key.
1218          *
1219          * @name $.each
1220          * @param Object obj The object, or array, to iterate over.
1221          * @param Function fn The function that will be executed on every object.
1222          * @type Object
1223          * @cat JavaScript
1224          */
1225         // args is for internal usage only
1226         each: function( obj, fn, args ) {
1227                 if ( obj.length == undefined )
1228                         for ( var i in obj )
1229                                 fn.apply( obj[i], args || [i, obj[i]] );
1230                 else
1231                         for ( var i = 0, ol = obj.length; i < ol; i++ )
1232                                 if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break;
1233                 return obj;
1234         },
1235         
1236         prop: function(elem, key, value){
1237                 // Handle executable functions
1238                 return value.constructor == Function &&
1239                         value.call( elem ) || value;
1240         },
1241
1242         className: {
1243                 // internal only, use addClass("class")
1244                 add: function( elem, c ){
1245                         jQuery.each( c.split(/\s+/), function(i, cur){
1246                                 if ( !jQuery.className.has( elem.className, cur ) )
1247                                         elem.className += ( elem.className ? " " : "" ) + cur;
1248                         });
1249                 },
1250                 // internal only, use removeClass("class")
1251                 remove: function( elem, c ){
1252             elem.className = c ?
1253                 jQuery.grep( elem.className.split(/\s+/), function(cur){
1254                                     return !jQuery.className.has( c, cur );     
1255                 }).join(' ') : "";
1256                 },
1257                 // internal only, use is(".class")
1258                 has: function( t, c ) {
1259                         t = t.className || t;
1260                         return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t );
1261                 }
1262         },
1263
1264         /**
1265          * Swap in/out style options.
1266          * @private
1267          */
1268         swap: function(e,o,f) {
1269                 for ( var i in o ) {
1270                         e.style["old"+i] = e.style[i];
1271                         e.style[i] = o[i];
1272                 }
1273                 f.apply( e, [] );
1274                 for ( var i in o )
1275                         e.style[i] = e.style["old"+i];
1276         },
1277
1278         css: function(e,p) {
1279                 if ( p == "height" || p == "width" ) {
1280                         var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
1281
1282                         for ( var i = 0, dl = d.length; i < dl; i++ ) {
1283                                 old["padding" + d[i]] = 0;
1284                                 old["border" + d[i] + "Width"] = 0;
1285                         }
1286
1287                         jQuery.swap( e, old, function() {
1288                                 if (jQuery.css(e,"display") != "none") {
1289                                         oHeight = e.offsetHeight;
1290                                         oWidth = e.offsetWidth;
1291                                 } else {
1292                                         e = jQuery(e.cloneNode(true))
1293                                                 .find(":radio").removeAttr("checked").end()
1294                                                 .css({
1295                                                         visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
1296                                                 }).appendTo(e.parentNode)[0];
1297
1298                                         var parPos = jQuery.css(e.parentNode,"position");
1299                                         if ( parPos == "" || parPos == "static" )
1300                                                 e.parentNode.style.position = "relative";
1301
1302                                         oHeight = e.clientHeight;
1303                                         oWidth = e.clientWidth;
1304
1305                                         if ( parPos == "" || parPos == "static" )
1306                                                 e.parentNode.style.position = "static";
1307
1308                                         e.parentNode.removeChild(e);
1309                                 }
1310                         });
1311
1312                         return p == "height" ? oHeight : oWidth;
1313                 }
1314
1315                 return jQuery.curCSS( e, p );
1316         },
1317
1318         curCSS: function(elem, prop, force) {
1319                 var ret;
1320                 
1321                 if (prop == 'opacity' && jQuery.browser.msie)
1322                         return jQuery.attr(elem.style, 'opacity');
1323                         
1324                 if (prop == "float" || prop == "cssFloat")
1325                     prop = jQuery.browser.msie ? "styleFloat" : "cssFloat";
1326
1327                 if (!force && elem.style[prop])
1328                         ret = elem.style[prop];
1329
1330                 else if (document.defaultView && document.defaultView.getComputedStyle) {
1331
1332                         if (prop == "cssFloat" || prop == "styleFloat")
1333                                 prop = "float";
1334
1335                         prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
1336                         var cur = document.defaultView.getComputedStyle(elem, null);
1337
1338                         if ( cur )
1339                                 ret = cur.getPropertyValue(prop);
1340                         else if ( prop == 'display' )
1341                                 ret = 'none';
1342                         else
1343                                 jQuery.swap(elem, { display: 'block' }, function() {
1344                                     var c = document.defaultView.getComputedStyle(this, '');
1345                                     ret = c && c.getPropertyValue(prop) || '';
1346                                 });
1347
1348                 } else if (elem.currentStyle) {
1349
1350                         var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});
1351                         ret = elem.currentStyle[prop] || elem.currentStyle[newProp];
1352                         
1353                 }
1354
1355                 return ret;
1356         },
1357         
1358         clean: function(a) {
1359                 var r = [];
1360                 
1361                 for ( var i = 0, al = a.length; i < al; i++ ) {
1362                         var arg = a[i];
1363                         
1364                          // Convert html string into DOM nodes
1365                         if ( typeof arg == "string" ) {
1366                                 // Trim whitespace, otherwise indexOf won't work as expected
1367                                 var s = jQuery.trim(arg), div = document.createElement("div"), tb = [];
1368
1369                                 var wrap =
1370                                          // option or optgroup
1371                                         !s.indexOf("<opt") &&
1372                                         [1, "<select>", "</select>"] ||
1373                                         
1374                                         (!s.indexOf("<thead") || !s.indexOf("<tbody") || !s.indexOf("<tfoot")) &&
1375                                         [1, "<table>", "</table>"] ||
1376                                         
1377                                         !s.indexOf("<tr") &&
1378                                         [2, "<table><tbody>", "</tbody></table>"] ||
1379                                         
1380                                         // <thead> matched above
1381                                         (!s.indexOf("<td") || !s.indexOf("<th")) &&
1382                                         [3, "<table><tbody><tr>", "</tr></tbody></table>"] ||
1383                                         
1384                                         [0,"",""];
1385
1386                                 // Go to html and back, then peel off extra wrappers
1387                                 div.innerHTML = wrap[1] + s + wrap[2];
1388                                 
1389                                 // Move to the right depth
1390                                 while ( wrap[0]-- )
1391                                         div = div.firstChild;
1392                                 
1393                                 // Remove IE's autoinserted <tbody> from table fragments
1394                                 if ( jQuery.browser.msie ) {
1395                                         
1396                                         // String was a <table>, *may* have spurious <tbody>
1397                                         if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 ) 
1398                                                 tb = div.firstChild && div.firstChild.childNodes;
1399                                                 
1400                                         // String was a bare <thead> or <tfoot>
1401                                         else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )
1402                                                 tb = div.childNodes;
1403
1404                                         for ( var n = tb.length-1; n >= 0 ; --n )
1405                                                 if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length )
1406                                                         tb[n].parentNode.removeChild(tb[n]);
1407                                         
1408                                 }
1409                                 
1410                                 arg = div.childNodes;
1411                         }
1412                         
1413                         if ( arg.nodeType )
1414                                 r.push( arg );
1415                         else
1416                                 r = jQuery.merge( r, arg );
1417
1418                 }
1419
1420                 return r;
1421         },
1422         
1423         attr: function(elem, name, value){
1424                 var fix = {
1425                         "for": "htmlFor",
1426                         "class": "className",
1427                         "float": jQuery.browser.msie ? "styleFloat" : "cssFloat",
1428                         cssFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",
1429                         innerHTML: "innerHTML",
1430                         className: "className",
1431                         value: "value",
1432                         disabled: "disabled",
1433                         checked: "checked",
1434                         readonly: "readOnly",
1435                         selected: "selected"
1436                 };
1437                 
1438                 // IE actually uses filters for opacity ... elem is actually elem.style
1439                 if ( name == "opacity" && jQuery.browser.msie && value != undefined ) {
1440                         // IE has trouble with opacity if it does not have layout
1441                         // Force it by setting the zoom level
1442                         elem.zoom = 1; 
1443
1444                         // Set the alpha filter to set the opacity
1445                         return elem.filter = elem.filter.replace(/alpha\([^\)]*\)/gi,"") +
1446                                 ( value == 1 ? "" : "alpha(opacity=" + value * 100 + ")" );
1447
1448                 } else if ( name == "opacity" && jQuery.browser.msie )
1449                         return elem.filter ? 
1450                                 parseFloat( elem.filter.match(/alpha\(opacity=(.*)\)/)[1] ) / 100 : 1;
1451                 
1452                 // Mozilla doesn't play well with opacity 1
1453                 if ( name == "opacity" && jQuery.browser.mozilla && value == 1 )
1454                         value = 0.9999;
1455
1456                 // Certain attributes only work when accessed via the old DOM 0 way
1457                 if ( fix[name] ) {
1458                         if ( value != undefined ) elem[fix[name]] = value;
1459                         return elem[fix[name]];
1460
1461                 } else if ( value == undefined && jQuery.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == 'FORM' && (name == 'action' || name == 'method') )
1462                         return elem.getAttributeNode(name).nodeValue;
1463
1464                 // IE elem.getAttribute passes even for style
1465                 else if ( elem.tagName ) {
1466                         if ( value != undefined ) elem.setAttribute( name, value );
1467                         return elem.getAttribute( name );
1468
1469                 } else {
1470                         name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});
1471                         if ( value != undefined ) elem[name] = value;
1472                         return elem[name];
1473                 }
1474         },
1475         
1476         /**
1477          * Remove the whitespace from the beginning and end of a string.
1478          *
1479          * @example $.trim("  hello, how are you?  ");
1480          * @result "hello, how are you?"
1481          *
1482          * @name $.trim
1483          * @type String
1484          * @param String str The string to trim.
1485          * @cat JavaScript
1486          */
1487         trim: function(t){
1488                 return t.replace(/^\s+|\s+$/g, "");
1489         },
1490
1491         makeArray: function( a ) {
1492                 var r = [];
1493
1494                 if ( a.constructor != Array )
1495                         for ( var i = 0, al = a.length; i < al; i++ )
1496                                 r.push( a[i] );
1497                 else
1498                         r = a.slice( 0 );
1499
1500                 return r;
1501         },
1502
1503         inArray: function( b, a ) {
1504                 for ( var i = 0, al = a.length; i < al; i++ )
1505                         if ( a[i] == b )
1506                                 return i;
1507                 return -1;
1508         },
1509
1510         /**
1511          * Merge two arrays together, removing all duplicates.
1512          *
1513          * The new array is: All the results from the first array, followed
1514          * by the unique results from the second array.
1515          *
1516          * @example $.merge( [0,1,2], [2,3,4] )
1517          * @result [0,1,2,3,4]
1518          * @desc Merges two arrays, removing the duplicate 2
1519          *
1520          * @example $.merge( [3,2,1], [4,3,2] )
1521          * @result [3,2,1,4]
1522          * @desc Merges two arrays, removing the duplicates 3 and 2
1523          *
1524          * @name $.merge
1525          * @type Array
1526          * @param Array first The first array to merge.
1527          * @param Array second The second array to merge.
1528          * @cat JavaScript
1529          */
1530         merge: function(first, second) {
1531                 var r = [].slice.call( first, 0 );
1532
1533                 // Now check for duplicates between the two arrays
1534                 // and only add the unique items
1535                 for ( var i = 0, sl = second.length; i < sl; i++ )
1536                         // Check for duplicates
1537                         if ( jQuery.inArray( second[i], r ) == -1 )
1538                                 // The item is unique, add it
1539                                 first.push( second[i] );
1540
1541                 return first;
1542         },
1543
1544         /**
1545          * Filter items out of an array, by using a filter function.
1546          *
1547          * The specified function will be passed two arguments: The
1548          * current array item and the index of the item in the array. The
1549          * function must return 'true' to keep the item in the array, 
1550          * false to remove it.
1551          *
1552          * @example $.grep( [0,1,2], function(i){
1553          *   return i > 0;
1554          * });
1555          * @result [1, 2]
1556          *
1557          * @name $.grep
1558          * @type Array
1559          * @param Array array The Array to find items in.
1560          * @param Function fn The function to process each item against.
1561          * @param Boolean inv Invert the selection - select the opposite of the function.
1562          * @cat JavaScript
1563          */
1564         grep: function(elems, fn, inv) {
1565                 // If a string is passed in for the function, make a function
1566                 // for it (a handy shortcut)
1567                 if ( typeof fn == "string" )
1568                         fn = new Function("a","i","return " + fn);
1569
1570                 var result = [];
1571
1572                 // Go through the array, only saving the items
1573                 // that pass the validator function
1574                 for ( var i = 0, el = elems.length; i < el; i++ )
1575                         if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
1576                                 result.push( elems[i] );
1577
1578                 return result;
1579         },
1580
1581         /**
1582          * Translate all items in an array to another array of items.
1583          *
1584          * The translation function that is provided to this method is 
1585          * called for each item in the array and is passed one argument: 
1586          * The item to be translated.
1587          *
1588          * The function can then return the translated value, 'null'
1589          * (to remove the item), or  an array of values - which will
1590          * be flattened into the full array.
1591          *
1592          * @example $.map( [0,1,2], function(i){
1593          *   return i + 4;
1594          * });
1595          * @result [4, 5, 6]
1596          * @desc Maps the original array to a new one and adds 4 to each value.
1597          *
1598          * @example $.map( [0,1,2], function(i){
1599          *   return i > 0 ? i + 1 : null;
1600          * });
1601          * @result [2, 3]
1602          * @desc Maps the original array to a new one and adds 1 to each
1603          * value if it is bigger then zero, otherwise it's removed-
1604          * 
1605          * @example $.map( [0,1,2], function(i){
1606          *   return [ i, i + 1 ];
1607          * });
1608          * @result [0, 1, 1, 2, 2, 3]
1609          * @desc Maps the original array to a new one, each element is added
1610          * with it's original value and the value plus one.
1611          *
1612          * @name $.map
1613          * @type Array
1614          * @param Array array The Array to translate.
1615          * @param Function fn The function to process each item against.
1616          * @cat JavaScript
1617          */
1618         map: function(elems, fn) {
1619                 // If a string is passed in for the function, make a function
1620                 // for it (a handy shortcut)
1621                 if ( typeof fn == "string" )
1622                         fn = new Function("a","return " + fn);
1623
1624                 var result = [], r = [];
1625
1626                 // Go through the array, translating each of the items to their
1627                 // new value (or values).
1628                 for ( var i = 0, el = elems.length; i < el; i++ ) {
1629                         var val = fn(elems[i],i);
1630
1631                         if ( val !== null && val != undefined ) {
1632                                 if ( val.constructor != Array ) val = [val];
1633                                 result = result.concat( val );
1634                         }
1635                 }
1636
1637                 var r = result.length ? [ result[0] ] : [];
1638
1639                 check: for ( var i = 1, rl = result.length; i < rl; i++ ) {
1640                         for ( var j = 0; j < i; j++ )
1641                                 if ( result[i] == r[j] )
1642                                         continue check;
1643
1644                         r.push( result[i] );
1645                 }
1646
1647                 return r;
1648         }
1649 });
1650
1651 /**
1652  * Contains flags for the useragent, read from navigator.userAgent.
1653  * Available flags are: safari, opera, msie, mozilla
1654  *
1655  * This property is available before the DOM is ready, therefore you can
1656  * use it to add ready events only for certain browsers.
1657  *
1658  * There are situations where object detections is not reliable enough, in that
1659  * cases it makes sense to use browser detection. Simply try to avoid both!
1660  *
1661  * A combination of browser and object detection yields quite reliable results.
1662  *
1663  * @example $.browser.msie
1664  * @desc Returns true if the current useragent is some version of microsoft's internet explorer
1665  *
1666  * @example if($.browser.safari) { $( function() { alert("this is safari!"); } ); }
1667  * @desc Alerts "this is safari!" only for safari browsers
1668  *
1669  * @property
1670  * @name $.browser
1671  * @type Boolean
1672  * @cat JavaScript
1673  */
1674  
1675 /*
1676  * Wheather the W3C compliant box model is being used.
1677  *
1678  * @property
1679  * @name $.boxModel
1680  * @type Boolean
1681  * @cat JavaScript
1682  */
1683 new function() {
1684         var b = navigator.userAgent.toLowerCase();
1685
1686         // Figure out what browser is being used
1687         jQuery.browser = {
1688                 safari: /webkit/.test(b),
1689                 opera: /opera/.test(b),
1690                 msie: /msie/.test(b) && !/opera/.test(b),
1691                 mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
1692         };
1693
1694         // Check to see if the W3C box model is being used
1695         jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
1696 };
1697
1698 /**
1699  * Get a set of elements containing the unique parents of the matched
1700  * set of elements.
1701  *
1702  * Can be filtered with an optional expressions.
1703  *
1704  * @example $("p").parent()
1705  * @before <div><p>Hello</p><p>Hello</p></div>
1706  * @result [ <div><p>Hello</p><p>Hello</p></div> ]
1707  * @desc Find the parent element of each paragraph.
1708  *
1709  * @example $("p").parent(".selected")
1710  * @before <div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
1711  * @result [ <div class="selected"><p>Hello Again</p></div> ]
1712  * @desc Find the parent element of each paragraph with a class "selected".
1713  *
1714  * @name parent
1715  * @type jQuery
1716  * @param String expr (optional) An expression to filter the parents with
1717  * @cat DOM/Traversing
1718  */
1719
1720 /**
1721  * Get a set of elements containing the unique ancestors of the matched
1722  * set of elements (except for the root element).
1723  *
1724  * Can be filtered with an optional expressions.
1725  *
1726  * @example $("span").parents()
1727  * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
1728  * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
1729  * @desc Find all parent elements of each span.
1730  *
1731  * @example $("span").parents("p")
1732  * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
1733  * @result [ <p><span>Hello</span></p> ]
1734  * @desc Find all parent elements of each span that is a paragraph.
1735  *
1736  * @name parents
1737  * @type jQuery
1738  * @param String expr (optional) An expression to filter the ancestors with
1739  * @cat DOM/Traversing
1740  */
1741
1742 /**
1743  * Get a set of elements containing the unique next siblings of each of the
1744  * matched set of elements.
1745  *
1746  * It only returns the very next sibling, not all next siblings.
1747  *
1748  * Can be filtered with an optional expressions.
1749  *
1750  * @example $("p").next()
1751  * @before <p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
1752  * @result [ <p>Hello Again</p>, <div><span>And Again</span></div> ]
1753  * @desc Find the very next sibling of each paragraph.
1754  *
1755  * @example $("p").next(".selected")
1756  * @before <p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
1757  * @result [ <p class="selected">Hello Again</p> ]
1758  * @desc Find the very next sibling of each paragraph that has a class "selected".
1759  *
1760  * @name next
1761  * @type jQuery
1762  * @param String expr (optional) An expression to filter the next Elements with
1763  * @cat DOM/Traversing
1764  */
1765
1766 /**
1767  * Get a set of elements containing the unique previous siblings of each of the
1768  * matched set of elements.
1769  *
1770  * Can be filtered with an optional expressions.
1771  *
1772  * It only returns the immediately previous sibling, not all previous siblings.
1773  *
1774  * @example $("p").prev()
1775  * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
1776  * @result [ <div><span>Hello Again</span></div> ]
1777  * @desc Find the very previous sibling of each paragraph.
1778  *
1779  * @example $("p").prev(".selected")
1780  * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
1781  * @result [ <div><span>Hello</span></div> ]
1782  * @desc Find the very previous sibling of each paragraph that has a class "selected".
1783  *
1784  * @name prev
1785  * @type jQuery
1786  * @param String expr (optional) An expression to filter the previous Elements with
1787  * @cat DOM/Traversing
1788  */
1789
1790 /**
1791  * Get a set of elements containing all of the unique siblings of each of the
1792  * matched set of elements.
1793  *
1794  * Can be filtered with an optional expressions.
1795  *
1796  * @example $("div").siblings()
1797  * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
1798  * @result [ <p>Hello</p>, <p>And Again</p> ]
1799  * @desc Find all siblings of each div.
1800  *
1801  * @example $("div").siblings(".selected")
1802  * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
1803  * @result [ <p class="selected">Hello Again</p> ]
1804  * @desc Find all siblings with a class "selected" of each div.
1805  *
1806  * @name siblings
1807  * @type jQuery
1808  * @param String expr (optional) An expression to filter the sibling Elements with
1809  * @cat DOM/Traversing
1810  */
1811
1812 /**
1813  * Get a set of elements containing all of the unique children of each of the
1814  * matched set of elements.
1815  *
1816  * Can be filtered with an optional expressions.
1817  *
1818  * @example $("div").children()
1819  * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
1820  * @result [ <span>Hello Again</span> ]
1821  * @desc Find all children of each div.
1822  *
1823  * @example $("div").children(".selected")
1824  * @before <div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>
1825  * @result [ <p class="selected">Hello Again</p> ]
1826  * @desc Find all children with a class "selected" of each div.
1827  *
1828  * @name children
1829  * @type jQuery
1830  * @param String expr (optional) An expression to filter the child Elements with
1831  * @cat DOM/Traversing
1832  */
1833 jQuery.each({
1834         parent: "a.parentNode",
1835         parents: "jQuery.parents(a)",
1836         next: "jQuery.nth(a,2,'nextSibling')",
1837         prev: "jQuery.nth(a,2,'previousSibling')",
1838         siblings: "jQuery.sibling(a.parentNode.firstChild,a)",
1839         children: "jQuery.sibling(a.firstChild)"
1840 }, function(i,n){
1841         jQuery.fn[ i ] = function(a) {
1842                 var ret = jQuery.map(this,n);
1843                 if ( a && typeof a == "string" )
1844                         ret = jQuery.filter(a,ret).r;
1845                 return this.set( ret );
1846         };
1847 });
1848
1849 /**
1850  * Append all of the matched elements to another, specified, set of elements.
1851  * This operation is, essentially, the reverse of doing a regular
1852  * $(A).append(B), in that instead of appending B to A, you're appending
1853  * A to B.
1854  *
1855  * @example $("p").appendTo("#foo");
1856  * @before <p>I would like to say: </p><div id="foo"></div>
1857  * @result <div id="foo"><p>I would like to say: </p></div>
1858  * @desc Appends all paragraphs to the element with the ID "foo"
1859  *
1860  * @name appendTo
1861  * @type jQuery
1862  * @param String expr A jQuery expression of elements to match.
1863  * @cat DOM/Manipulation
1864  */
1865
1866 /**
1867  * Prepend all of the matched elements to another, specified, set of elements.
1868  * This operation is, essentially, the reverse of doing a regular
1869  * $(A).prepend(B), in that instead of prepending B to A, you're prepending
1870  * A to B.
1871  *
1872  * @example $("p").prependTo("#foo");
1873  * @before <p>I would like to say: </p><div id="foo"><b>Hello</b></div>
1874  * @result <div id="foo"><p>I would like to say: </p><b>Hello</b></div>
1875  * @desc Prepends all paragraphs to the element with the ID "foo"
1876  *
1877  * @name prependTo
1878  * @type jQuery
1879  * @param String expr A jQuery expression of elements to match.
1880  * @cat DOM/Manipulation
1881  */
1882
1883 /**
1884  * Insert all of the matched elements before another, specified, set of elements.
1885  * This operation is, essentially, the reverse of doing a regular
1886  * $(A).before(B), in that instead of inserting B before A, you're inserting
1887  * A before B.
1888  *
1889  * @example $("p").insertBefore("#foo");
1890  * @before <div id="foo">Hello</div><p>I would like to say: </p>
1891  * @result <p>I would like to say: </p><div id="foo">Hello</div>
1892  * @desc Same as $("#foo").before("p")
1893  *
1894  * @name insertBefore
1895  * @type jQuery
1896  * @param String expr A jQuery expression of elements to match.
1897  * @cat DOM/Manipulation
1898  */
1899
1900 /**
1901  * Insert all of the matched elements after another, specified, set of elements.
1902  * This operation is, essentially, the reverse of doing a regular
1903  * $(A).after(B), in that instead of inserting B after A, you're inserting
1904  * A after B.
1905  *
1906  * @example $("p").insertAfter("#foo");
1907  * @before <p>I would like to say: </p><div id="foo">Hello</div>
1908  * @result <div id="foo">Hello</div><p>I would like to say: </p>
1909  * @desc Same as $("#foo").after("p")
1910  *
1911  * @name insertAfter
1912  * @type jQuery
1913  * @param String expr A jQuery expression of elements to match.
1914  * @cat DOM/Manipulation
1915  */
1916
1917 jQuery.each({
1918         appendTo: "append",
1919         prependTo: "prepend",
1920         insertBefore: "before",
1921         insertAfter: "after"
1922 }, function(i,n){
1923         jQuery.fn[ i ] = function(){
1924                 var a = arguments;
1925                 return this.each(function(){
1926                         for ( var j = 0, al = a.length; j < al; j++ )
1927                                 jQuery(a[j])[n]( this );
1928                 });
1929         };
1930 });
1931
1932 /**
1933  * Remove an attribute from each of the matched elements.
1934  *
1935  * @example $("input").removeAttr("disabled")
1936  * @before <input disabled="disabled"/>
1937  * @result <input/>
1938  *
1939  * @name removeAttr
1940  * @type jQuery
1941  * @param String name The name of the attribute to remove.
1942  * @cat DOM/Attributes
1943  */
1944
1945 /**
1946  * Adds the specified class to each of the set of matched elements.
1947  *
1948  * @example $("p").addClass("selected")
1949  * @before <p>Hello</p>
1950  * @result [ <p class="selected">Hello</p> ]
1951  *
1952  * @name addClass
1953  * @type jQuery
1954  * @param String class A CSS class to add to the elements
1955  * @cat DOM/Attributes
1956  * @see removeClass(String)
1957  */
1958
1959 /**
1960  * Removes all or the specified class from the set of matched elements.
1961  *
1962  * @example $("p").removeClass()
1963  * @before <p class="selected">Hello</p>
1964  * @result [ <p>Hello</p> ]
1965  *
1966  * @example $("p").removeClass("selected")
1967  * @before <p class="selected first">Hello</p>
1968  * @result [ <p class="first">Hello</p> ]
1969  *
1970  * @name removeClass
1971  * @type jQuery
1972  * @param String class (optional) A CSS class to remove from the elements
1973  * @cat DOM/Attributes
1974  * @see addClass(String)
1975  */
1976
1977 /**
1978  * Adds the specified class if it is not present, removes it if it is
1979  * present.
1980  *
1981  * @example $("p").toggleClass("selected")
1982  * @before <p>Hello</p><p class="selected">Hello Again</p>
1983  * @result [ <p class="selected">Hello</p>, <p>Hello Again</p> ]
1984  *
1985  * @name toggleClass
1986  * @type jQuery
1987  * @param String class A CSS class with which to toggle the elements
1988  * @cat DOM/Attributes
1989  */
1990
1991 /**
1992  * Removes all matched elements from the DOM. This does NOT remove them from the
1993  * jQuery object, allowing you to use the matched elements further.
1994  *
1995  * Can be filtered with an optional expressions.
1996  *
1997  * @example $("p").remove();
1998  * @before <p>Hello</p> how are <p>you?</p>
1999  * @result how are
2000  *
2001  * @example $("p").remove(".hello");
2002  * @before <p class="hello">Hello</p> how are <p>you?</p>
2003  * @result how are <p>you?</p>
2004  *
2005  * @name remove
2006  * @type jQuery
2007  * @param String expr (optional) A jQuery expression to filter elements by.
2008  * @cat DOM/Manipulation
2009  */
2010
2011 /**
2012  * Removes all child nodes from the set of matched elements.
2013  *
2014  * @example $("p").empty()
2015  * @before <p>Hello, <span>Person</span> <a href="#">and person</a></p>
2016  * @result [ <p></p> ]
2017  *
2018  * @name empty
2019  * @type jQuery
2020  * @cat DOM/Manipulation
2021  */
2022
2023 jQuery.each( {
2024         removeAttr: function( key ) {
2025                 jQuery.attr( this, key, "" );
2026                 this.removeAttribute( key );
2027         },
2028         addClass: function(c){
2029                 jQuery.className.add(this,c);
2030         },
2031         removeClass: function(c){
2032                 jQuery.className.remove(this,c);
2033         },
2034         toggleClass: function( c ){
2035                 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);
2036         },
2037         remove: function(a){
2038                 if ( !a || jQuery.filter( a, [this] ).r )
2039                         this.parentNode.removeChild( this );
2040         },
2041         empty: function() {
2042                 while ( this.firstChild )
2043                         this.removeChild( this.firstChild );
2044         }
2045 }, function(i,n){
2046         jQuery.fn[ i ] = function() {
2047                 return this.each( n, arguments );
2048         };
2049 });
2050
2051 /**
2052  * Reduce the set of matched elements to a single element.
2053  * The position of the element in the set of matched elements
2054  * starts at 0 and goes to length - 1.
2055  *
2056  * @example $("p").eq(1)
2057  * @before <p>This is just a test.</p><p>So is this</p>
2058  * @result [ <p>So is this</p> ]
2059  *
2060  * @name eq
2061  * @type jQuery
2062  * @param Number pos The index of the element that you wish to limit to.
2063  * @cat Core
2064  */
2065
2066 /**
2067  * Reduce the set of matched elements to all elements before a given position.
2068  * The position of the element in the set of matched elements
2069  * starts at 0 and goes to length - 1.
2070  *
2071  * @example $("p").lt(1)
2072  * @before <p>This is just a test.</p><p>So is this</p>
2073  * @result [ <p>This is just a test.</p> ]
2074  *
2075  * @name lt
2076  * @type jQuery
2077  * @param Number pos Reduce the set to all elements below this position.
2078  * @cat Core
2079  */
2080
2081 /**
2082  * Reduce the set of matched elements to all elements after a given position.
2083  * The position of the element in the set of matched elements
2084  * starts at 0 and goes to length - 1.
2085  *
2086  * @example $("p").gt(0)
2087  * @before <p>This is just a test.</p><p>So is this</p>
2088  * @result [ <p>So is this</p> ]
2089  *
2090  * @name gt
2091  * @type jQuery
2092  * @param Number pos Reduce the set to all elements after this position.
2093  * @cat Core
2094  */
2095
2096 /**
2097  * Filter the set of elements to those that contain the specified text.
2098  *
2099  * @example $("p").contains("test")
2100  * @before <p>This is just a test.</p><p>So is this</p>
2101  * @result [ <p>This is just a test.</p> ]
2102  *
2103  * @name contains
2104  * @type jQuery
2105  * @param String str The string that will be contained within the text of an element.
2106  * @cat DOM/Traversing
2107  */
2108 jQuery.each( [ "eq", "lt", "gt", "contains" ], function(i,n){
2109         jQuery.fn[ n ] = function(num,fn) {
2110                 return this.filter( ":" + n + "(" + num + ")", fn );
2111         };
2112 });