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