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