Fixed two quick bugs from the JSLint overhaul.
[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(){addEvent(this,t,f);});
218                 },
219                 unbind: function(t,f) {
220                         return this.each(function(){removeEvent(this,t,f);});
221                 },
222                 trigger: function(t) {
223                         return this.each(function(){triggerEvent(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 $.apply = function(o,f,a) {
319         a = a || [];
320         if ( f.apply ) {
321                 return f.apply( o, a );
322         } else {
323                 var p = [];
324                 for (var i = 0; i < a.length; i++) {
325                         p[i] = 'a['+i+']';
326                 }
327                 o.$$exec = this;
328                 var r = eval('o.$$exec(' + p.join(',') + ')');
329                 o.$$exec = null;
330                 return r;
331         }
332 };
333
334 $.getCSS = function(e,p) {
335         // Adapted from Prototype 1.4.0
336         if ( p == 'height' || p == 'width' ) {
337                 if ($.getCSS(e,"display") != 'none') {
338                         return p == 'height' ?
339                                 e.offsetHeight || parseInt(e.style.height,10) : 
340                                 e.offsetWidth || parseInt(e.style.width,10);
341                 }
342                 var els = e.style;
343                 var ov = els.visibility;
344                 var op = els.position;
345                 var od = els.display;
346                 els.visibility = 'hidden';
347                 els.position = 'absolute';
348                 els.display = '';
349                 var oHeight = e.clientHeight || parseInt(e.style.height,10);
350                 var oWidth = e.clientWidth || parseInt(e.style.width,10);
351                 els.display = od;
352                 els.position = op;
353                 els.visibility = ov;
354                 return p == 'height' ? oHeight : oWidth;
355         }
356         
357         if (e.style[p]) {
358                 return e.style[p];
359         } else if (e.currentStyle) {
360                 return e.currentStyle[p];
361         } else if (document.defaultView && document.defaultView.getComputedStyle) {
362                 p = p.replace(/([A-Z])/g,"-$1");
363                 p = p.toLowerCase();
364                 var s = document.defaultView.getComputedStyle(e,"");
365                 var r = s ? s.getPropertyValue(p) : p;
366                 return r;
367         } else {
368                 return null;
369         }
370 };
371 $.css = $.getCSS;
372
373 $.clean = function(a) {
374         var r = [];
375         for ( var i = 0; i < a.length; i++ ) {
376                 if ( a[i].constructor == String ) {
377                         var div = document.createElement("div");
378                         div.innerHTML = a[i];
379                         for ( var j = 0; j < div.childNodes.length; j++ ) {
380                                 r[r.length] = div.childNodes[j];
381                         }
382                 } else if ( a[i].length ) {
383                         for ( var k = 0; k < a[i].length; k++ ) {
384                                 r[r.length] = a[i][k];
385                         }
386                 } else if ( a[i] !== null ) {
387                         r[r.length] = 
388                                 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
389                 }
390         }
391         return r;
392 };
393
394 $.g = {
395         '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
396         '#': "a.id == m[2]",
397         ':': {
398                 lt: "i < m[3]-0",
399                 gt: "i > m[3]-0",
400                 nth: "m[3] - 0 == i",
401                 eq: "m[3] - 0 == i",
402                 first: "i == 0",
403                 last: "i == r.length - 1",
404                 even: "i % 2 == 0",
405                 odd: "i % 2 == 1",
406                 "first-child": "$.sibling(a,0).cur",
407                 "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))",
408                 "last-child": "$.sibling(a,0,true).cur",
409                 "nth-last-child": "$.sibling(a,m[3],true).cur",
410                 "first-of-type": "$.ofType(a,0)",
411                 "nth-of-type": "$.ofType(a,m[3])",
412                 "last-of-type": "$.ofType(a,0,true)",
413                 "nth-last-of-type": "$.ofType(a,m[3],true)",
414                 "only-of-type": "$.ofType(a) == 1",
415                 "only-child": "$.sibling(a).length == 1",
416                 parent: "a.childNodes.length > 0",
417                 empty: "a.childNodes.length == 0",
418                 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
419                 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
420                 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
421                 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
422                 enabled: "a.disabled == false",
423                 disabled: "a.disabled",
424                 checked: "a.checked"
425         },
426         // TODO: Write getAttribute helper
427         ".": "$.hasWord(a,m[2])",
428         "@": {
429                 "=": "$.attr(a,m[3]) == m[4]",
430                 "!=": "$.attr(a,m[3]) != m[4]",
431                 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
432                 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
433                 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
434                 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
435                 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
436                 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
437         },
438         "[": "$.Select(m[2],a).length > 0"
439 };
440
441 $.fn = {};
442
443 $.Select = function( t, context ) {
444         context = context || $.context || document;
445         if ( t.constructor != String ) {
446                 return [t];
447         }
448         
449         if ( t.indexOf("//") === 0 ) {
450                 context = context.documentElement;
451                 t = t.substr(2,t.length);
452         } else if ( t.indexOf("/") === 0 ) {
453                 context = context.documentElement;
454                 t = t.substr(1,t.length);
455                 // FIX Assume the root element is right :(
456                 if ( t.indexOf('/') ) {
457                         t = t.substr(t.indexOf('/'),t.length);
458                 }
459         }
460         
461         var ret = [context];
462         var done = [];
463         var last = null;
464   
465         while ( t.length > 0 && last != t ) {
466     var r = [];
467                 last = t;
468             
469     t = $.cleanSpaces(t);
470             
471     var re = new RegExp( "^//", "i" );
472     t = t.replace( re, "" );
473         
474     if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
475                         if ( t.indexOf('/') === 0 ) {
476                                 t = t.substr(1,t.length);
477                         }
478                         r = $.map( ret, function(a){ return a.parentNode; } );
479                         t = t.substr(2,t.length);
480                         t = $.cleanSpaces(t);
481     } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
482                         r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
483                         t = t.substr(1,t.length);
484                         t = $.cleanSpaces(t);
485     } else if ( t.indexOf('+') === 0 ) {
486                         r = $.map( ret, function(a){ return $.sibling(a).next; } );
487                         t = t.substr(1,t.length);
488                         t = $.cleanSpaces(t);
489     } else if ( t.indexOf('~') === 0 ) {
490                         r = $.map( ret, function(a){
491                                 var r = [];
492                                 var s = $.sibling(a);
493                                 if ( s.n > 0 ) {
494                                         for ( var i = s.n; i < s.length; i++ ) {
495                                                 r[r.length] = s[i];
496                                         }
497                                 }
498                                 return r;
499                         });
500                         t = t.substr(1,t.length);
501                         t = $.cleanSpaces(t);
502     } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
503                         if ( ret[0] == context ) { ret.shift(); }
504                         done = $.merge( done, ret );
505                         r = ret = [context];
506                         t = " " + t.substr(1,t.length);
507     } else {
508                         var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
509                         var m = re2.exec(t);
510                         
511                         if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
512                                 var oid = document.getElementById(m[2]);
513                                 r = oid ? [oid] : [];
514                                 t = t.replace( re, "" );
515                         } else {
516                                 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
517         
518                                 for ( var i = 0; i < ret.length; i++ ) {
519                                         var o = ret[i];
520                                         if ( o ) {
521                                                 switch( m[2] ) {
522                                                         case '*':
523                                                                 r = $.merge( $.getAll(o), r );
524                                                         break;
525                                                         case 'text': case 'radio': case 'checkbox': case 'hidden':
526                                                         case 'button': case 'submit': case 'image': case 'password':
527                                                         case 'reset': case 'file':
528                                                                 r = $.merge( $.grep( $.tag(o,"input"), 
529                                                                         function(a){ return a.type == m[2]; }), r );
530                                                         break;
531                                                         case 'input':
532                                                                 r = $.merge( $.tag(o,"input"), r );
533                                                                 r = $.merge( $.tag(o,"select"), r );
534                                                                 r = $.merge( $.tag(o,"textarea"), r );
535                                                         break;
536                                                         default:
537                                                                 r = $.merge( r, $.tag(o,m[2]) );
538                                                         break;
539                                                 }
540                                         }
541                                 }
542                         }
543                 }
544
545                 var val = $.filter(t,r);
546                 ret = r = val.r;
547                 t = $.cleanSpaces(val.t);
548         }
549
550         if ( ret && ret[0] == context ) { ret.shift(); }
551         done = $.merge( done, ret );
552         return done;
553 };
554
555 $.tag = function(a,b){
556         return a && typeof a.getElementsByTagName != "undefined" ?
557                 a.getElementsByTagName( b ) : [];
558 };
559
560 $.attr = function(o,a,v){
561         if ( a && a.constructor == String ) {
562                 var fix = {
563                         'for': 'htmlFor',
564                         'text': 'cssText',
565                         'class': 'className',
566                         'float': 'cssFloat'
567                 };
568                 a = (fix[a] && fix[a].replace && fix[a]) || a;
569                 var r = new RegExp("-([a-z])","ig");
570                 a = a.replace(r,function(z,b){return b.toUpperCase();});
571                 if ( v !== null ) {
572                         o[a] = v;
573                         if ( o.setAttribute ) {
574                                 o.setAttribute(a,v);
575                         }
576                 } 
577                 return o[a] || o.getAttribute(a) || '';
578         } else {
579                 return '';
580         }
581 };
582
583 $.filter = function(t,r,not) {
584         var g = $.grep;
585         if ( not === false ) {
586                 g = function(a,f) {return $.grep(a,f,true);};
587         }
588         
589         while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
590                 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
591                 var m = re.exec(t);
592                 
593                 if ( m !== null ) {
594                         m = ['', '@', m[2], m[1], m[3]];
595                 } else {
596                         re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
597                         m = re.exec(t);
598                         
599                         if ( m === null ) {
600                                 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
601                                 m = re.exec(t);
602                                 
603                                 if ( m === null ) {
604                                         re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
605                                         m = re.exec(t);
606                                 }
607                         }
608                 }
609                 t = t.replace( re, "" );
610                 
611                 if ( m[1] == ":" && m[2] == "not" ) {
612                         r = $.filter(m[3],r,false).r;
613                 } else {
614                         var f = null;
615
616                         if ( $.g[m[1]].constructor == String ) {
617                                 f = $.g[m[1]];
618                         } else if ( $.g[m[1]][m[2]] ) {
619                                 f = $.g[m[1]][m[2]];
620                         }
621                                                 
622                         if ( f !== null ) {
623                                 eval('f = function(a,i){return ' + f + '}');
624                                 r = g( r, f );
625                         }
626                 }
627         }
628
629         return { r: r, t: t };
630 };
631
632 $.parents = function(a){
633         var b = [];
634         var c = a.parentNode;
635         while ( c !== null && c != document ) {
636                 b[b.length] = c;
637                 c = c.parentNode;
638         }
639         return b;
640 };
641
642 $.cleanSpaces = function(t){
643         return t.replace(/^\s+|\s+$/g, '');
644 };
645
646 $.ofType = function(a,n,e) {
647         var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
648         if ( e ) { n = t.length - n - 1; }
649         return n !== null ? t[n] == a : t.length;
650 };
651
652 $.sibling = function(a,n,e) {
653         var type = [];
654         var tmp = a.parentNode.childNodes;
655         for ( var i = 0; i < tmp.length; i++ ) {
656                 if ( tmp[i].nodeType == 1 ) {
657                         type[type.length] = tmp[i];
658                 }
659                 if ( tmp[i] == a ) {
660                         type.n = type.length - 1;
661                 }
662         }
663         if ( e ) { n = type.length - n - 1; }
664         type.cur = ( type[n] == a );
665         type.prev = ( type.n > 0 ? type[type.n - 1] : null );
666         type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
667         return type;
668 };
669
670 $.hasWord = function(e,a) {
671         if ( e === null ) { return false; }
672         if ( e.className !== null ) { e = e.className; }
673         return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
674 };
675
676 $.getAll = function(o,r) {
677         r = r || [];
678         var s = o.childNodes;
679         for ( var i = 0; i < s.length; i++ ) {
680                 if ( s[i].nodeType == 1 ) {
681                         r[r.length] = s[i];
682                         $.getAll( s[i], r );
683                 }
684         }
685         return r;
686 };
687
688 $.merge = function(a,b) {
689         var d = [];
690         for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
691         
692         for ( var i = 0; i < a.length; i++ ) {
693                 var c = true;
694                 for ( var j = 0; j < b.length; j++ ) {
695                         if ( a[i] == b[j] ) {
696                                 c = false;
697                         }
698                 }
699                 if ( c ) {
700                         d[d.length] = a[i];
701                 }
702         }
703
704         return d;
705 };
706
707 $.grep = function(a,f,s) {
708         var r = [];
709         if ( a !== null ) {
710                 for ( var i = 0; i < a.length; i++ ) {
711                         if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
712                                 r[r.length] = a[i];
713                         }
714                 }
715         }
716         return r;
717 };
718
719 $.map = function(a,f) {
720         var r = [];
721         for ( var i = 0; i < a.length; i++ ) {
722                 var t = f(a[i],i);
723                 if ( t !== null ) {
724                         if ( t.constructor != Array ) { t = [t]; }
725                         r = $.merge( t, r );
726                 }
727         }
728         return r;
729 };
730
731 // Bind an event to an element
732 // Original by Dean Edwards
733 function addEvent(element, type, handler) {
734         if ( element.location ) { element = window; } // Ughhhhh....
735         if (!handler.$$guid) { handler.$$guid = addEvent.guid++; }
736         if (!element.events) { element.events = {}; }
737         var handlers = element.events[type];
738         if (!handlers) {
739                 handlers = element.events[type] = {};
740                 if (element["on" + type]) {
741                         handlers[0] = element["on" + type];
742                 }
743         }
744         handlers[handler.$$guid] = handler;
745         element["on" + type] = handleEvent;
746 }
747
748 addEvent.guid = 1;
749
750 // Detach an event or set of events from an element
751 function removeEvent(element, type, handler) {
752         if (element.events) {
753                 if (type && element.events[type]) {
754                         if ( handler ) {
755                                 delete element.events[type][handler.$$guid];
756                         } else {
757                                 for ( var i in element.events[type] ) {
758                                         delete element.events[type][i];
759                                 }
760                         }
761                 } else {
762                         for ( var j in element.events ) {
763                                 removeEvent( element, j );
764                         }
765                 }
766         }
767 }
768
769 function triggerEvent(element,type,data) {
770         data = data || [{ type: type }];
771         if ( element && element["on" + type] ) {
772                 $.apply( element, element["on" + type], data );
773         }
774 }
775
776 function handleEvent(event) {
777         var returnValue = true;
778         event = event || fixEvent(window.event);
779         var handlers = [];
780         for ( var j in this.events[event.type] ) {
781                 handlers[handlers.length] = this.events[event.type][j];
782         }
783         for ( var i = 0; i < handlers.length; i++ ) {
784                 try {
785                         if ( handlers[i].constructor == Function ) {
786                                 this.$$handleEvent = handlers[i];
787                                 if (this.$$handleEvent(event) === false) {
788                                         event.preventDefault();
789                                         event.stopPropagation();
790                                         returnValue = false;
791                                 }
792                         }
793                 } catch(e){}
794         }
795         return returnValue;
796 }
797
798 function fixEvent(event) {
799         event.preventDefault = fixEvent.preventDefault;
800         event.stopPropagation = fixEvent.stopPropagation;
801         return event;
802 }
803
804 fixEvent.preventDefault = function() {
805         this.returnValue = false;
806 };
807
808 fixEvent.stopPropagation = function() {
809         this.cancelBubble = true;
810 };
811
812 // Move to module
813
814 $.fn.text = function(e) {
815         e = e || this.cur;
816         var t = "";
817         for ( var j = 0; j < e.length; j++ ) {
818                 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
819                         t += e[j].childNodes[i].nodeType != 1 ?
820                                 e[j].childNodes[i].nodeValue :
821                                 $.fn.text(e[j].childNodes[i].childNodes);
822                 }
823         }
824         return t;
825 };
826
827 /*setTimeout(function(){
828         if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
829                 throw "Error: You are overwriting jQuery, please include jQuery last.";
830 }, 1000);*/