Getting ready for 1.0a.
[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         // Watch for when a jQuery object is passed in as an arg
21         if ( a && a.jquery )
22                 return a;
23         
24         // If the context is global, return a new object
25         if ( window == this )
26                 return new jQuery(a,c);
27         
28         // Find the matching elements and save them for later
29         this.cur = jQuery.Select(
30                 a || jQuery.context || document,
31                 c && c.jquery && c.get(0) || c
32         );
33 }
34
35 /**
36  * The jQuery query object.
37  */
38 if ( !window.$ )
39   var $ = jQuery;
40
41 /*
42  * Handle support for overriding other $() functions. Way too many libraries
43  * provide this function to simply ignore it and overwrite it.
44  */
45 else
46   var $ = function(a,c) {
47     // Check to see if this is a possible collision case
48     if ( !c && a.constructor == String && 
49       
50         // Make sure that the expression is a colliding one
51         !/[^a-zA-Z0-9_-]/.test(a) &&
52         
53         // and that there are no elements that match it
54         // (this is the one truly ambiguous case)
55         !document.getElementsByTagName(a).length ) {
56           
57       // Only return the element if it's  found
58       var obj = document.getElementById(a);
59       if ( obj ) return obj;
60       
61     }
62     
63     return jQuery(a,c);
64   };
65
66 jQuery.fn = jQuery.prototype = {
67         /**
68          * The current SVN version of jQuery.
69          *
70          * @private
71          * @type String
72          */
73         jquery: "$Rev$",
74         
75         /**
76          * The number of elements currently matched.
77          *
78          * @type Number
79          */
80         size: function() {
81                 return this.get().length;
82         },
83         
84         /**
85          * Access the elements matched. If a number is provided,
86          * the Nth element is returned, otherwise, an array of all
87          * matched items is returned.
88          *
89          * @type Array,DOMElement
90          */
91         get: function(num) {
92                 return num == undefined ? this.cur : this.cur[num];
93         },
94         
95         each: function(f) {
96                 for ( var i = 0; i < this.size(); i++ )
97                         f.apply( this.get(i), [i] );
98                 return this;
99         },
100         set: function(a,b) {
101                 return this.each(function(){
102                         if ( b === undefined )
103                                 for ( var j in a )
104                                         jQuery.attr(this,j,a[j]);
105                         else
106                                 jQuery.attr(this,a,b);
107                 });
108         },
109         html: function(h) {
110                 return h == undefined && this.size() ?
111                         this.get(0).innerHTML : this.set( "innerHTML", h );
112         },
113         val: function(h) {
114                 return h == undefined && this.size() ?
115                         this.get(0).value : this.set( "value", h );
116         },
117         text: function(e) {
118                 e = e || this.get();
119                 var t = "";
120                 for ( var j = 0; j < e.length; j++ )
121                         for ( var i = 0; i < e[j].childNodes.length; i++ )
122                                 t += e[j].childNodes[i].nodeType != 1 ?
123                                         e[j].childNodes[i].nodeValue :
124                                         jQuery.fn.text(e[j].childNodes[i].childNodes);
125                 return t;
126         },
127         
128         css: function(a,b) {
129                 return a.constructor != String || b ?
130                         this.each(function(){
131                                 if ( b === undefined )
132                                         for ( var j in a )
133                                                 jQuery.attr(this.style,j,a[j]);
134                                 else
135                                         jQuery.attr(this.style,a,b);
136                         }) : jQuery.css( this.get(0), a );
137         },
138         toggle: function() {
139                 return this.each(function(){
140                         var d = jQuery.css(this,"display");
141                         if ( !d || d == "none" )
142                                 $(this).show();
143                         else
144                                 $(this).hide();
145                 });
146         },
147         show: function() {
148                 return this.each(function(){
149                         this.style.display = this.oldblock ? this.oldblock : "";
150                         if ( jQuery.css(this,"display") == "none" )
151                                 this.style.display = "block";
152                 });
153         },
154         hide: function() {
155                 return this.each(function(){
156                         this.oldblock = jQuery.css(this,"display");
157                         if ( this.oldblock == "none" )
158                                 this.oldblock = "block";
159                         this.style.display = "none";
160                 });
161         },
162         addClass: function(c) {
163                 return this.each(function(){
164                         jQuery.className.add(this,c);
165                 });
166         },
167         removeClass: function(c) {
168                 return this.each(function(){
169                         jQuery.className.remove(this,c);
170                 });
171         },
172
173         toggleClass: function(c) {
174                 return this.each(function(){
175                         if (jQuery.hasWord(this,c))
176                                 jQuery.className.remove(this,c);
177                         else
178                                 jQuery.className.add(this,c);
179                 });
180         },
181         remove: function() {
182                 this.each(function(){this.parentNode.removeChild( this );});
183                 return this.pushStack( [] );
184         },
185         
186         wrap: function() {
187                 var a = jQuery.clean(arguments);
188                 return this.each(function(){
189                         var b = a[0].cloneNode(true);
190                         this.parentNode.insertBefore( b, this );
191                         while ( b.firstChild )
192                                 b = b.firstChild;
193                         b.appendChild( this );
194                 });
195         },
196         
197         append: function() {
198                 var clone = this.size() > 1;
199                 var a = jQuery.clean(arguments);
200                 return this.domManip(function(){
201                         for ( var i = 0; i < a.length; i++ )
202                                 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
203                 });
204         },
205         
206         appendTo: function() {
207                 var a = arguments;
208                 return this.each(function(){
209                         for ( var i = 0; i < a.length; i++ )
210                                 $(a[i]).append( this );
211                 });
212         },
213         
214         prepend: function() {
215                 var clone = this.size() > 1;
216                 var a = jQuery.clean(arguments);
217                 return this.domManip(function(){
218                         for ( var i = a.length - 1; i >= 0; i-- )
219                                 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
220                 });
221         },
222         
223         before: function() {
224                 var clone = this.size() > 1;
225                 var a = jQuery.clean(arguments);
226                 return this.each(function(){
227                         for ( var i = 0; i < a.length; i++ )
228                                 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
229                 });
230         },
231         
232         after: function() {
233                 var clone = this.size() > 1;
234                 var a = jQuery.clean(arguments);
235                 return this.each(function(){
236                         for ( var i = a.length - 1; i >= 0; i-- )
237                                 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
238                 });
239         },
240         
241         empty: function() {
242                 return this.each(function(){
243                         while ( this.firstChild )
244                                 this.removeChild( this.firstChild );
245                 });
246         },
247         
248         bind: function(t,f) {
249                 return this.each(function(){jQuery.event.add(this,t,f);});
250         },
251         unbind: function(t,f) {
252                 return this.each(function(){jQuery.event.remove(this,t,f);});
253         },
254         trigger: function(t) {
255                 return this.each(function(){jQuery.event.trigger(this,t);});
256         },
257         
258         pushStack: function(a) {
259                 if ( !this.stack ) this.stack = [];
260                 this.stack.unshift( this.cur );
261                 if ( a ) this.cur = a;
262                 return this;
263         },
264         
265         find: function(t) {
266                 var ret = [];
267                 this.each(function(){
268                         ret = jQuery.merge( ret, jQuery.Select(t,this) );
269                 });
270                 this.pushStack( ret );
271                 return this;
272         },
273         
274         end: function() {
275                 this.cur = this.stack.shift();
276                 return this;
277         },
278         
279         parent: function(a) {
280                 var ret = jQuery.map(this.cur,"d.parentNode");
281                 if ( a ) ret = jQuery.filter(a,ret).r;
282                 return this.pushStack(ret);
283         },
284         
285         parents: function(a) {
286                 var ret = jQuery.map(this.cur,jQuery.parents);
287                 if ( a ) ret = jQuery.filter(a,ret).r;
288                 return this.pushStack(ret);
289         },
290         
291         siblings: function(a) {
292                 // Incorrect, need to exclude current element
293                 var ret = jQuery.map(this.cur,jQuery.sibling);
294                 if ( a ) ret = jQuery.filter(a,ret).r;
295                 return this.pushStack(ret);
296         },
297         
298         filter: function(t) {
299                 return this.pushStack( jQuery.filter(t,this.cur).r );
300         },
301         not: function(t) {
302                 return this.pushStack( t.constructor == String ?
303                         jQuery.filter(t,this.cur,false).r :
304                         jQuery.grep(this.cur,function(a){ return a != t; }) );
305         },
306         add: function(t) {
307                 return this.pushStack( jQuery.merge( this.cur, t.constructor == String ?
308                         jQuery.Select(t) : t.constructor == Array ? t : [t] ) );
309         },
310         
311         /**
312          * A wrapper function for each() to be used by append and prepend.
313          * Handles cases where you're trying to modify the inner contents of
314          * a table, when you actually need to work with the tbody.
315          *
316          * @member jQuery
317          * @param {String} expr The expression with which to filter
318          * @type Boolean
319          */
320         is: function(expr) {
321                 return jQuery.filter(expr,this.cur).r.length > 0;
322         },
323         
324         /**
325          * A wrapper function for each() to be used by append and prepend.
326          * Handles cases where you're trying to modify the inner contents of
327          * a table, when you actually need to work with the tbody.
328          *
329          * @private
330          * @member jQuery
331          * @param {Function} fn The function doing the DOM manipulation.
332          * @type jQuery
333          */
334         domManip: function(fn){
335                 return this.each(function(){
336                         var obj = this;
337                         
338                         if ( this.nodeName == "TABLE" ) {
339                                 var tbody = this.getElementsByTagName("tbody");
340
341                                 if ( !tbody.length ) {
342                                         obj = document.createElement("tbody");
343                                         this.appendChild( obj );
344                                 } else
345                                         obj = tbody[0];
346                         }
347         
348                         fn.apply( obj );
349                 });
350         }
351 };
352
353 jQuery.className = {
354         add: function(o,c){
355                 if (jQuery.hasWord(o,c)) return;
356                 o.className += ( o.className ? " " : "" ) + c;
357         },
358         remove: function(o,c){
359                 o.className = !c ? "" :
360                         o.className.replace(
361                                 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
362         }
363 };
364
365 (function(){
366         var b = navigator.userAgent.toLowerCase();
367
368         // Figure out what browser is being used
369         jQuery.browser =
370                 ( /webkit/.test(b) && "safari" ) ||
371                 ( /opera/.test(b) && "opera" ) ||
372                 ( /msie/.test(b) && "msie" ) ||
373                 ( !/compatible/.test(b) && "mozilla" ) ||
374                 "other";
375
376         // Check to see if the W3C box model is being used
377         jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
378 })();
379
380 jQuery.css = function(e,p) {
381         // Adapted from Prototype 1.4.0
382         if ( p == "height" || p == "width" ) {
383
384                 // Handle extra width/height provided by the W3C box model
385                 var ph = (!jQuery.boxModel ? 0 :
386                         jQuery.css(e,"paddingTop") + jQuery.css(e,"paddingBottom") +
387                         jQuery.css(e,"borderTopWidth") + jQuery.css(e,"borderBottomWidth")) || 0;
388
389                 var pw = (!jQuery.boxModel ? 0 :
390                         jQuery.css(e,"paddingLeft") + jQuery.css(e,"paddingRight") +
391                         jQuery.css(e,"borderLeftWidth") + jQuery.css(e,"borderRightWidth")) || 0;
392
393                 var oHeight, oWidth;
394
395                 if (jQuery.css(e,"display") != 'none') {
396                         oHeight = e.offsetHeight || parseInt(e.style.height) || 0;
397                         oWidth = e.offsetWidth || parseInt(e.style.width) || 0;
398                 } else {
399                         var els = e.style;
400                         var ov = els.visibility;
401                         var op = els.position;
402                         var od = els.display;
403                         els.visibility = "hidden";
404                         els.position = "absolute";
405                         els.display = "";
406                         oHeight = e.clientHeight || parseInt(e.style.height);
407                         oWidth = e.clientWidth || parseInt(e.style.width);
408                         els.display = od;
409                         els.position = op;
410                         els.visibility = ov;
411                 }
412
413                 return p == "height" ?
414                         (oHeight - ph < 0 ? 0 : oHeight - ph) :
415                         (oWidth - pw < 0 ? 0 : oWidth - pw);
416         }
417         
418         var r;
419
420         if (e.style[p])
421                 r = e.style[p];
422         else if (e.currentStyle)
423                 r = e.currentStyle[p];
424         else if (document.defaultView && document.defaultView.getComputedStyle) {
425                 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
426                 var s = document.defaultView.getComputedStyle(e,"");
427                 r = s ? s.getPropertyValue(p) : null;
428         }
429         
430         return /top|right|left|bottom/i.test(p) ? parseFloat( r ) : r;
431 };
432
433 jQuery.clean = function(a) {
434         var r = [];
435         for ( var i = 0; i < a.length; i++ ) {
436                 if ( a[i].constructor == String ) {
437
438                         if ( !a[i].indexOf("<tr") ) {
439                                 var tr = true;
440                                 a[i] = "<table>" + a[i] + "</table>";
441                         } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
442                                 var td = true;
443                                 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
444                         }
445
446                         var div = document.createElement("div");
447                         div.innerHTML = a[i];
448
449                         if ( tr || td ) {
450                                 div = div.firstChild.firstChild;
451                                 if ( td ) div = div.firstChild;
452                         }
453
454                         for ( var j = 0; j < div.childNodes.length; j++ )
455                                 r[r.length] = div.childNodes[j];
456                 } else if ( a[i].length && !a[i].nodeType )
457                         for ( var k = 0; k < a[i].length; k++ )
458                                 r[r.length] = a[i][k];
459                 else if ( a[i] !== null )
460                         r[r.length] =
461                                 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
462         }
463         return r;
464 };
465
466 jQuery.g = {
467         "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
468         "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
469         ":": {
470                 lt: "i<m[3]-0",
471                 gt: "i>m[3]-0",
472                 nth: "m[3]-0==i",
473                 eq: "m[3]-0==i",
474                 first: "i==0",
475                 last: "i==r.length-1",
476                 even: "i%2==0",
477                 odd: "i%2==1",
478                 "first-child": "jQuery.sibling(a,0).cur",
479                 "nth-child": "(m[3]=='even'?jQuery.sibling(a,m[3]).n%2==0:(m[3]=='odd'?jQuery.sibling(a,m[3]).n%2==1:jQuery.sibling(a,m[3]).cur))",
480                 "last-child": "jQuery.sibling(a,0,true).cur",
481                 "nth-last-child": "jQuery.sibling(a,m[3],true).cur",
482                 "first-of-type": "jQuery.ofType(a,0)",
483                 "nth-of-type": "jQuery.ofType(a,m[3])",
484                 "last-of-type": "jQuery.ofType(a,0,true)",
485                 "nth-last-of-type": "jQuery.ofType(a,m[3],true)",
486                 "only-of-type": "jQuery.ofType(a)==1",
487                 "only-child": "jQuery.sibling(a).length==1",
488                 parent: "a.childNodes.length",
489                 empty: "!a.childNodes.length",
490                 root: "a==(a.ownerDocument||document).documentElement",
491                 contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
492                 visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')",
493                 hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
494                 enabled: "!a.disabled",
495                 disabled: "a.disabled",
496                 checked: "a.checked"
497         },
498         ".": "jQuery.hasWord(a,m[2])",
499         "@": {
500                 "=": "jQuery.attr(a,m[3])==m[4]",
501                 "!=": "jQuery.attr(a,m[3])!=m[4]",
502                 "~=": "jQuery.hasWord(jQuery.attr(a,m[3]),m[4])",
503                 "|=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
504                 "^=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
505                 "$=": "jQuery.attr(a,m[3]).substr( jQuery.attr(a,m[3]).length - m[4].length,m[4].length )==m[4]",
506                 "*=": "jQuery.attr(a,m[3]).indexOf(m[4])>=0",
507                 "": "m[3]=='*'?a.attributes.length>0:jQuery.attr(a,m[3])"
508         },
509         "[": "jQuery.Select(m[2],a).length"
510 };
511
512 jQuery.token = [
513         "\\.\\.|/\\.\\.", "a.parentNode",
514         ">|/", "jQuery.sibling(a.firstChild)",
515         "\\+", "jQuery.sibling(a).next",
516         "~", function(a){
517                 var r = [];
518                 var s = jQuery.sibling(a);
519                 if ( s.n > 0 )
520                         for ( var i = s.n; i < s.length; i++ )
521                                 r[r.length] = s[i];
522                 return r;
523         }
524 ];
525
526 jQuery.Select = function( t, context ) {
527         context = context || jQuery.context || document;
528         if ( t.constructor != String )
529                 return t.constructor == Array ? t : [t];
530
531         if ( !t.indexOf("//") ) {
532                 context = context.documentElement;
533                 t = t.substr(2,t.length);
534         } else if ( !t.indexOf("/") ) {
535                 context = context.documentElement;
536                 t = t.substr(1,t.length);
537                 // FIX Assume the root element is right :(
538                 if ( t.indexOf("/") >= 1 )
539                         t = t.substr(t.indexOf("/"),t.length);
540         }
541
542         var ret = [context];
543         var done = [];
544         var last = null;
545
546         while ( t.length > 0 && last != t ) {
547     var r = [];
548                 last = t;
549
550     t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
551                 
552                 var foundToken = false;
553                 
554                 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
555                         var re = new RegExp("^(" + jQuery.token[i] + ")");
556                         var m = re.exec(t);
557                         
558                         if ( m ) {
559                                 r = ret = jQuery.map( ret, jQuery.token[i+1] );
560                                 t = jQuery.cleanSpaces( t.replace( re, "" ) );
561                                 foundToken = true;
562                         }
563                 }
564                 
565                 if ( !foundToken ) {
566
567                         if ( !t.indexOf(",") || !t.indexOf("|") ) {
568                                 if ( ret[0] == context ) ret.shift();
569                                 done = jQuery.merge( done, ret );
570                                 r = ret = [context];
571                                 t = " " + t.substr(1,t.length);
572                         } else {
573                                 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
574                                 var m = re2.exec(t);
575         
576                                 if ( m[1] == "#" ) {
577                                         // Ummm, should make this work in all XML docs
578                                         var oid = document.getElementById(m[2]);
579                                         r = ret = oid ? [oid] : [];
580                                         t = t.replace( re2, "" );
581                                 } else {
582                                         if ( !m[2] || m[1] == "." ) m[2] = "*";
583         
584                                         for ( var i = 0; i < ret.length; i++ )
585                                                 r = jQuery.merge( r,
586                                                         m[2] == "*" ?
587                                                                 jQuery.getAll(ret[i]) :
588                                                                 ret[i].getElementsByTagName(m[2])
589                                                 );
590                                 }
591                         }
592                         
593                 }
594
595                 if ( t ) {
596                         var val = jQuery.filter(t,r);
597                         ret = r = val.r;
598                         t = jQuery.cleanSpaces(val.t);
599                 }
600         }
601
602         if ( ret && ret[0] == context ) ret.shift();
603         done = jQuery.merge( done, ret );
604
605         return done;
606 };
607
608 jQuery.getAll = function(o,r) {
609         r = r || [];
610         var s = o.childNodes;
611         for ( var i = 0; i < s.length; i++ )
612                 if ( s[i].nodeType == 1 ) {
613                         r[r.length] = s[i];
614                         jQuery.getAll( s[i], r );
615                 }
616         return r;
617 };
618
619 jQuery.attr = function(o,a,v){
620         if ( a && a.constructor == String ) {
621                 var fix = {
622                         "for": "htmlFor",
623                         "class": "className",
624                         "float": "cssFloat"
625                 };
626                 a = (fix[a] && fix[a].replace && fix[a]) || a;
627                 var r = /-([a-z])/ig;
628                 a = a.replace(r,function(z,b){return b.toUpperCase();});
629                 if ( v != undefined ) {
630                         o[a] = v;
631                         if ( o.setAttribute && a != "disabled" )
632                                 o.setAttribute(a,v);
633                 }
634                 return o[a] || o.getAttribute(a) || "";
635         } else
636                 return "";
637 };
638
639 jQuery.filter = function(t,r,not) {
640         var g = jQuery.grep;
641         if ( not === false )
642                 g = function(a,f) {return jQuery.grep(a,f,true);};
643
644         while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
645                 var re = /^\[ *@([a-z0-9*()_-]+) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
646                 var m = re.exec(t);
647
648                 if ( m )
649                         m = ["", "@", m[2], m[1], m[3]];
650                 else {
651                         re = /^(\[) *([^\]]*) *\]/i;
652                         m = re.exec(t);
653
654                         if ( !m ) {
655                                 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
656                                 m = re.exec(t);
657
658                                 if ( !m ) {
659                                         re = /^([:\.#]*)([a-z0-9*_-]*)/i;
660                                         m = re.exec(t);
661                                 }
662                         }
663                 }
664                 t = t.replace( re, "" );
665
666                 if ( m[1] == ":" && m[2] == "not" )
667                         r = jQuery.filter(m[3],r,false).r;
668                 else {
669                         var f = null;
670
671                         if ( jQuery.g[m[1]].constructor == String )
672                                 f = jQuery.g[m[1]];
673                         else if ( jQuery.g[m[1]][m[2]] )
674                                 f = jQuery.g[m[1]][m[2]];
675
676                         if ( f ) {
677                                 eval("f = function(a,i){return " + f + "}");
678                                 r = g( r, f );
679                         }
680                 }
681         }
682
683         return { r: r, t: t };
684 };
685
686 jQuery.parents = function(a){
687         var b = [];
688         var c = a.parentNode;
689         while ( c && c != document ) {
690                 b[b.length] = c;
691                 c = c.parentNode;
692         }
693         return b;
694 };
695
696 jQuery.cleanSpaces = function(t){
697         return t.replace(/^\s+|\s+$/g, "");
698 };
699
700 jQuery.ofType = function(a,n,e) {
701         var t = jQuery.grep(jQuery.sibling(a),function(b){ return b.nodeName == a.nodeName; });
702         if ( e ) n = t.length - n - 1;
703         return n != undefined ? t[n] == a : t.length;
704 };
705
706 jQuery.sibling = function(a,n,e) {
707         var type = [];
708         var tmp = a.parentNode.childNodes;
709         for ( var i = 0; i < tmp.length; i++ ) {
710                 if ( tmp[i].nodeType == 1 )
711                         type[type.length] = tmp[i];
712                 if ( tmp[i] == a )
713                         type.n = type.length - 1;
714         }
715         if ( e ) n = type.length - n - 1;
716         type.cur = ( type[n] == a );
717         type.prev = ( type.n > 0 ? type[type.n - 1] : null );
718         type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
719         return type;
720 };
721
722 jQuery.hasWord = function(e,a) {
723         if ( e == undefined ) return;
724         if ( e.className ) e = e.className;
725         return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
726 };
727
728 jQuery.merge = function(a,b) {
729         var d = [];
730         for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
731
732         for ( var i = 0; i < a.length; i++ ) {
733                 var c = true;
734                 for ( var j = 0; j < b.length; j++ )
735                         if ( a[i] == b[j] )
736                                 c = false;
737                 if ( c ) d[d.length] = a[i];
738         }
739
740         return d;
741 };
742
743 jQuery.grep = function(a,f,s) {
744         if ( f.constructor == String )
745                 f = new Function("a","i","return " + f);
746         var r = [];
747         if ( a )
748                 for ( var i = 0; i < a.length; i++ )
749                         if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
750                                 r[r.length] = a[i];
751         return r;
752 };
753
754 jQuery.map = function(a,f) {
755         if ( f.constructor == String )
756                 f = new Function("a","return " + f);
757         
758         var r = [];
759         for ( var i = 0; i < a.length; i++ ) {
760                 var t = f(a[i],i);
761                 if ( t !== null ) {
762                         if ( t.constructor != Array ) t = [t];
763                         r = jQuery.merge( t, r );
764                 }
765         }
766         return r;
767 };
768
769 jQuery.event = {
770
771         // Bind an event to an element
772         // Original by Dean Edwards
773         add: function(element, type, handler) {
774                 // For whatever reason, IE has trouble passing the window object
775                 // around, causing it to be cloned in the process
776                 if ( jQuery.browser == "msie" && element.setInterval != undefined )
777                         element = window;
778         
779                 if (!handler.guid) handler.guid = jQuery.event.guid++;
780                 if (!element.events) element.events = {};
781                 var handlers = element.events[type];
782                 if (!handlers) {
783                         handlers = element.events[type] = {};
784                         if (element["on" + type])
785                                 handlers[0] = element["on" + type];
786                 }
787                 handlers[handler.guid] = handler;
788                 element["on" + type] = jQuery.event.handle;
789         },
790         
791         guid: 1,
792         
793         // Detach an event or set of events from an element
794         remove: function(element, type, handler) {
795                 if (element.events)
796                         if (type && element.events[type])
797                                 if ( handler )
798                                         delete element.events[type][handler.guid];
799                                 else
800                                         for ( var i in element.events[type] )
801                                                 delete element.events[type][i];
802                         else
803                                 for ( var j in element.events )
804                                         jQuery.event.remove( element, j );
805         },
806         
807         trigger: function(element,type,data) {
808                 data = data || [ jQuery.event.fix({ type: type }) ];
809                 if ( element && element["on" + type] )
810                         element["on" + type].apply( element, data );
811         },
812         
813         handle: function(event) {
814                 if ( !event && !window.event ) return;
815         
816                 var returnValue = true, handlers = [];
817                 event = event || jQuery.event.fix(window.event);
818         
819                 for ( var j in this.events[event.type] )
820                         handlers[handlers.length] = this.events[event.type][j];
821         
822                 for ( var i = 0; i < handlers.length; i++ ) {
823                         if ( handlers[i].constructor == Function ) {
824                                 this.handleEvent = handlers[i];
825                                 if (this.handleEvent(event) === false) {
826                                         event.preventDefault();
827                                         event.stopPropagation();
828                                         returnValue = false;
829                                 }
830                         }
831                 }
832                 return returnValue;
833         },
834         
835         fix: function(event) {
836                 event.preventDefault = function() {
837                         this.returnValue = false;
838                 };
839                 
840                 event.stopPropagation = function() {
841                         this.cancelBubble = true;
842                 };
843                 
844                 return event;
845         }
846
847 };