More bug fixes and added documentation - passes the test suite now.
[jquery.git] / jquery / jquery.js
1 /*
2  * jQuery - New Wave Javascript
3  *
4  * Copyright (c) 2006 John Resig (jquery.com)
5  * Licensed under the MIT License:
6  *   http://www.opensource.org/licenses/mit-license.php
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  * @constructor
18  */
19 function jQuery(a,c) {
20
21         // Shortcut for document ready (because $(document).each() is silly)
22         if ( a && a.constructor == Function )
23                 return $(document).ready(a);
24
25         // Make sure t hat a selection was provided
26         a = a || jQuery.context || document;
27
28         /*
29          * Handle support for overriding other $() functions. Way too many libraries
30          * provide this function to simply ignore it and overwrite it.
31          */
32
33         // Check to see if this is a possible collision case
34         if ( jQuery._$ && !c && a.constructor == String && 
35       
36                 // Make sure that the expression is a colliding one
37                 !/[^a-zA-Z0-9_-]/.test(a) &&
38         
39                 // and that there are no elements that match it
40                 // (this is the one truly ambiguous case)
41                 !document.getElementsByTagName(a).length )
42
43                         // Use the default method, in case it works some voodoo
44                         return jQuery._$( a );
45
46         // Watch for when a jQuery object is passed as the selector
47         if ( a.jquery )
48                 return a;
49
50         // Watch for when a jQuery object is passed at the context
51         if ( c && c.jquery )
52                 return $(c.get()).find(a);
53         
54         // If the context is global, return a new object
55         if ( window == this )
56                 return new jQuery(a,c);
57
58         // Watch for when an array is passed in
59         this.get( a.constructor == Array ?
60                 // Assume that it's an array of DOM Elements
61                 a :
62
63                 // Find the matching elements and save them for later
64                 jQuery.find( a, c ) );
65
66         var fn = arguments[ arguments.length - 1 ];
67         if ( fn && fn.constructor == Function )
68                 this.each(fn);
69 }
70
71 // Map over the $ in case of overwrite
72 if ( $ )
73         jQuery._$ = $;
74
75 // Map the jQuery namespace to the '$' one
76 var $ = jQuery;
77
78 jQuery.fn = jQuery.prototype = {
79         /**
80          * The current SVN version of jQuery.
81          *
82          * @private
83          * @property
84          * @name jquery
85          * @type String
86          */
87         jquery: "$Rev$",
88         
89         /**
90          * The number of elements currently matched.
91          *
92          * @example $("img").length;
93          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
94          * @result 2
95          *
96          * @property
97          * @name length
98          * @type Number
99          */
100         
101         /**
102          * The number of elements currently matched.
103          *
104          * @example $("img").size();
105          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
106          * @result 2
107          *
108          * @name size
109          * @type Number
110          */
111         size: function() {
112                 return this.length;
113         },
114         
115         /**
116          * Access all matched elements. This serves as a backwards-compatible
117          * way of accessing all matched elements (other than the jQuery object
118          * itself, which is, in fact, an array of elements).
119          *
120          * @example $("img").get();
121          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
122          * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
123          *
124          * @name get
125          * @type Array<Element>
126          */
127          
128         /**
129          * Access a single matched element. <tt>num</tt> is used to access the 
130          * <tt>num</tt>th element matched.
131          *
132          * @example $("img").get(1);
133          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
134          * @result [ <img src="test1.jpg"/> ]
135          *
136          * @name get
137          * @type Element
138          * @param Number num Access the element in the <tt>num</tt>th position.
139          */
140          
141         /**
142          * Set the jQuery object to an array of elements.
143          *
144          * @example $("img").get([ document.body ]);
145          * @result $("img").get() == [ document.body ]
146          *
147          * @private
148          * @name get
149          * @type jQuery
150          * @param Elements elems An array of elements
151          */
152         get: function( num ) {
153                 // Watch for when an array (of elements) is passed in
154                 if ( num && num.constructor == Array ) {
155                 
156                         // Use a tricky hack to make the jQuery object
157                         // look and feel like an array
158                         this.length = 0;
159                         [].push.apply( this, num );
160                         
161                         return this;
162                 } else
163                         return num == undefined ?
164
165                                 // Return a 'clean' array
166                                 jQuery.map( this, function(a){ return a } ) :
167
168                                 // Return just the object
169                                 this[num];
170         },
171         
172         /**
173          * Execute a function within the context of every matched element.
174          * This means that every time the passed-in function is executed
175          * (which is once for every element matched) the 'this' keyword
176          * points to the specific element.
177          *
178          * Additionally, the function, when executed, is passed a single
179          * argument representing the position of the element in the matched
180          * set.
181          *
182          * @example $("img").each(function(){ this.src = "test.jpg"; });
183          * @before <img/> <img/>
184          * @result <img src="test.jpg"/> <img src="test.jpg"/>
185          *
186          * @name each
187          * @type jQuery
188          * @param Function fn A function to execute
189          */
190         each: function( fn ) {
191                 // Iterate through all of the matched elements
192                 for ( var i = 0; i < this.length; i++ )
193                 
194                         // Execute the function within the context of each element
195                         fn.apply( this[i], [i] );
196                 
197                 return this;
198         },
199         
200         /**
201          * Access a property on the first matched element.
202          * This method makes it easy to retreive a property value
203          * from the first matched element.
204          *
205          * @example $("img").attr("src");
206          * @before <img src="test.jpg"/>
207          * @result test.jpg
208          *
209          * @name attr
210          * @type Object
211          * @param String name The name of the property to access.
212          */
213          
214         /**
215          * Set a hash of key/value object properties to all matched elements.
216          * This serves as the best way to set a large number of properties
217          * on all matched elements.
218          *
219          * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
220          * @before <img/>
221          * @result <img src="test.jpg" alt="Test Image"/>
222          *
223          * @name attr
224          * @type jQuery
225          * @param Hash prop A set of key/value pairs to set as object properties.
226          */
227          
228         /**
229          * Set a single property to a value, on all matched elements.
230          *
231          * @example $("img").attr("src","test.jpg");
232          * @before <img/>
233          * @result <img src="test.jpg"/>
234          *
235          * @name attr
236          * @type jQuery
237          * @param String key The name of the property to set.
238          * @param Object value The value to set the property to.
239          */
240         attr: function( key, value, type ) {
241                 // Check to see if we're setting style values
242                 return key.constructor != String || value ?
243                         this.each(function(){
244                                 // See if we're setting a hash of styles
245                                 if ( value == undefined )
246                                         // Set all the styles
247                                         for ( var prop in key )
248                                                 jQuery.attr(
249                                                         type ? this.style : this,
250                                                         prop, key[prop]
251                                                 );
252                                 
253                                 // See if we're setting a single key/value style
254                                 else
255                                         jQuery.attr(
256                                                 type ? this.style : this,
257                                                 key, value
258                                         );
259                         }) :
260                         
261                         // Look for the case where we're accessing a style value
262                         jQuery[ type || "attr" ]( this[0], key );
263         },
264         
265         /**
266          * Access a style property on the first matched element.
267          * This method makes it easy to retreive a style property value
268          * from the first matched element.
269          *
270          * @example $("p").css("red");
271          * @before <p style="color:red;">Test Paragraph.</p>
272          * @result red
273          *
274          * @name css
275          * @type Object
276          * @param String name The name of the property to access.
277          */
278          
279         /**
280          * Set a hash of key/value style properties to all matched elements.
281          * This serves as the best way to set a large number of style properties
282          * on all matched elements.
283          *
284          * @example $("p").css({ color: "red", background: "blue" });
285          * @before <p>Test Paragraph.</p>
286          * @result <p style="color:red; background:blue;">Test Paragraph.</p>
287          *
288          * @name css
289          * @type jQuery
290          * @param Hash prop A set of key/value pairs to set as style properties.
291          */
292          
293         /**
294          * Set a single style property to a value, on all matched elements.
295          *
296          * @example $("p").css("color","red");
297          * @before <p>Test Paragraph.</p>
298          * @result <p style="color:red;">Test Paragraph.</p>
299          *
300          * @name css
301          * @type jQuery
302          * @param String key The name of the property to set.
303          * @param Object value The value to set the property to.
304          */
305         css: function( key, value ) {
306                 return this.attr( key, value, "css" );
307         },
308         
309         /**
310          * Retreive the text contents of all matched elements. The result is
311          * a string that contains the combined text contents of all matched
312          * elements. This method works on both HTML and XML documents.
313          *
314          * @example $("p").text();
315          * @before <p>Test Paragraph.</p>
316          * @result Test Paragraph.
317          *
318          * @name text
319          * @type String
320          */
321         text: function(e) {
322                 e = e || this;
323                 var t = "";
324                 for ( var j = 0; j < e.length; j++ ) {
325                         var r = e[j].childNodes;
326                         for ( var i = 0; i < r.length; i++ )
327                                 t += r[i].nodeType != 1 ?
328                                         r[i].nodeValue : jQuery.fn.text([ r[i] ]);
329                 }
330                 return t;
331         },
332         
333         /**
334          * Wrap all matched elements with a structure of other elements.
335          * This wrapping process is most useful for injecting additional
336          * stucture into a document, without ruining the original semantic
337          * qualities of a document.
338          *
339          * The way that is works is that it goes through the first element argument
340          * provided and finds the deepest element within the structure - it is that
341          * element that will en-wrap everything else.
342          *
343          * @example $("p").wrap("<div class='wrap'></div>");
344          * @before <p>Test Paragraph.</p>
345          * @result <div class='wrap'><p>Test Paragraph.</p></div>
346          *
347          * @name wrap
348          * @type jQuery
349          * @any String html A string of HTML, that will be created on the fly and wrapped around the target.
350          * @any Element elem A DOM element that will be wrapped.
351          * @any Array<Element> elems An array of elements, the first of which will be wrapped.
352          * @any Object obj Any object, converted to a string, then a text node.
353          */
354         wrap: function() {
355                 // The elements to wrap the target around
356                 var a = jQuery.clean(arguments);
357                 
358                 // Wrap each of the matched elements individually
359                 return this.each(function(){
360                         // Clone the structure that we're using to wrap
361                         var b = a[0].cloneNode(true);
362                         
363                         // Insert it before the element to be wrapped
364                         this.parentNode.insertBefore( b, this );
365                         
366                         // Find he deepest point in the wrap structure
367                         while ( b.firstChild )
368                                 b = b.firstChild;
369                         
370                         // Move the matched element to within the wrap structure
371                         b.appendChild( this );
372                 });
373         },
374         
375         /**
376          * Append any number of elements to the inside of all matched elements.
377          * This operation is similar to doing an <tt>appendChild</tt> to all the 
378          * specified elements, adding them into the document.
379          * 
380          * @example $("p").append("<b>Hello</b>");
381          * @before <p>I would like to say: </p>
382          * @result <p>I would like to say: <b>Hello</b></p>
383          *
384          * @name append
385          * @type jQuery
386          * @any String html A string of HTML, that will be created on the fly and appended to the target.
387          * @any Element elem A DOM element that will be appended.
388          * @any Array<Element> elems An array of elements, all of which will be appended.
389          * @any Object obj Any object, converted to a string, then a text node.
390          */
391         append: function() {
392                 return this.domManip(arguments, true, 1, function(a){
393                         this.appendChild( a );
394                 });
395         },
396         
397         /**
398          * Prepend any number of elements to the inside of all matched elements.
399          * This operation is the best way to insert a set of elements inside, at the 
400          * beginning, of all the matched element.
401          * 
402          * @example $("p").prepend("<b>Hello</b>");
403          * @before <p>, how are you?</p>
404          * @result <p><b>Hello</b>, how are you?</p>
405          *
406          * @name prepend
407          * @type jQuery
408          * @any String html A string of HTML, that will be created on the fly and prepended to the target.
409          * @any Element elem A DOM element that will be prepended.
410          * @any Array<Element> elems An array of elements, all of which will be prepended.
411          * @any Object obj Any object, converted to a string, then a text node.
412          */
413         prepend: function() {
414                 return this.domManip(arguments, true, -1, function(a){
415                         this.insertBefore( a, this.firstChild );
416                 });
417         },
418         
419         /**
420          * Insert any number of elements before each of the matched elements.
421          * 
422          * @example $("p").before("<b>Hello</b>");
423          * @before <p>how are you?</p>
424          * @result <b>Hello</b><p>how are you?</p>
425          *
426          * @name before
427          * @type jQuery
428          * @any String html A string of HTML, that will be created on the fly and inserted.
429          * @any Element elem A DOM element that will beinserted.
430          * @any Array<Element> elems An array of elements, all of which will be inserted.
431          * @any Object obj Any object, converted to a string, then a text node.
432          */
433         before: function() {
434                 return this.domManip(arguments, false, 1, function(a){
435                         this.parentNode.insertBefore( a, this );
436                 });
437         },
438         
439         /**
440          * Insert any number of elements after each of the matched elements.
441          * 
442          * @example $("p").after("<p>I'm doing fine.</p>");
443          * @before <p>How are you?</p>
444          * @result <p>How are you?</p><p>I'm doing fine.</p>
445          *
446          * @name after
447          * @type jQuery
448          * @any String html A string of HTML, that will be created on the fly and inserted.
449          * @any Element elem A DOM element that will beinserted.
450          * @any Array<Element> elems An array of elements, all of which will be inserted.
451          * @any Object obj Any object, converted to a string, then a text node.
452          */
453         after: function() {
454                 return this.domManip(arguments, false, -1, function(a){
455                         this.parentNode.insertBefore( a, this.nextSibling );
456                 });
457         },
458         
459         /**
460          * End the most recent 'destructive' operation, reverting the list of matched elements
461          * back to its previous state. After an end operation, the list of matched elements will 
462          * revert to the last state of matched elements.
463          *
464          * @example $("p").find("span").end();
465          * @before <p><span>Hello</span>, how are you?</p>
466          * @result $("p").find("span").end() == [ <p>...</p> ]
467          *
468          * @name end
469          * @type jQuery
470          */
471         end: function() {
472                 return this.get( this.stack.pop() );
473         },
474         
475         /**
476          * Searches for all elements that match the specified expression.
477          * This method is the optimal way of finding additional descendant
478          * elements with which to process.
479          *
480          * All searching is done using a jQuery expression. The expression can be 
481          * written using CSS 1-3 Selector sytax, or basic XPath.
482          *
483          * @example $("p").find("span");
484          * @before <p><span>Hello</span>, how are you?</p>
485          * @result $("p").find("span") == [ <span>Hello</span> ]
486          *
487          * @name find
488          * @type jQuery
489          * @param String expr An expression to search with.
490          */
491         find: function(t) {
492                 return this.pushStack( jQuery.map( this, function(a){
493                         return jQuery.find(t,a);
494                 }), arguments );
495         },
496         
497         filter: function(t) {
498                 return t.constructor == Array ?
499                         // Multi Filtering
500                         this.pushStack( jQuery.map(this,function(a){
501                                 for ( var i = 0; i < t.length; i++ )
502                                         if ( jQuery.filter(t[i],[a]).r.length )
503                                                 return a;
504                         }), arguments ) :
505                         
506                         this.pushStack( jQuery.filter(t,this).r, arguments );
507         },
508         
509         not: function(t) {
510                 return this.pushStack( t.constructor == String ?
511                         jQuery.filter(t,this,false).r :
512                         jQuery.grep(this,function(a){ return a != t; }), arguments );
513         },
514         
515         add: function(t) {
516                 return this.pushStack( jQuery.merge( this, t.constructor == String ?
517                         jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments );
518         },
519         
520         /**
521          * A wrapper function for each() to be used by append and prepend.
522          * Handles cases where you're trying to modify the inner contents of
523          * a table, when you actually need to work with the tbody.
524          *
525          * @member jQuery
526          * @param {String} expr The expression with which to filter
527          * @type Boolean
528          */
529         is: function(expr) {
530                 return jQuery.filter(expr,this).r.length > 0;
531         },
532         
533         /**
534          * 
535          *
536          * @private
537          * @name domManip
538          * @param Array args
539          * @param Boolean table
540          * @param Number int
541          * @param Function fn The function doing the DOM manipulation.
542          * @type jQuery
543          */
544         domManip: function(args, table, dir, fn){
545                 var clone = this.size() > 1;
546                 var a = jQuery.clean(args);
547                 
548                 return this.each(function(){
549                         var obj = this;
550                         
551                         if ( table && this.nodeName == "TABLE" ) {
552                                 var tbody = this.getElementsByTagName("tbody");
553
554                                 if ( !tbody.length ) {
555                                         obj = document.createElement("tbody");
556                                         this.appendChild( obj );
557                                 } else
558                                         obj = tbody[0];
559                         }
560         
561                         for ( var i = ( dir < 0 ? a.length - 1 : 0 );
562                                 i != ( dir < 0 ? dir : a.length ); i += dir )
563                                         fn.apply( obj, [ a[i] ] );
564                 });
565         },
566         
567         /**
568          * 
569          *
570          * @private
571          * @name pushStack
572          * @param Array a
573          * @param Array args
574          * @type jQuery
575          */
576         pushStack: function(a,args) {
577                 var fn = args && args[args.length-1];
578
579                 if ( !fn || fn.constructor != Function ) {
580                         if ( !this.stack ) this.stack = [];
581                         this.stack.push( this.get() );
582                         this.get( a );
583                 } else {
584                         var old = this.get();
585                         this.get( a );
586                         if ( fn.constructor == Function )
587                                 return this.each( fn );
588                         this.get( old );
589                 }
590
591                 return this;
592         },
593         
594         extend: function(obj,prop) {\r           if ( !prop ) {\r                 prop = obj;\r                    obj = this;\r            }\r      \r               for ( var i in prop )\r                  obj[i] = prop[i];\r      \r               return obj;\r    }
595 };
596
597 jQuery.extend = jQuery.fn.extend;
598
599 new function() {
600         var b = navigator.userAgent.toLowerCase();
601
602         // Figure out what browser is being used
603         jQuery.browser = {
604                 safari: /webkit/.test(b),
605                 opera: /opera/.test(b),
606                 msie: /msie/.test(b) && !/opera/.test(b),
607                 mozilla: /mozilla/.test(b) && !/compatible/.test(b)
608         };
609
610         // Check to see if the W3C box model is being used
611         jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
612
613         var axis = {
614                 parent: "a.parentNode",
615                 parents: jQuery.parents,
616                 ancestors: jQuery.parents,
617                 next: "a.nextSibling",
618                 prev: "a.previousSibling",
619                 siblings: jQuery.sibling
620         };
621         
622         for ( var i in axis ) new function(){
623                 var t = axis[i];
624                 jQuery.fn[ i ] = function(a) {
625                         var ret = jQuery.map(this,t);
626                         if ( a ) ret = jQuery.filter(a,ret).r;
627                         return this.pushStack( ret, arguments );
628                 };
629         }
630         
631         /*var to = ["append","prepend","before","after"];
632         
633         for ( var i = 0; i < to.length; i++ ) new function(){
634                 var n = to[i];
635                 jQuery.fn[ n + "To" ] = function(){
636                         var a = arguments;
637                         return this.each(function(){
638                                 for ( var i = 0; i < a.length; i++ )
639                                         $(a[i])[n]( this );
640                         });
641                 };
642         }*/
643         
644         var each = {
645                 show: function(){
646                         this.style.display = this.oldblock ? this.oldblock : "";
647                         if ( jQuery.css(this,"display") == "none" )
648                                 this.style.display = "block";
649                 },
650                 
651                 hide: function(){
652                         this.oldblock = jQuery.css(this,"display");
653                         if ( this.oldblock == "none" )
654                                 this.oldblock = "block";
655                         this.style.display = "none";
656                 },
657                 
658                 toggle: function(){
659                         var d = jQuery.css(this,"display");
660                         $(this)[ !d || d == "none" ? "show" : "hide" ]();
661                 },
662                 
663                 addClass: function(c){
664                         jQuery.className.add(this,c);
665                 },
666                 
667                 removeClass: function(c){
668                         jQuery.className.remove(this,c);
669                 },
670         
671                 toggleClass: function( c ){
672                         jQuery.className[ jQuery.className.has(this,a) ? "remove" : "add" ](this,c);
673                 },
674                 
675                 remove: function(a){
676                         if ( !a || jQuery.filter( [this], a ).r )
677                                 this.parentNode.removeChild( this );
678                 },
679         
680                 empty: function(){
681                         while ( this.firstChild )
682                                 this.removeChild( this.firstChild );
683                 },
684                 
685                 bind: function( type, fn ) {
686                         jQuery.event.add( this, type, fn );
687                 },
688                 
689                 unbind: function( type, fn ) {
690                         jQuery.event.remove( this, type, fn );
691                 },
692                 
693                 trigger: function( type ) {
694                         jQuery.event.trigger( this, type );
695                 }
696         };
697         
698         for ( var i in each ) new function() {
699                 var n = each[i];
700                 jQuery.fn[ i ] = function() {
701                         var args = arguments;
702                         return this.each(function(){
703                                 n.apply( this, args );
704                         });
705                 };
706         }
707         
708         var attr = {
709                 val: "value",
710                 html: "innerHTML",
711                 value: null,
712                 id: null,
713                 title: null,
714                 name: null,
715                 href: null,
716                 src: null,
717                 rel: null
718         };
719         
720         for ( var i in attr ) new function() {
721                 var n = attr[i];
722                 jQuery.fn[ i ] = function(h) {
723                         return h == undefined ?
724                                 this.length ? this[0][n] : null :
725                                 this.attr( n, h );
726                 };
727         }
728         
729         var css = "width,height,top,left,position,float,overflow,color,background".split(",");
730         
731         for ( var i in css ) new function() {
732                 var n = css[i];
733                 jQuery.fn[ i ] = function(h) {
734                         return h == undefined ?
735                                 this.length ? jQuery.css( this[0], n ) : null :
736                                 this.css( n, h );
737                 };
738         }
739
740 }
741
742 jQuery.extend({
743         className: {
744                 add: function(o,c){
745                         if (jQuery.className.has(o,c)) return;
746                         o.className += ( o.className ? " " : "" ) + c;
747                 },
748                 remove: function(o,c){
749                         o.className = !c ? "" :
750                                 o.className.replace(
751                                         new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
752                 },
753                 has: function(e,a) {
754                         if ( e.className )
755                                 e = e.className;
756                         return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
757                 }
758         },
759         
760         /**
761          * Swap in/out style options.
762          * @private
763          */
764         swap: function(e,o,f) {
765                 for ( var i in o ) {
766                         e.style["old"+i] = e.style[i];
767                         e.style[i] = o[i];
768                 }
769                 f.apply( e, [] );
770                 for ( var i in o )
771                         e.style[i] = e.style["old"+i];
772         },
773         
774         css: function(e,p) {
775                 if ( p == "height" || p == "width" ) {
776                         var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
777         
778                         for ( var i in d ) {
779                                 old["padding" + d[i]] = 0;
780                                 old["border" + d[i] + "Width"] = 0;
781                         }
782         
783                         jQuery.swap( e, old, function() {
784                                 if (jQuery.css(e,"display") != "none") {
785                                         oHeight = e.offsetHeight;
786                                         oWidth = e.offsetWidth;
787                                 } else
788                                         jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "" },
789                                                 function(){
790                                                         oHeight = e.clientHeight;
791                                                         oWidth = e.clientWidth;
792                                                 });
793                         });
794         
795                         return p == "height" ? oHeight : oWidth;
796                 }
797                 
798                 var r;
799         
800                 if (e.style[p])
801                         r = e.style[p];
802                 else if (e.currentStyle)
803                         r = e.currentStyle[p];
804                 else if (document.defaultView && document.defaultView.getComputedStyle) {
805                         p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
806                         var s = document.defaultView.getComputedStyle(e,"");
807                         r = s ? s.getPropertyValue(p) : null;
808                 }
809                 
810                 return r;
811         },
812         
813         clean: function(a) {
814                 var r = [];
815                 for ( var i = 0; i < a.length; i++ ) {
816                         if ( a[i].constructor == String ) {
817         
818                                 if ( !a[i].indexOf("<tr") ) {
819                                         var tr = true;
820                                         a[i] = "<table>" + a[i] + "</table>";
821                                 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
822                                         var td = true;
823                                         a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
824                                 }
825         
826                                 var div = document.createElement("div");
827                                 div.innerHTML = a[i];
828         
829                                 if ( tr || td ) {
830                                         div = div.firstChild.firstChild;
831                                         if ( td ) div = div.firstChild;
832                                 }
833         
834                                 for ( var j = 0; j < div.childNodes.length; j++ )
835                                         r.push( div.childNodes[j] );
836                         } else if ( a[i].jquery || a[i].length && !a[i].nodeType )
837                                 for ( var k = 0; k < a[i].length; k++ )
838                                         r.push( a[i][k] );
839                         else if ( a[i] !== null )
840                                 r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) );
841                 }
842                 return r;
843         },
844         
845         expr: {
846                 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
847                 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
848                 ":": {
849                         // Position Checks
850                         lt: "i<m[3]-0",
851                         gt: "i>m[3]-0",
852                         nth: "m[3]-0==i",
853                         eq: "m[3]-0==i",
854                         first: "i==0",
855                         last: "i==r.length-1",
856                         even: "i%2==0",
857                         odd: "i%2",
858                         
859                         // Child Checks
860                         "first-child": "jQuery.sibling(a,0).cur",
861                         "last-child": "jQuery.sibling(a,0).last",
862                         "only-child": "jQuery.sibling(a).length==1",
863                         
864                         // Parent Checks
865                         parent: "a.childNodes.length",
866                         empty: "!a.childNodes.length",
867                         
868                         // Text Check
869                         contains: "(a.innerText||a.innerHTML).indexOf(m[3])>=0",
870                         
871                         // Visibility
872                         visible: "jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!='hidden'",
873                         hidden: "jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')=='hidden'",
874                         
875                         // Form elements
876                         enabled: "!a.disabled",
877                         disabled: "a.disabled",
878                         checked: "a.checked"
879                 },
880                 ".": "jQuery.className.has(a,m[2])",
881                 "@": {
882                         "=": "z==m[4]",
883                         "!=": "z!=m[4]",
884                         "^=": "!z.indexOf(m[4])",
885                         "$=": "z.substr(z.length - m[4].length,m[4].length)==m[4]",
886                         "*=": "z.indexOf(m[4])>=0",
887                         "": "z"
888                 },
889                 "[": "jQuery.find(m[2],a).length"
890         },
891         
892         token: [
893                 "\\.\\.|/\\.\\.", "a.parentNode",
894                 ">|/", "jQuery.sibling(a.firstChild)",
895                 "\\+", "jQuery.sibling(a).next",
896                 "~", function(a){
897                         var r = [];
898                         var s = jQuery.sibling(a);
899                         if ( s.n > 0 )
900                                 for ( var i = s.n; i < s.length; i++ )
901                                         r.push( s[i] );
902                         return r;
903                 }
904         ],
905         
906         find: function( t, context ) {
907                 // Make sure that the context is a DOM Element
908                 if ( context && context.getElementsByTagName == undefined )
909                         context = null;
910         
911                 // Set the correct context (if none is provided)
912                 context = context || jQuery.context || document;
913         
914                 if ( t.constructor != String ) return [t];
915         
916                 if ( !t.indexOf("//") ) {
917                         context = context.documentElement;
918                         t = t.substr(2,t.length);
919                 } else if ( !t.indexOf("/") ) {
920                         context = context.documentElement;
921                         t = t.substr(1,t.length);
922                         // FIX Assume the root element is right :(
923                         if ( t.indexOf("/") >= 1 )
924                                 t = t.substr(t.indexOf("/"),t.length);
925                 }
926         
927                 var ret = [context];
928                 var done = [];
929                 var last = null;
930         
931                 while ( t.length > 0 && last != t ) {
932                         var r = [];
933                         last = t;
934         
935                         t = jQuery.trim(t).replace( /^\/\//i, "" );
936                         
937                         var foundToken = false;
938                         
939                         for ( var i = 0; i < jQuery.token.length; i += 2 ) {
940                                 var re = new RegExp("^(" + jQuery.token[i] + ")");
941                                 var m = re.exec(t);
942                                 
943                                 if ( m ) {
944                                         r = ret = jQuery.map( ret, jQuery.token[i+1] );
945                                         t = jQuery.trim( t.replace( re, "" ) );
946                                         foundToken = true;
947                                 }
948                         }
949                         
950                         if ( !foundToken ) {
951                                 if ( !t.indexOf(",") || !t.indexOf("|") ) {
952                                         if ( ret[0] == context ) ret.shift();
953                                         done = jQuery.merge( done, ret );
954                                         r = ret = [context];
955                                         t = " " + t.substr(1,t.length);
956                                 } else {
957                                         var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
958                                         var m = re2.exec(t);
959                 
960                                         if ( m[1] == "#" ) {
961                                                 // Ummm, should make this work in all XML docs
962                                                 var oid = document.getElementById(m[2]);
963                                                 r = ret = oid ? [oid] : [];
964                                                 t = t.replace( re2, "" );
965                                         } else {
966                                                 if ( !m[2] || m[1] == "." ) m[2] = "*";
967                 
968                                                 for ( var i = 0; i < ret.length; i++ )
969                                                         r = jQuery.merge( r,
970                                                                 m[2] == "*" ?
971                                                                         jQuery.getAll(ret[i]) :
972                                                                         ret[i].getElementsByTagName(m[2])
973                                                         );
974                                         }
975                                 }
976                         }
977         
978                         if ( t ) {
979                                 var val = jQuery.filter(t,r);
980                                 ret = r = val.r;
981                                 t = jQuery.trim(val.t);
982                         }
983                 }
984         
985                 if ( ret && ret[0] == context ) ret.shift();
986                 done = jQuery.merge( done, ret );
987         
988                 return done;
989         },
990         
991         getAll: function(o,r) {
992                 r = r || [];
993                 var s = o.childNodes;
994                 for ( var i = 0; i < s.length; i++ )
995                         if ( s[i].nodeType == 1 ) {
996                                 r.push( s[i] );
997                                 jQuery.getAll( s[i], r );
998                         }
999                 return r;
1000         },
1001         
1002         attr: function(o,a,v){
1003                 if ( a && a.constructor == String ) {
1004                         var fix = {
1005                                 "for": "htmlFor",
1006                                 "class": "className",
1007                                 "float": "cssFloat"
1008                         };
1009                         
1010                         a = (fix[a] && fix[a].replace && fix[a] || a)
1011                                 .replace(/-([a-z])/ig,function(z,b){
1012                                         return b.toUpperCase();
1013                                 });
1014                         
1015                         if ( v != undefined ) {
1016                                 o[a] = v;
1017                                 if ( o.setAttribute && a != "disabled" )
1018                                         o.setAttribute(a,v);
1019                         }
1020                         
1021                         return o[a] || o.getAttribute && o.getAttribute(a) || "";
1022                 } else
1023                         return "";
1024         },
1025         
1026         filter: function(t,r,not) {
1027                 // Figure out if we're doing regular, or inverse, filtering
1028                 var g = not !== false ? jQuery.grep :
1029                         function(a,f) {return jQuery.grep(a,f,true);};
1030                 
1031                 // Look for a string-like sequence
1032                 var str = "([a-zA-Z*_-][a-zA-Z0-9_-]*)";
1033                 
1034                 // Look for something (optionally) enclosed with quotes
1035                 var qstr = " *'?\"?([^'\"]*)'?\"? *";
1036         
1037                 while ( t && /^[a-zA-Z\[*:.#]/.test(t) ) {
1038                         // Handles:
1039                         // [@foo], [@foo=bar], etc.
1040                         var re = new RegExp("^\\[ *@" + str + " *([!*$^=]*) *" + qstr + "\\]");
1041                         var m = re.exec(t);
1042         
1043                         // Re-organize the match
1044                         if ( m ) m = ["", "@", m[2], m[1], m[3]];
1045                                 
1046                         // Handles:
1047                         // [div], [.foo]
1048                         if ( !m ) {
1049                                 re = new RegExp("^(\\[)" + qstr + "\\]");
1050                                 m = re.exec(t);
1051                         }
1052                         
1053                         // Handles:
1054                         // :contains(test), :not(.foo)
1055                         if ( !m ) {
1056                                 re = new RegExp("^(:)" + str + "\\(" + qstr + "\\)");
1057                                 m = re.exec(t);
1058                         }
1059                         
1060                         // Handles:
1061                         // :foo, .foo, #foo, foo
1062                         if ( !m ) {
1063                                 re = new RegExp("^([:.#]*)" + str);
1064                                 m = re.exec(t);
1065                         }
1066                         
1067                         // Remove what we just matched
1068                         t = t.replace( re, "" );
1069         
1070                         // :not() is a special case that can be optomized by
1071                         // keeping it out of the expression list
1072                         if ( m[1] == ":" && m[2] == "not" )
1073                                 r = jQuery.filter(m[3],r,false).r;
1074                         
1075                         // Otherwise, find the expression to execute
1076                         else {
1077                                 var f = jQuery.expr[m[1]];
1078                                 if ( f.constructor != String )
1079                                         f = jQuery.expr[m[1]][m[2]];
1080                                         
1081                                 // Build a custom macro to enclose it
1082                                 eval("f = function(a,i){" + 
1083                                         ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) + 
1084                                         "return " + f + "}");
1085                                 
1086                                 // Execute it against the current filter
1087                                 r = g( r, f );
1088                         }
1089                 }
1090         
1091                 // Return an array of filtered elements (r)
1092                 // and the modified expression string (t)
1093                 return { r: r, t: t };
1094         },
1095         
1096         /**
1097          * Remove the whitespace from the beginning and end of a string.
1098          *
1099          * @private
1100          * @name $.trim
1101          * @type String
1102          * @param String str The string to trim.
1103          */
1104         trim: function(t){
1105                 return t.replace(/^\s+|\s+$/g, "");
1106         },
1107         
1108         /**
1109          * All ancestors of a given element.
1110          *
1111          * @private
1112          * @name $.parents
1113          * @type Array<Element>
1114          * @param Element elem The element to find the ancestors of.
1115          */
1116         parents: function(a){
1117                 var b = [];
1118                 var c = a.parentNode;
1119                 while ( c && c != document ) {
1120                         b.push( c );
1121                         c = c.parentNode;
1122                 }
1123                 return b;
1124         },
1125         
1126         /**
1127          * All elements on a specified axis.
1128          *
1129          * @private
1130          * @name $.sibling
1131          * @type Array
1132          * @param Element elem The element to find all the siblings of (including itself).
1133          */
1134         sibling: function(a,n) {
1135                 var type = [];
1136                 var tmp = a.parentNode.childNodes;
1137                 for ( var i = 0; i < tmp.length; i++ ) {
1138                         if ( tmp[i].nodeType == 1 )
1139                                 type.push( tmp[i] );
1140                         if ( tmp[i] == a )
1141                                 type.n = type.length - 1;
1142                 }
1143                 type.last = type.n == type.length - 1;
1144                 type.cur =
1145                         n == "even" && type.n % 2 == 0 ||
1146                         n == "odd" && type.n % 2 ||
1147                         type[n] == a;
1148                 type.next = type[type.n + 1];
1149                 return type;
1150         },
1151         
1152         /**
1153          * Merge two arrays together, removing all duplicates.
1154          *
1155          * @private
1156          * @name $.merge
1157          * @type Array
1158          * @param Array a The first array to merge.
1159          * @param Array b The second array to merge.
1160          */
1161         merge: function(a,b) {
1162                 var d = [];
1163                 
1164                 // Move b over to the new array (this helps to avoid
1165                 // StaticNodeList instances)
1166                 for ( var k = 0; k < b.length; k++ )
1167                         d[k] = b[k];
1168         
1169                 // Now check for duplicates between a and b and only
1170                 // add the unique items
1171                 for ( var i = 0; i < a.length; i++ ) {
1172                         var c = true;
1173                         
1174                         // The collision-checking process
1175                         for ( var j = 0; j < b.length; j++ )
1176                                 if ( a[i] == b[j] )
1177                                         c = false;
1178                                 
1179                         // If the item is unique, add it
1180                         if ( c )
1181                                 d.push( a[i] );
1182                 }
1183         
1184                 return d;
1185         },
1186         
1187         /**
1188          * Remove items that aren't matched in an array. The function passed
1189          * in to this method will be passed two arguments: 'a' (which is the
1190          * array item) and 'i' (which is the index of the item in the array).
1191          *
1192          * @private
1193          * @name $.grep
1194          * @type Array
1195          * @param Array array The Array to find items in.
1196          * @param Function fn The function to process each item against.
1197          * @param Boolean inv Invert the selection - select the opposite of the function.
1198          */
1199         grep: function(a,f,s) {
1200                 // If a string is passed in for the function, make a function
1201                 // for it (a handy shortcut)
1202                 if ( f.constructor == String )
1203                         f = new Function("a","i","return " + f);
1204                         
1205                 var r = [];
1206                 
1207                 // Go through the array, only saving the items
1208                 // that pass the validator function
1209                 for ( var i = 0; i < a.length; i++ )
1210                         if ( !s && f(a[i],i) || s && !f(a[i],i) )
1211                                 r.push( a[i] );
1212                 
1213                 return r;
1214         },
1215         
1216         /**
1217          * Translate all items in array to another array of items. The translation function
1218          * that is provided to this method is passed one argument: 'a' (the item to be 
1219          * translated). If an array is returned, that array is mapped out and merged into
1220          * the full array. Additionally, returning 'null' or 'undefined' will delete the item
1221          * from the array. Both of these changes imply that the size of the array may not
1222          * be the same size upon completion, as it was when it started.
1223          *
1224          * @private
1225          * @name $.map
1226          * @type Array
1227          * @param Array array The Array to translate.
1228          * @param Function fn The function to process each item against.
1229          */
1230         map: function(a,f) {
1231                 // If a string is passed in for the function, make a function
1232                 // for it (a handy shortcut)
1233                 if ( f.constructor == String )
1234                         f = new Function("a","return " + f);
1235                 
1236                 var r = [];
1237                 
1238                 // Go through the array, translating each of the items to their
1239                 // new value (or values).
1240                 for ( var i = 0; i < a.length; i++ ) {
1241                         var t = f(a[i],i);
1242                         if ( t !== null && t != undefined ) {
1243                                 if ( t.constructor != Array ) t = [t];
1244                                 r = jQuery.merge( t, r );
1245                         }
1246                 }
1247                 return r;
1248         },
1249         
1250         /*
1251          * A number of helper functions used for managing events.
1252          * Many of the ideas behind this code orignated from Dean Edwards' addEvent library.
1253          */
1254         event: {
1255         
1256                 // Bind an event to an element
1257                 // Original by Dean Edwards
1258                 add: function(element, type, handler) {
1259                         // For whatever reason, IE has trouble passing the window object
1260                         // around, causing it to be cloned in the process
1261                         if ( jQuery.browser.msie && element.setInterval != undefined )
1262                                 element = window;
1263                 
1264                         // Make sure that the function being executed has a unique ID
1265                         if ( !handler.guid )
1266                                 handler.guid = this.guid++;
1267                                 
1268                         // Init the element's event structure
1269                         if (!element.events)
1270                                 element.events = {};
1271                         
1272                         // Get the current list of functions bound to this event
1273                         var handlers = element.events[type];
1274                         
1275                         // If it hasn't been initialized yet
1276                         if (!handlers) {
1277                                 // Init the event handler queue
1278                                 handlers = element.events[type] = {};
1279                                 
1280                                 // Remember an existing handler, if it's already there
1281                                 if (element["on" + type])
1282                                         handlers[0] = element["on" + type];
1283                         }
1284                         
1285                         // Add the function to the element's handler list
1286                         handlers[handler.guid] = handler;
1287                         
1288                         // And bind the global event handler to the element
1289                         element["on" + type] = this.handle;
1290         
1291                         // Remember the function in a global list (for triggering)
1292                         if (!this.global[type])
1293                                 this.global[type] = [];
1294                         this.global[type].push( element );
1295                 },
1296                 
1297                 guid: 1,
1298                 global: {},
1299                 
1300                 // Detach an event or set of events from an element
1301                 remove: function(element, type, handler) {
1302                         if (element.events)
1303                                 if (type && element.events[type])
1304                                         if ( handler )
1305                                                 delete element.events[type][handler.guid];
1306                                         else
1307                                                 for ( var i in element.events[type] )
1308                                                         delete element.events[type][i];
1309                                 else
1310                                         for ( var j in element.events )
1311                                                 this.remove( element, j );
1312                 },
1313                 
1314                 trigger: function(type,data,element) {
1315                         // Touch up the incoming data
1316                         data = data || [];
1317         
1318                         // Handle a global trigger
1319                         if ( !element ) {
1320                                 var g = this.global[type];
1321                                 if ( g )
1322                                         for ( var i = 0; i < g.length; i++ )
1323                                                 this.trigger( type, data, g[i] );
1324         
1325                         // Handle triggering a single element
1326                         } else if ( element["on" + type] ) {
1327                                 // Pass along a fake event
1328                                 data.unshift( this.fix({ type: type, target: element }) );
1329         
1330                                 // Trigger the event
1331                                 element["on" + type].apply( element, data );
1332                         }
1333                 },
1334                 
1335                 handle: function(event) {
1336                         event = event || jQuery.event.fix( window.event );
1337         
1338                         // If no correct event was found, fail
1339                         if ( !event ) return;
1340                 
1341                         var returnValue = true;
1342                 
1343                         for ( var j in this.events[event.type] ) {
1344                                 if (this.events[event.type][j](event) === false) {
1345                                         event.preventDefault();
1346                                         event.stopPropagation();
1347                                         returnValue = false;
1348                                 }
1349                         }
1350                         
1351                         return returnValue;
1352                 },
1353                 
1354                 fix: function(event) {
1355                         if ( event ) {
1356                                 event.preventDefault = function() {
1357                                         this.returnValue = false;
1358                                 };
1359                         
1360                                 event.stopPropagation = function() {
1361                                         this.cancelBubble = true;
1362                                 };
1363                         }
1364                         
1365                         return event;
1366                 }
1367         
1368         }
1369 });