I've begun the long process of fixing the messed up tab/spaces.
[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 in a )
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 in a )
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                 parents: function(a) {
238                         return this;
239                 },
240                 
241                 filter: function(t) {
242                         this.cur = $.filter(t,this.cur).r;
243                         return this;
244                 },
245                 not: function(t) {
246                         this.cur = t.constructor == String ?
247                                 $.filter(t,this.cur,false).r :
248                                 $.grep(this.cur,function(a){return a != t;});
249                         return this;
250                 },
251                 add: function(t) {
252                         this.cur = $.merge( this.cur, t.constructor == String ?
253                                 $.Select(t) : t.constructor == Array ? t : [t] );
254                         return this;
255                 },
256                 is: function(t) {
257                         return $.filter(t,this.cur).r.length > 0;
258                 },
259                 isNot: function(t) {
260                         return !this.s(t);
261                 }
262         };
263         
264         // TODO: Remove need to return this
265         for ( var i in $.fn ) {
266                 if ( self[i] != null )
267                         self["_"+i] = self[i];
268                 self[i] = $.fn[i];
269         }
270         
271         if ( typeof Prototype != "undefined" && $a.constructor != String ) {
272                 if ( $c ) $a = self.get();
273                 for ( var i in self ) {(function(j){
274                         try {
275                                 if ( $a[j] == null ) {
276                                         $a[j] = function() {
277                                                 return $.apply(self,self[j],arguments);
278                                         };
279                                 }
280                         } catch(e) {}
281                 })(i);}
282                 return $a;
283         }
284         
285         return self;
286 }
287
288 $.apply = function(o,f,a) {
289         a = a || [];
290         if ( f.apply )
291                 return f.apply( o, a );
292         else {
293                 var p = [];
294                 for (var i = 0; i < a.length; i++)
295                         p[i] = 'a['+i+']';
296                 o.$$exec = this;
297                 var r = eval('o.$$exec(' + p.join(',') + ')');
298                 o.$$exec = null;
299                 return r;
300         }
301 };
302
303 $.getCSS = function(e,p) {
304         // Adapted from Prototype 1.4.0
305         if ( p == 'height' || p == 'width' ) {
306                 if ($.getCSS(e,"display") != 'none')
307                         return p == 'height' ?
308                                 e.offsetHeight || parseInt(e.style.height) : 
309                                 e.offsetWidth || parseInt(e.style.width);
310                 var els = e.style;
311                 var ov = els.visibility;
312                 var op = els.position;
313                 var od = els.display;
314                 els.visibility = 'hidden';
315                 els.position = 'absolute';
316                 els.display = '';
317                 var oHeight = e.clientHeight || parseInt(e.style.height);
318                 var oWidth = e.clientWidth || parseInt(e.style.width);
319                 els.display = od;
320                 els.position = op;
321                 els.visibility = ov;
322                 return p == 'height' ? oHeight : oWidth;
323         }
324         
325         if (e.style[p])
326                 return e.style[p];
327         else if (e.currentStyle)
328                 return e.currentStyle[p];
329         else if (document.defaultView && document.defaultView.getComputedStyle) {
330                 p = p.replace(/([A-Z])/g,"-$1");
331                 p = p.toLowerCase();
332                 var s = document.defaultView.getComputedStyle(e,"");
333                 var r = s ? s.getPropertyValue(p) : p;
334                 return r;
335         } else
336                 return null;
337 };
338 $.css = $.getCSS;
339
340 $.clean = function(a) {
341         var r = [];
342         for ( var i = 0; i < a.length; i++ )
343                 if ( a[i].constructor == String ) {
344                         var div = document.createElement("div");
345                         div.innerHTML = a[i];
346                         for ( var j = 0; j < div.childNodes.length; j++ )
347                                 r[r.length] = div.childNodes[j];
348                 } else if ( a[i].length )
349                         for ( var j = 0; j < a[i].length; j++ )
350                                 r[r.length] = a[i][j];
351                 else if ( a[i] != null )
352                         r[r.length] = 
353                                 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
354         return r;
355 };
356
357 $.g = {
358         '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
359         '#': "a.id == m[2]",
360         ':': {
361                 lt: "i < m[3]-0",
362                 gt: "i > m[3]-0",
363                 nth: "m[3] - 0 == i",
364                 eq: "m[3] - 0 == i",
365                 first: "i == 0",
366                 last: "i == r.length - 1",
367                 even: "i % 2 == 0",
368                 odd: "i % 2 == 1",
369                 "first-child": "$.sibling(a,0).cur",
370                 "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))",
371                 "last-child": "$.sibling(a,0,true).cur",
372                 "nth-last-child": "$.sibling(a,m[3],true).cur",
373                 "first-of-type": "$.ofType(a,0)",
374                 "nth-of-type": "$.ofType(a,m[3])",
375                 "last-of-type": "$.ofType(a,0,true)",
376                 "nth-last-of-type": "$.ofType(a,m[3],true)",
377                 "only-of-type": "$.ofType(a) == 1",
378                 "only-child": "$.sibling(a).length == 1",
379                 parent: "a.childNodes.length > 0",
380                 empty: "a.childNodes.length == 0",
381                 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
382                 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
383                 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
384                 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
385                 enabled: "a.disabled == false",
386                 disabled: "a.disabled",
387                 checked: "a.checked"
388         },
389         // TODO: Write getAttribute helper
390         ".": "$.hasWord(a.className||a.getAttribute('class'),m[2])",
391         "@": {
392                 "=": "$.attr(a,m[3]) == m[4]",
393                 "!=": "$.attr(a,m[3]) != m[4]",
394                 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
395                 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
396                 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
397                 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
398                 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
399                 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
400         },
401         "[": "$.Select(m[2],a).length > 0"
402 };
403
404 $.fn = {};
405
406 $.Select = function( t, context ) {
407         context = context || $.context || document;
408         if ( t.constructor != String ) return [t];
409         
410         if ( t.indexOf("//") == 0 ) {
411                 context = context.documentElement;
412                 t = t.substr(2,t.length);
413         } else if ( t.indexOf("/") == 0 ) {
414                 context = context.documentElement;
415                 t = t.substr(1,t.length);
416                 // FIX Assume the root element is right :(
417                 if ( t.indexOf('/') )
418                         t = t.substr(t.indexOf('/'),t.length);
419         }
420         
421         var ret = [context];
422         var done = [];
423         var last = null;
424   
425         while ( t.length > 0 && last != t ) {
426             var r = [];
427                         last = t;
428             
429             t = $.cleanSpaces(t);
430             
431             var re = new RegExp( "^//", "i" );
432             t = t.replace( re, "" );
433         
434             if ( t.indexOf('..') == 0 || t.indexOf('/..') == 0 ) {
435                         if ( t.indexOf('/') == 0 )
436                                 t = t.substr(1,t.length);
437                         r = $.map( ret, function(a){ return a.parentNode; } );
438                         t = t.substr(2,t.length);
439                         t = $.cleanSpaces(t);
440             } else if ( t.indexOf('>') == 0 || t.indexOf('/') == 0 ) {
441                         r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
442                         t = t.substr(1,t.length);
443                         t = $.cleanSpaces(t);
444             } else if ( t.indexOf('+') == 0 ) {
445                         r = $.map( ret, function(a){ return $.sibling(a).next; } );
446                         t = t.substr(1,t.length);
447                         t = $.cleanSpaces(t);
448             } else if ( t.indexOf('~') == 0 ) {
449                         r = $.map( ret, function(a){
450                                 var r = [];
451                                 var s = $.sibling(a);
452                                 if ( s.n > 0 )
453                                         for ( var i = s.n; i < s.length; i++ )
454                                                 r[r.length] = s[i];
455                                         return r;
456                         });
457                         t = t.substr(1,t.length);
458                         t = $.cleanSpaces(t);
459             } else if ( t.indexOf(',') == 0 || t.indexOf('|') == 0 ) {
460                         if ( ret[0] == context ) ret.shift();
461                         done = $.merge( done, ret );
462                         r = ret = [context];
463                         t = " " + t.substr(1,t.length);
464             } else {
465                         var re = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
466                         var m = re.exec(t);
467                                 
468                         if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
469                                 var oid = document.getElementById(m[2]);
470                                 r = oid ? [oid] : [];
471                                 t = t.replace( re, "" );
472                         } else {
473                                 if ( m[2] == "" || m[1] == "." ) m[2] = "*";
474         
475                                 for ( var i = 0; i < ret.length; i++ ) {
476                                         var o = ret[i];
477                                         if ( o ) {
478                                                 switch( m[2] ) {
479                                                         case '*':
480                                                                 r = $.merge( $.getAll(o), r );
481                                                         break;
482                                                         case 'text': case 'radio': case 'checkbox': case 'hidden':
483                                                         case 'button': case 'submit': case 'image': case 'password':
484                                                         case 'reset': case 'file':
485                                                                 r = $.merge( $.grep( $.tag(o,"input"), 
486                                                                         function(a){ return a.type == m[2] }), r );
487                                                         break;
488                                                         case 'input':
489                                                                 r = $.merge( $.tag(o,"input"), r );
490                                                                 r = $.merge( $.tag(o,"select"), r );
491                                                                 r = $.merge( $.tag(o,"textarea"), r );
492                                                         break;
493                                                         default:
494                                                                 r = $.merge( r, $.tag(o,m[2]) );
495                                                         break;
496                                                 }
497                                         }
498                                 }
499                         }
500                 }
501
502                 var val = $.filter(t,r);
503                 ret = r = val.r;
504                 t = $.cleanSpaces(val.t);
505         }
506
507         if ( ret && ret[0] == context ) ret.shift();
508         done = $.merge( done, ret );
509         return done;
510 };
511
512 $.tag = function(a,b){
513         return a && typeof a.getElementsByTagName != "undefined" ?
514                 a.getElementsByTagName( b ) : [];
515 };
516
517 $.attr = function(o,a,v){
518   if ( a && a.constructor == String ) {
519     var fix = {
520       'for': 'htmlFor',
521       'text': 'cssText',
522       'class': 'className',
523       'float': 'cssFloat'
524     };
525     a = (fix[a] && fix[a].replace && fix[a]) || a;
526     var r = new RegExp("-([a-z])","ig");
527     a = a.replace(r,function(z,b){return b.toUpperCase();});
528     if ( v != null ) {
529       o[a] = v;
530       if ( o.setAttribute ) o.setAttribute(a,v);
531     } 
532     return o[a] || o.getAttribute(a) || '';
533   } else return '';
534 };
535
536 $.filter = function(t,r,not) {
537         var g = $.grep;
538         if ( not == false ) var g = function(a,f) {return $.grep(a,f,true);};
539         
540         while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
541                 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
542                 var m = re.exec(t);
543                 
544                 if ( m != null ) {
545                         m = ['', '@', m[2], m[1], m[3]];
546                 } else {
547                         var re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "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                                 if ( m == null ) {
555                                         var re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
556                                         var m = re.exec(t);
557                                 }
558                         }
559                 }
560                 t = t.replace( re, "" );
561                 
562                 if ( m[1] == ":" && m[2] == "not" )
563                         r = $.filter(m[3],r,false).r;
564                 else {
565                         if ( $.g[m[1]].constructor == String )
566                                 var f = $.g[m[1]];
567                         else if ( $.g[m[1]][m[2]] )
568                                 var f = $.g[m[1]][m[2]];
569                                                 
570                         if ( f != null ) {
571                                 eval("f = function(a,i){return " + f + "}");
572                                 r = g( r, f );
573                         }
574                 }
575         }
576         return { r: r, t: t };
577 };
578
579 $.parents = function(a){
580         var b = [];
581         var c = a.parentNode;
582         while ( c != null && c != document ) {
583                 b[b.length] = c;
584                 c = c.parentNode;
585         }
586         return b;
587 };
588
589 $.cleanSpaces = function(t){return t.replace(/^\s+|\s+$/g, '')};
590
591 $.ofType = function(a,n,e) {
592   var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName});
593   if ( e ) n = t.length - n - 1;
594   return n != null ? t[n] == a : t.length;
595 };
596
597 $.sibling = function(a,n,e) {
598   var type = [];
599   var tmp = a.parentNode.childNodes;
600   for ( var i = 0; i < tmp.length; i++ ) {
601     if ( tmp[i].nodeType == 1 )
602       type[type.length] = tmp[i];
603     if ( tmp[i] == a )
604       type.n = type.length - 1;
605   }
606   if ( e ) n = type.length - n - 1;
607   type.cur = ( type[n] == a );
608   type.prev = ( type.n > 0 ? type[type.n - 1] : null );
609   type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
610   return type;
611 };
612
613 $.hasWord = function(e,a) {
614   if ( e == null ) return false;
615   if ( e.className != null ) e = e.className;
616   return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e)
617 };
618
619 $.getAll = function(o,r) {
620         r = r || [];
621         var s = o.childNodes;
622         for ( var i = 0; i < s.length; i++ ) {
623                 if ( s[i].nodeType == 1 ) {
624                         r[r.length] = s[i];
625                         $.getAll( s[i], r );
626                 }
627         }
628         return r;
629 };
630
631 $.merge = function(a,b) {
632         var d = [];
633         for ( var j = 0; j < b.length; j++ )
634                 d[j] = b[j];
635         
636   for ( var i = 0; i < a.length; i++ ) {
637     var c = true;
638     for ( var j = 0; j < b.length; j++ )
639       if ( a[i] == b[j] )
640         c = false;
641                 if ( c )
642                         d[d.length] = a[i];
643   }
644         return d;
645 };
646
647 $.grep = function(a,f,s) {
648   var r = [];
649         if ( a != null )
650                 for ( var i = 0; i < a.length; i++ )
651                         if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
652                                 r[r.length] = a[i];
653   return r;
654 };
655
656 $.map = function(a,f) {
657   var r = [];
658   for ( var i = 0; i < a.length; i++ ) {
659     var t = f(a[i],i);
660     if ( t != null ) {
661       if ( t.constructor != Array ) t = [t];
662                         r = $.merge( t, r );
663                 }
664   }
665   return r;
666 };
667
668 // Bind an event to an element
669 // Original by Dean Edwards
670 function addEvent(element, type, handler) {
671         if ( element.location ) element = window; // Ughhhhh....
672         if (!handler.$$guid) handler.$$guid = addEvent.guid++;
673         if (!element.events) element.events = {};
674         var handlers = element.events[type];
675         if (!handlers) {
676                 handlers = element.events[type] = {};
677                 if (element["on" + type])
678                         handlers[0] = element["on" + type];
679         }
680         handlers[handler.$$guid] = handler;
681         element["on" + type] = handleEvent;
682 };
683 addEvent.guid = 1;
684
685 // Detach an event or set of events from an element
686 function removeEvent(element, type, handler) {
687         if (element.events) {
688                 if (type && element.events[type]) {
689                         if ( handler ) {
690                                 delete element.events[type][handler.$$guid];
691                         } else {
692                                 for ( var i in element.events[type] )
693                                         delete element.events[type][i];
694                         }
695                 } else {
696                         for ( var i in element.events )
697                                 removeEvent( element, i );
698                 }
699         }
700 };
701
702 function triggerEvent(element,type) {
703   if ( element["on" + type] )
704     element["on" + type]({ type: type });
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);