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