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;
15 // Map over the $ in case of overwrite
19 * Create a new jQuery Object
22 var $ = jQuery = function(a,c) {
24 * Handle support for overriding other $() functions. Way too many libraries
25 * provide this function to simply ignore it and overwrite it.
28 // Check to see if this is a possible collision case
29 if ( _$ && !c && ( a.constructor == String &&
31 // Make sure that the expression is a colliding one
32 !/[^a-zA-Z0-9_-]/.test(a) &&
34 // and that there are no elements that match it
35 // (this is the one truly ambiguous case)
36 !document.getElementsByTagName(a).length ) ||
38 // Watch for an array being passed in (Prototype 1.5)
39 a.constructor == Array )
41 // Use the default method, in case it works some voodoo
44 // Watch for when a jQuery object is passed in as an arg
48 // If the context is global, return a new object
50 return new jQuery(a,c);
52 // Find the matching elements and save them for later
53 this.cur = jQuery.Select(
54 a || jQuery.context || document,
55 c && c.jquery && c.get(0) || c
59 jQuery.fn = jQuery.prototype = {
61 * The current SVN version of jQuery.
69 * The number of elements currently matched.
74 return this.get().length;
78 * Access the elements matched. If a number is provided,
79 * the Nth element is returned, otherwise, an array of all
80 * matched items is returned.
82 * @type Array,DOMElement
85 return num == undefined ? this.cur : this.cur[num];
89 for ( var i = 0; i < this.size(); i++ )
90 f.apply( this.get(i), [i] );
94 return this.each(function(){
95 if ( b === undefined )
97 jQuery.attr(this,j,a[j]);
99 jQuery.attr(this,a,b);
103 return h == undefined && this.size() ?
104 this.get(0).innerHTML : this.set( "innerHTML", h );
107 return h == undefined && this.size() ?
108 this.get(0).value : this.set( "value", h );
113 for ( var j = 0; j < e.length; j++ ) {
114 var r = e[j].childNodes;
115 for ( var i = 0; i < r.length; i++ )
116 t += r[i].nodeType != 1 ?
117 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
123 return a.constructor != String || b ?
124 this.each(function(){
125 if ( b === undefined )
127 jQuery.attr(this.style,j,a[j]);
129 jQuery.attr(this.style,a,b);
130 }) : jQuery.css( this.get(0), a );
133 return this.each(function(){
134 var d = jQuery.css(this,"display");
135 if ( !d || d == "none" )
142 return this.each(function(){
143 this.style.display = this.oldblock ? this.oldblock : "";
144 if ( jQuery.css(this,"display") == "none" )
145 this.style.display = "block";
149 return this.each(function(){
150 this.oldblock = jQuery.css(this,"display");
151 if ( this.oldblock == "none" )
152 this.oldblock = "block";
153 this.style.display = "none";
156 addClass: function(c) {
157 return this.each(function(){
158 jQuery.className.add(this,c);
161 removeClass: function(c) {
162 return this.each(function(){
163 jQuery.className.remove(this,c);
167 toggleClass: function(c) {
168 return this.each(function(){
169 if (jQuery.hasWord(this,c))
170 jQuery.className.remove(this,c);
172 jQuery.className.add(this,c);
176 return this.each(function(){
177 this.parentNode.removeChild( this );
182 var a = jQuery.clean(arguments);
183 return this.each(function(){
184 var b = a[0].cloneNode(true);
185 this.parentNode.insertBefore( b, this );
186 while ( b.firstChild )
188 b.appendChild( this );
193 var clone = this.size() > 1;
194 var a = jQuery.clean(arguments);
195 return this.domManip(function(){
196 for ( var i = 0; i < a.length; i++ )
197 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
201 appendTo: function() {
203 return this.each(function(){
204 for ( var i = 0; i < a.length; i++ )
205 $(a[i]).append( this );
209 prepend: function() {
210 var clone = this.size() > 1;
211 var a = jQuery.clean(arguments);
212 return this.domManip(function(){
213 for ( var i = a.length - 1; i >= 0; i-- )
214 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
219 var clone = this.size() > 1;
220 var a = jQuery.clean(arguments);
221 return this.each(function(){
222 for ( var i = 0; i < a.length; i++ )
223 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
228 var clone = this.size() > 1;
229 var a = jQuery.clean(arguments);
230 return this.each(function(){
231 for ( var i = a.length - 1; i >= 0; i-- )
232 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
237 return this.each(function(){
238 while ( this.firstChild )
239 this.removeChild( this.firstChild );
243 bind: function(t,f) {
244 return this.each(function(){jQuery.event.add(this,t,f);});
246 unbind: function(t,f) {
247 return this.each(function(){jQuery.event.remove(this,t,f);});
249 trigger: function(t) {
250 return this.each(function(){jQuery.event.trigger(this,t);});
253 pushStack: function(a) {
254 if ( !this.stack ) this.stack = [];
255 this.stack.unshift( this.cur );
256 if ( a ) this.cur = a;
262 this.each(function(){
263 ret = jQuery.merge( ret, jQuery.Select(t,this) );
265 this.pushStack( ret );
270 this.cur = this.stack.shift();
274 parent: function(a) {
275 var ret = jQuery.map(this.cur,"a.parentNode");
276 if ( a ) ret = jQuery.filter(a,ret).r;
277 return this.pushStack(ret);
280 parents: function(a) {
281 var ret = jQuery.map(this.cur,jQuery.parents);
282 if ( a ) ret = jQuery.filter(a,ret).r;
283 return this.pushStack(ret);
286 siblings: function(a) {
287 // Incorrect, need to exclude current element
288 var ret = jQuery.map(this.cur,jQuery.sibling);
289 if ( a ) ret = jQuery.filter(a,ret).r;
290 return this.pushStack(ret);
293 filter: function(t) {
294 return this.pushStack( jQuery.filter(t,this.cur).r );
297 return this.pushStack( t.constructor == String ?
298 jQuery.filter(t,this.cur,false).r :
299 jQuery.grep(this.cur,function(a){ return a != t; }) );
302 return this.pushStack( jQuery.merge( this.cur, t.constructor == String ?
303 jQuery.Select(t) : t.constructor == Array ? t : [t] ) );
307 * A wrapper function for each() to be used by append and prepend.
308 * Handles cases where you're trying to modify the inner contents of
309 * a table, when you actually need to work with the tbody.
312 * @param {String} expr The expression with which to filter
316 return jQuery.filter(expr,this.cur).r.length > 0;
320 * A wrapper function for each() to be used by append and prepend.
321 * Handles cases where you're trying to modify the inner contents of
322 * a table, when you actually need to work with the tbody.
326 * @param {Function} fn The function doing the DOM manipulation.
329 domManip: function(fn){
330 return this.each(function(){
333 if ( this.nodeName == "TABLE" ) {
334 var tbody = this.getElementsByTagName("tbody");
336 if ( !tbody.length ) {
337 obj = document.createElement("tbody");
338 this.appendChild( obj );
350 if (jQuery.hasWord(o,c)) return;
351 o.className += ( o.className ? " " : "" ) + c;
353 remove: function(o,c){
354 o.className = !c ? "" :
356 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
361 var b = navigator.userAgent.toLowerCase();
363 // Figure out what browser is being used
365 ( /webkit/.test(b) && "safari" ) ||
366 ( /opera/.test(b) && "opera" ) ||
367 ( /msie/.test(b) && "msie" ) ||
368 ( !/compatible/.test(b) && "mozilla" ) ||
371 // Check to see if the W3C box model is being used
372 jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
375 $.swap = function(e,o,f) {
377 e.style["old"+i] = e.style[i];
382 e.style[i] = e.style["old"+i];
385 jQuery.css = function(e,p) {
386 // Adapted from Prototype 1.4.0
387 if ( p == "height" || p == "width" ) {
388 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
391 old["padding" + d[i]] = 0;
392 old["border" + d[i] + "Width"] = 0;
395 $.swap( e, old, function() {
396 if (jQuery.css(e,"display") != 'none') {
397 oHeight = e.offsetHeight;
398 oWidth = e.offsetWidth;
400 $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' },
402 oHeight = e.clientHeight;
403 oWidth = e.clientWidth;
407 return p == "height" ? oHeight : oWidth;
414 else if (e.currentStyle)
415 r = e.currentStyle[p];
416 else if (document.defaultView && document.defaultView.getComputedStyle) {
417 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
418 var s = document.defaultView.getComputedStyle(e,"");
419 r = s ? s.getPropertyValue(p) : null;
425 jQuery.clean = function(a) {
427 for ( var i = 0; i < a.length; i++ ) {
428 if ( a[i].constructor == String ) {
430 if ( !a[i].indexOf("<tr") ) {
432 a[i] = "<table>" + a[i] + "</table>";
433 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
435 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
438 var div = document.createElement("div");
439 div.innerHTML = a[i];
442 div = div.firstChild.firstChild;
443 if ( td ) div = div.firstChild;
446 for ( var j = 0; j < div.childNodes.length; j++ )
447 r[r.length] = div.childNodes[j];
448 } else if ( a[i].length && !a[i].nodeType )
449 for ( var k = 0; k < a[i].length; k++ )
450 r[r.length] = a[i][k];
451 else if ( a[i] !== null )
453 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
459 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
460 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
467 last: "i==r.length-1",
470 "first-child": "jQuery.sibling(a,0).cur",
471 "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))",
472 "last-child": "jQuery.sibling(a,0,true).cur",
473 "nth-last-child": "jQuery.sibling(a,m[3],true).cur",
474 "first-of-type": "jQuery.ofType(a,0)",
475 "nth-of-type": "jQuery.ofType(a,m[3])",
476 "last-of-type": "jQuery.ofType(a,0,true)",
477 "nth-last-of-type": "jQuery.ofType(a,m[3],true)",
478 "only-of-type": "jQuery.ofType(a)==1",
479 "only-child": "jQuery.sibling(a).length==1",
480 parent: "a.childNodes.length",
481 empty: "!a.childNodes.length",
482 root: "a==(a.ownerDocument||document).documentElement",
483 contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
484 visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')",
485 hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
486 enabled: "!a.disabled",
487 disabled: "a.disabled",
490 ".": "jQuery.hasWord(a,m[2])",
492 "=": "jQuery.attr(a,m[3])==m[4]",
493 "!=": "jQuery.attr(a,m[3])!=m[4]",
494 "~=": "jQuery.hasWord(jQuery.attr(a,m[3]),m[4])",
495 "|=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
496 "^=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
497 "$=": "jQuery.attr(a,m[3]).substr( jQuery.attr(a,m[3]).length - m[4].length,m[4].length )==m[4]",
498 "*=": "jQuery.attr(a,m[3]).indexOf(m[4])>=0",
499 "": "m[3]=='*'?a.attributes.length>0:jQuery.attr(a,m[3])"
501 "[": "jQuery.Select(m[2],a).length"
505 "\\.\\.|/\\.\\.", "a.parentNode",
506 ">|/", "jQuery.sibling(a.firstChild)",
507 "\\+", "jQuery.sibling(a).next",
510 var s = jQuery.sibling(a);
512 for ( var i = s.n; i < s.length; i++ )
518 jQuery.Select = function( t, context ) {
519 context = context || jQuery.context || document;
520 if ( t.constructor != String )
521 return t.constructor == Array ? t : [t];
523 if ( !t.indexOf("//") ) {
524 context = context.documentElement;
525 t = t.substr(2,t.length);
526 } else if ( !t.indexOf("/") ) {
527 context = context.documentElement;
528 t = t.substr(1,t.length);
529 // FIX Assume the root element is right :(
530 if ( t.indexOf("/") >= 1 )
531 t = t.substr(t.indexOf("/"),t.length);
538 while ( t.length > 0 && last != t ) {
542 t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
544 var foundToken = false;
546 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
547 var re = new RegExp("^(" + jQuery.token[i] + ")");
551 r = ret = jQuery.map( ret, jQuery.token[i+1] );
552 t = jQuery.cleanSpaces( t.replace( re, "" ) );
559 if ( !t.indexOf(",") || !t.indexOf("|") ) {
560 if ( ret[0] == context ) ret.shift();
561 done = jQuery.merge( done, ret );
563 t = " " + t.substr(1,t.length);
565 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
569 // Ummm, should make this work in all XML docs
570 var oid = document.getElementById(m[2]);
571 r = ret = oid ? [oid] : [];
572 t = t.replace( re2, "" );
574 if ( !m[2] || m[1] == "." ) m[2] = "*";
576 for ( var i = 0; i < ret.length; i++ )
579 jQuery.getAll(ret[i]) :
580 ret[i].getElementsByTagName(m[2])
588 var val = jQuery.filter(t,r);
590 t = jQuery.cleanSpaces(val.t);
594 if ( ret && ret[0] == context ) ret.shift();
595 done = jQuery.merge( done, ret );
600 jQuery.getAll = function(o,r) {
602 var s = o.childNodes;
603 for ( var i = 0; i < s.length; i++ )
604 if ( s[i].nodeType == 1 ) {
606 jQuery.getAll( s[i], r );
611 jQuery.attr = function(o,a,v){
612 if ( a && a.constructor == String ) {
615 "class": "className",
618 a = (fix[a] && fix[a].replace && fix[a]) || a;
619 var r = /-([a-z])/ig;
620 a = a.replace(r,function(z,b){return b.toUpperCase();});
621 if ( v != undefined ) {
623 if ( o.setAttribute && a != "disabled" )
626 return o[a] || o.getAttribute(a) || "";
631 jQuery.filter = function(t,r,not) {
634 g = function(a,f) {return jQuery.grep(a,f,true);};
636 while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
637 var re = /^\[ *@([a-z0-9*()_-]+) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
641 m = ["", "@", m[2], m[1], m[3]];
643 re = /^(\[) *([^\]]*) *\]/i;
647 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
651 re = /^([:\.#]*)([a-z0-9*_-]*)/i;
656 t = t.replace( re, "" );
658 if ( m[1] == ":" && m[2] == "not" )
659 r = jQuery.filter(m[3],r,false).r;
663 if ( jQuery.g[m[1]].constructor == String )
665 else if ( jQuery.g[m[1]][m[2]] )
666 f = jQuery.g[m[1]][m[2]];
669 eval("f = function(a,i){return " + f + "}");
675 return { r: r, t: t };
678 jQuery.parents = function(a){
680 var c = a.parentNode;
681 while ( c && c != document ) {
688 jQuery.cleanSpaces = function(t){
689 return t.replace(/^\s+|\s+$/g, "");
692 jQuery.ofType = function(a,n,e) {
693 var t = jQuery.grep(jQuery.sibling(a),function(b){ return b.nodeName == a.nodeName; });
694 if ( e ) n = t.length - n - 1;
695 return n != undefined ? t[n] == a : t.length;
698 jQuery.sibling = function(a,n,e) {
700 var tmp = a.parentNode.childNodes;
701 for ( var i = 0; i < tmp.length; i++ ) {
702 if ( tmp[i].nodeType == 1 )
703 type[type.length] = tmp[i];
705 type.n = type.length - 1;
707 if ( e ) n = type.length - n - 1;
708 type.cur = ( type[n] == a );
709 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
710 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
714 jQuery.hasWord = function(e,a) {
715 if ( e == undefined ) return;
716 if ( e.className ) e = e.className;
717 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
720 jQuery.merge = function(a,b) {
722 for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
724 for ( var i = 0; i < a.length; i++ ) {
726 for ( var j = 0; j < b.length; j++ )
729 if ( c ) d[d.length] = a[i];
735 jQuery.grep = function(a,f,s) {
736 if ( f.constructor == String )
737 f = new Function("a","i","return " + f);
740 for ( var i = 0; i < a.length; i++ )
741 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
746 jQuery.map = function(a,f) {
747 if ( f.constructor == String )
748 f = new Function("a","return " + f);
751 for ( var i = 0; i < a.length; i++ ) {
753 if ( t !== null && t != undefined ) {
754 if ( t.constructor != Array ) t = [t];
755 r = jQuery.merge( t, r );
763 // Bind an event to an element
764 // Original by Dean Edwards
765 add: function(element, type, handler) {
766 // For whatever reason, IE has trouble passing the window object
767 // around, causing it to be cloned in the process
768 if ( jQuery.browser == "msie" && element.setInterval != undefined )
771 if (!handler.guid) handler.guid = jQuery.event.guid++;
772 if (!element.events) element.events = {};
773 var handlers = element.events[type];
775 handlers = element.events[type] = {};
776 if (element["on" + type])
777 handlers[0] = element["on" + type];
779 handlers[handler.guid] = handler;
780 element["on" + type] = jQuery.event.handle;
785 // Detach an event or set of events from an element
786 remove: function(element, type, handler) {
788 if (type && element.events[type])
790 delete element.events[type][handler.guid];
792 for ( var i in element.events[type] )
793 delete element.events[type][i];
795 for ( var j in element.events )
796 jQuery.event.remove( element, j );
799 trigger: function(element,type,data) {
800 data = data || [ jQuery.event.fix({ type: type }) ];
801 if ( element && element["on" + type] )
802 element["on" + type].apply( element, data );
805 handle: function(event) {
806 // Handle adding events to items in IFrames, in IE
808 jQuery.event.fix( ((this.ownerDocument || this.document ||
809 this).parentWindow || window).event );
811 // If no correct event was found, fail
812 if ( !event ) return;
814 var returnValue = true, handlers = [];
816 for ( var j in this.events[event.type] )
817 handlers[handlers.length] = this.events[event.type][j];
819 for ( var i = 0; i < handlers.length; i++ )
820 if ( handlers[i].constructor == Function ) {
821 this.handleEvent = handlers[i];
822 if (this.handleEvent(event) === false) {
823 event.preventDefault();
824 event.stopPropagation();
831 fix: function(event) {
833 event.preventDefault = function() {
834 this.returnValue = false;
837 event.stopPropagation = function() {
838 this.cancelBubble = true;