4 * Copyright (c) 2006 John Resig (ejohn.org)
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 function jQuery(a,c) {
17 a || $.context || document,
18 c && c.$jquery && c.get(0) || c
22 if ( window.$ == undefined )
23 var $ = function(a,c) {
24 return new jQuery(a,c);
27 jQuery.prototype = $.fn = {
30 // The only two getters
32 return this.get().length;
35 return i == undefined ? this.cur : this.cur[i];
39 for ( var i = 0; i < this.size(); i++ )
40 f.apply( this.get(i), [i] );
44 return this.each(function(){
53 return h == undefined && this.size() ?
54 this.get(0).innerHTML : this.set( "innerHTML", h );
57 return h == undefined && this.size() ?
58 this.get(0).value : this.set( "value", h );
63 for ( var j = 0; j < e.length; j++ )
64 for ( var i = 0; i < e[j].childNodes.length; i++ )
65 t += e[j].childNodes[i].nodeType != 1 ?
66 e[j].childNodes[i].nodeValue :
67 $.fn.text(e[j].childNodes[i].childNodes);
72 return a.constructor != String || b ?
76 $.attr(this.style,j,a[j]);
78 $.attr(this.style,a,b);
79 }) : $.css( this.get(0), a );
82 return this.each(function(){
83 var d = $.css(this,"display");
84 if ( d == "none" || d === "" )
91 return this.each(function(){
92 this.style.display = this.oldblock ? this.oldblock : "";
93 if ( $.css(this,"display") == "none" )
94 this.style.display = "block";
98 return this.each(function(){
99 this.oldblock = $.css(this,"display");
100 if ( this.oldblock == "none" )
101 this.oldblock = "block";
102 this.style.display = "none";
105 addClass: function(c) {
106 return this.each(function(){
110 removeClass: function(c) {
111 return this.each(function(){
112 $.class.remove(this,c);
116 toggleClass: function(c) {
117 return this.each(function(){
118 if ($.hasWord(this,c))
119 $.class.remove(this,c);
125 this.each(function(){this.parentNode.removeChild( this );});
131 var a = $.clean(arguments);
132 return this.each(function(){
133 var b = a[0].cloneNode(true);
134 this.parentNode.insertBefore( b, this );
135 while ( b.firstChild )
137 b.appendChild( this );
142 var clone = this.size() > 1;
143 var a = $.clean(arguments);
144 return this.domManip(function(){
145 for ( var i = 0; i < a.length; i++ )
146 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
150 appendTo: function() {
152 return this.each(function(){
153 for ( var i = 0; i < a.length; i++ )
154 $(a[i]).append( this );
158 prepend: function() {
159 var clone = this.size() > 1;
160 var a = $.clean(arguments);
161 return this.domManip(function(){
162 for ( var i = a.length - 1; i >= 0; i-- )
163 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
168 var clone = this.size() > 1;
169 var a = $.clean(arguments);
170 return this.each(function(){
171 for ( var i = 0; i < a.length; i++ )
172 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
177 var clone = this.size() > 1;
178 var a = $.clean(arguments);
179 return this.each(function(){
180 for ( var i = a.length - 1; i >= 0; i-- )
181 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
186 return this.each(function(){
187 while ( this.firstChild )
188 this.removeChild( this.firstChild );
192 bind: function(t,f) {
193 return this.each(function(){$.event.add(this,t,f);});
195 unbind: function(t,f) {
196 return this.each(function(){$.event.remove(this,t,f);});
198 trigger: function(t) {
199 return this.each(function(){$.event.trigger(this,t);});
203 var old = [], ret = [];
204 this.each(function(){
205 old[old.length] = this;
206 ret = $.merge( ret, $.Select(t,this) );
217 parent: function(a) {
218 this.cur = $.map(this.cur,"d.parentNode");
219 if ( a ) this.cur = $.filter(a,this.cur).r;
223 parents: function(a) {
224 this.cur = $.map(this.cur,$.parents);
225 if ( a ) this.cur = $.filter(a,this.cur).r;
229 siblings: function(a) {
230 // Incorrect, need to exclude current element
231 this.cur = $.map(this.cur,$.sibling);
232 if ( a ) this.cur = $.filter(a,this.cur).r;
236 filter: function(t) {
237 this.cur = $.filter(t,this.cur).r;
241 this.cur = t.constructor == String ?
242 $.filter(t,this.cur,false).r :
243 $.grep(this.cur,function(a){ return a != t; });
247 this.cur = $.merge( this.cur, t.constructor == String ?
248 $.Select(t) : t.constructor == Array ? t : [t] );
252 return $.filter(t,this.cur).r.length > 0;
256 * A wrapper function for each() to be used by append and prepend.
257 * Handles cases where you're trying to modify the inner contents of
258 * a table, when you actually need to work with the tbody.
260 domManip: function(fn){
261 return this.each(function(){
264 if ( this.nodeName == "TABLE" ) {
265 var tbody = this.getElementsByTagName("tbody");
267 if ( !tbody.length ) {
268 obj = document.createElement("tbody");
269 this.appendChild( obj );
281 if ($.hasWord(o,c)) return;
282 o.className += ( o.className.length > 0 ? " " : "" ) + c;
284 remove: function(o,c){
285 o.className = !c ? "" :
287 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
292 var b = navigator.userAgent.toLowerCase();
294 // Figure out what browser is being used
296 ( /webkit/.test(b) && "safari" ) ||
297 ( /opera/.test(b) && "opera" ) ||
298 ( /msie/.test(b) && "msie" ) ||
299 ( !/compatible/.test(b) && "mozilla" ) ||
302 // Check to see if the W3C box model is being used
303 $.boxModel = ( $.browser != "msie" || document.compatMode == "CSS1Compat" );
306 $.css = function(e,p) {
307 // Adapted from Prototype 1.4.0
308 if ( p == "height" || p == "width" ) {
310 // Handle extra width/height provided by the W3C box model
311 var ph = (!$.boxModel ? 0 :
312 $.css(e,"paddingTop") + $.css(e,"paddingBottom") +
313 $.css(e,"borderTopWidth") + $.css(e,"borderBottomWidth")) || 0;
315 var pw = (!$.boxModel ? 0 :
316 $.css(e,"paddingLeft") + $.css(e,"paddingRight") +
317 $.css(e,"borderLeftWidth") + $.css(e,"borderRightWidth")) || 0;
321 if ($.css(e,"display") != 'none') {
322 oHeight = e.offsetHeight || parseInt(e.style.height) || 0;
323 oWidth = e.offsetWidth || parseInt(e.style.width) || 0;
326 var ov = els.visibility;
327 var op = els.position;
328 var od = els.display;
329 els.visibility = "hidden";
330 els.position = "absolute";
332 oHeight = e.clientHeight || parseInt(e.style.height);
333 oWidth = e.clientWidth || parseInt(e.style.width);
339 return p == "height" ?
340 (oHeight - ph < 0 ? 0 : oHeight - ph) :
341 (oWidth - pw < 0 ? 0 : oWidth - pw);
348 else if (e.currentStyle)
349 r = e.currentStyle[p];
350 else if (document.defaultView && document.defaultView.getComputedStyle) {
351 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
352 var s = document.defaultView.getComputedStyle(e,"");
353 r = s ? s.getPropertyValue(p) : null;
356 return /top|right|left|bottom/i.test(p) ? parseFloat( r ) : r;
359 $.clean = function(a) {
361 for ( var i = 0; i < a.length; i++ ) {
362 if ( a[i].constructor == String ) {
364 if ( !a[i].indexOf("<tr") ) {
366 a[i] = "<table>" + a[i] + "</table>";
367 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
369 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
372 var div = document.createElement("div");
373 div.innerHTML = a[i];
376 div = div.firstChild.firstChild;
377 if ( td ) div = div.firstChild;
380 for ( var j = 0; j < div.childNodes.length; j++ )
381 r[r.length] = div.childNodes[j];
382 } else if ( a[i].length && !a[i].nodeType )
383 for ( var k = 0; k < a[i].length; k++ )
384 r[r.length] = a[i][k];
385 else if ( a[i] !== null )
387 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
393 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
394 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
401 last: "i==r.length-1",
404 "first-child": "$.sibling(a,0).cur",
405 "nth-child": "(m[3]=='even'?$.sibling(a,m[3]).n%2==0:(m[3]=='odd'?$.sibling(a,m[3]).n%2==1:$.sibling(a,m[3]).cur))",
406 "last-child": "$.sibling(a,0,true).cur",
407 "nth-last-child": "$.sibling(a,m[3],true).cur",
408 "first-of-type": "$.ofType(a,0)",
409 "nth-of-type": "$.ofType(a,m[3])",
410 "last-of-type": "$.ofType(a,0,true)",
411 "nth-last-of-type": "$.ofType(a,m[3],true)",
412 "only-of-type": "$.ofType(a)==1",
413 "only-child": "$.sibling(a).length==1",
414 parent: "a.childNodes.length",
415 empty: "!a.childNodes.length",
416 root: "a==(a.ownerDocument||document).documentElement",
417 contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
418 visible: "(!a.type||a.type!='hidden')&&($.css(a,'display')!= 'none'&&$.css(a,'visibility')!= 'hidden')",
419 hidden: "(a.type&&a.type == 'hidden')||$.css(a,'display')=='none'||$.css(a,'visibility')== 'hidden'",
420 enabled: "a.disabled==false",
421 disabled: "a.disabled",
424 ".": "$.hasWord(a,m[2])",
426 "=": "$.attr(a,m[3])==m[4]",
427 "!=": "$.attr(a,m[3])!=m[4]",
428 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
429 "|=": "!$.attr(a,m[3]).indexOf(m[4])",
430 "^=": "!$.attr(a,m[3]).indexOf(m[4])",
431 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length,m[4].length )==m[4]",
432 "*=": "$.attr(a,m[3]).indexOf(m[4])>=0",
433 "": "m[3]=='*'?a.attributes.length>0:$.attr(a,m[3])"
435 "[": "$.Select(m[2],a).length"
439 "\\.\\.|/\\.\\.", "a.parentNode",
440 ">|/", "$.sibling(a.firstChild)",
441 "\\+", "$.sibling(a).next",
444 var s = $.sibling(a);
446 for ( var i = s.n; i < s.length; i++ )
452 $.Select = function( t, context ) {
453 context = context || $.context || document;
454 if ( t.constructor != String ) return [t];
456 if ( !t.indexOf("//") ) {
457 context = context.documentElement;
458 t = t.substr(2,t.length);
459 } else if ( !t.indexOf("/") ) {
460 context = context.documentElement;
461 t = t.substr(1,t.length);
462 // FIX Assume the root element is right :(
463 if ( t.indexOf("/") >= 1 )
464 t = t.substr(t.indexOf("/"),t.length);
471 while ( t.length > 0 && last != t ) {
475 t = $.cleanSpaces(t).replace( /^\/\//i, "" );
477 var foundToken = false;
479 for ( var i = 0; i < $.token.length; i += 2 ) {
480 var re = new RegExp("^(" + $.token[i] + ")");
484 r = ret = $.map( ret, $.token[i+1] );
485 t = $.cleanSpaces( t.replace( re, "" ) );
492 if ( !t.indexOf(",") || !t.indexOf("|") ) {
493 if ( ret[0] == context ) ret.shift();
494 done = $.merge( done, ret );
496 t = " " + t.substr(1,t.length);
498 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
502 // Ummm, should make this work in all XML docs
503 var oid = document.getElementById(m[2]);
504 r = ret = oid ? [oid] : [];
505 t = t.replace( re2, "" );
507 if ( !m[2] || m[1] == "." ) m[2] = "*";
509 for ( var i = 0; i < ret.length; i++ )
510 r = $.merge( r, $.tag(ret[i],m[2]) );
517 var val = $.filter(t,r);
519 t = $.cleanSpaces(val.t);
523 if ( ret && ret[0] == context ) ret.shift();
524 done = $.merge( done, ret );
529 $.tag = function(a,b){
530 return a && a.getElementsByTagName != undefined ?
531 a.getElementsByTagName( b ) : [];
534 $.attr = function(o,a,v){
535 if ( a && a.constructor == String ) {
538 "class": "className",
541 a = (fix[a] && fix[a].replace && fix[a]) || a;
542 var r = /-([a-z])/ig;
543 a = a.replace(r,function(z,b){return b.toUpperCase();});
544 if ( v != undefined ) {
546 if ( o.setAttribute && a != "disabled" )
549 return o[a] || o.getAttribute(a) || "";
554 $.filter = function(t,r,not) {
557 g = function(a,f) {return $.grep(a,f,true);};
559 while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
560 var re = /^\[ *@([a-z0-9*()_-]+) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
564 m = ["", "@", m[2], m[1], m[3]];
566 re = /^(\[) *([^\]]*) *\]/i;
570 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
574 re = /^([:\.#]*)([a-z0-9*_-]*)/i;
579 t = t.replace( re, "" );
581 if ( m[1] == ":" && m[2] == "not" )
582 r = $.filter(m[3],r,false).r;
586 if ( $.g[m[1]].constructor == String )
588 else if ( $.g[m[1]][m[2]] )
592 eval("f = function(a,i){return " + f + "}");
598 return { r: r, t: t };
601 $.parents = function(a){
603 var c = a.parentNode;
604 while ( c && c != document ) {
611 $.cleanSpaces = function(t){
612 return t.replace(/^\s+|\s+$/g, "");
615 $.ofType = function(a,n,e) {
616 var t = $.grep($.sibling(a),function(b){ return b.nodeName == a.nodeName; });
617 if ( e ) n = t.length - n - 1;
618 return n != undefined ? t[n] == a : t.length;
621 $.sibling = function(a,n,e) {
623 var tmp = a.parentNode.childNodes;
624 for ( var i = 0; i < tmp.length; i++ ) {
625 if ( tmp[i].nodeType == 1 )
626 type[type.length] = tmp[i];
628 type.n = type.length - 1;
630 if ( e ) n = type.length - n - 1;
631 type.cur = ( type[n] == a );
632 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
633 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
637 $.hasWord = function(e,a) {
638 if ( e == undefined ) return;
639 if ( e.className ) e = e.className;
640 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
643 $.getAll = function(o,r) {
645 var s = o.childNodes;
646 for ( var i = 0; i < s.length; i++ )
647 if ( s[i].nodeType == 1 ) {
654 $.merge = function(a,b) {
656 for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
658 for ( var i = 0; i < a.length; i++ ) {
660 for ( var j = 0; j < b.length; j++ )
663 if ( c ) d[d.length] = a[i];
669 $.grep = function(a,f,s) {
670 if ( f.constructor == String )
671 f = new Function("a","i","return " + f);
673 if ( a != undefined )
674 for ( var i = 0; i < a.length; i++ )
675 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
680 $.map = function(a,f) {
681 if ( f.constructor == String )
682 f = new Function("a","return " + f);
685 for ( var i = 0; i < a.length; i++ ) {
688 if ( t.constructor != Array ) t = [t];
697 // Bind an event to an element
698 // Original by Dean Edwards
699 $.event.add = function(element, type, handler) {
700 // For whatever reason, IE has trouble passing the window object
701 // around, causing it to be cloned in the process
702 if ( $.browser == "msie" && element.setInterval != undefined )
705 if (!handler.$$guid) handler.$$guid = $.event.add.guid++;
706 if (!element.events) element.events = {};
707 var handlers = element.events[type];
709 handlers = element.events[type] = {};
710 if (element["on" + type])
711 handlers[0] = element["on" + type];
713 handlers[handler.$$guid] = handler;
714 element["on" + type] = $.event.handle;
717 $.event.add.guid = 1;
719 // Detach an event or set of events from an element
720 $.event.remove = function(element, type, handler) {
722 if (type && element.events[type])
724 delete element.events[type][handler.$$guid];
726 for ( var i in element.events[type] )
727 delete element.events[type][i];
729 for ( var j in element.events )
730 $.event.remove( element, j );
733 $.event.trigger = function(element,type,data) {
734 data = data || [ $.event.fix({ type: type }) ];
735 if ( element && element["on" + type] )
736 element["on" + type].apply( element, data );
739 $.event.handle = function(event) {
740 if ( !event && !window.event ) return;
742 var returnValue = true, handlers = [];
743 event = event || $.event.fix(window.event);
745 for ( var j in this.events[event.type] )
746 handlers[handlers.length] = this.events[event.type][j];
748 for ( var i = 0; i < handlers.length; i++ ) {
749 if ( handlers[i].constructor == Function ) {
750 this.$$handleEvent = handlers[i];
751 if (this.$$handleEvent(event) === false) {
752 event.preventDefault();
753 event.stopPropagation();
761 $.event.fix = function(event) {
762 event.preventDefault = $.event.fix.preventDefault;
763 event.stopPropagation = $.event.fix.stopPropagation;
767 $.event.fix.preventDefault = function() {
768 this.returnValue = false;
771 $.event.fix.stopPropagation = function() {
772 this.cancelBubble = true;