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