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