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