2 * jQuery (http://jquery.com/)
3 * By John Resig (http://ejohn.org/)
4 * Under an Attribution, Share Alike License
11 var $a = a || $.context || document;
12 var $c = c && c.$jquery && c.get(0) || c;
14 // Since we're using Prototype's $ function,
15 // be nice and have backwards compatability
16 if ( typeof Prototype != "undefined" ) {
17 if ( $a.constructor == String ) {
18 var re = new RegExp( "[^a-zA-Z0-9_-]" );
20 $c = $c && $c.documentElement || document;
21 if ( $c.getElementsByTagName($a).length == 0 ) {
22 var obj = $c.getElementById($a);
23 if ( obj != null ) return obj;
26 } else if ( $a.constructor == Array ) {
27 return $.map( $a, function(b){
28 if ( b.constructor == String )
29 return document.getElementById(b);
35 // Load Dynamic Function List
40 // The only two getters
41 size: function() {return this.get().length},
43 return i == null ? this.cur : this.cur[i];
47 for ( var i = 0; i < this.size(); i++ )
48 $.apply( this.get(i), f, [i] );
52 return this.each(function(){
61 return h == null && this.size() ?
62 this.get(0).innerHTML : this.set( "innerHTML", h );
65 return h == null && this.size() ?
66 this.get(0).value : this.set( "value", h );
70 return this.each(function(){
73 $.attr(this.style,j,a[j]);
75 $.attr(this.style,a,b);
79 return this.each(function(){
80 var d = $.getCSS(this,"display");
81 if ( d == "none" || d == '' )
88 return this.each(function(){
89 this.style.display = this.$$oldblock ? this.$$oldblock : '';
90 if ( $.getCSS(this,"display") == "none" ) this.style.display = 'block';
94 return this.each(function(){
95 this.$$oldblock = $.getCSS(this,"display");
96 if ( this.$$oldblock == "none" ) this.$$oldblock = 'block';
97 this.style.display = 'none';
100 addClass: function(c) {
101 return this.each(function(){
102 if ($.hasWord(this,c)) return;
103 this.className += ( this.className.length > 0 ? " " : "" ) + c;
106 removeClass: function(c) {
107 return this.each(function(){
108 this.className = c == null ? '' :
109 this.className.replace(
110 new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
114 toggleClass: function(c) {
115 return this.each(function(){
116 if ($.hasWord(this,c))
118 this.className.replace(
119 new RegExp('(\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
121 this.className += ( this.className.length > 0 ? " " : "" ) + 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 ) b = b.firstChild;
136 b.appendChild( this );
141 var clone = this.size() > 1;
142 var a = $.clean(arguments);
143 return this.each(function(){
145 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
149 appendTo: function() {
151 return this.each(function(){
152 for ( var i = 0; i < a.length; i++ )
153 $(a[i]).append( this );
157 prepend: function() {
158 var clone = this.size() > 1;
159 var a = $.clean(arguments);
160 return this.each(function(){
161 for ( var i = a.length - 1; i >= 0; i-- )
162 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
167 var clone = this.size() > 1;
168 var a = $.clean(arguments);
169 return this.each(function(){
171 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
176 var clone = this.size() > 1;
177 var a = $.clean(arguments);
178 return this.each(function(){
179 for ( var i = a.length - 1; i >= 0; i-- )
180 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
185 return this.each(function(){
186 while ( this.firstChild )
187 this.removeChild( this.firstChild );
191 bind: function(t,f) {
192 return this.each(function(){addEvent(this,t,f);});
194 unbind: function(t,f) {
195 return this.each(function(){removeEvent(this,t,f);});
197 trigger: function(t) {
198 return this.each(function(){triggerEvent(this,t);});
202 var old = [], ret = [];
203 this.each(function(){
204 old[old.length] = this;
205 ret = $.merge( ret, $.Select(t,this) );
216 parent: function(a) {
217 if ( a == null ) a = 1;
218 this.cur = $.map(this.cur,function(d){
219 var b = $.parents(d);
222 else if ( a.constructor == String ) {
223 var c = $.filter(a,b);
224 return c.length > 0 ? c[0] : null;
226 return b.length >= a ? b[a-1] : null;
231 parents: function(a) {
235 filter: function(t) {
236 this.cur = $.filter(t,this.cur).r;
240 this.cur = t.constructor == String ?
241 $.filter(t,this.cur,false).r :
242 $.grep(this.cur,function(a){return a != t;});
246 this.cur = $.merge( this.cur, t.constructor == String ?
247 $.Select(t) : t.constructor == Array ? t : [t] );
251 return $.filter(t,this.cur).r.length > 0;
258 // TODO: Remove need to return this
259 for ( var i in $.fn ) {
260 if ( self[i] != null )
261 self["_"+i] = self[i];
265 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
266 if ( $c ) $a = self.get();
267 for ( var i in self ) {(function(j){
269 if ( $a[j] == null ) {
271 return $.apply(self,self[j],arguments);
282 $.apply = function(o,f,a) {
285 return f.apply( o, a );
288 for (var i = 0; i < a.length; i++)
291 var r = eval('o.$$exec(' + p.join(',') + ')');
297 $.getCSS = function(e,p) {
298 // Adapted from Prototype 1.4.0
299 if ( p == 'height' || p == 'width' ) {
300 if ($.getCSS(e,"display") != 'none')
301 return p == 'height' ?
302 e.offsetHeight || parseInt(e.style.height) :
303 e.offsetWidth || parseInt(e.style.width);
305 var ov = els.visibility;
306 var op = els.position;
307 var od = els.display;
308 els.visibility = 'hidden';
309 els.position = 'absolute';
311 var oHeight = e.clientHeight || parseInt(e.style.height);
312 var oWidth = e.clientWidth || parseInt(e.style.width);
316 return p == 'height' ? oHeight : oWidth;
321 else if (e.currentStyle)
322 return e.currentStyle[p];
323 else if (document.defaultView && document.defaultView.getComputedStyle) {
324 p = p.replace(/([A-Z])/g,"-$1");
326 var s = document.defaultView.getComputedStyle(e,"");
327 var r = s ? s.getPropertyValue(p) : p;
334 $.clean = function(a) {
336 for ( var i = 0; i < a.length; i++ )
337 if ( a[i].constructor == String ) {
338 var div = document.createElement("div");
339 div.innerHTML = a[i];
340 for ( var j = 0; j < div.childNodes.length; j++ )
341 r[r.length] = div.childNodes[j];
342 } else if ( a[i].length )
343 for ( var j = 0; j < a[i].length; j++ )
344 r[r.length] = a[i][j];
345 else if ( a[i] != null )
347 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
352 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
357 nth: "m[3] - 0 == i",
360 last: "i == r.length - 1",
363 "first-child": "$.sibling(a,0).cur",
364 "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))",
365 "last-child": "$.sibling(a,0,true).cur",
366 "nth-last-child": "$.sibling(a,m[3],true).cur",
367 "first-of-type": "$.ofType(a,0)",
368 "nth-of-type": "$.ofType(a,m[3])",
369 "last-of-type": "$.ofType(a,0,true)",
370 "nth-last-of-type": "$.ofType(a,m[3],true)",
371 "only-of-type": "$.ofType(a) == 1",
372 "only-child": "$.sibling(a).length == 1",
373 parent: "a.childNodes.length > 0",
374 empty: "a.childNodes.length == 0",
375 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
376 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
377 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
378 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
379 enabled: "a.disabled == false",
380 disabled: "a.disabled",
383 // TODO: Write getAttribute helper
384 ".": "$.hasWord(a.className||a.getAttribute('class'),m[2])",
386 "=": "$.attr(a,m[3]) == m[4]",
387 "!=": "$.attr(a,m[3]) != m[4]",
388 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
389 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
390 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
391 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
392 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
393 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
395 "[": "$.Select(m[2],a).length > 0"
400 $.Select = function( t, context ) {
401 context = context || $.context || document;
402 if ( t.constructor != String ) return [t];
404 if ( t.indexOf("//") == 0 ) {
405 context = context.documentElement;
406 t = t.substr(2,t.length);
407 } else if ( t.indexOf("/") == 0 ) {
408 context = context.documentElement;
409 t = t.substr(1,t.length);
410 // FIX Assume the root element is right :(
411 if ( t.indexOf('/') )
412 t = t.substr(t.indexOf('/'),t.length);
419 while ( t.length > 0 && last != t ) {
423 t = $.cleanSpaces(t);
425 var re = new RegExp( "^//", "i" );
426 t = t.replace( re, "" );
428 if ( t.indexOf('..') == 0 || t.indexOf('/..') == 0 ) {
429 if ( t.indexOf('/') == 0 )
430 t = t.substr(1,t.length);
431 r = $.map( ret, function(a){ return a.parentNode; } );
432 t = t.substr(2,t.length);
433 t = $.cleanSpaces(t);
434 } else if ( t.indexOf('>') == 0 || t.indexOf('/') == 0 ) {
435 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
436 t = t.substr(1,t.length);
437 t = $.cleanSpaces(t);
438 } else if ( t.indexOf('+') == 0 ) {
439 r = $.map( ret, function(a){ return $.sibling(a).next; } );
440 t = t.substr(1,t.length);
441 t = $.cleanSpaces(t);
442 } else if ( t.indexOf('~') == 0 ) {
443 r = $.map( ret, function(a){
445 var s = $.sibling(a);
447 for ( var i = s.n; i < s.length; i++ )
451 t = t.substr(1,t.length);
452 t = $.cleanSpaces(t);
453 } else if ( t.indexOf(',') == 0 || t.indexOf('|') == 0 ) {
454 if ( ret[0] == context ) ret.shift();
455 done = $.merge( done, ret );
457 t = " " + t.substr(1,t.length);
459 var re = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
462 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
463 var oid = document.getElementById(m[2]);
464 r = oid ? [oid] : [];
465 t = t.replace( re, "" );
467 if ( m[2] == "" || m[1] == "." ) m[2] = "*";
469 for ( var i = 0; i < ret.length; i++ ) {
474 r = $.merge( $.getAll(o), r );
476 case 'text': case 'radio': case 'checkbox': case 'hidden':
477 case 'button': case 'submit': case 'image': case 'password':
478 case 'reset': case 'file':
479 r = $.merge( $.grep( $.tag(o,"input"),
480 function(a){ return a.type == m[2] }), r );
483 r = $.merge( $.tag(o,"input"), r );
484 r = $.merge( $.tag(o,"select"), r );
485 r = $.merge( $.tag(o,"textarea"), r );
488 r = $.merge( r, $.tag(o,m[2]) );
496 var val = $.filter(t,r);
498 t = $.cleanSpaces(val.t);
501 if ( ret && ret[0] == context ) ret.shift();
502 done = $.merge( done, ret );
506 $.tag = function(a,b){
507 return a && typeof a.getElementsByTagName != "undefined" ?
508 a.getElementsByTagName( b ) : [];
511 $.attr = function(o,a,v){
512 if ( a && a.constructor == String ) {
516 'class': 'className',
519 a = (fix[a] && fix[a].replace && fix[a]) || a;
520 var r = new RegExp("-([a-z])","ig");
521 a = a.replace(r,function(z,b){return b.toUpperCase();});
524 if ( o.setAttribute ) o.setAttribute(a,v);
526 return o[a] || o.getAttribute(a) || '';
530 $.filter = function(t,r,not) {
532 if ( not == false ) var g = function(a,f) {return $.grep(a,f,true);};
534 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
535 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
539 m = ['', '@', m[2], m[1], m[3]];
541 var re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
545 var re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
549 var re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
554 t = t.replace( re, "" );
556 if ( m[1] == ":" && m[2] == "not" )
557 r = $.filter(m[3],r,false).r;
559 if ( $.g[m[1]].constructor == String )
561 else if ( $.g[m[1]][m[2]] )
562 var f = $.g[m[1]][m[2]];
565 eval("f = function(a,i){return " + f + "}");
570 return { r: r, t: t };
573 $.parents = function(a){
575 var c = a.parentNode;
576 while ( c != null && c != c.documentElement ) {
583 $.cleanSpaces = function(t){return t.replace(/^\s+|\s+$/g, '')};
585 $.ofType = function(a,n,e) {
586 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName});
587 if ( e ) n = t.length - n - 1;
588 return n != null ? t[n] == a : t.length;
591 $.sibling = function(a,n,e) {
593 var tmp = a.parentNode.childNodes;
594 for ( var i = 0; i < tmp.length; i++ ) {
595 if ( tmp[i].nodeType == 1 )
596 type[type.length] = tmp[i];
598 type.n = type.length - 1;
600 if ( e ) n = type.length - n - 1;
601 type.cur = ( type[n] == a );
602 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
603 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
607 $.hasWord = function(e,a) {
608 if ( e == null ) return false;
609 if ( e.className != null ) e = e.className;
610 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e)
613 $.getAll = function(o,r) {
615 var s = o.childNodes;
616 for ( var i = 0; i < s.length; i++ ) {
617 if ( s[i].nodeType == 1 ) {
625 $.merge = function(a,b) {
627 for ( var j = 0; j < b.length; j++ )
630 for ( var i = 0; i < a.length; i++ ) {
632 for ( var j = 0; j < b.length; j++ )
641 $.grep = function(a,f,s) {
644 for ( var i = 0; i < a.length; i++ )
645 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
650 $.map = function(a,f) {
652 for ( var i = 0; i < a.length; i++ ) {
655 if ( t.constructor != Array ) t = [t];
662 // Bind an event to an element
663 // Original by Dean Edwards
664 function addEvent(element, type, handler) {
665 if ( element.location ) element = window; // Ughhhhh....
666 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
667 if (!element.events) element.events = {};
668 var handlers = element.events[type];
670 handlers = element.events[type] = {};
671 if (element["on" + type])
672 handlers[0] = element["on" + type];
674 handlers[handler.$$guid] = handler;
675 element["on" + type] = handleEvent;
679 // Detach an event or set of events from an element
680 function removeEvent(element, type, handler) {
681 if (element.events) {
682 if (type && element.events[type]) {
684 delete element.events[type][handler.$$guid];
686 for ( var i in element.events[type] )
687 delete element.events[type][i];
690 for ( var i in element.events )
691 removeEvent( element, i );
696 function triggerEvent(element,type) {
697 if ( element["on" + type] )
698 element["on" + type]({ type: type });
701 function handleEvent(event) {
702 var returnValue = true;
703 event = event || fixEvent(window.event);
705 for ( var i in this.events[event.type] )
706 handlers[handlers.length] = this.events[event.type][i];
707 for ( var i = 0; i < handlers.length; i++ ) {
709 if ( handlers[i].constructor == Function ) {
710 this.$$handleEvent = handlers[i];
711 if (this.$$handleEvent(event) === false) {
712 event.preventDefault();
713 event.stopPropagation();
722 function fixEvent(event) {
723 event.preventDefault = fixEvent.preventDefault;
724 event.stopPropagation = fixEvent.stopPropagation;
727 fixEvent.preventDefault = function() {
728 this.returnValue = false;
730 fixEvent.stopPropagation = function() {
731 this.cancelBubble = true;
736 $.fn.text = function(e) {
739 for ( var j = 0; j < e.length; j++ ) {
740 for ( var i = 0; i < e[j].childNodes.length; i++ )
741 t += e[j].childNodes[i].nodeType != 1 ?
742 e[j].childNodes[i].nodeValue :
743 $.fn.text(e[j].childNodes[i].childNodes);
748 setTimeout(function(){
749 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
750 throw "Error: You are overwriting jQuery, please include jQuery last.";