2 * JQuery (http://jquery.com/)
3 * By John Resig (http://ejohn.org/)
4 * Under an Attribution, Share Alike License
8 var $a = a || $.context || document;
9 var $c = c && c.$jquery && c.get(0) || c;
11 // Since we're using Prototype's $ function,
12 // be nice and have backwards compatability
13 if ( typeof Prototype != "undefined" ) {
14 if ( $a.constructor == String ) {
15 var re = new RegExp( "[^a-zA-Z0-9_-]" );
17 $c = $c && $c.documentElement || document;
18 if ( $c.getElementsByTagName($a).length == 0 ) {
19 var obj = $c.getElementById($a);
20 if ( obj != null ) return obj;
23 } else if ( $a.constructor == Array ) {
24 return $.map( $a, function(b){
25 if ( b.constructor == String )
26 return document.getElementById(b);
32 // Load Dynamic Function List
37 // The only two getters
38 size: function() {return this.get().length},
40 return i == null ? this.cur : this.cur[i];
44 for ( var i = 0; i < this.size(); i++ )
45 $.apply( this.get(i), f, [i] );
49 return this.each(function(){
58 return h == null && this.size() ?
59 this.get(0).innerHTML : this.set( "innerHTML", h );
62 return h == null && this.size() ?
63 this.get(0).value : this.set( "value", h );
67 return this.each(function(){
70 $.attr(this.style,j,a[j]);
72 $.attr(this.style,a,b);
76 return this.each(function(){
77 var d = $.getCSS(this,"display");
78 if ( d == "none" || d == '' )
85 return this.each(function(){
86 this.style.display = this.$$oldblock ? this.$$oldblock : '';
87 if ( $.getCSS(this,"display") == "none" ) this.style.display = 'block';
91 return this.each(function(){
92 this.$$oldblock = $.getCSS(this,"display");
93 if ( this.$$oldblock == "none" ) this.$$oldblock = 'block';
94 this.style.display = 'none';
97 addClass: function(c) {
98 return this.each(function(){
99 if ($.hasWord(this,c)) return;
100 this.className += ( this.className.length > 0 ? " " : "" ) + c;
103 removeClass: function(c) {
104 return this.each(function(){
105 this.className = c == null ? '' :
106 this.className.replace(
107 new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
111 toggleClass: function(c) {
112 return this.each(function(){
113 if ($.hasWord(this,c))
115 this.className.replace(
116 new RegExp('(\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
118 this.className += ( this.className.length > 0 ? " " : "" ) + c;
122 this.each(function(){this.parentNode.removeChild( this );});
128 var a = $.clean(arguments);
129 return this.each(function(){
130 var b = a[0].cloneNode(true);
131 this.parentNode.insertBefore( b, this );
132 while ( b.firstChild ) b = b.firstChild;
133 b.appendChild( this );
138 var clone = this.size() > 1;
139 var a = $.clean(arguments);
140 return this.each(function(){
142 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
146 appendTo: function() {
148 return this.each(function(){
149 for ( var i = 0; i < a.length; i++ )
150 $(a[i]).append( this );
154 prepend: function() {
155 var clone = this.size() > 1;
156 var a = $.clean(arguments);
157 return this.each(function(){
158 for ( var i = a.length - 1; i >= 0; i-- )
159 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
164 var clone = this.size() > 1;
165 var a = $.clean(arguments);
166 return this.each(function(){
168 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
173 var clone = this.size() > 1;
174 var a = $.clean(arguments);
175 return this.each(function(){
176 for ( var i = a.length - 1; i >= 0; i-- )
177 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
182 return this.each(function(){
183 while ( this.firstChild )
184 this.removeChild( this.firstChild );
188 bind: function(t,f) {
189 return this.each(function(){addEvent(this,t,f);});
191 unbind: function(t,f) {
192 return this.each(function(){removeEvent(this,t,f);});
194 trigger: function(t) {
195 return this.each(function(){triggerEvent(this,t);});
199 var old = [], ret = [];
200 this.each(function(){
201 old[old.length] = this;
202 ret = $.merge( ret, $.Select(t,this) );
213 parent: function(a) {
214 if ( a == null ) a = 1;
215 this.cur = $.map(this.cur,function(d){
216 var b = $.parents(d);
219 else if ( a.constructor == String ) {
220 var c = $.filter(a,b);
221 return c.length > 0 ? c[0] : null;
223 return b.length >= a ? b[a-1] : null;
228 parents: function(a) {
232 filter: function(t) {
233 this.cur = $.filter(t,this.cur).r;
237 this.cur = t.constructor == String ?
238 $.filter(t,this.cur,false).r :
239 $.grep(this.cur,function(a){return a != t;});
243 this.cur = $.merge( this.cur, t.constructor == String ?
244 $.Select(t) : t.constructor == Array ? t : [t] );
248 return $.filter(t,this.cur).r.length > 0;
255 // TODO: Remove need to return this
256 for ( var i in $.fn ) {
257 if ( self[i] != null )
258 self["_"+i] = self[i];
262 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
263 if ( $c ) $a = self.get();
264 for ( var i in self ) {(function(j){
266 if ( $a[j] == null ) {
268 return $.apply(self,self[j],arguments);
279 $.apply = function(o,f,a) {
282 return f.apply( o, a );
285 for (var i = 0; i < a.length; i++)
288 var r = eval('o.$$exec(' + p.join(',') + ')');
294 $.getCSS = function(e,p) {
295 // Adapted from Prototype 1.4.0
296 if ( p == 'height' || p == 'width' ) {
297 if ($.getCSS(e,"display") != 'none')
298 return p == 'height' ?
299 e.offsetHeight || parseInt(e.style.height) :
300 e.offsetWidth || parseInt(e.style.width);
302 var ov = els.visibility;
303 var op = els.position;
304 var od = els.display;
305 els.visibility = 'hidden';
306 els.position = 'absolute';
308 var oHeight = e.clientHeight || parseInt(e.style.height);
309 var oWidth = e.clientWidth || parseInt(e.style.width);
313 return p == 'height' ? oHeight : oWidth;
318 else if (e.currentStyle)
319 return e.currentStyle[p];
320 else if (document.defaultView && document.defaultView.getComputedStyle) {
321 p = p.replace(/([A-Z])/g,"-$1");
323 var s = document.defaultView.getComputedStyle(e,"");
324 var r = s ? s.getPropertyValue(p) : p;
331 $.clean = function(a) {
333 for ( var i = 0; i < a.length; i++ )
334 if ( a[i].constructor == String ) {
335 var div = document.createElement("div");
336 div.innerHTML = a[i];
337 for ( var j = 0; j < div.childNodes.length; j++ )
338 r[r.length] = div.childNodes[j];
339 } else if ( a[i].length )
340 for ( var j = 0; j < a[i].length; j++ )
341 r[r.length] = a[i][j];
342 else if ( a[i] != null )
344 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
349 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
354 nth: "m[3] - 0 == i",
357 last: "i == r.length - 1",
360 "first-child": "$.sibling(a,0).cur",
361 "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))",
362 "last-child": "$.sibling(a,0,true).cur",
363 "nth-last-child": "$.sibling(a,m[3],true).cur",
364 "first-of-type": "$.ofType(a,0)",
365 "nth-of-type": "$.ofType(a,m[3])",
366 "last-of-type": "$.ofType(a,0,true)",
367 "nth-last-of-type": "$.ofType(a,m[3],true)",
368 "only-of-type": "$.ofType(a) == 1",
369 "only-child": "$.sibling(a).length == 1",
370 parent: "a.childNodes.length > 0",
371 empty: "a.childNodes.length == 0",
372 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
373 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
374 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
375 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
376 enabled: "a.disabled == false",
377 disabled: "a.disabled",
380 // TODO: Write getAttribute helper
381 ".": "$.hasWord(a.className||a.getAttribute('class'),m[2])",
383 "=": "$.attr(a,m[3]) == m[4]",
384 "!=": "$.attr(a,m[3]) != m[4]",
385 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
386 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
387 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
388 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
389 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
390 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
392 "[": "$.Select(m[2],a).length > 0"
397 $.Select = function( t, context ) {
398 context = context || $.context || document;
399 if ( t.constructor != String ) return [t];
401 if ( t.indexOf("//") == 0 ) {
402 context = context.documentElement;
403 t = t.substr(2,t.length);
404 } else if ( t.indexOf("/") == 0 ) {
405 context = context.documentElement;
406 t = t.substr(1,t.length);
407 // FIX Assume the root element is right :(
408 if ( t.indexOf('/') )
409 t = t.substr(t.indexOf('/'),t.length);
416 while ( t.length > 0 && last != t ) {
420 t = $.cleanSpaces(t);
422 var re = new RegExp( "^//", "i" );
423 t = t.replace( re, "" );
425 if ( t.indexOf('..') == 0 || t.indexOf('/..') == 0 ) {
426 if ( t.indexOf('/') == 0 )
427 t = t.substr(1,t.length);
428 r = $.map( ret, function(a){ return a.parentNode; } );
429 t = t.substr(2,t.length);
430 t = $.cleanSpaces(t);
431 } else if ( t.indexOf('>') == 0 || t.indexOf('/') == 0 ) {
432 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
433 t = t.substr(1,t.length);
434 t = $.cleanSpaces(t);
435 } else if ( t.indexOf('+') == 0 ) {
436 r = $.map( ret, function(a){ return $.sibling(a).next; } );
437 t = t.substr(1,t.length);
438 t = $.cleanSpaces(t);
439 } else if ( t.indexOf('~') == 0 ) {
440 r = $.map( ret, function(a){
442 var s = $.sibling(a);
444 for ( var i = s.n; i < s.length; i++ )
448 t = t.substr(1,t.length);
449 t = $.cleanSpaces(t);
450 } else if ( t.indexOf(',') == 0 || t.indexOf('|') == 0 ) {
451 if ( ret[0] == context ) ret.shift();
452 done = $.merge( done, ret );
454 t = " " + t.substr(1,t.length);
456 var re = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
459 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
460 var oid = document.getElementById(m[2]);
461 r = oid ? [oid] : [];
462 t = t.replace( re, "" );
464 if ( m[2] == "" || m[1] == "." ) m[2] = "*";
466 for ( var i = 0; i < ret.length; i++ ) {
471 r = $.merge( $.getAll(o), r );
473 case 'text': case 'radio': case 'checkbox': case 'hidden':
474 case 'button': case 'submit': case 'image': case 'password':
475 case 'reset': case 'file':
476 r = $.merge( $.grep( $.tag(o,"input"),
477 function(a){ return a.type == m[2] }), r );
480 r = $.merge( $.tag(o,"input"), r );
481 r = $.merge( $.tag(o,"select"), r );
482 r = $.merge( $.tag(o,"textarea"), r );
485 r = $.merge( r, $.tag(o,m[2]) );
493 var val = $.filter(t,r);
495 t = $.cleanSpaces(val.t);
498 if ( ret && ret[0] == context ) ret.shift();
499 done = $.merge( done, ret );
503 $.tag = function(a,b){
504 return a && typeof a.getElementsByTagName != "undefined" ?
505 a.getElementsByTagName( b ) : [];
508 $.attr = function(o,a,v){
509 if ( a && a.constructor == String ) {
513 'class': 'className',
516 a = (fix[a] && fix[a].replace && fix[a]) || a;
517 var r = new RegExp("-([a-z])","ig");
518 a = a.replace(r,function(z,b){return b.toUpperCase();});
521 if ( o.setAttribute ) o.setAttribute(a,v);
523 return o[a] || o.getAttribute(a) || '';
527 $.filter = function(t,r,not) {
529 if ( not == false ) var g = function(a,f) {return $.grep(a,f,true);};
531 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
532 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
536 m = ['', '@', m[2], m[1], m[3]];
538 var re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
542 var re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
546 var re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
551 t = t.replace( re, "" );
553 if ( m[1] == ":" && m[2] == "not" )
554 r = $.filter(m[3],r,false).r;
556 if ( $.g[m[1]].constructor == String )
558 else if ( $.g[m[1]][m[2]] )
559 var f = $.g[m[1]][m[2]];
562 eval("f = function(a,i){return " + f + "}");
567 return { r: r, t: t };
570 $.parents = function(a){
572 var c = a.parentNode;
573 while ( c != null && c != c.documentElement ) {
580 $.cleanSpaces = function(t){return t.replace(/^\s+|\s+$/g, '')};
582 $.ofType = function(a,n,e) {
583 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName});
584 if ( e ) n = t.length - n - 1;
585 return n != null ? t[n] == a : t.length;
588 $.sibling = function(a,n,e) {
590 var tmp = a.parentNode.childNodes;
591 for ( var i = 0; i < tmp.length; i++ ) {
592 if ( tmp[i].nodeType == 1 )
593 type[type.length] = tmp[i];
595 type.n = type.length - 1;
597 if ( e ) n = type.length - n - 1;
598 type.cur = ( type[n] == a );
599 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
600 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
604 $.hasWord = function(e,a) {
605 if ( e == null ) return false;
606 if ( e.className != null ) e = e.className;
607 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e)
610 $.getAll = function(o,r) {
612 var s = o.childNodes;
613 for ( var i = 0; i < s.length; i++ ) {
614 if ( s[i].nodeType == 1 ) {
622 $.merge = function(a,b) {
624 for ( var j = 0; j < b.length; j++ )
627 for ( var i = 0; i < a.length; i++ ) {
629 for ( var j = 0; j < b.length; j++ )
638 $.grep = function(a,f,s) {
641 for ( var i = 0; i < a.length; i++ )
642 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
647 $.map = function(a,f) {
649 for ( var i = 0; i < a.length; i++ ) {
652 if ( t.constructor != Array ) t = [t];
659 // Bind an event to an element
660 // Original by Dean Edwards
661 function addEvent(element, type, handler) {
662 if ( element.location ) element = window; // Ughhhhh....
663 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
664 if (!element.events) element.events = {};
665 var handlers = element.events[type];
667 handlers = element.events[type] = {};
668 if (element["on" + type])
669 handlers[0] = element["on" + type];
671 handlers[handler.$$guid] = handler;
672 element["on" + type] = handleEvent;
676 // Detach an event or set of events from an element
677 function removeEvent(element, type, handler) {
678 if (element.events) {
679 if (type && element.events[type]) {
681 delete element.events[type][handler.$$guid];
683 for ( var i in element.events[type] )
684 delete element.events[type][i];
687 for ( var i in element.events )
688 removeEvent( element, i );
693 function triggerEvent(element,type) {
694 if ( element["on" + type] )
695 element["on" + type]({ type: type });
698 function handleEvent(event) {
699 var returnValue = true;
700 event = event || fixEvent(window.event);
702 for ( var i in this.events[event.type] )
703 handlers[handlers.length] = this.events[event.type][i];
704 for ( var i = 0; i < handlers.length; i++ ) {
706 if ( handlers[i].constructor == Function ) {
707 this.$$handleEvent = handlers[i];
708 if (this.$$handleEvent(event) === false) {
709 event.preventDefault();
710 event.stopPropagation();
719 function fixEvent(event) {
720 event.preventDefault = fixEvent.preventDefault;
721 event.stopPropagation = fixEvent.stopPropagation;
724 fixEvent.preventDefault = function() {
725 this.returnValue = false;
727 fixEvent.stopPropagation = function() {
728 this.cancelBubble = true;
733 $.fn.text = function(e) {
736 for ( var j = 0; j < e.length; j++ ) {
737 for ( var i = 0; i < e[j].childNodes.length; i++ )
738 t += e[j].childNodes[i].nodeType != 1 ?
739 e[j].childNodes[i].nodeValue :
740 $.fn.text(e[j].childNodes[i].childNodes);
745 setTimeout(function(){
746 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
747 throw "Error: You are overwriting jQuery, please include jQuery last.";