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