Made the parsing engine extensible.
[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 && jQuery.fn.ready )
23                 return jQuery(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
47         // Watch for when a jQuery object is passed as the selector
48         if ( a.jquery )
49                 return a;
50
51         // Watch for when a jQuery object is passed at the context
52         if ( c && c.jquery )
53                 return jQuery(c.get()).find(a);
54         
55         // If the context is global, return a new object
56         if ( window == this )
57                 return new jQuery(a,c);
58
59         // Handle HTML strings
60         var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
61         if ( m ) a = jQuery.clean( [ m[1] ] );
62
63         // Watch for when an array is passed in
64         this.get( a.constructor == Array || a.length && a[0].nodeType ?
65                 // Assume that it's an array of DOM Elements
66                 jQuery.merge( a, [] ) :
67
68                 // Find the matching elements and save them for later
69                 jQuery.find( a, c ) );
70
71         var fn = arguments[ arguments.length - 1 ];
72         if ( fn && fn.constructor == Function )
73                 this.each(fn);
74 }
75
76 // Map over the $ in case of overwrite
77 if ( $ )
78         jQuery._$ = $;
79
80 // Map the jQuery namespace to the '$' one
81 var $ = jQuery;
82
83 jQuery.fn = jQuery.prototype = {
84         /**
85          * The current SVN version of jQuery.
86          *
87          * @private
88          * @property
89          * @name jquery
90          * @type String
91          */
92         jquery: "$Rev$",
93         
94         /**
95          * The number of elements currently matched.
96          *
97          * @example $("img").length;
98          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
99          * @result 2
100          *
101          * @property
102          * @name length
103          * @type Number
104          */
105         
106         /**
107          * The number of elements currently matched.
108          *
109          * @example $("img").size();
110          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
111          * @result 2
112          *
113          * @name size
114          * @type Number
115          */
116         size: function() {
117                 return this.length;
118         },
119         
120         /**
121          * Access all matched elements. This serves as a backwards-compatible
122          * way of accessing all matched elements (other than the jQuery object
123          * itself, which is, in fact, an array of elements).
124          *
125          * @example $("img").get();
126          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
127          * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
128          *
129          * @name get
130          * @type Array<Element>
131          */
132          
133         /**
134          * Access a single matched element. <tt>num</tt> is used to access the 
135          * <tt>num</tt>th element matched.
136          *
137          * @example $("img").get(1);
138          * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
139          * @result [ <img src="test1.jpg"/> ]
140          *
141          * @name get
142          * @type Element
143          * @param Number num Access the element in the <tt>num</tt>th position.
144          */
145          
146         /**
147          * Set the jQuery object to an array of elements.
148          *
149          * @example $("img").get([ document.body ]);
150          * @result $("img").get() == [ document.body ]
151          *
152          * @private
153          * @name get
154          * @type jQuery
155          * @param Elements elems An array of elements
156          */
157         get: function( num ) {
158                 // Watch for when an array (of elements) is passed in
159                 if ( num && num.constructor == Array ) {
160
161                         // Use a tricky hack to make the jQuery object
162                         // look and feel like an array
163                         this.length = 0;
164                         [].push.apply( this, num );
165                         
166                         return this;
167                 } else
168                         return num == undefined ?
169
170                                 // Return a 'clean' array
171                                 jQuery.map( this, function(a){ return a } ) :
172
173                                 // Return just the object
174                                 this[num];
175         },
176
177         /**
178          * Execute a function within the context of every matched element.
179          * This means that every time the passed-in function is executed
180          * (which is once for every element matched) the 'this' keyword
181          * points to the specific element.
182          *
183          * Additionally, the function, when executed, is passed a single
184          * argument representing the position of the element in the matched
185          * set.
186          *
187          * @example $("img").each(function(){ this.src = "test.jpg"; });
188          * @before <img/> <img/>
189          * @result <img src="test.jpg"/> <img src="test.jpg"/>
190          *
191          * @name each
192          * @type jQuery
193          * @param Function fn A function to execute
194          */
195         each: function( fn, args ) {
196                 // Iterate through all of the matched elements
197                 for ( var i = 0; i < this.length; i++ )
198                 
199                         // Execute the function within the context of each element
200                         fn.apply( this[i], args || [i] );
201                 
202                 return this;
203         },
204         
205         /**
206          * Access a property on the first matched element.
207          * This method makes it easy to retreive a property value
208          * from the first matched element.
209          *
210          * @example $("img").attr("src");
211          * @before <img src="test.jpg"/>
212          * @result test.jpg
213          *
214          * @name attr
215          * @type Object
216          * @param String name The name of the property to access.
217          */
218          
219         /**
220          * Set a hash of key/value object properties to all matched elements.
221          * This serves as the best way to set a large number of properties
222          * on all matched elements.
223          *
224          * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
225          * @before <img/>
226          * @result <img src="test.jpg" alt="Test Image"/>
227          *
228          * @name attr
229          * @type jQuery
230          * @param Hash prop A set of key/value pairs to set as object properties.
231          */
232          
233         /**
234          * Set a single property to a value, on all matched elements.
235          *
236          * @example $("img").attr("src","test.jpg");
237          * @before <img/>
238          * @result <img src="test.jpg"/>
239          *
240          * @name attr
241          * @type jQuery
242          * @param String key The name of the property to set.
243          * @param Object value The value to set the property to.
244          */
245         attr: function( key, value, type ) {
246                 // Check to see if we're setting style values
247                 return key.constructor != String || value ?
248                         this.each(function(){
249                                 // See if we're setting a hash of styles
250                                 if ( value == undefined )
251                                         // Set all the styles
252                                         for ( var prop in key )
253                                                 jQuery.attr(
254                                                         type ? this.style : this,
255                                                         prop, key[prop]
256                                                 );
257                                 
258                                 // See if we're setting a single key/value style
259                                 else
260                                         jQuery.attr(
261                                                 type ? this.style : this,
262                                                 key, value
263                                         );
264                         }) :
265                         
266                         // Look for the case where we're accessing a style value
267                         jQuery[ type || "attr" ]( this[0], key );
268         },
269         
270         /**
271          * Access a style property on the first matched element.
272          * This method makes it easy to retreive a style property value
273          * from the first matched element.
274          *
275          * @example $("p").css("red");
276          * @before <p style="color:red;">Test Paragraph.</p>
277          * @result red
278          *
279          * @name css
280          * @type Object
281          * @param String name The name of the property to access.
282          */
283          
284         /**
285          * Set a hash of key/value style properties to all matched elements.
286          * This serves as the best way to set a large number of style properties
287          * on all matched elements.
288          *
289          * @example $("p").css({ color: "red", background: "blue" });
290          * @before <p>Test Paragraph.</p>
291          * @result <p style="color:red; background:blue;">Test Paragraph.</p>
292          *
293          * @name css
294          * @type jQuery
295          * @param Hash prop A set of key/value pairs to set as style properties.
296          */
297          
298         /**
299          * Set a single style property to a value, on all matched elements.
300          *
301          * @example $("p").css("color","red");
302          * @before <p>Test Paragraph.</p>
303          * @result <p style="color:red;">Test Paragraph.</p>
304          *
305          * @name css
306          * @type jQuery
307          * @param String key The name of the property to set.
308          * @param Object value The value to set the property to.
309          */
310         css: function( key, value ) {
311                 return this.attr( key, value, "css" );
312         },
313         
314         /**
315          * Retreive the text contents of all matched elements. The result is
316          * a string that contains the combined text contents of all matched
317          * elements. This method works on both HTML and XML documents.
318          *
319          * @example $("p").text();
320          * @before <p>Test Paragraph.</p>
321          * @result Test Paragraph.
322          *
323          * @name text
324          * @type String
325          */
326         text: function(e) {
327                 e = e || this;
328                 var t = "";
329                 for ( var j = 0; j < e.length; j++ ) {
330                         var r = e[j].childNodes;
331                         for ( var i = 0; i < r.length; i++ )
332                                 t += r[i].nodeType != 1 ?
333                                         r[i].nodeValue : jQuery.fn.text([ r[i] ]);
334                 }
335                 return t;
336         },
337         
338         /**
339          * Wrap all matched elements with a structure of other elements.
340          * This wrapping process is most useful for injecting additional
341          * stucture into a document, without ruining the original semantic
342          * qualities of a document.
343          *
344          * The way that is works is that it goes through the first element argument
345          * provided and finds the deepest element within the structure - it is that
346          * element that will en-wrap everything else.
347          *
348          * @example $("p").wrap("<div class='wrap'></div>");
349          * @before <p>Test Paragraph.</p>
350          * @result <div class='wrap'><p>Test Paragraph.</p></div>
351          *
352          * @name wrap
353          * @type jQuery
354          * @any String html A string of HTML, that will be created on the fly and wrapped around the target.
355          * @any Element elem A DOM element that will be wrapped.
356          * @any Array<Element> elems An array of elements, the first of which will be wrapped.
357          * @any Object obj Any object, converted to a string, then a text node.
358          */
359         wrap: function() {
360                 // The elements to wrap the target around
361                 var a = jQuery.clean(arguments);
362                 
363                 // Wrap each of the matched elements individually
364                 return this.each(function(){
365                         // Clone the structure that we're using to wrap
366                         var b = a[0].cloneNode(true);
367                         
368                         // Insert it before the element to be wrapped
369                         this.parentNode.insertBefore( b, this );
370                         
371                         // Find he deepest point in the wrap structure
372                         while ( b.firstChild )
373                                 b = b.firstChild;
374                         
375                         // Move the matched element to within the wrap structure
376                         b.appendChild( this );
377                 });
378         },
379         
380         /**
381          * Append any number of elements to the inside of all matched elements.
382          * This operation is similar to doing an <tt>appendChild</tt> to all the 
383          * specified elements, adding them into the document.
384          * 
385          * @example $("p").append("<b>Hello</b>");
386          * @before <p>I would like to say: </p>
387          * @result <p>I would like to say: <b>Hello</b></p>
388          *
389          * @name append
390          * @type jQuery
391          * @any String html A string of HTML, that will be created on the fly and appended to the target.
392          * @any Element elem A DOM element that will be appended.
393          * @any Array<Element> elems An array of elements, all of which will be appended.
394          * @any Object obj Any object, converted to a string, then a text node.
395          */
396         append: function() {
397                 return this.domManip(arguments, true, 1, function(a){
398                         this.appendChild( a );
399                 });
400         },
401         
402         /**
403          * Prepend any number of elements to the inside of all matched elements.
404          * This operation is the best way to insert a set of elements inside, at the 
405          * beginning, of all the matched element.
406          * 
407          * @example $("p").prepend("<b>Hello</b>");
408          * @before <p>, how are you?</p>
409          * @result <p><b>Hello</b>, how are you?</p>
410          *
411          * @name prepend
412          * @type jQuery
413          * @any String html A string of HTML, that will be created on the fly and prepended to the target.
414          * @any Element elem A DOM element that will be prepended.
415          * @any Array<Element> elems An array of elements, all of which will be prepended.
416          * @any Object obj Any object, converted to a string, then a text node.
417          */
418         prepend: function() {
419                 return this.domManip(arguments, true, -1, function(a){
420                         this.insertBefore( a, this.firstChild );
421                 });
422         },
423         
424         /**
425          * Insert any number of elements before each of the matched elements.
426          * 
427          * @example $("p").before("<b>Hello</b>");
428          * @before <p>how are you?</p>
429          * @result <b>Hello</b><p>how are you?</p>
430          *
431          * @name before
432          * @type jQuery
433          * @any String html A string of HTML, that will be created on the fly and inserted.
434          * @any Element elem A DOM element that will beinserted.
435          * @any Array<Element> elems An array of elements, all of which will be inserted.
436          * @any Object obj Any object, converted to a string, then a text node.
437          */
438         before: function() {
439                 return this.domManip(arguments, false, 1, function(a){
440                         this.parentNode.insertBefore( a, this );
441                 });
442         },
443         
444         /**
445          * Insert any number of elements after each of the matched elements.
446          * 
447          * @example $("p").after("<p>I'm doing fine.</p>");
448          * @before <p>How are you?</p>
449          * @result <p>How are you?</p><p>I'm doing fine.</p>
450          *
451          * @name after
452          * @type jQuery
453          * @any String html A string of HTML, that will be created on the fly and inserted.
454          * @any Element elem A DOM element that will beinserted.
455          * @any Array<Element> elems An array of elements, all of which will be inserted.
456          * @any Object obj Any object, converted to a string, then a text node.
457          */
458         after: function() {
459                 return this.domManip(arguments, false, -1, function(a){
460                         this.parentNode.insertBefore( a, this.nextSibling );
461                 });
462         },
463         
464         /**
465          * End the most recent 'destructive' operation, reverting the list of matched elements
466          * back to its previous state. After an end operation, the list of matched elements will 
467          * revert to the last state of matched elements.
468          *
469          * @example $("p").find("span").end();
470          * @before <p><span>Hello</span>, how are you?</p>
471          * @result $("p").find("span").end() == [ <p>...</p> ]
472          *
473          * @name end
474          * @type jQuery
475          */
476         end: function() {
477                 return this.get( this.stack.pop() );
478         },
479         
480         /**
481          * Searches for all elements that match the specified expression.
482          * This method is the optimal way of finding additional descendant
483          * elements with which to process.
484          *
485          * All searching is done using a jQuery expression. The expression can be 
486          * written using CSS 1-3 Selector syntax, or basic XPath.
487          *
488          * @example $("p").find("span");
489          * @before <p><span>Hello</span>, how are you?</p>
490          * @result $("p").find("span") == [ <span>Hello</span> ]
491          *
492          * @name find
493          * @type jQuery
494          * @param String expr An expression to search with.
495          */
496         find: function(t) {
497                 return this.pushStack( jQuery.map( this, function(a){
498                         return jQuery.find(t,a);
499                 }), arguments );
500         },
501         
502         /**
503          * Removes all elements from the set of matched elements that do not 
504          * match the specified expression. This method is used to narrow down
505          * the results of a search.
506          *
507          * All searching is done using a jQuery expression. The expression
508          * can be written using CSS 1-3 Selector syntax, or basic XPath.
509          * 
510          * @example $("p").filter(".selected")
511          * @before <p class="selected">Hello</p><p>How are you?</p>
512          * @result $("p").filter(".selected") == [ <p class="selected">Hello</p> ]
513          *
514          * @name filter
515          * @type jQuery
516          * @param String expr An expression to search with.
517          */
518
519         /**
520          * Removes all elements from the set of matched elements that do not
521          * match at least one of the expressions passed to the function. This 
522          * method is used when you want to filter the set of matched elements 
523          * through more than one expression.
524          *
525          * Elements will be retained in the jQuery object if they match at
526          * least one of the expressions passed.
527          *
528          * @example $("p").filter([".selected", ":first"])
529          * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
530          * @result $("p").filter([".selected", ":first"]) == [ <p>Hello</p>, <p class="selected">And Again</p> ]
531          *
532          * @name filter
533          * @type jQuery
534          * @param Array<String> exprs A set of expressions to evaluate against
535          */
536         filter: function(t) {
537                 return this.pushStack(
538                         t.constructor == Array &&
539                         jQuery.map(this,function(a){
540                                 for ( var i = 0; i < t.length; i++ )
541                                         if ( jQuery.filter(t[i],[a]).r.length )
542                                                 return a;
543                         }) ||
544
545                         t.constructor == Boolean &&
546                         ( t ? this.get() : [] ) ||
547
548                         t.constructor == Function &&
549                         jQuery.grep( this, t ) ||
550
551                         jQuery.filter(t,this).r, arguments );
552         },
553         
554         /**
555          * Removes the specified Element from the set of matched elements. This
556          * method is used to remove a single Element from a jQuery object.
557          *
558          * @example $("p").not( document.getElementById("selected") )
559          * @before <p>Hello</p><p id="selected">Hello Again</p>
560          * @result [ <p>Hello</p> ]
561          *
562          * @name not
563          * @type jQuery
564          * @param Element el An element to remove from the set
565          */
566
567         /**
568          * Removes elements matching the specified expression from the set
569          * of matched elements. This method is used to remove one or more
570          * elements from a jQuery object.
571          * 
572          * @example $("p").not("#selected")
573          * @before <p>Hello</p><p id="selected">Hello Again</p>
574          * @result [ <p>Hello</p> ]
575          *
576          * @name not
577          * @type jQuery
578          * @param String expr An expression with which to remove matching elements
579          */
580         not: function(t) {
581                 return this.pushStack( t.constructor == String ?
582                         jQuery.filter(t,this,false).r :
583                         jQuery.grep(this,function(a){ return a != t; }), arguments );
584         },
585
586         /**
587          * Adds the elements matched by the expression to the jQuery object. This
588          * can be used to concatenate the result sets of two expressions.
589          *
590          * @example $("p").add("span")
591          * @before <p>Hello</p><p><span>Hello Again</span></p>
592          * @result [ <p>Hello</p>, <span>Hello Again</span> ]
593          *
594          * @name add
595          * @type jQuery
596          * @param String expr An expression whose matched elements are added
597          */
598
599         /**
600          * Adds each of the Elements in the array to the set of matched elements.
601          * This is used to add a set of Elements to a jQuery object.
602          *
603          * @example $("p").add([document.getElementById("a"), document.getElementById("b")])
604          * @before <p>Hello</p><p><span id="a">Hello Again</span><span id="b">And Again</span></p>
605          * @result [ <p>Hello</p>, <span id="a">Hello Again</span>, <span id="b">And Again</span> ]
606          *
607          * @name add
608          * @type jQuery
609          * @param Array<Element> els An array of Elements to add
610          */
611
612         /**
613          * Adds a single Element to the set of matched elements. This is used to
614          * add a single Element to a jQuery object.
615          *
616          * @example $("p").add( document.getElementById("a") )
617          * @before <p>Hello</p><p><span id="a">Hello Again</span></p>
618          * @result [ <p>Hello</p>, <span id="a">Hello Again</span> ]
619          *
620          * @name add
621          * @type jQuery
622          * @param Element el An Element to add
623          */
624         add: function(t) {
625                 return this.pushStack( jQuery.merge( this, t.constructor == String ?
626                         jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments );
627         },
628         
629         /**
630          * A wrapper function for each() to be used by append and prepend.
631          * Handles cases where you're trying to modify the inner contents of
632          * a table, when you actually need to work with the tbody.
633          *
634          * @member jQuery
635          * @param {String} expr The expression with which to filter
636          * @type Boolean
637          */
638         is: function(expr) {
639                 return expr ? jQuery.filter(expr,this).r.length > 0 : this.length > 0;
640         },
641         
642         /**
643          * 
644          *
645          * @private
646          * @name domManip
647          * @param Array args
648          * @param Boolean table
649          * @param Number int
650          * @param Function fn The function doing the DOM manipulation.
651          * @type jQuery
652          */
653         domManip: function(args, table, dir, fn){
654                 var clone = this.size() > 1;
655                 var a = jQuery.clean(args);
656                 
657                 return this.each(function(){
658                         var obj = this;
659                         
660                         if ( table && this.nodeName == "TABLE" ) {
661                                 var tbody = this.getElementsByTagName("tbody");
662
663                                 if ( !tbody.length ) {
664                                         obj = document.createElement("tbody");
665                                         this.appendChild( obj );
666                                 } else
667                                         obj = tbody[0];
668                         }
669                         //alert( obj );
670                         for ( var i = ( dir < 0 ? a.length - 1 : 0 );
671                                 i != ( dir < 0 ? dir : a.length ); i += dir ) {
672                                         fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );
673                                         //alert( fn );
674                         }
675                 });
676         },
677         
678         /**
679          * 
680          *
681          * @private
682          * @name pushStack
683          * @param Array a
684          * @param Array args
685          * @type jQuery
686          */
687         pushStack: function(a,args) {
688                 var fn = args && args[args.length-1];
689
690                 if ( !fn || fn.constructor != Function ) {
691                         if ( !this.stack ) this.stack = [];
692                         this.stack.push( this.get() );
693                         this.get( a );
694                 } else {
695                         var old = this.get();
696                         this.get( a );
697                         if ( fn.constructor == Function )
698                                 return this.each( fn );
699                         this.get( old );
700                 }
701
702                 return this;
703         },
704         
705         extend: function(obj,prop) {
706                 if ( !prop ) { prop = obj; obj = this; }
707                 for ( var i in prop ) obj[i] = prop[i];
708                 return obj;
709         }
710 };
711
712 jQuery.extend = jQuery.fn.extend;
713
714 new function() {
715         var b = navigator.userAgent.toLowerCase();
716
717         // Figure out what browser is being used
718         jQuery.browser = {
719                 safari: /webkit/.test(b),
720                 opera: /opera/.test(b),
721                 msie: /msie/.test(b) && !/opera/.test(b),
722                 mozilla: /mozilla/.test(b) && !/compatible/.test(b)
723         };
724
725         // Check to see if the W3C box model is being used
726         jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
727
728         var axis = {
729                 /**
730                  * Get a set of elements containing the unique parents of the matched
731                  * set of elements.
732                  *
733                  * @example $("p").parent()
734                  * @before <div><p>Hello</p><p>Hello</p></div>
735                  * @result [ <div><p>Hello</p><p>Hello</p></div> ]
736                  *
737                  * @name parent
738                  * @type jQuery
739                  */
740
741                 /**
742                  * Get a set of elements containing the unique parents of the matched
743                  * set of elements, and filtered by an expression.
744                  *
745                  * @example $("p").parent(".selected")
746                  * @before <div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
747                  * @result [ <div class="selected"><p>Hello Again</p></div> ]
748                  *
749                  * @name parent
750                  * @type jQuery
751                  * @param String expr An expression to filter the parents with
752                  */
753                 parent: "a.parentNode",
754
755                 /**
756                  * Get a set of elements containing the unique ancestors of the matched
757                  * set of elements.
758                  *
759                  * @example $("span").ancestors()
760                  * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
761                  * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ] 
762                  *
763                  * @name ancestors
764                  * @type jQuery
765                  */
766
767                 /**
768                  * Get a set of elements containing the unique ancestors of the matched
769                  * set of elements, and filtered by an expression.
770                  *
771                  * @example $("span").ancestors("p")
772                  * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
773                  * @result [ <p><span>Hello</span></p> ] 
774                  *
775                  * @name ancestors
776                  * @type jQuery
777                  * @param String expr An expression to filter the ancestors with
778                  */
779                 ancestors: jQuery.parents,
780                 
781                 /**
782                  * A synonym for ancestors
783                  */
784                 parents: jQuery.parents,
785
786                 /**
787                  * Get a set of elements containing the unique next siblings of each of the 
788                  * matched set of elements.
789                  * 
790                  * It only returns the very next sibling, not all next siblings.
791                  *
792                  * @example $("p").next()
793                  * @before <p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
794                  * @result [ <p>Hello Again</p>, <div><span>And Again</span></div> ]
795                  *
796                  * @name next
797                  * @type jQuery
798                  */
799
800                 /**
801                  * Get a set of elements containing the unique next siblings of each of the 
802                  * matched set of elements, and filtered by an expression.
803                  * 
804                  * It only returns the very next sibling, not all next siblings.
805                  *
806                  * @example $("p").next(".selected")
807                  * @before <p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
808                  * @result [ <p class="selected">Hello Again</p> ]
809                  *
810                  * @name next
811                  * @type jQuery
812                  * @param String expr An expression to filter the next Elements with
813                  */
814                 next: "jQuery.sibling(a).next",
815
816                 /**
817                  * Get a set of elements containing the unique previous siblings of each of the 
818                  * matched set of elements.
819                  * 
820                  * It only returns the immediately previous sibling, not all previous siblings.
821                  *
822                  * @example $("p").previous()
823                  * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
824                  * @result [ <div><span>Hello Again</span></div> ]
825                  *
826                  * @name prev
827                  * @type jQuery
828                  */
829
830                 /**
831                  * Get a set of elements containing the unique previous siblings of each of the 
832                  * matched set of elements, and filtered by an expression.
833                  * 
834                  * It only returns the immediately previous sibling, not all previous siblings.
835                  *
836                  * @example $("p").previous("selected")
837                  * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
838                  * @result [ <div><span>Hello</span></div> ]
839                  *
840                  * @name prev
841                  * @type jQuery
842                  * @param String expr An expression to filter the previous Elements with
843                  */
844                 prev: "jQuery.sibling(a).prev",
845
846                 /**
847                  * Get a set of elements containing all of the unique siblings of each of the 
848                  * matched set of elements.
849                  * 
850                  * @example $("div").siblings()
851                  * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
852                  * @result [ <p>Hello</p>, <p>And Again</p> ]
853                  *
854                  * @name siblings
855                  * @type jQuery
856                  */
857
858                 /**
859                  * Get a set of elements containing all of the unique siblings of each of the 
860                  * matched set of elements, and filtered by an expression.
861                  *
862                  * @example $("div").siblings("selected")
863                  * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
864                  * @result [ <p class="selected">Hello Again</p> ]
865                  *
866                  * @name siblings
867                  * @type jQuery
868                  * @param String expr An expression to filter the sibling Elements with
869                  */
870                 siblings: jQuery.sibling
871         };
872         
873         for ( var i in axis ) new function(){
874                 var t = axis[i];
875                 jQuery.fn[ i ] = function(a) {
876                         var ret = jQuery.map(this,t);
877                         if ( a && a.constructor == String )
878                                 ret = jQuery.filter(a,ret).r;
879                         return this.pushStack( ret, arguments );
880                 };
881         };
882
883         // appendTo, prependTo, beforeTo, afterTo
884         
885         var to = ["append","prepend","before","after"];
886         
887         for ( var i = 0; i < to.length; i++ ) new function(){
888                 var n = to[i];
889                 jQuery.fn[ n + "To" ] = function(){
890                         var a = arguments;
891                         return this.each(function(){
892                                 for ( var i = 0; i < a.length; i++ )
893                                         $(a[i])[n]( this );
894                         });
895                 };
896         };
897         
898         var each = {
899                 /**
900                  * Displays each of the set of matched elements if they are hidden.
901                  * 
902                  * @example $("p").show()
903                  * @before <p style="display: none">Hello</p>
904                  * @result [ <p style="display: block">Hello</p> ]
905                  *
906                  * @name show
907                  * @type jQuery
908                  */
909                 show: function(){
910                         this.style.display = this.oldblock ? this.oldblock : "";
911                         if ( jQuery.css(this,"display") == "none" )
912                                 this.style.display = "block";
913                 },
914
915                 /**
916                  * Hides each of the set of matched elements if they are shown.
917                  *
918                  * @example $("p").hide()
919                  * @before <p>Hello</p>
920                  * @result [ <p style="display: none">Hello</p> ]
921                  *
922                  * @name hide
923                  * @type jQuery
924                  */
925                 hide: function(){
926                         this.oldblock = jQuery.css(this,"display");
927                         if ( this.oldblock == "none" )
928                                 this.oldblock = "block";
929                         this.style.display = "none";
930                 },
931                 
932                 /**
933                  * Toggles each of the set of matched elements. If they are shown,
934                  * toggle makes them hidden. If they are hidden, toggle
935                  * makes them shown.
936                  *
937                  * @example $("p").toggle()
938                  * @before <p>Hello</p><p style="display: none">Hello Again</p>
939                  * @result [ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
940                  *
941                  * @name toggle
942                  * @type jQuery
943                  */
944                 toggle: function(){
945                         var d = jQuery.css(this,"display");
946                         $(this)[ !d || d == "none" ? "show" : "hide" ]();
947                 },
948                 
949                 /**
950                  * Adds the specified class to each of the set of matched elements.
951                  *
952                  * @example ("p").addClass("selected")
953                  * @before <p>Hello</p>
954                  * @result [ <p class="selected">Hello</p> ]
955                  * 
956                  * @name addClass
957                  * @type jQuery
958                  * @param String class A CSS class to add to the elements
959                  */
960                 addClass: function(c){
961                         jQuery.className.add(this,c);
962                 },
963                 
964                 /**
965                  * The opposite of addClass. Removes the specified class from the
966                  * set of matched elements.
967                  *
968                  * @example ("p").removeClass("selected")
969                  * @before <p class="selected">Hello</p>
970                  * @result [ <p>Hello</p> ]
971                  *
972                  * @name removeClass
973                  * @type jQuery
974                  * @param String class A CSS class to remove from the elements
975                  */
976                 removeClass: function(c){
977                         jQuery.className.remove(this,c);
978                 },
979         
980                 /**
981                  * Adds the specified class if it is present. Remove it if it is
982                  * not present.
983                  *
984                  * @example ("p").toggleClass("selected")
985                  * @before <p>Hello</p><p class="selected">Hello Again</p>
986                  * @result [ <p class="selected">Hello</p>, <p>Hello Again</p> ]
987                  *
988                  * @name toggleClass
989                  * @type jQuery
990                  * @param String class A CSS class with which to toggle the elements
991                  */
992                 toggleClass: function( c ){
993                         jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this,c);
994                 },
995                 
996                 /**
997                  * TODO: Document
998                  */
999                 remove: function(a){
1000                         if ( !a || jQuery.filter( [this], a ).r )
1001                                 this.parentNode.removeChild( this );
1002                 },
1003         
1004                 /**
1005                  * Removes all child nodes from the set of matched elements.
1006                  *
1007                  * @example ("p").empty()
1008                  * @before <p>Hello, <span>Person</span> <a href="#">and person</a></p>
1009                  * @result [ <p></p> ]
1010                  *
1011                  * @name empty
1012                  * @type jQuery
1013                  */
1014                 empty: function(){
1015                         while ( this.firstChild )
1016                                 this.removeChild( this.firstChild );
1017                 },
1018                 
1019                 /**
1020                  * Binds a particular event (like click) to a each of a set of match elements.
1021                  *
1022                  * @example $("p").bind( "click", function() { alert("Hello"); } )
1023                  * @before <p>Hello</p>
1024                  * @result [ <p>Hello</p> ]
1025                  *
1026                  * Cancel a default action and prevent it from bubbling by returning false
1027                  * from your function.
1028                  *
1029                  * @example $("form").bind( "submit", function() { return false; } )
1030                  *
1031                  * Cancel a default action by using the preventDefault method.
1032                  *
1033                  * @example $("form").bind( "submit", function() { e.preventDefault(); } )
1034                  *
1035                  * Stop an event from bubbling by using the stopPropogation method.
1036                  *
1037                  * @example $("form").bind( "submit", function() { e.stopPropogation(); } )
1038                  *
1039                  * @name bind
1040                  * @type jQuery
1041                  * @param String type An event type
1042                  * @param Function fn A function to bind to the event on each of the set of matched elements
1043                  */
1044                 bind: function( type, fn ) {
1045                         if ( fn.constructor == String )
1046                                 fn = new Function("e", ( !fn.indexOf(".") ? "$(this)" : "return " ) + fn);
1047                         jQuery.event.add( this, type, fn );
1048                 },
1049                 
1050                 /**
1051                  * The opposite of bind. Removes a bound event from each of a set of matched
1052                  * elements. You must pass the identical function that was used in the original 
1053                  * bind method.
1054                  *
1055                  * @example $("p").unbind( "click", function() { alert("Hello"); } )
1056                  * @before <p>Hello</p>
1057                  * @result [ <p>Hello</p> ]
1058                  *
1059                  * @name unbind
1060                  * @type jQuery
1061                  * @param String type An event type
1062                  * @param Function fn A function to unbind from the event on each of the set of matched elements
1063                  */
1064                 unbind: function( type, fn ) {
1065                         jQuery.event.remove( this, type, fn );
1066                 },
1067                 
1068                 /**
1069                  * Trigger a particular event.
1070                  *
1071                  * @example $("p").trigger("click")
1072                  * @before <p>Hello</p>
1073                  * @result [ <p>Hello</p> ]
1074                  *
1075                  * @name trigger
1076                  * @type jQuery
1077                  * @param String type An event type
1078                  */
1079                 trigger: function( type ) {
1080                         jQuery.event.trigger( this, type );
1081                 }
1082         };
1083         
1084         for ( var i in each ) new function() {
1085                 var n = each[i];
1086                 jQuery.fn[ i ] = function() {
1087                         return this.each( n, arguments );
1088                 };
1089         };
1090         
1091         var attr = {
1092                 val: "value",
1093                 html: "innerHTML",
1094                 value: null,
1095                 id: null,
1096                 title: null,
1097                 name: null,
1098                 href: null,
1099                 src: null,
1100                 rel: null
1101         };
1102         
1103         for ( var i in attr ) new function() {
1104                 var n = attr[i] || i;
1105                 jQuery.fn[ i ] = function(h) {
1106                         return h == undefined ?
1107                                 this.length ? this[0][n] : null :
1108                                 this.attr( n, h );
1109                 };
1110         }
1111         
1112         var css = "width,height,top,left,position,float,overflow,color,background".split(",");
1113         
1114         for ( var i in css ) new function() {
1115                 var n = css[i];
1116                 jQuery.fn[ i ] = function(h) {
1117                         return h == undefined ?
1118                                 this.length ? jQuery.css( this[0], n ) : null :
1119                                 this.css( n, h );
1120                 };
1121         }
1122
1123 }
1124
1125 jQuery.extend({
1126         className: {
1127                 add: function(o,c){
1128                         if (jQuery.className.has(o,c)) return;
1129                         o.className += ( o.className ? " " : "" ) + c;
1130                 },
1131                 remove: function(o,c){
1132                         o.className = !c ? "" :
1133                                 o.className.replace(
1134                                         new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
1135                 },
1136                 has: function(e,a) {
1137                         if ( e.className )
1138                                 e = e.className;
1139                         return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
1140                 }
1141         },
1142         
1143         /**
1144          * Swap in/out style options.
1145          * @private
1146          */
1147         swap: function(e,o,f) {
1148                 for ( var i in o ) {
1149                         e.style["old"+i] = e.style[i];
1150                         e.style[i] = o[i];
1151                 }
1152                 f.apply( e, [] );
1153                 for ( var i in o )
1154                         e.style[i] = e.style["old"+i];
1155         },
1156         
1157         css: function(e,p) {
1158                 if ( p == "height" || p == "width" ) {
1159                         var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
1160         
1161                         for ( var i in d ) {
1162                                 old["padding" + d[i]] = 0;
1163                                 old["border" + d[i] + "Width"] = 0;
1164                         }
1165         
1166                         jQuery.swap( e, old, function() {
1167                                 if (jQuery.css(e,"display") != "none") {
1168                                         oHeight = e.offsetHeight;
1169                                         oWidth = e.offsetWidth;
1170                                 } else
1171                                         jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "" },
1172                                                 function(){
1173                                                         oHeight = e.clientHeight;
1174                                                         oWidth = e.clientWidth;
1175                                                 });
1176                         });
1177         
1178                         return p == "height" ? oHeight : oWidth;
1179                 }
1180                 
1181                 var r;
1182         
1183                 if (e.style[p])
1184                         r = e.style[p];
1185                 else if (e.currentStyle)
1186                         r = e.currentStyle[p];
1187                 else if (document.defaultView && document.defaultView.getComputedStyle) {
1188                         p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
1189                         var s = document.defaultView.getComputedStyle(e,"");
1190                         r = s ? s.getPropertyValue(p) : null;
1191                 }
1192                 
1193                 return r;
1194         },
1195         
1196         clean: function(a) {
1197                 var r = [];
1198                 for ( var i = 0; i < a.length; i++ ) {
1199                         if ( a[i].constructor == String ) {
1200         
1201                                 if ( !a[i].indexOf("<tr") ) {
1202                                         var tr = true;
1203                                         a[i] = "<table>" + a[i] + "</table>";
1204                                 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
1205                                         var td = true;
1206                                         a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
1207                                 }
1208         
1209                                 var div = document.createElement("div");
1210                                 div.innerHTML = a[i];
1211         
1212                                 if ( tr || td ) {
1213                                         div = div.firstChild.firstChild;
1214                                         if ( td ) div = div.firstChild;
1215                                 }
1216         
1217                                 for ( var j = 0; j < div.childNodes.length; j++ )
1218                                         r.push( div.childNodes[j] );
1219                         } else if ( a[i].jquery || a[i].length && !a[i].nodeType )
1220                                 for ( var k = 0; k < a[i].length; k++ )
1221                                         r.push( a[i][k] );
1222                         else if ( a[i] !== null )
1223                                 r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) );
1224                 }
1225                 return r;
1226         },
1227         
1228         expr: {
1229                 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
1230                 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
1231                 ":": {
1232                         // Position Checks
1233                         lt: "i<m[3]-0",
1234                         gt: "i>m[3]-0",
1235                         nth: "m[3]-0==i",
1236                         eq: "m[3]-0==i",
1237                         first: "i==0",
1238                         last: "i==r.length-1",
1239                         even: "i%2==0",
1240                         odd: "i%2",
1241                         
1242                         // Child Checks
1243                         "first-child": "jQuery.sibling(a,0).cur",
1244                         "last-child": "jQuery.sibling(a,0).last",
1245                         "only-child": "jQuery.sibling(a).length==1",
1246                         
1247                         // Parent Checks
1248                         parent: "a.childNodes.length",
1249                         empty: "!a.childNodes.length",
1250                         
1251                         // Text Check
1252                         contains: "(a.innerText||a.innerHTML).indexOf(m[3])>=0",
1253                         
1254                         // Visibility
1255                         visible: "jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!='hidden'",
1256                         hidden: "jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')=='hidden'",
1257                         
1258                         // Form elements
1259                         enabled: "!a.disabled",
1260                         disabled: "a.disabled",
1261                         checked: "a.checked"
1262                 },
1263                 ".": "jQuery.className.has(a,m[2])",
1264                 "@": {
1265                         "=": "z==m[4]",
1266                         "!=": "z!=m[4]",
1267                         "^=": "!z.indexOf(m[4])",
1268                         "$=": "z.substr(z.length - m[4].length,m[4].length)==m[4]",
1269                         "*=": "z.indexOf(m[4])>=0",
1270                         "": "z"
1271                 },
1272                 "[": "jQuery.find(m[2],a).length"
1273         },
1274         
1275         token: [
1276                 "\\.\\.|/\\.\\.", "a.parentNode",
1277                 ">|/", "jQuery.sibling(a.firstChild)",
1278                 "\\+", "jQuery.sibling(a).next",
1279                 "~", function(a){
1280                         var r = [];
1281                         var s = jQuery.sibling(a);
1282                         if ( s.n > 0 )
1283                                 for ( var i = s.n; i < s.length; i++ )
1284                                         r.push( s[i] );
1285                         return r;
1286                 }
1287         ],
1288         
1289         find: function( t, context ) {
1290                 // Make sure that the context is a DOM Element
1291                 if ( context && context.getElementsByTagName == undefined )
1292                         context = null;
1293         
1294                 // Set the correct context (if none is provided)
1295                 context = context || jQuery.context || document;
1296         
1297                 if ( t.constructor != String ) return [t];
1298         
1299                 if ( !t.indexOf("//") ) {
1300                         context = context.documentElement;
1301                         t = t.substr(2,t.length);
1302                 } else if ( !t.indexOf("/") ) {
1303                         context = context.documentElement;
1304                         t = t.substr(1,t.length);
1305                         // FIX Assume the root element is right :(
1306                         if ( t.indexOf("/") >= 1 )
1307                                 t = t.substr(t.indexOf("/"),t.length);
1308                 }
1309         
1310                 var ret = [context];
1311                 var done = [];
1312                 var last = null;
1313         
1314                 while ( t.length > 0 && last != t ) {
1315                         var r = [];
1316                         last = t;
1317         
1318                         t = jQuery.trim(t).replace( /^\/\//i, "" );
1319                         
1320                         var foundToken = false;
1321                         
1322                         for ( var i = 0; i < jQuery.token.length; i += 2 ) {
1323                                 var re = new RegExp("^(" + jQuery.token[i] + ")");
1324                                 var m = re.exec(t);
1325                                 
1326                                 if ( m ) {
1327                                         r = ret = jQuery.map( ret, jQuery.token[i+1] );
1328                                         t = jQuery.trim( t.replace( re, "" ) );
1329                                         foundToken = true;
1330                                 }
1331                         }
1332                         
1333                         if ( !foundToken ) {
1334                                 if ( !t.indexOf(",") || !t.indexOf("|") ) {
1335                                         if ( ret[0] == context ) ret.shift();
1336                                         done = jQuery.merge( done, ret );
1337                                         r = ret = [context];
1338                                         t = " " + t.substr(1,t.length);
1339                                 } else {
1340                                         var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
1341                                         var m = re2.exec(t);
1342                 
1343                                         if ( m[1] == "#" ) {
1344                                                 // Ummm, should make this work in all XML docs
1345                                                 var oid = document.getElementById(m[2]);
1346                                                 r = ret = oid ? [oid] : [];
1347                                                 t = t.replace( re2, "" );
1348                                         } else {
1349                                                 if ( !m[2] || m[1] == "." ) m[2] = "*";
1350                 
1351                                                 for ( var i = 0; i < ret.length; i++ )
1352                                                         r = jQuery.merge( r,
1353                                                                 m[2] == "*" ?
1354                                                                         jQuery.getAll(ret[i]) :
1355                                                                         ret[i].getElementsByTagName(m[2])
1356                                                         );
1357                                         }
1358                                 }
1359                         }
1360         
1361                         if ( t ) {
1362                                 var val = jQuery.filter(t,r);
1363                                 ret = r = val.r;
1364                                 t = jQuery.trim(val.t);
1365                         }
1366                 }
1367         
1368                 if ( ret && ret[0] == context ) ret.shift();
1369                 done = jQuery.merge( done, ret );
1370         
1371                 return done;
1372         },
1373         
1374         getAll: function(o,r) {
1375                 r = r || [];
1376                 var s = o.childNodes;
1377                 for ( var i = 0; i < s.length; i++ )
1378                         if ( s[i].nodeType == 1 ) {
1379                                 r.push( s[i] );
1380                                 jQuery.getAll( s[i], r );
1381                         }
1382                 return r;
1383         },
1384         
1385         attr: function(o,a,v){
1386                 if ( a && a.constructor == String ) {
1387                         var fix = {
1388                                 "for": "htmlFor",
1389                                 "class": "className",
1390                                 "float": "cssFloat"
1391                         };
1392                         
1393                         a = (fix[a] && fix[a].replace && fix[a] || a)
1394                                 .replace(/-([a-z])/ig,function(z,b){
1395                                         return b.toUpperCase();
1396                                 });
1397                         
1398                         if ( v != undefined ) {
1399                                 o[a] = v;
1400                                 if ( o.setAttribute && a != "disabled" )
1401                                         o.setAttribute(a,v);
1402                         }
1403                         
1404                         return o[a] || o.getAttribute && o.getAttribute(a) || "";
1405                 } else
1406                         return "";
1407         },
1408
1409         // The regular expressions that power the parsing engine
1410         parse: [
1411                 // Match: [@value='test'], [@foo]
1412                 "\\[ *(@)S *([!*$^=]*) *Q\\]",
1413
1414                 // Match: [div], [div p]
1415                 "(\\[)Q\\]",
1416
1417                 // Match: :contains('foo')
1418                 "(:)S\\(Q\\)",
1419
1420                 // Match: :even, :last-chlid
1421                 "([:.#]*)S"
1422         ],
1423
1424         parseSwap: [ 1, 0, 0, 0 ],
1425         
1426         filter: function(t,r,not) {
1427                 // Figure out if we're doing regular, or inverse, filtering
1428                 var g = not !== false ? jQuery.grep :
1429                         function(a,f) {return jQuery.grep(a,f,true);};
1430                 
1431                 while ( t && /^[a-z[({<*:.#]/i.test(t) ) {
1432
1433                         for ( var i = 0; i < jQuery.parse.length; i++ ) {
1434                                 var re = new RegExp( "^" + jQuery.parse[i]
1435
1436                                         // Look for a string-like sequence
1437                                         .replace( 'S', "([a-z*_-][a-z0-9_-]*)" )
1438
1439                                         // Look for something (optionally) enclosed with quotes
1440                                         .replace( 'Q', " *'?\"?([^'\"]*)'?\"? *" ), "i" );
1441
1442                                 var m = re.exec( t );
1443
1444                                 if ( m ) {
1445                                         // Re-organize the match
1446                                         if ( jQuery.parseSwap[i] )
1447                                                 m = ["", m[1], m[3], m[2], m[4]];
1448
1449                                         // Remove what we just matched
1450                                         t = t.replace( re, "" );
1451
1452                                         break;
1453                                 }
1454                         }
1455         
1456                         // :not() is a special case that can be optomized by
1457                         // keeping it out of the expression list
1458                         if ( m[1] == ":" && m[2] == "not" )
1459                                 r = jQuery.filter(m[3],r,false).r;
1460                         
1461                         // Otherwise, find the expression to execute
1462                         else {
1463                                 var f = jQuery.expr[m[1]];
1464                                 if ( f.constructor != String )
1465                                         f = jQuery.expr[m[1]][m[2]];
1466                                         
1467                                 // Build a custom macro to enclose it
1468                                 eval("f = function(a,i){" + 
1469                                         ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) + 
1470                                         "return " + f + "}");
1471                                 
1472                                 // Execute it against the current filter
1473                                 r = g( r, f );
1474                         }
1475                 }
1476         
1477                 // Return an array of filtered elements (r)
1478                 // and the modified expression string (t)
1479                 return { r: r, t: t };
1480         },
1481         
1482         /**
1483          * Remove the whitespace from the beginning and end of a string.
1484          *
1485          * @private
1486          * @name $.trim
1487          * @type String
1488          * @param String str The string to trim.
1489          */
1490         trim: function(t){
1491                 return t.replace(/^\s+|\s+$/g, "");
1492         },
1493         
1494         /**
1495          * All ancestors of a given element.
1496          *
1497          * @private
1498          * @name $.parents
1499          * @type Array<Element>
1500          * @param Element elem The element to find the ancestors of.
1501          */
1502         parents: function(a){
1503                 var b = [];
1504                 var c = a.parentNode;
1505                 while ( c && c != document ) {
1506                         b.push( c );
1507                         c = c.parentNode;
1508                 }
1509                 return b;
1510         },
1511         
1512         /**
1513          * All elements on a specified axis.
1514          *
1515          * @private
1516          * @name $.sibling
1517          * @type Array
1518          * @param Element elem The element to find all the siblings of (including itself).
1519          */
1520         sibling: function(a,n) {
1521                 var type = [];
1522                 var tmp = a.parentNode.childNodes;
1523                 for ( var i = 0; i < tmp.length; i++ ) {
1524                         if ( tmp[i].nodeType == 1 )
1525                                 type.push( tmp[i] );
1526                         if ( tmp[i] == a )
1527                                 type.n = type.length - 1;
1528                 }
1529                 type.last = type.n == type.length - 1;
1530                 type.cur =
1531                         n == "even" && type.n % 2 == 0 ||
1532                         n == "odd" && type.n % 2 ||
1533                         type[n] == a;
1534                 type.prev = type[type.n - 1];
1535                 type.next = type[type.n + 1];
1536                 return type;
1537         },
1538         
1539         /**
1540          * Merge two arrays together, removing all duplicates.
1541          *
1542          * @private
1543          * @name $.merge
1544          * @type Array
1545          * @param Array a The first array to merge.
1546          * @param Array b The second array to merge.
1547          */
1548         merge: function(a,b) {
1549                 var d = [];
1550                 
1551                 // Move b over to the new array (this helps to avoid
1552                 // StaticNodeList instances)
1553                 for ( var k = 0; k < b.length; k++ )
1554                         d[k] = b[k];
1555         
1556                 // Now check for duplicates between a and b and only
1557                 // add the unique items
1558                 for ( var i = 0; i < a.length; i++ ) {
1559                         var c = true;
1560                         
1561                         // The collision-checking process
1562                         for ( var j = 0; j < b.length; j++ )
1563                                 if ( a[i] == b[j] )
1564                                         c = false;
1565                                 
1566                         // If the item is unique, add it
1567                         if ( c )
1568                                 d.push( a[i] );
1569                 }
1570         
1571                 return d;
1572         },
1573         
1574         /**
1575          * Remove items that aren't matched in an array. The function passed
1576          * in to this method will be passed two arguments: 'a' (which is the
1577          * array item) and 'i' (which is the index of the item in the array).
1578          *
1579          * @private
1580          * @name $.grep
1581          * @type Array
1582          * @param Array array The Array to find items in.
1583          * @param Function fn The function to process each item against.
1584          * @param Boolean inv Invert the selection - select the opposite of the function.
1585          */
1586         grep: function(a,f,s) {
1587                 // If a string is passed in for the function, make a function
1588                 // for it (a handy shortcut)
1589                 if ( f.constructor == String )
1590                         f = new Function("a","i","return " + f);
1591                         
1592                 var r = [];
1593                 
1594                 // Go through the array, only saving the items
1595                 // that pass the validator function
1596                 for ( var i = 0; i < a.length; i++ )
1597                         if ( !s && f(a[i],i) || s && !f(a[i],i) )
1598                                 r.push( a[i] );
1599                 
1600                 return r;
1601         },
1602         
1603         /**
1604          * Translate all items in array to another array of items. The translation function
1605          * that is provided to this method is passed one argument: 'a' (the item to be 
1606          * translated). If an array is returned, that array is mapped out and merged into
1607          * the full array. Additionally, returning 'null' or 'undefined' will delete the item
1608          * from the array. Both of these changes imply that the size of the array may not
1609          * be the same size upon completion, as it was when it started.
1610          *
1611          * @private
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          */
1617         map: function(a,f) {
1618                 // If a string is passed in for the function, make a function
1619                 // for it (a handy shortcut)
1620                 if ( f.constructor == String )
1621                         f = new Function("a","return " + f);
1622                 
1623                 var r = [];
1624                 
1625                 // Go through the array, translating each of the items to their
1626                 // new value (or values).
1627                 for ( var i = 0; i < a.length; i++ ) {
1628                         var t = f(a[i],i);
1629                         if ( t !== null && t != undefined ) {
1630                                 if ( t.constructor != Array ) t = [t];
1631                                 r = jQuery.merge( t, r );
1632                         }
1633                 }
1634                 return r;
1635         },
1636         
1637         /*
1638          * A number of helper functions used for managing events.
1639          * Many of the ideas behind this code orignated from Dean Edwards' addEvent library.
1640          */
1641         event: {
1642         
1643                 // Bind an event to an element
1644                 // Original by Dean Edwards
1645                 add: function(element, type, handler) {
1646                         // For whatever reason, IE has trouble passing the window object
1647                         // around, causing it to be cloned in the process
1648                         if ( jQuery.browser.msie && element.setInterval != undefined )
1649                                 element = window;
1650                 
1651                         // Make sure that the function being executed has a unique ID
1652                         if ( !handler.guid )
1653                                 handler.guid = this.guid++;
1654                                 
1655                         // Init the element's event structure
1656                         if (!element.events)
1657                                 element.events = {};
1658                         
1659                         // Get the current list of functions bound to this event
1660                         var handlers = element.events[type];
1661                         
1662                         // If it hasn't been initialized yet
1663                         if (!handlers) {
1664                                 // Init the event handler queue
1665                                 handlers = element.events[type] = {};
1666                                 
1667                                 // Remember an existing handler, if it's already there
1668                                 if (element["on" + type])
1669                                         handlers[0] = element["on" + type];
1670                         }
1671
1672                         // Add the function to the element's handler list
1673                         handlers[handler.guid] = handler;
1674                         
1675                         // And bind the global event handler to the element
1676                         element["on" + type] = this.handle;
1677         
1678                         // Remember the function in a global list (for triggering)
1679                         if (!this.global[type])
1680                                 this.global[type] = [];
1681                         this.global[type].push( element );
1682                 },
1683                 
1684                 guid: 1,
1685                 global: {},
1686                 
1687                 // Detach an event or set of events from an element
1688                 remove: function(element, type, handler) {
1689                         if (element.events)
1690                                 if (type && element.events[type])
1691                                         if ( handler )
1692                                                 delete element.events[type][handler.guid];
1693                                         else
1694                                                 for ( var i in element.events[type] )
1695                                                         delete element.events[type][i];
1696                                 else
1697                                         for ( var j in element.events )
1698                                                 this.remove( element, j );
1699                 },
1700                 
1701                 trigger: function(type,data,element) {
1702                         // Touch up the incoming data
1703                         data = data || [];
1704         
1705                         // Handle a global trigger
1706                         if ( !element ) {
1707                                 var g = this.global[type];
1708                                 if ( g )
1709                                         for ( var i = 0; i < g.length; i++ )
1710                                                 this.trigger( type, data, g[i] );
1711         
1712                         // Handle triggering a single element
1713                         } else if ( element["on" + type] ) {
1714                                 // Pass along a fake event
1715                                 data.unshift( this.fix({ type: type, target: element }) );
1716         
1717                                 // Trigger the event
1718                                 element["on" + type].apply( element, data );
1719                         }
1720                 },
1721                 
1722                 handle: function(event) {
1723                         event = event || jQuery.event.fix( window.event );
1724         
1725                         // If no correct event was found, fail
1726                         if ( !event ) return;
1727                 
1728                         var returnValue = true;
1729
1730                         var c = this.events[event.type];
1731                 
1732                         for ( var j in c ) {
1733                                 if ( c[j].apply( this, [event] ) === false ) {
1734                                         event.preventDefault();
1735                                         event.stopPropagation();
1736                                         returnValue = false;
1737                                 }
1738                         }
1739                         
1740                         return returnValue;
1741                 },
1742                 
1743                 fix: function(event) {
1744                         if ( event ) {
1745                                 event.preventDefault = function() {
1746                                         this.returnValue = false;
1747                                 };
1748                         
1749                                 event.stopPropagation = function() {
1750                                         this.cancelBubble = true;
1751                                 };
1752                         }
1753                         
1754                         return event;
1755                 }
1756         
1757         }
1758 });