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