9e59f9dad8a202bd4c5a9f7d855f164e3246b67a
[jquery.git] / src / jquery / jquery.js
1 /*
2  * jQuery @VERSION - New Wave Javascript
3  *
4  * Copyright (c) 2007 John Resig (jquery.com)
5  * Dual licensed under the MIT (MIT-LICENSE.txt)
6  * and GPL (GPL-LICENSE.txt) licenses.
7  *
8  * $Date$
9  * $Rev$
10  */
11
12 // Map over jQuery in case of overwrite
13 if ( typeof jQuery != "undefined" )
14         var _jQuery = jQuery;
15
16 var jQuery = window.jQuery = function(a,c) {
17         // If the context is global, return a new object
18         if ( window == this || !this.init )
19                 return new jQuery(a,c);
20         
21         return this.init(a,c);
22 };
23
24 // Map over the $ in case of overwrite
25 if ( typeof $ != "undefined" )
26         var _$ = $;
27         
28 // Map the jQuery namespace to the '$' one
29 window.$ = jQuery;
30
31 var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/;
32
33 jQuery.fn = jQuery.prototype = {
34         init: function(a,c) {
35                 // Make sure that a selection was provided
36                 a = a || document;
37
38                 // Handle HTML strings
39                 if ( typeof a  == "string" ) {
40                         var m = quickExpr.exec(a);
41                         if ( m && (m[1] || !c) ) {
42                                 // HANDLE: $(html) -> $(array)
43                                 if ( m[1] )
44                                         a = jQuery.clean( [ m[1] ], c );
45
46                                 // HANDLE: $("#id")
47                                 else {
48                                         var tmp = document.getElementById( m[3] );
49                                         if ( tmp )
50                                                 // Handle the case where IE and Opera return items
51                                                 // by name instead of ID
52                                                 if ( tmp.id != m[3] )
53                                                         return jQuery().find( a );
54                                                 else {
55                                                         this[0] = tmp;
56                                                         this.length = 1;
57                                                         return this;
58                                                 }
59                                         else
60                                                 a = [];
61                                 }
62
63                         // HANDLE: $(expr)
64                         } else
65                                 return new jQuery( c ).find( a );
66
67                 // HANDLE: $(function)
68                 // Shortcut for document ready
69                 } else if ( jQuery.isFunction(a) )
70                         return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a );
71
72                 return this.setArray(
73                         // HANDLE: $(array)
74                         a.constructor == Array && a ||
75
76                         // HANDLE: $(arraylike)
77                         // Watch for when an array-like object is passed as the selector
78                         (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a ) ||
79
80                         // HANDLE: $(*)
81                         [ a ] );
82         },
83         
84         jquery: "@VERSION",
85
86         size: function() {
87                 return this.length;
88         },
89         
90         length: 0,
91
92         get: function( num ) {
93                 return num == undefined ?
94
95                         // Return a 'clean' array
96                         jQuery.makeArray( this ) :
97
98                         // Return just the object
99                         this[num];
100         },
101         
102         pushStack: function( a ) {
103                 var ret = jQuery(a);
104                 ret.prevObject = this;
105                 return ret;
106         },
107         
108         setArray: function( a ) {
109                 this.length = 0;
110                 Array.prototype.push.apply( this, a );
111                 return this;
112         },
113
114         each: function( fn, args ) {
115                 return jQuery.each( this, fn, args );
116         },
117
118         index: function( obj ) {
119                 var pos = -1;
120                 this.each(function(i){
121                         if ( this == obj ) pos = i;
122                 });
123                 return pos;
124         },
125
126         attr: function( key, value, type ) {
127                 var obj = key;
128                 
129                 // Look for the case where we're accessing a style value
130                 if ( key.constructor == String )
131                         if ( value == undefined )
132                                 return this.length && jQuery[ type || "attr" ]( this[0], key ) || undefined;
133                         else {
134                                 obj = {};
135                                 obj[ key ] = value;
136                         }
137                 
138                 // Check to see if we're setting style values
139                 return this.each(function(index){
140                         // Set all the styles
141                         for ( var prop in obj )
142                                 jQuery.attr(
143                                         type ? this.style : this,
144                                         prop, jQuery.prop(this, obj[prop], type, index, prop)
145                                 );
146                 });
147         },
148
149         css: function( key, value ) {
150                 return this.attr( key, value, "curCSS" );
151         },
152
153         text: function(e) {
154                 if ( typeof e != "object" && e != null )
155                         return this.empty().append( document.createTextNode( e ) );
156
157                 var t = "";
158                 jQuery.each( e || this, function(){
159                         jQuery.each( this.childNodes, function(){
160                                 if ( this.nodeType != 8 )
161                                         t += this.nodeType != 1 ?
162                                                 this.nodeValue : jQuery.fn.text([ this ]);
163                         });
164                 });
165                 return t;
166         },
167
168         wrapAll: function(html) {
169                 if ( this[0] )
170                         // The elements to wrap the target around
171                         jQuery(html, this[0].ownerDocument)
172                                 .clone()
173                                 .insertBefore(this[0])
174                                 .map(function(){
175                                         var elem = this;
176                                         while ( elem.firstChild )
177                                                 elem = elem.firstChild;
178                                         return elem;
179                                 })
180                                 .append(this);
181
182                 return this;
183         },
184
185         wrapInner: function(html) {
186                 return this.each(function(){
187                         jQuery(this).contents().wrapAll(html);
188                 });
189         },
190
191         wrap: function(html) {
192                 return this.each(function(){
193                         jQuery(this).wrapAll(html);
194                 });
195         },
196
197         append: function() {
198                 return this.domManip(arguments, true, 1, function(a){
199                         this.appendChild( a );
200                 });
201         },
202
203         prepend: function() {
204                 return this.domManip(arguments, true, -1, function(a){
205                         this.insertBefore( a, this.firstChild );
206                 });
207         },
208         
209         before: function() {
210                 return this.domManip(arguments, false, 1, function(a){
211                         this.parentNode.insertBefore( a, this );
212                 });
213         },
214
215         after: function() {
216                 return this.domManip(arguments, false, -1, function(a){
217                         this.parentNode.insertBefore( a, this.nextSibling );
218                 });
219         },
220
221         end: function() {
222                 return this.prevObject || jQuery([]);
223         },
224
225         find: function(t) {
226                 var data = jQuery.map(this, function(a){ return jQuery.find(t,a); });
227                 return this.pushStack( /[^+>] [^+>]/.test( t ) || t.indexOf("..") > -1 ?
228                         jQuery.unique( data ) : data );
229         },
230
231         clone: function() {
232                 var $this = this.add(this.find("*"));
233                 if (jQuery.browser.msie) {
234                         // Need to remove events on the element and its descendants
235                         $this.each(function() {
236                                 this._$events = {};
237                                 for (var type in this.$events)
238                                         this._$events[type] = jQuery.extend({},this.$events[type]);
239                         }).unbind();
240                 }
241
242                 // Do the clone
243                 var r = this.pushStack( jQuery.map( this, function(a){
244                         return a.cloneNode( true );
245                 }) );
246
247                 if (jQuery.browser.msie) {
248                         $this.each(function() {
249                                 // Add the events back to the original and its descendants
250                                 var events = this._$events;
251                                 for (var type in events)
252                                         for (var handler in events[type])
253                                                 jQuery.event.add(this, type, events[type][handler], events[type][handler].data);
254                                 this._$events = null;
255                         });
256                 }
257
258                 // copy form values over
259                 var inputs = r.add(r.find('*')).filter('select,input[@type=checkbox]');
260                 $this.filter('select,input[@type=checkbox]').each(function(i) {
261                         if (this.selectedIndex)
262                                 inputs[i].selectedIndex = this.selectedIndex;
263                         if (this.checked)
264                                 inputs[i].checked = true;
265                 });
266
267                 // Return the cloned set
268                 return r;
269         },
270
271         filter: function(t) {
272                 return this.pushStack(
273                         jQuery.isFunction( t ) &&
274                         jQuery.grep(this, function(el, index){
275                                 return t.apply(el, [index]);
276                         }) ||
277
278                         jQuery.multiFilter(t,this) );
279         },
280
281         not: function(t) {
282                 return this.pushStack(
283                         t.constructor == String &&
284                         jQuery.multiFilter(t, this, true) ||
285
286                         jQuery.grep(this, function(a) {
287                                 return ( t.constructor == Array || t.jquery )
288                                         ? jQuery.inArray( a, t ) < 0
289                                         : a != t;
290                         })
291                 );
292         },
293
294         add: function(t) {
295                 return this.pushStack( jQuery.merge(
296                         this.get(),
297                         t.constructor == String ?
298                                 jQuery(t).get() :
299                                 t.length != undefined && (!t.nodeName || t.nodeName == "FORM") ?
300                                         t : [t] )
301                 );
302         },
303
304         is: function(expr) {
305                 return expr ? jQuery.multiFilter(expr,this).length > 0 : false;
306         },
307         
308         val: function( val ) {
309                 return val == undefined ?
310                         ( this.length ? this[0].value : null ) :
311                         this.attr( "value", val );
312         },
313         
314         html: function( val ) {
315                 return val == undefined ?
316                         ( this.length ? this[0].innerHTML : null ) :
317                         this.empty().append( val );
318         },
319
320         replaceWith: function( val ) {
321                 return this.after( val ).remove();
322         },
323
324         slice: function() {
325                 return this.pushStack( Array.prototype.slice.apply( this, arguments ) );
326         },
327
328         map: function(fn) {
329                 return this.pushStack(jQuery.map( this, function(elem,i){
330                         return fn.call( elem, i, elem );
331                 }));
332         },
333
334         andSelf: function() {
335                 return this.add( this.prevObject );
336         },
337         
338         domManip: function(args, table, dir, fn) {
339                 var clone = this.length > 1, a; 
340
341                 return this.each(function(){
342                         if ( !a ) {
343                                 a = jQuery.clean(args, this.ownerDocument);
344                                 if ( dir < 0 )
345                                         a.reverse();
346                         }
347
348                         var obj = this;
349
350                         if ( table && jQuery.nodeName(this, "table") && jQuery.nodeName(a[0], "tr") )
351                                 obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));
352
353                         jQuery.each( a, function(){
354                                 if ( jQuery.nodeName(this, "script") ) {
355                                         if ( this.src )
356                                                 jQuery.ajax({ url: this.src, async: false, dataType: "script" });
357                                         else
358                                                 jQuery.globalEval( this.text || this.textContent || this.innerHTML || "" );
359                                 } else
360                                         fn.apply( obj, [ clone ? this.cloneNode(true) : this ] );
361                         });
362                 });
363         }
364 };
365
366 jQuery.extend = jQuery.fn.extend = function() {
367         // copy reference to target object
368         var target = arguments[0] || {}, a = 1, al = arguments.length, deep = false;
369
370         // Handle a deep copy situation
371         if ( target.constructor == Boolean ) {
372                 deep = target;
373                 target = arguments[1] || {};
374         }
375
376         // extend jQuery itself if only one argument is passed
377         if ( al == 1 ) {
378                 target = this;
379                 a = 0;
380         }
381
382         var prop;
383
384         for ( ; a < al; a++ )
385                 // Only deal with non-null/undefined values
386                 if ( (prop = arguments[a]) != null )
387                         // Extend the base object
388                         for ( var i in prop ) {
389                                 // Prevent never-ending loop
390                                 if ( target == prop[i] )
391                                         continue;
392
393                                 // Recurse if we're merging object values
394                                 if ( deep && typeof prop[i] == 'object' && target[i] )
395                                         jQuery.extend( target[i], prop[i] );
396
397                                 // Don't bring in undefined values
398                                 else if ( prop[i] != undefined )
399                                         target[i] = prop[i];
400                         }
401
402         // Return the modified object
403         return target;
404 };
405
406 jQuery.extend({
407         noConflict: function(deep) {
408                 window.$ = _$;
409                 if ( deep )
410                         window.jQuery = _jQuery;
411                 return jQuery;
412         },
413
414         // This may seem like some crazy code, but trust me when I say that this
415         // is the only cross-browser way to do this. --John
416         isFunction: function( fn ) {
417                 return !!fn && typeof fn != "string" && !fn.nodeName && 
418                         fn.constructor != Array && /function/i.test( fn + "" );
419         },
420         
421         // check if an element is in a XML document
422         isXMLDoc: function(elem) {
423                 return elem.documentElement && !elem.body ||
424                         elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
425         },
426
427         // Evalulates a script in a global context
428         // Evaluates Async. in Safari 2 :-(
429         globalEval: function( data ) {
430                 data = jQuery.trim( data );
431                 if ( data ) {
432                         if ( window.execScript )
433                                 window.execScript( data );
434                         else if ( jQuery.browser.safari )
435                                 // safari doesn't provide a synchronous global eval
436                                 window.setTimeout( data, 0 );
437                         else
438                                 eval.call( window, data );
439                 }
440         },
441
442         nodeName: function( elem, name ) {
443                 return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
444         },
445
446         // args is for internal usage only
447         each: function( obj, fn, args ) {
448                 if ( args ) {
449                         if ( obj.length == undefined )
450                                 for ( var i in obj )
451                                         fn.apply( obj[i], args );
452                         else
453                                 for ( var i = 0, ol = obj.length; i < ol; i++ )
454                                         if ( fn.apply( obj[i], args ) === false ) break;
455
456                 // A special, fast, case for the most common use of each
457                 } else {
458                         if ( obj.length == undefined )
459                                 for ( var i in obj )
460                                         fn.call( obj[i], i, obj[i] );
461                         else
462                                 for ( var i = 0, ol = obj.length, val = obj[0]; 
463                                         i < ol && fn.call(val,i,val) !== false; val = obj[++i] ){}
464                 }
465
466                 return obj;
467         },
468         
469         prop: function(elem, value, type, index, prop){
470                         // Handle executable functions
471                         if ( jQuery.isFunction( value ) )
472                                 value = value.call( elem, [index] );
473                                 
474                         // exclude the following css properties to add px
475                         var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i;
476
477                         // Handle passing in a number to a CSS property
478                         return value && value.constructor == Number && type == "curCSS" && !exclude.test(prop) ?
479                                 value + "px" :
480                                 value;
481         },
482
483         className: {
484                 // internal only, use addClass("class")
485                 add: function( elem, c ){
486                         jQuery.each( (c || "").split(/\s+/), function(i, cur){
487                                 if ( !jQuery.className.has( elem.className, cur ) )
488                                         elem.className += ( elem.className ? " " : "" ) + cur;
489                         });
490                 },
491
492                 // internal only, use removeClass("class")
493                 remove: function( elem, c ){
494                         elem.className = c != undefined ?
495                                 jQuery.grep( elem.className.split(/\s+/), function(cur){
496                                         return !jQuery.className.has( c, cur ); 
497                                 }).join(" ") : "";
498                 },
499
500                 // internal only, use is(".class")
501                 has: function( t, c ) {
502                         return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1;
503                 }
504         },
505
506         swap: function(e,o,f) {
507                 for ( var i in o ) {
508                         e.style["old"+i] = e.style[i];
509                         e.style[i] = o[i];
510                 }
511                 f.apply( e, [] );
512                 for ( var i in o )
513                         e.style[i] = e.style["old"+i];
514         },
515
516         css: function(e,p) {
517                 if ( p == "height" || p == "width" ) {
518                         var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
519
520                         jQuery.each( d, function(){
521                                 old["padding" + this] = 0;
522                                 old["border" + this + "Width"] = 0;
523                         });
524
525                         jQuery.swap( e, old, function() {
526                                 if ( jQuery(e).is(':visible') ) {
527                                         oHeight = e.offsetHeight;
528                                         oWidth = e.offsetWidth;
529                                 } else {
530                                         e = jQuery(e.cloneNode(true))
531                                                 .find(":radio").removeAttr("checked").end()
532                                                 .css({
533                                                         visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
534                                                 }).appendTo(e.parentNode)[0];
535
536                                         var parPos = jQuery.css(e.parentNode,"position") || "static";
537                                         if ( parPos == "static" )
538                                                 e.parentNode.style.position = "relative";
539
540                                         oHeight = e.clientHeight;
541                                         oWidth = e.clientWidth;
542
543                                         if ( parPos == "static" )
544                                                 e.parentNode.style.position = "static";
545
546                                         e.parentNode.removeChild(e);
547                                 }
548                         });
549
550                         return p == "height" ? oHeight : oWidth;
551                 }
552
553                 return jQuery.curCSS( e, p );
554         },
555
556         curCSS: function(elem, prop, force) {
557                 var ret, stack = [], swap = [];
558
559                 // A helper method for determining if an element's values are broken
560                 function color(a){
561                         if ( !jQuery.browser.safari )
562                                 return false;
563
564                         var ret = document.defaultView.getComputedStyle(a,null);
565                         return !ret || ret.getPropertyValue("color") == "";
566                 }
567
568                 if (prop == "opacity" && jQuery.browser.msie) {
569                         ret = jQuery.attr(elem.style, "opacity");
570                         return ret == "" ? "1" : ret;
571                 }
572                 
573                 if (prop.match(/float/i))
574                         prop = styleFloat;
575
576                 if (!force && elem.style[prop])
577                         ret = elem.style[prop];
578
579                 else if (document.defaultView && document.defaultView.getComputedStyle) {
580
581                         if (prop.match(/float/i))
582                                 prop = "float";
583
584                         prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
585                         var cur = document.defaultView.getComputedStyle(elem, null);
586
587                         if ( cur && !color(elem) )
588                                 ret = cur.getPropertyValue(prop);
589
590                         // If the element isn't reporting its values properly in Safari
591                         // then some display: none elements are involved
592                         else {
593                                 // Locate all of the parent display: none elements
594                                 for ( var a = elem; a && color(a); a = a.parentNode )
595                                         stack.unshift(a);
596
597                                 // Go through and make them visible, but in reverse
598                                 // (It would be better if we knew the exact display type that they had)
599                                 for ( a = 0; a < stack.length; a++ )
600                                         if ( color(stack[a]) ) {
601                                                 swap[a] = stack[a].style.display;
602                                                 stack[a].style.display = "block";
603                                         }
604
605                                 // Since we flip the display style, we have to handle that
606                                 // one special, otherwise get the value
607                                 ret = prop == "display" && swap[stack.length-1] != null ?
608                                         "none" :
609                                         document.defaultView.getComputedStyle(elem,null).getPropertyValue(prop) || "";
610
611                                 // Finally, revert the display styles back
612                                 for ( a = 0; a < swap.length; a++ )
613                                         if ( swap[a] != null )
614                                                 stack[a].style.display = swap[a];
615                         }
616
617                         if ( prop == "opacity" && ret == "" )
618                                 ret = "1";
619
620                 } else if (elem.currentStyle) {
621                         var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});
622                         ret = elem.currentStyle[prop] || elem.currentStyle[newProp];
623                 }
624
625                 return ret;
626         },
627         
628         clean: function(a, doc) {
629                 var r = [];
630                 doc = doc || document;
631
632                 jQuery.each( a, function(i,arg){
633                         if ( !arg ) return;
634
635                         if ( arg.constructor == Number )
636                                 arg = arg.toString();
637                         
638                         // Convert html string into DOM nodes
639                         if ( typeof arg == "string" ) {
640                                 // Fix "XHTML"-style tags in all browsers
641                                 arg = arg.replace(/(<(\w+)[^>]*?)\/>/g, function(m, all, tag){
642                                         return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area)$/i)? m : all+"></"+tag+">";
643                                 });
644
645                                 // Trim whitespace, otherwise indexOf won't work as expected
646                                 var s = jQuery.trim(arg).toLowerCase(), div = doc.createElement("div"), tb = [];
647
648                                 var wrap =
649                                         // option or optgroup
650                                         !s.indexOf("<opt") &&
651                                         [1, "<select>", "</select>"] ||
652                                         
653                                         !s.indexOf("<leg") &&
654                                         [1, "<fieldset>", "</fieldset>"] ||
655                                         
656                                         s.match(/^<(thead|tbody|tfoot|colg|cap)/) &&
657                                         [1, "<table>", "</table>"] ||
658                                         
659                                         !s.indexOf("<tr") &&
660                                         [2, "<table><tbody>", "</tbody></table>"] ||
661                                         
662                                         // <thead> matched above
663                                         (!s.indexOf("<td") || !s.indexOf("<th")) &&
664                                         [3, "<table><tbody><tr>", "</tr></tbody></table>"] ||
665                                         
666                                         !s.indexOf("<col") &&
667                                         [2, "<table><tbody></tbody><colgroup>", "</colgroup></table>"] ||
668
669                                         // IE can't serialize <link> and <script> tags normally
670                                         jQuery.browser.msie &&
671                                         [1, "div<div>", "</div>"] ||
672                                         
673                                         [0,"",""];
674
675                                 // Go to html and back, then peel off extra wrappers
676                                 div.innerHTML = wrap[1] + arg + wrap[2];
677                                 
678                                 // Move to the right depth
679                                 while ( wrap[0]-- )
680                                         div = div.lastChild;
681                                 
682                                 // Remove IE's autoinserted <tbody> from table fragments
683                                 if ( jQuery.browser.msie ) {
684                                         
685                                         // String was a <table>, *may* have spurious <tbody>
686                                         if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 ) 
687                                                 tb = div.firstChild && div.firstChild.childNodes;
688                                                 
689                                         // String was a bare <thead> or <tfoot>
690                                         else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )
691                                                 tb = div.childNodes;
692
693                                         for ( var n = tb.length-1; n >= 0 ; --n )
694                                                 if ( jQuery.nodeName(tb[n], "tbody") && !tb[n].childNodes.length )
695                                                         tb[n].parentNode.removeChild(tb[n]);
696         
697                                         // IE completely kills leading whitespace when innerHTML is used        
698                                         if ( /^\s/.test(arg) )  
699                                                 div.insertBefore( doc.createTextNode( arg.match(/^\s*/)[0] ), div.firstChild );
700
701                                 }
702                                 
703                                 arg = jQuery.makeArray( div.childNodes );
704                         }
705
706                         if ( 0 === arg.length && (!jQuery.nodeName(arg, "form") && !jQuery.nodeName(arg, "select")) )
707                                 return;
708
709                         if ( arg[0] == undefined || jQuery.nodeName(arg, "form") || arg.options )
710                                 r.push( arg );
711                         else
712                                 r = jQuery.merge( r, arg );
713
714                 });
715
716                 return r;
717         },
718         
719         attr: function(elem, name, value){
720                 var fix = jQuery.isXMLDoc(elem) ? {} : jQuery.props;
721
722                 // Safari mis-reports the default selected property of a hidden option
723                 // Accessing the parent's selectedIndex property fixes it
724                 if ( name == "selected" && jQuery.browser.safari )
725                         elem.parentNode.selectedIndex;
726                 
727                 // Certain attributes only work when accessed via the old DOM 0 way
728                 if ( fix[name] ) {
729                         if ( value != undefined ) elem[fix[name]] = value;
730                         return elem[fix[name]];
731                 } else if ( jQuery.browser.msie && name == "style" )
732                         return jQuery.attr( elem.style, "cssText", value );
733
734                 else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName(elem, "form") && (name == "action" || name == "method") )
735                         return elem.getAttributeNode(name).nodeValue;
736
737                 // IE elem.getAttribute passes even for style
738                 else if ( elem.tagName ) {
739
740                         if ( value != undefined ) {
741                                 if ( name == "type" && jQuery.nodeName(elem,"input") && elem.parentNode )
742                                         throw "type property can't be changed";
743                                 elem.setAttribute( name, value );
744                         }
745
746                         if ( jQuery.browser.msie && /href|src/.test(name) && !jQuery.isXMLDoc(elem) ) 
747                                 return elem.getAttribute( name, 2 );
748
749                         return elem.getAttribute( name );
750
751                 // elem is actually elem.style ... set the style
752                 } else {
753                         // IE actually uses filters for opacity
754                         if ( name == "opacity" && jQuery.browser.msie ) {
755                                 if ( value != undefined ) {
756                                         // IE has trouble with opacity if it does not have layout
757                                         // Force it by setting the zoom level
758                                         elem.zoom = 1; 
759         
760                                         // Set the alpha filter to set the opacity
761                                         elem.filter = (elem.filter || "").replace(/alpha\([^)]*\)/,"") +
762                                                 (parseFloat(value).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
763                                 }
764         
765                                 return elem.filter ? 
766                                         (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() : "";
767                         }
768                         name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});
769                         if ( value != undefined ) elem[name] = value;
770                         return elem[name];
771                 }
772         },
773         
774         trim: function(t){
775                 return (t||"").replace(/^\s+|\s+$/g, "");
776         },
777
778         makeArray: function( a ) {
779                 var r = [];
780
781                 // Need to use typeof to fight Safari childNodes crashes
782                 if ( typeof a != "array" )
783                         for ( var i = 0, al = a.length; i < al; i++ )
784                                 r.push( a[i] );
785                 else
786                         r = a.slice( 0 );
787
788                 return r;
789         },
790
791         inArray: function( b, a ) {
792                 for ( var i = 0, al = a.length; i < al; i++ )
793                         if ( a[i] == b )
794                                 return i;
795                 return -1;
796         },
797
798         merge: function(first, second) {
799                 // We have to loop this way because IE & Opera overwrite the length
800                 // expando of getElementsByTagName
801
802                 // Also, we need to make sure that the correct elements are being returned
803                 // (IE returns comment nodes in a '*' query)
804                 if ( jQuery.browser.msie ) {
805                         for ( var i = 0; second[i]; i++ )
806                                 if ( second[i].nodeType != 8 )
807                                         first.push(second[i]);
808                 } else
809                         for ( var i = 0; second[i]; i++ )
810                                 first.push(second[i]);
811
812                 return first;
813         },
814
815         unique: function(first) {
816                 var r = [], num = jQuery.mergeNum++;
817
818                 try {
819                         for ( var i = 0, fl = first.length; i < fl; i++ )
820                                 if ( num != first[i].mergeNum ) {
821                                         first[i].mergeNum = num;
822                                         r.push(first[i]);
823                                 }
824                 } catch(e) {
825                         r = first;
826                 }
827
828                 return r;
829         },
830
831         mergeNum: 0,
832
833         grep: function(elems, fn, inv) {
834                 // If a string is passed in for the function, make a function
835                 // for it (a handy shortcut)
836                 if ( typeof fn == "string" )
837                         fn = eval("false||function(a,i){return " + fn + "}");
838
839                 var result = [];
840
841                 // Go through the array, only saving the items
842                 // that pass the validator function
843                 for ( var i = 0, el = elems.length; i < el; i++ )
844                         if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
845                                 result.push( elems[i] );
846
847                 return result;
848         },
849
850         map: function(elems, fn) {
851                 // If a string is passed in for the function, make a function
852                 // for it (a handy shortcut)
853                 if ( typeof fn == "string" )
854                         fn = eval("false||function(a){return " + fn + "}");
855
856                 var result = [];
857
858                 // Go through the array, translating each of the items to their
859                 // new value (or values).
860                 for ( var i = 0, el = elems.length; i < el; i++ ) {
861                         var val = fn(elems[i],i);
862
863                         if ( val !== null && val != undefined ) {
864                                 if ( val.constructor != Array ) val = [val];
865                                 result = result.concat( val );
866                         }
867                 }
868
869                 return result;
870         }
871 });
872
873 var userAgent = navigator.userAgent.toLowerCase();
874
875 // Figure out what browser is being used
876 jQuery.browser = {
877         version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
878         safari: /webkit/.test(userAgent),
879         opera: /opera/.test(userAgent),
880         msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
881         mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
882 };
883
884 var styleFloat = jQuery.browser.msie ? "styleFloat" : "cssFloat";
885         
886 jQuery.extend({
887         // Check to see if the W3C box model is being used
888         boxModel: !jQuery.browser.msie || document.compatMode == "CSS1Compat",
889         
890         styleFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",
891         
892         props: {
893                 "for": "htmlFor",
894                 "class": "className",
895                 "float": styleFloat,
896                 cssFloat: styleFloat,
897                 styleFloat: styleFloat,
898                 innerHTML: "innerHTML",
899                 className: "className",
900                 value: "value",
901                 disabled: "disabled",
902                 checked: "checked",
903                 readonly: "readOnly",
904                 selected: "selected",
905                 maxlength: "maxLength"
906         }
907 });
908
909 jQuery.each({
910         parent: "a.parentNode",
911         parents: "jQuery.parents(a)",
912         next: "jQuery.nth(a,2,'nextSibling')",
913         prev: "jQuery.nth(a,2,'previousSibling')",
914         siblings: "jQuery.sibling(a.parentNode.firstChild,a)",
915         children: "jQuery.sibling(a.firstChild)",
916         contents: "jQuery.nodeName(a,'iframe')?a.contentDocument||a.contentWindow.document:jQuery.makeArray(a.childNodes)"
917 }, function(i,n){
918         jQuery.fn[ i ] = function(a) {
919                 var ret = jQuery.map(this,n);
920                 if ( a && typeof a == "string" )
921                         ret = jQuery.multiFilter(a,ret);
922                 return this.pushStack( jQuery.unique(ret) );
923         };
924 });
925
926 jQuery.each({
927         appendTo: "append",
928         prependTo: "prepend",
929         insertBefore: "before",
930         insertAfter: "after",
931         replaceAll: "replaceWith"
932 }, function(i,n){
933         jQuery.fn[ i ] = function(){
934                 var a = arguments;
935                 return this.each(function(){
936                         for ( var j = 0, al = a.length; j < al; j++ )
937                                 jQuery(a[j])[n]( this );
938                 });
939         };
940 });
941
942 jQuery.each( {
943         removeAttr: function( key ) {
944                 jQuery.attr( this, key, "" );
945                 this.removeAttribute( key );
946         },
947         addClass: function(c){
948                 jQuery.className.add(this,c);
949         },
950         removeClass: function(c){
951                 jQuery.className.remove(this,c);
952         },
953         toggleClass: function( c ){
954                 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);
955         },
956         remove: function(a){
957                 if ( !a || jQuery.filter( a, [this] ).r.length )
958                         this.parentNode.removeChild( this );
959         },
960         empty: function() {
961                 while ( this.firstChild )
962                         this.removeChild( this.firstChild );
963         }
964 }, function(i,n){
965         jQuery.fn[ i ] = function() {
966                 return this.each( n, arguments );
967         };
968 });
969
970 jQuery.each( [ "height", "width" ], function(i,n){
971         jQuery.fn[ n ] = function(h) {
972                 return h == undefined ?
973                         ( this.length ? jQuery.css( this[0], n ) : null ) :
974                         this.css( n, h.constructor == String ? h : h + "px" );
975         };
976 });