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 for ( var i = 0; i < e[j].childNodes.length; i++ )
115 t += e[j].childNodes[i].nodeType != 1 ?
116 e[j].childNodes[i].nodeValue :
117 jQuery.fn.text(e[j].childNodes[i].childNodes);
122 return a.constructor != String || b ?
123 this.each(function(){
124 if ( b === undefined )
126 jQuery.attr(this.style,j,a[j]);
128 jQuery.attr(this.style,a,b);
129 }) : jQuery.css( this.get(0), a );
132 return this.each(function(){
133 var d = jQuery.css(this,"display");
134 if ( !d || d == "none" )
141 return this.each(function(){
142 this.style.display = this.oldblock ? this.oldblock : "";
143 if ( jQuery.css(this,"display") == "none" )
144 this.style.display = "block";
148 return this.each(function(){
149 this.oldblock = jQuery.css(this,"display");
150 if ( this.oldblock == "none" )
151 this.oldblock = "block";
152 this.style.display = "none";
155 addClass: function(c) {
156 return this.each(function(){
157 jQuery.className.add(this,c);
160 removeClass: function(c) {
161 return this.each(function(){
162 jQuery.className.remove(this,c);
166 toggleClass: function(c) {
167 return this.each(function(){
168 if (jQuery.hasWord(this,c))
169 jQuery.className.remove(this,c);
171 jQuery.className.add(this,c);
175 this.each(function(){this.parentNode.removeChild( this );});
176 return this.pushStack( [] );
180 var a = jQuery.clean(arguments);
181 return this.each(function(){
182 var b = a[0].cloneNode(true);
183 this.parentNode.insertBefore( b, this );
184 while ( b.firstChild )
186 b.appendChild( this );
191 var clone = this.size() > 1;
192 var a = jQuery.clean(arguments);
193 return this.domManip(function(){
194 for ( var i = 0; i < a.length; i++ )
195 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
199 appendTo: function() {
201 return this.each(function(){
202 for ( var i = 0; i < a.length; i++ )
203 $(a[i]).append( this );
207 prepend: function() {
208 var clone = this.size() > 1;
209 var a = jQuery.clean(arguments);
210 return this.domManip(function(){
211 for ( var i = a.length - 1; i >= 0; i-- )
212 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
217 var clone = this.size() > 1;
218 var a = jQuery.clean(arguments);
219 return this.each(function(){
220 for ( var i = 0; i < a.length; i++ )
221 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
226 var clone = this.size() > 1;
227 var a = jQuery.clean(arguments);
228 return this.each(function(){
229 for ( var i = a.length - 1; i >= 0; i-- )
230 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
235 return this.each(function(){
236 while ( this.firstChild )
237 this.removeChild( this.firstChild );
241 bind: function(t,f) {
242 return this.each(function(){jQuery.event.add(this,t,f);});
244 unbind: function(t,f) {
245 return this.each(function(){jQuery.event.remove(this,t,f);});
247 trigger: function(t) {
248 return this.each(function(){jQuery.event.trigger(this,t);});
251 pushStack: function(a) {
252 if ( !this.stack ) this.stack = [];
253 this.stack.unshift( this.cur );
254 if ( a ) this.cur = a;
260 this.each(function(){
261 ret = jQuery.merge( ret, jQuery.Select(t,this) );
263 this.pushStack( ret );
268 this.cur = this.stack.shift();
272 parent: function(a) {
273 var ret = jQuery.map(this.cur,"d.parentNode");
274 if ( a ) ret = jQuery.filter(a,ret).r;
275 return this.pushStack(ret);
278 parents: function(a) {
279 var ret = jQuery.map(this.cur,jQuery.parents);
280 if ( a ) ret = jQuery.filter(a,ret).r;
281 return this.pushStack(ret);
284 siblings: function(a) {
285 // Incorrect, need to exclude current element
286 var ret = jQuery.map(this.cur,jQuery.sibling);
287 if ( a ) ret = jQuery.filter(a,ret).r;
288 return this.pushStack(ret);
291 filter: function(t) {
292 return this.pushStack( jQuery.filter(t,this.cur).r );
295 return this.pushStack( t.constructor == String ?
296 jQuery.filter(t,this.cur,false).r :
297 jQuery.grep(this.cur,function(a){ return a != t; }) );
300 return this.pushStack( jQuery.merge( this.cur, t.constructor == String ?
301 jQuery.Select(t) : t.constructor == Array ? t : [t] ) );
305 * A wrapper function for each() to be used by append and prepend.
306 * Handles cases where you're trying to modify the inner contents of
307 * a table, when you actually need to work with the tbody.
310 * @param {String} expr The expression with which to filter
314 return jQuery.filter(expr,this.cur).r.length > 0;
318 * A wrapper function for each() to be used by append and prepend.
319 * Handles cases where you're trying to modify the inner contents of
320 * a table, when you actually need to work with the tbody.
324 * @param {Function} fn The function doing the DOM manipulation.
327 domManip: function(fn){
328 return this.each(function(){
331 if ( this.nodeName == "TABLE" ) {
332 var tbody = this.getElementsByTagName("tbody");
334 if ( !tbody.length ) {
335 obj = document.createElement("tbody");
336 this.appendChild( obj );
348 if (jQuery.hasWord(o,c)) return;
349 o.className += ( o.className ? " " : "" ) + c;
351 remove: function(o,c){
352 o.className = !c ? "" :
354 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
359 var b = navigator.userAgent.toLowerCase();
361 // Figure out what browser is being used
363 ( /webkit/.test(b) && "safari" ) ||
364 ( /opera/.test(b) && "opera" ) ||
365 ( /msie/.test(b) && "msie" ) ||
366 ( !/compatible/.test(b) && "mozilla" ) ||
369 // Check to see if the W3C box model is being used
370 jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
373 jQuery.css = function(e,p) {
374 // Adapted from Prototype 1.4.0
375 if ( p == "height" || p == "width" ) {
377 // Handle extra width/height provided by the W3C box model
378 var ph = (!jQuery.boxModel ? 0 :
379 jQuery.css(e,"paddingTop") + jQuery.css(e,"paddingBottom") +
380 jQuery.css(e,"borderTopWidth") + jQuery.css(e,"borderBottomWidth")) || 0;
382 var pw = (!jQuery.boxModel ? 0 :
383 jQuery.css(e,"paddingLeft") + jQuery.css(e,"paddingRight") +
384 jQuery.css(e,"borderLeftWidth") + jQuery.css(e,"borderRightWidth")) || 0;
388 if (jQuery.css(e,"display") != 'none') {
389 oHeight = e.offsetHeight || parseInt(e.style.height) || 0;
390 oWidth = e.offsetWidth || parseInt(e.style.width) || 0;
393 var ov = els.visibility;
394 var op = els.position;
395 var od = els.display;
396 els.visibility = "hidden";
397 els.position = "absolute";
399 oHeight = e.clientHeight || parseInt(e.style.height);
400 oWidth = e.clientWidth || parseInt(e.style.width);
406 return p == "height" ?
407 (oHeight - ph < 0 ? 0 : oHeight - ph) :
408 (oWidth - pw < 0 ? 0 : oWidth - pw);
415 else if (e.currentStyle)
416 r = e.currentStyle[p];
417 else if (document.defaultView && document.defaultView.getComputedStyle) {
418 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
419 var s = document.defaultView.getComputedStyle(e,"");
420 r = s ? s.getPropertyValue(p) : null;
423 return /top|right|left|bottom/i.test(p) ? parseFloat( r ) : r;
426 jQuery.clean = function(a) {
428 for ( var i = 0; i < a.length; i++ ) {
429 if ( a[i].constructor == String ) {
431 if ( !a[i].indexOf("<tr") ) {
433 a[i] = "<table>" + a[i] + "</table>";
434 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
436 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
439 var div = document.createElement("div");
440 div.innerHTML = a[i];
443 div = div.firstChild.firstChild;
444 if ( td ) div = div.firstChild;
447 for ( var j = 0; j < div.childNodes.length; j++ )
448 r[r.length] = div.childNodes[j];
449 } else if ( a[i].length && !a[i].nodeType )
450 for ( var k = 0; k < a[i].length; k++ )
451 r[r.length] = a[i][k];
452 else if ( a[i] !== null )
454 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
460 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
461 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
468 last: "i==r.length-1",
471 "first-child": "jQuery.sibling(a,0).cur",
472 "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))",
473 "last-child": "jQuery.sibling(a,0,true).cur",
474 "nth-last-child": "jQuery.sibling(a,m[3],true).cur",
475 "first-of-type": "jQuery.ofType(a,0)",
476 "nth-of-type": "jQuery.ofType(a,m[3])",
477 "last-of-type": "jQuery.ofType(a,0,true)",
478 "nth-last-of-type": "jQuery.ofType(a,m[3],true)",
479 "only-of-type": "jQuery.ofType(a)==1",
480 "only-child": "jQuery.sibling(a).length==1",
481 parent: "a.childNodes.length",
482 empty: "!a.childNodes.length",
483 root: "a==(a.ownerDocument||document).documentElement",
484 contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
485 visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')",
486 hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
487 enabled: "!a.disabled",
488 disabled: "a.disabled",
491 ".": "jQuery.hasWord(a,m[2])",
493 "=": "jQuery.attr(a,m[3])==m[4]",
494 "!=": "jQuery.attr(a,m[3])!=m[4]",
495 "~=": "jQuery.hasWord(jQuery.attr(a,m[3]),m[4])",
496 "|=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
497 "^=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
498 "$=": "jQuery.attr(a,m[3]).substr( jQuery.attr(a,m[3]).length - m[4].length,m[4].length )==m[4]",
499 "*=": "jQuery.attr(a,m[3]).indexOf(m[4])>=0",
500 "": "m[3]=='*'?a.attributes.length>0:jQuery.attr(a,m[3])"
502 "[": "jQuery.Select(m[2],a).length"
506 "\\.\\.|/\\.\\.", "a.parentNode",
507 ">|/", "jQuery.sibling(a.firstChild)",
508 "\\+", "jQuery.sibling(a).next",
511 var s = jQuery.sibling(a);
513 for ( var i = s.n; i < s.length; i++ )
519 jQuery.Select = function( t, context ) {
520 context = context || jQuery.context || document;
521 if ( t.constructor != String )
522 return t.constructor == Array ? t : [t];
524 if ( !t.indexOf("//") ) {
525 context = context.documentElement;
526 t = t.substr(2,t.length);
527 } else if ( !t.indexOf("/") ) {
528 context = context.documentElement;
529 t = t.substr(1,t.length);
530 // FIX Assume the root element is right :(
531 if ( t.indexOf("/") >= 1 )
532 t = t.substr(t.indexOf("/"),t.length);
539 while ( t.length > 0 && last != t ) {
543 t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
545 var foundToken = false;
547 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
548 var re = new RegExp("^(" + jQuery.token[i] + ")");
552 r = ret = jQuery.map( ret, jQuery.token[i+1] );
553 t = jQuery.cleanSpaces( t.replace( re, "" ) );
560 if ( !t.indexOf(",") || !t.indexOf("|") ) {
561 if ( ret[0] == context ) ret.shift();
562 done = jQuery.merge( done, ret );
564 t = " " + t.substr(1,t.length);
566 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
570 // Ummm, should make this work in all XML docs
571 var oid = document.getElementById(m[2]);
572 r = ret = oid ? [oid] : [];
573 t = t.replace( re2, "" );
575 if ( !m[2] || m[1] == "." ) m[2] = "*";
577 for ( var i = 0; i < ret.length; i++ )
580 jQuery.getAll(ret[i]) :
581 ret[i].getElementsByTagName(m[2])
589 var val = jQuery.filter(t,r);
591 t = jQuery.cleanSpaces(val.t);
595 if ( ret && ret[0] == context ) ret.shift();
596 done = jQuery.merge( done, ret );
601 jQuery.getAll = function(o,r) {
603 var s = o.childNodes;
604 for ( var i = 0; i < s.length; i++ )
605 if ( s[i].nodeType == 1 ) {
607 jQuery.getAll( s[i], r );
612 jQuery.attr = function(o,a,v){
613 if ( a && a.constructor == String ) {
616 "class": "className",
619 a = (fix[a] && fix[a].replace && fix[a]) || a;
620 var r = /-([a-z])/ig;
621 a = a.replace(r,function(z,b){return b.toUpperCase();});
622 if ( v != undefined ) {
624 if ( o.setAttribute && a != "disabled" )
627 return o[a] || o.getAttribute(a) || "";
632 jQuery.filter = function(t,r,not) {
635 g = function(a,f) {return jQuery.grep(a,f,true);};
637 while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
638 var re = /^\[ *@([a-z0-9*()_-]+) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
642 m = ["", "@", m[2], m[1], m[3]];
644 re = /^(\[) *([^\]]*) *\]/i;
648 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
652 re = /^([:\.#]*)([a-z0-9*_-]*)/i;
657 t = t.replace( re, "" );
659 if ( m[1] == ":" && m[2] == "not" )
660 r = jQuery.filter(m[3],r,false).r;
664 if ( jQuery.g[m[1]].constructor == String )
666 else if ( jQuery.g[m[1]][m[2]] )
667 f = jQuery.g[m[1]][m[2]];
670 eval("f = function(a,i){return " + f + "}");
676 return { r: r, t: t };
679 jQuery.parents = function(a){
681 var c = a.parentNode;
682 while ( c && c != document ) {
689 jQuery.cleanSpaces = function(t){
690 return t.replace(/^\s+|\s+$/g, "");
693 jQuery.ofType = function(a,n,e) {
694 var t = jQuery.grep(jQuery.sibling(a),function(b){ return b.nodeName == a.nodeName; });
695 if ( e ) n = t.length - n - 1;
696 return n != undefined ? t[n] == a : t.length;
699 jQuery.sibling = function(a,n,e) {
701 var tmp = a.parentNode.childNodes;
702 for ( var i = 0; i < tmp.length; i++ ) {
703 if ( tmp[i].nodeType == 1 )
704 type[type.length] = tmp[i];
706 type.n = type.length - 1;
708 if ( e ) n = type.length - n - 1;
709 type.cur = ( type[n] == a );
710 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
711 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
715 jQuery.hasWord = function(e,a) {
716 if ( e == undefined ) return;
717 if ( e.className ) e = e.className;
718 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
721 jQuery.merge = function(a,b) {
723 for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
725 for ( var i = 0; i < a.length; i++ ) {
727 for ( var j = 0; j < b.length; j++ )
730 if ( c ) d[d.length] = a[i];
736 jQuery.grep = function(a,f,s) {
737 if ( f.constructor == String )
738 f = new Function("a","i","return " + f);
741 for ( var i = 0; i < a.length; i++ )
742 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
747 jQuery.map = function(a,f) {
748 if ( f.constructor == String )
749 f = new Function("a","return " + f);
752 for ( var i = 0; i < a.length; i++ ) {
755 if ( t.constructor != Array ) t = [t];
756 r = jQuery.merge( t, r );
764 // Bind an event to an element
765 // Original by Dean Edwards
766 add: function(element, type, handler) {
767 // For whatever reason, IE has trouble passing the window object
768 // around, causing it to be cloned in the process
769 if ( jQuery.browser == "msie" && element.setInterval != undefined )
772 if (!handler.guid) handler.guid = jQuery.event.guid++;
773 if (!element.events) element.events = {};
774 var handlers = element.events[type];
776 handlers = element.events[type] = {};
777 if (element["on" + type])
778 handlers[0] = element["on" + type];
780 handlers[handler.guid] = handler;
781 element["on" + type] = jQuery.event.handle;
786 // Detach an event or set of events from an element
787 remove: function(element, type, handler) {
789 if (type && element.events[type])
791 delete element.events[type][handler.guid];
793 for ( var i in element.events[type] )
794 delete element.events[type][i];
796 for ( var j in element.events )
797 jQuery.event.remove( element, j );
800 trigger: function(element,type,data) {
801 data = data || [ jQuery.event.fix({ type: type }) ];
802 if ( element && element["on" + type] )
803 element["on" + type].apply( element, data );
806 handle: function(event) {
807 if ( !event && !window.event ) return;
809 var returnValue = true, handlers = [];
810 event = event || jQuery.event.fix(window.event);
812 for ( var j in this.events[event.type] )
813 handlers[handlers.length] = this.events[event.type][j];
815 for ( var i = 0; i < handlers.length; i++ ) {
816 if ( handlers[i].constructor == Function ) {
817 this.handleEvent = handlers[i];
818 if (this.handleEvent(event) === false) {
819 event.preventDefault();
820 event.stopPropagation();
828 fix: function(event) {
829 event.preventDefault = function() {
830 this.returnValue = false;
833 event.stopPropagation = function() {
834 this.cancelBubble = true;