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 this.cur = $.map(this.cur,function(d){
220 if ( a ) this.cur = $.filter(a,this.cur).r;
224 parents: function(a) {
225 this.cur = $.map(this.cur,$.parents);
226 if ( a ) this.cur = $.filter(a,this.cur).r;
230 siblings: function(a) {
231 // Incorrect, need to exclude current element
232 this.cur = $.map(this.cur,$.sibling);
233 if ( a ) this.cur = $.filter(a,this.cur).r;
237 parents: function(a) {
241 filter: function(t) {
242 this.cur = $.filter(t,this.cur).r;
246 this.cur = t.constructor == String ?
247 $.filter(t,this.cur,false).r :
248 $.grep(this.cur,function(a){return a != t;});
252 this.cur = $.merge( this.cur, t.constructor == String ?
253 $.Select(t) : t.constructor == Array ? t : [t] );
257 return $.filter(t,this.cur).r.length > 0;
264 // TODO: Remove need to return this
265 for ( var i in $.fn ) {
266 if ( self[i] != null )
267 self["_"+i] = self[i];
271 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
272 if ( $c ) $a = self.get();
273 for ( var i in self ) {(function(j){
275 if ( $a[j] == null ) {
277 return $.apply(self,self[j],arguments);
288 $.apply = function(o,f,a) {
291 return f.apply( o, a );
294 for (var i = 0; i < a.length; i++)
297 var r = eval('o.$$exec(' + p.join(',') + ')');
303 $.getCSS = function(e,p) {
304 // Adapted from Prototype 1.4.0
305 if ( p == 'height' || p == 'width' ) {
306 if ($.getCSS(e,"display") != 'none')
307 return p == 'height' ?
308 e.offsetHeight || parseInt(e.style.height) :
309 e.offsetWidth || parseInt(e.style.width);
311 var ov = els.visibility;
312 var op = els.position;
313 var od = els.display;
314 els.visibility = 'hidden';
315 els.position = 'absolute';
317 var oHeight = e.clientHeight || parseInt(e.style.height);
318 var oWidth = e.clientWidth || parseInt(e.style.width);
322 return p == 'height' ? oHeight : oWidth;
327 else if (e.currentStyle)
328 return e.currentStyle[p];
329 else if (document.defaultView && document.defaultView.getComputedStyle) {
330 p = p.replace(/([A-Z])/g,"-$1");
332 var s = document.defaultView.getComputedStyle(e,"");
333 var r = s ? s.getPropertyValue(p) : p;
340 $.clean = function(a) {
342 for ( var i = 0; i < a.length; i++ )
343 if ( a[i].constructor == String ) {
344 var div = document.createElement("div");
345 div.innerHTML = a[i];
346 for ( var j = 0; j < div.childNodes.length; j++ )
347 r[r.length] = div.childNodes[j];
348 } else if ( a[i].length )
349 for ( var j = 0; j < a[i].length; j++ )
350 r[r.length] = a[i][j];
351 else if ( a[i] != null )
353 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
358 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
363 nth: "m[3] - 0 == i",
366 last: "i == r.length - 1",
369 "first-child": "$.sibling(a,0).cur",
370 "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))",
371 "last-child": "$.sibling(a,0,true).cur",
372 "nth-last-child": "$.sibling(a,m[3],true).cur",
373 "first-of-type": "$.ofType(a,0)",
374 "nth-of-type": "$.ofType(a,m[3])",
375 "last-of-type": "$.ofType(a,0,true)",
376 "nth-last-of-type": "$.ofType(a,m[3],true)",
377 "only-of-type": "$.ofType(a) == 1",
378 "only-child": "$.sibling(a).length == 1",
379 parent: "a.childNodes.length > 0",
380 empty: "a.childNodes.length == 0",
381 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
382 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
383 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
384 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
385 enabled: "a.disabled == false",
386 disabled: "a.disabled",
389 // TODO: Write getAttribute helper
390 ".": "$.hasWord(a.className||a.getAttribute('class'),m[2])",
392 "=": "$.attr(a,m[3]) == m[4]",
393 "!=": "$.attr(a,m[3]) != m[4]",
394 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
395 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
396 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
397 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
398 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
399 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
401 "[": "$.Select(m[2],a).length > 0"
406 $.Select = function( t, context ) {
407 context = context || $.context || document;
408 if ( t.constructor != String ) return [t];
410 if ( t.indexOf("//") == 0 ) {
411 context = context.documentElement;
412 t = t.substr(2,t.length);
413 } else if ( t.indexOf("/") == 0 ) {
414 context = context.documentElement;
415 t = t.substr(1,t.length);
416 // FIX Assume the root element is right :(
417 if ( t.indexOf('/') )
418 t = t.substr(t.indexOf('/'),t.length);
425 while ( t.length > 0 && last != t ) {
429 t = $.cleanSpaces(t);
431 var re = new RegExp( "^//", "i" );
432 t = t.replace( re, "" );
434 if ( t.indexOf('..') == 0 || t.indexOf('/..') == 0 ) {
435 if ( t.indexOf('/') == 0 )
436 t = t.substr(1,t.length);
437 r = $.map( ret, function(a){ return a.parentNode; } );
438 t = t.substr(2,t.length);
439 t = $.cleanSpaces(t);
440 } else if ( t.indexOf('>') == 0 || t.indexOf('/') == 0 ) {
441 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
442 t = t.substr(1,t.length);
443 t = $.cleanSpaces(t);
444 } else if ( t.indexOf('+') == 0 ) {
445 r = $.map( ret, function(a){ return $.sibling(a).next; } );
446 t = t.substr(1,t.length);
447 t = $.cleanSpaces(t);
448 } else if ( t.indexOf('~') == 0 ) {
449 r = $.map( ret, function(a){
451 var s = $.sibling(a);
453 for ( var i = s.n; i < s.length; i++ )
457 t = t.substr(1,t.length);
458 t = $.cleanSpaces(t);
459 } else if ( t.indexOf(',') == 0 || t.indexOf('|') == 0 ) {
460 if ( ret[0] == context ) ret.shift();
461 done = $.merge( done, ret );
463 t = " " + t.substr(1,t.length);
465 var re = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
468 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
469 var oid = document.getElementById(m[2]);
470 r = oid ? [oid] : [];
471 t = t.replace( re, "" );
473 if ( m[2] == "" || m[1] == "." ) m[2] = "*";
475 for ( var i = 0; i < ret.length; i++ ) {
480 r = $.merge( $.getAll(o), r );
482 case 'text': case 'radio': case 'checkbox': case 'hidden':
483 case 'button': case 'submit': case 'image': case 'password':
484 case 'reset': case 'file':
485 r = $.merge( $.grep( $.tag(o,"input"),
486 function(a){ return a.type == m[2] }), r );
489 r = $.merge( $.tag(o,"input"), r );
490 r = $.merge( $.tag(o,"select"), r );
491 r = $.merge( $.tag(o,"textarea"), r );
494 r = $.merge( r, $.tag(o,m[2]) );
502 var val = $.filter(t,r);
504 t = $.cleanSpaces(val.t);
507 if ( ret && ret[0] == context ) ret.shift();
508 done = $.merge( done, ret );
512 $.tag = function(a,b){
513 return a && typeof a.getElementsByTagName != "undefined" ?
514 a.getElementsByTagName( b ) : [];
517 $.attr = function(o,a,v){
518 if ( a && a.constructor == String ) {
522 'class': 'className',
525 a = (fix[a] && fix[a].replace && fix[a]) || a;
526 var r = new RegExp("-([a-z])","ig");
527 a = a.replace(r,function(z,b){return b.toUpperCase();});
530 if ( o.setAttribute ) o.setAttribute(a,v);
532 return o[a] || o.getAttribute(a) || '';
536 $.filter = function(t,r,not) {
538 if ( not == false ) var g = function(a,f) {return $.grep(a,f,true);};
540 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
541 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
545 m = ['', '@', m[2], m[1], m[3]];
547 var re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
551 var re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
555 var re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
560 t = t.replace( re, "" );
562 if ( m[1] == ":" && m[2] == "not" )
563 r = $.filter(m[3],r,false).r;
565 if ( $.g[m[1]].constructor == String )
567 else if ( $.g[m[1]][m[2]] )
568 var f = $.g[m[1]][m[2]];
571 eval("f = function(a,i){return " + f + "}");
576 return { r: r, t: t };
579 $.parents = function(a){
581 var c = a.parentNode;
582 while ( c != null && c != document ) {
589 $.cleanSpaces = function(t){return t.replace(/^\s+|\s+$/g, '')};
591 $.ofType = function(a,n,e) {
592 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName});
593 if ( e ) n = t.length - n - 1;
594 return n != null ? t[n] == a : t.length;
597 $.sibling = function(a,n,e) {
599 var tmp = a.parentNode.childNodes;
600 for ( var i = 0; i < tmp.length; i++ ) {
601 if ( tmp[i].nodeType == 1 )
602 type[type.length] = tmp[i];
604 type.n = type.length - 1;
606 if ( e ) n = type.length - n - 1;
607 type.cur = ( type[n] == a );
608 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
609 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
613 $.hasWord = function(e,a) {
614 if ( e == null ) return false;
615 if ( e.className != null ) e = e.className;
616 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e)
619 $.getAll = function(o,r) {
621 var s = o.childNodes;
622 for ( var i = 0; i < s.length; i++ ) {
623 if ( s[i].nodeType == 1 ) {
631 $.merge = function(a,b) {
633 for ( var j = 0; j < b.length; j++ )
636 for ( var i = 0; i < a.length; i++ ) {
638 for ( var j = 0; j < b.length; j++ )
647 $.grep = function(a,f,s) {
650 for ( var i = 0; i < a.length; i++ )
651 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
656 $.map = function(a,f) {
658 for ( var i = 0; i < a.length; i++ ) {
661 if ( t.constructor != Array ) t = [t];
668 // Bind an event to an element
669 // Original by Dean Edwards
670 function addEvent(element, type, handler) {
671 if ( element.location ) element = window; // Ughhhhh....
672 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
673 if (!element.events) element.events = {};
674 var handlers = element.events[type];
676 handlers = element.events[type] = {};
677 if (element["on" + type])
678 handlers[0] = element["on" + type];
680 handlers[handler.$$guid] = handler;
681 element["on" + type] = handleEvent;
685 // Detach an event or set of events from an element
686 function removeEvent(element, type, handler) {
687 if (element.events) {
688 if (type && element.events[type]) {
690 delete element.events[type][handler.$$guid];
692 for ( var i in element.events[type] )
693 delete element.events[type][i];
696 for ( var i in element.events )
697 removeEvent( element, i );
702 function triggerEvent(element,type) {
703 if ( element["on" + type] )
704 element["on" + type]({ type: type });
707 function handleEvent(event) {
708 var returnValue = true;
709 event = event || fixEvent(window.event);
711 for ( var i in this.events[event.type] )
712 handlers[handlers.length] = this.events[event.type][i];
713 for ( var i = 0; i < handlers.length; i++ ) {
715 if ( handlers[i].constructor == Function ) {
716 this.$$handleEvent = handlers[i];
717 if (this.$$handleEvent(event) === false) {
718 event.preventDefault();
719 event.stopPropagation();
728 function fixEvent(event) {
729 event.preventDefault = fixEvent.preventDefault;
730 event.stopPropagation = fixEvent.stopPropagation;
733 fixEvent.preventDefault = function() {
734 this.returnValue = false;
736 fixEvent.stopPropagation = function() {
737 this.cancelBubble = true;
742 $.fn.text = function(e) {
745 for ( var j = 0; j < e.length; j++ ) {
746 for ( var i = 0; i < e[j].childNodes.length; i++ )
747 t += e[j].childNodes[i].nodeType != 1 ?
748 e[j].childNodes[i].nodeValue :
749 $.fn.text(e[j].childNodes[i].childNodes);
754 setTimeout(function(){
755 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
756 throw "Error: You are overwriting jQuery, please include jQuery last.";