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