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