2 * jQuery - New Wave Javascript
4 * Copyright (c) 2006 John Resig (jquery.com)
5 * Licensed under the MIT License:
6 * http://www.opensource.org/licenses/mit-license.php
12 // Global undefined variable
13 window.undefined = window.undefined;
16 * Create a new jQuery Object
19 function jQuery(a,c) {
21 * Handle support for overriding other $() functions. Way too many libraries
22 * provide this function to simply ignore it and overwrite it.
25 // Check to see if this is a possible collision case
26 if ( jQuery._$ && !c && a.constructor == String &&
28 // Make sure that the expression is a colliding one
29 !/[^a-zA-Z0-9_-]/.test(a) &&
31 // and that there are no elements that match it
32 // (this is the one truly ambiguous case)
33 !document.getElementsByTagName(a).length )
35 // Use the default method, in case it works some voodoo
36 return jQuery._$( a );
38 // Make sure t hat a selection was provided
39 a = a || jQuery.context || document;
41 // Watch for when a jQuery object is passed as the selector
45 // Watch for when a jQuery object is passed at the context
47 return $(c.get()).find(a);
49 // If the context is global, return a new object
51 return new jQuery(a,c);
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
58 // Find the matching elements and save them for later
59 jQuery.Select( a, c ) );
61 var fn = arguments[ arguments.length - 1 ];
62 if ( fn && fn.constructor == Function )
66 // Map over the $ in case of overwrite
70 // Map the jQuery namespace to the '$' one
73 jQuery.fn = jQuery.prototype = {
75 * The current SVN version of jQuery.
83 * The number of elements currently matched.
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.
96 * @type Array,DOMElement
99 if ( num && num.constructor == Array ) {
101 [].push.apply( this, num );
103 return num == undefined ?
105 // Return a 'clean' array
106 $.map( this, function(a){ return a } ) :
108 // Return just the object
113 for ( var i = 0; i < this.size(); i++ )
114 f.apply( this[i], [i] );
118 return this.each(function(){
119 if ( b === undefined )
121 jQuery.attr(this,j,a[j]);
123 jQuery.attr(this,a,b);
127 return h == undefined && this.size() ?
128 this[0].innerHTML : this.set( "innerHTML", h );
131 return h == undefined && this.size() ?
132 this[0].value : this.set( "value", h );
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] ]);
147 return a.constructor != String || b ?
148 this.each(function(){
149 if ( b === undefined )
151 jQuery.attr(this.style,j,a[j]);
153 jQuery.attr(this.style,a,b);
154 }) : jQuery.css( this[0], a );
157 return this.each(function(){
158 var d = jQuery.css(this,"display");
159 if ( !d || d == "none" )
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";
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";
180 addClass: function(c) {
181 return this.each(function(){
182 jQuery.className.add(this,c);
185 removeClass: function(c) {
186 return this.each(function(){
187 jQuery.className.remove(this,c);
191 toggleClass: function(c) {
192 return this.each(function(){
193 if (jQuery.hasWord(this,c))
194 jQuery.className.remove(this,c);
196 jQuery.className.add(this,c);
200 return this.each(function(){
201 this.parentNode.removeChild( this );
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 )
212 b.appendChild( this );
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] );
225 appendTo: function() {
227 return this.each(function(){
228 for ( var i = 0; i < a.length; i++ )
229 $(a[i]).append( this );
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 );
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 );
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 );
261 return this.each(function(){
262 while ( this.firstChild )
263 this.removeChild( this.firstChild );
267 bind: function(t,f) {
268 return this.each(function(){jQuery.event.add(this,t,f);});
270 unbind: function(t,f) {
271 return this.each(function(){jQuery.event.remove(this,t,f);});
273 trigger: function(t) {
274 return this.each(function(){jQuery.event.trigger(this,t);});
277 pushStack: function(a,args) {
278 var fn = args[args.length-1];
280 if ( !fn || fn.constructor != Function ) {
281 if ( !this.stack ) this.stack = [];
282 this.stack.push( this.get() );
285 var old = this.get();
287 if ( fn.constructor == Function )
288 return this.each( fn );
296 this.get( this.stack.pop() );
301 return this.pushStack( $.map( this, function(a){
302 return jQuery.Select(t,a);
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 );
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 );
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 );
325 filter: function(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 )
334 return this.pushStack( jQuery.filter(t,this).r, arguments );
337 return this.pushStack( t.constructor == String ?
338 jQuery.filter(t,this,false).r :
339 jQuery.grep(this,function(a){ return a != t; }), arguments );
342 return this.pushStack( jQuery.merge( this, t.constructor == String ?
343 jQuery.Select(t) : t.constructor == Array ? t : [t] ), arguments );
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.
352 * @param {String} expr The expression with which to filter
356 return jQuery.filter(expr,this).r.length > 0;
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.
366 * @param {Function} fn The function doing the DOM manipulation.
369 domManip: function(fn){
370 return this.each(function(){
373 if ( this.nodeName == "TABLE" ) {
374 var tbody = this.getElementsByTagName("tbody");
376 if ( !tbody.length ) {
377 obj = document.createElement("tbody");
378 this.appendChild( obj );
389 * Similar to the Prototype $A() function, only it allows you to
390 * forcefully pass array-like structures into $().
392 jQuery.A = function(a){
393 // Create a temporary, clean, array
396 // and copy the old array contents over to it
397 for ( var i = 0; i < a.length; i++ )
400 // Return the sane jQuery object
406 if (jQuery.hasWord(o,c)) return;
407 o.className += ( o.className ? " " : "" ) + c;
409 remove: function(o,c){
410 o.className = !c ? "" :
412 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
417 var b = navigator.userAgent.toLowerCase();
419 // Figure out what browser is being used
421 ( /webkit/.test(b) && "safari" ) ||
422 ( /opera/.test(b) && "opera" ) ||
423 ( /msie/.test(b) && "msie" ) ||
424 ( !/compatible/.test(b) && "mozilla" ) ||
427 // Check to see if the W3C box model is being used
428 jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
431 $.swap = function(e,o,f) {
433 e.style["old"+i] = e.style[i];
438 e.style[i] = e.style["old"+i];
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"];
447 old["padding" + d[i]] = 0;
448 old["border" + d[i] + "Width"] = 0;
451 $.swap( e, old, function() {
452 if (jQuery.css(e,"display") != 'none') {
453 oHeight = e.offsetHeight;
454 oWidth = e.offsetWidth;
456 $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' },
458 oHeight = e.clientHeight;
459 oWidth = e.clientWidth;
463 return p == "height" ? oHeight : oWidth;
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;
481 jQuery.clean = function(a) {
483 for ( var i = 0; i < a.length; i++ ) {
484 if ( a[i].constructor == String ) {
486 if ( !a[i].indexOf("<tr") ) {
488 a[i] = "<table>" + a[i] + "</table>";
489 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
491 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
494 var div = document.createElement("div");
495 div.innerHTML = a[i];
498 div = div.firstChild.firstChild;
499 if ( td ) div = div.firstChild;
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 )
509 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
515 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
516 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
523 last: "i==r.length-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",
546 ".": "jQuery.hasWord(a,m[2])",
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])"
557 "[": "jQuery.Select(m[2],a).length"
561 "\\.\\.|/\\.\\.", "a.parentNode",
562 ">|/", "jQuery.sibling(a.firstChild)",
563 "\\+", "jQuery.sibling(a).next",
566 var s = jQuery.sibling(a);
568 for ( var i = s.n; i < s.length; i++ )
574 jQuery.Select = function( t, context ) {
575 // Make sure that the context is a DOM Element
576 if ( context && context.getElementsByTagName == undefined )
579 // Set the correct context (if none is provided)
580 context = context || jQuery.context || document;
582 if ( t.constructor != String ) return [t];
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);
599 while ( t.length > 0 && last != t ) {
603 t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
605 var foundToken = false;
607 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
608 var re = new RegExp("^(" + jQuery.token[i] + ")");
612 r = ret = jQuery.map( ret, jQuery.token[i+1] );
613 t = jQuery.cleanSpaces( t.replace( re, "" ) );
620 if ( !t.indexOf(",") || !t.indexOf("|") ) {
621 if ( ret[0] == context ) ret.shift();
622 done = jQuery.merge( done, ret );
624 t = " " + t.substr(1,t.length);
626 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
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, "" );
635 if ( !m[2] || m[1] == "." ) m[2] = "*";
637 for ( var i = 0; i < ret.length; i++ )
640 jQuery.getAll(ret[i]) :
641 ret[i].getElementsByTagName(m[2])
649 var val = jQuery.filter(t,r);
651 t = jQuery.cleanSpaces(val.t);
655 if ( ret && ret[0] == context ) ret.shift();
656 done = jQuery.merge( done, ret );
661 jQuery.getAll = function(o,r) {
663 var s = o.childNodes;
664 for ( var i = 0; i < s.length; i++ )
665 if ( s[i].nodeType == 1 ) {
667 jQuery.getAll( s[i], r );
672 jQuery.attr = function(o,a,v){
673 if ( a && a.constructor == String ) {
676 "class": "className",
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 ) {
684 if ( o.setAttribute && a != "disabled" )
687 return o[a] || o.getAttribute(a) || "";
692 jQuery.filter = function(t,r,not) {
695 g = function(a,f) {return jQuery.grep(a,f,true);};
697 while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
698 var re = /^\[ *@([a-z*_-][a-z0-9()_-]*) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
702 m = ["", "@", m[2], m[1], m[3]];
704 re = /^(\[) *([^\]]*) *\]/i;
708 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
712 re = /^([:\.#]*)([a-z0-9*_-]*)/i;
717 t = t.replace( re, "" );
719 if ( m[1] == ":" && m[2] == "not" )
720 r = jQuery.filter(m[3],r,false).r;
724 if ( jQuery.g[m[1]].constructor == String )
726 else if ( jQuery.g[m[1]][m[2]] )
727 f = jQuery.g[m[1]][m[2]];
730 eval("f = function(a,i){return " + f + "}");
736 return { r: r, t: t };
739 jQuery.parents = function(a){
741 var c = a.parentNode;
742 while ( c && c != document ) {
749 jQuery.cleanSpaces = function(t){
750 return t.replace(/^\s+|\s+$/g, "");
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;
759 jQuery.sibling = function(a,n,e) {
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];
766 type.n = type.length - 1;
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 );
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);
781 jQuery.merge = function(a,b) {
783 for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
785 for ( var i = 0; i < a.length; i++ ) {
787 for ( var j = 0; j < b.length; j++ )
790 if ( c ) d[d.length] = a[i];
796 jQuery.grep = function(a,f,s) {
797 if ( f.constructor == String )
798 f = new Function("a","i","return " + f);
801 for ( var i = 0; i < a.length; i++ )
802 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
807 jQuery.map = function(a,f) {
808 if ( f.constructor == String )
809 f = new Function("a","return " + f);
812 for ( var i = 0; i < a.length; i++ ) {
814 if ( t !== null && t != undefined ) {
815 if ( t.constructor != Array ) t = [t];
816 r = jQuery.merge( t, r );
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 )
832 if (!handler.guid) handler.guid = jQuery.event.guid++;
833 if (!element.events) element.events = {};
834 var handlers = element.events[type];
836 handlers = element.events[type] = {};
837 if (element["on" + type])
838 handlers[0] = element["on" + type];
840 handlers[handler.guid] = handler;
841 element["on" + type] = this.handle;
843 if (!this.global[type])
844 this.global[type] = [];
845 this.global[type].push( element );
851 // Detach an event or set of events from an element
852 remove: function(element, type, handler) {
854 if (type && element.events[type])
856 delete element.events[type][handler.guid];
858 for ( var i in element.events[type] )
859 delete element.events[type][i];
861 for ( var j in element.events )
862 this.remove( element, j );
865 trigger: function(type,data,element) {
866 // Touch up the incoming data
869 // Handle a global trigger
871 var g = this.global[type];
873 for ( var i = 0; i < g.length; i++ )
874 this.trigger( type, data, g[i] );
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 }) );
882 element["on" + type].apply( element, data );
886 handle: function(event) {
887 // Handle adding events to items in IFrames, in IE
889 jQuery.event.fix( ((this.ownerDocument || this.document ||
890 this).parentWindow || window).event );
892 // If no correct event was found, fail
893 if ( !event ) return;
895 var returnValue = true, handlers = [];
897 for ( var j in this.events[event.type] )
898 handlers[handlers.length] = this.events[event.type][j];
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();
912 fix: function(event) {
914 event.preventDefault = function() {
915 this.returnValue = false;
918 event.stopPropagation = function() {
919 this.cancelBubble = true;