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(){
144 for ( var i = 0; i < a.length; i++ )
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(){
170 for ( var i = 0; i < a.length; i++ )
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 filter: function(t) {
238 this.cur = $.filter(t,this.cur).r;
242 this.cur = t.constructor == String ?
243 $.filter(t,this.cur,false).r :
244 $.grep(this.cur,function(a){return a != t;});
248 this.cur = $.merge( this.cur, t.constructor == String ?
249 $.Select(t) : t.constructor == Array ? t : [t] );
253 return $.filter(t,this.cur).r.length > 0;
260 // TODO: Remove need to return this
261 for ( var i in $.fn ) {
262 if ( self[i] != null )
263 self["_"+i] = self[i];
267 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
268 if ( $c ) $a = self.get();
269 for ( var i in self ) {(function(j){
271 if ( $a[j] == null ) {
273 return $.apply(self,self[j],arguments);
284 $.apply = function(o,f,a) {
287 return f.apply( o, a );
290 for (var i = 0; i < a.length; i++)
293 var r = eval('o.$$exec(' + p.join(',') + ')');
299 $.getCSS = function(e,p) {
300 // Adapted from Prototype 1.4.0
301 if ( p == 'height' || p == 'width' ) {
302 if ($.getCSS(e,"display") != 'none')
303 return p == 'height' ?
304 e.offsetHeight || parseInt(e.style.height) :
305 e.offsetWidth || parseInt(e.style.width);
307 var ov = els.visibility;
308 var op = els.position;
309 var od = els.display;
310 els.visibility = 'hidden';
311 els.position = 'absolute';
313 var oHeight = e.clientHeight || parseInt(e.style.height);
314 var oWidth = e.clientWidth || parseInt(e.style.width);
318 return p == 'height' ? oHeight : oWidth;
323 else if (e.currentStyle)
324 return e.currentStyle[p];
325 else if (document.defaultView && document.defaultView.getComputedStyle) {
326 p = p.replace(/([A-Z])/g,"-$1");
328 var s = document.defaultView.getComputedStyle(e,"");
329 var r = s ? s.getPropertyValue(p) : p;
336 $.clean = function(a) {
338 for ( var i = 0; i < a.length; i++ )
339 if ( a[i].constructor == String ) {
340 var div = document.createElement("div");
341 div.innerHTML = a[i];
342 for ( var j = 0; j < div.childNodes.length; j++ )
343 r[r.length] = div.childNodes[j];
344 } else if ( a[i].length )
345 for ( var j = 0; j < a[i].length; j++ )
346 r[r.length] = a[i][j];
347 else if ( a[i] != null )
349 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
354 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
359 nth: "m[3] - 0 == i",
362 last: "i == r.length - 1",
365 "first-child": "$.sibling(a,0).cur",
366 "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))",
367 "last-child": "$.sibling(a,0,true).cur",
368 "nth-last-child": "$.sibling(a,m[3],true).cur",
369 "first-of-type": "$.ofType(a,0)",
370 "nth-of-type": "$.ofType(a,m[3])",
371 "last-of-type": "$.ofType(a,0,true)",
372 "nth-last-of-type": "$.ofType(a,m[3],true)",
373 "only-of-type": "$.ofType(a) == 1",
374 "only-child": "$.sibling(a).length == 1",
375 parent: "a.childNodes.length > 0",
376 empty: "a.childNodes.length == 0",
377 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
378 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
379 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
380 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
381 enabled: "a.disabled == false",
382 disabled: "a.disabled",
385 // TODO: Write getAttribute helper
386 ".": "$.hasWord(a.className||a.getAttribute('class'),m[2])",
388 "=": "$.attr(a,m[3]) == m[4]",
389 "!=": "$.attr(a,m[3]) != m[4]",
390 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
391 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
392 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
393 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
394 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
395 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
397 "[": "$.Select(m[2],a).length > 0"
402 $.Select = function( t, context ) {
403 context = context || $.context || document;
404 if ( t.constructor != String ) return [t];
406 if ( t.indexOf("//") == 0 ) {
407 context = context.documentElement;
408 t = t.substr(2,t.length);
409 } else if ( t.indexOf("/") == 0 ) {
410 context = context.documentElement;
411 t = t.substr(1,t.length);
412 // FIX Assume the root element is right :(
413 if ( t.indexOf('/') )
414 t = t.substr(t.indexOf('/'),t.length);
421 while ( t.length > 0 && last != t ) {
425 t = $.cleanSpaces(t);
427 var re = new RegExp( "^//", "i" );
428 t = t.replace( re, "" );
430 if ( t.indexOf('..') == 0 || t.indexOf('/..') == 0 ) {
431 if ( t.indexOf('/') == 0 )
432 t = t.substr(1,t.length);
433 r = $.map( ret, function(a){ return a.parentNode; } );
434 t = t.substr(2,t.length);
435 t = $.cleanSpaces(t);
436 } else if ( t.indexOf('>') == 0 || t.indexOf('/') == 0 ) {
437 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
438 t = t.substr(1,t.length);
439 t = $.cleanSpaces(t);
440 } else if ( t.indexOf('+') == 0 ) {
441 r = $.map( ret, function(a){ return $.sibling(a).next; } );
442 t = t.substr(1,t.length);
443 t = $.cleanSpaces(t);
444 } else if ( t.indexOf('~') == 0 ) {
445 r = $.map( ret, function(a){
447 var s = $.sibling(a);
449 for ( var i = s.n; i < s.length; i++ )
453 t = t.substr(1,t.length);
454 t = $.cleanSpaces(t);
455 } else if ( t.indexOf(',') == 0 || t.indexOf('|') == 0 ) {
456 if ( ret[0] == context ) ret.shift();
457 done = $.merge( done, ret );
459 t = " " + t.substr(1,t.length);
461 var re = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
464 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
465 var oid = document.getElementById(m[2]);
466 r = oid ? [oid] : [];
467 t = t.replace( re, "" );
469 if ( m[2] == "" || m[1] == "." ) m[2] = "*";
471 for ( var i = 0; i < ret.length; i++ ) {
476 r = $.merge( $.getAll(o), r );
478 case 'text': case 'radio': case 'checkbox': case 'hidden':
479 case 'button': case 'submit': case 'image': case 'password':
480 case 'reset': case 'file':
481 r = $.merge( $.grep( $.tag(o,"input"),
482 function(a){ return a.type == m[2] }), r );
485 r = $.merge( $.tag(o,"input"), r );
486 r = $.merge( $.tag(o,"select"), r );
487 r = $.merge( $.tag(o,"textarea"), r );
490 r = $.merge( r, $.tag(o,m[2]) );
498 var val = $.filter(t,r);
500 t = $.cleanSpaces(val.t);
503 if ( ret && ret[0] == context ) ret.shift();
504 done = $.merge( done, ret );
508 $.tag = function(a,b){
509 return a && typeof a.getElementsByTagName != "undefined" ?
510 a.getElementsByTagName( b ) : [];
513 $.attr = function(o,a,v){
514 if ( a && a.constructor == String ) {
518 'class': 'className',
521 a = (fix[a] && fix[a].replace && fix[a]) || a;
522 var r = new RegExp("-([a-z])","ig");
523 a = a.replace(r,function(z,b){return b.toUpperCase();});
526 if ( o.setAttribute ) o.setAttribute(a,v);
528 return o[a] || o.getAttribute(a) || '';
532 $.filter = function(t,r,not) {
534 if ( not == false ) var g = function(a,f) {return $.grep(a,f,true);};
536 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
537 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
541 m = ['', '@', m[2], m[1], m[3]];
543 var re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
547 var re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
551 var re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
556 t = t.replace( re, "" );
558 if ( m[1] == ":" && m[2] == "not" )
559 r = $.filter(m[3],r,false).r;
561 if ( $.g[m[1]].constructor == String )
563 else if ( $.g[m[1]][m[2]] )
564 var f = $.g[m[1]][m[2]];
567 eval("f = function(a,i){return " + f + "}");
572 return { r: r, t: t };
575 $.parents = function(a){
577 var c = a.parentNode;
578 while ( c != null && c != document ) {
585 $.cleanSpaces = function(t){
586 return t.replace(/^\s+|\s+$/g, '')
589 $.ofType = function(a,n,e) {
590 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName});
591 if ( e ) n = t.length - n - 1;
592 return n != null ? t[n] == a : t.length;
595 $.sibling = function(a,n,e) {
597 var tmp = a.parentNode.childNodes;
598 for ( var i = 0; i < tmp.length; i++ ) {
599 if ( tmp[i].nodeType == 1 )
600 type[type.length] = tmp[i];
602 type.n = type.length - 1;
604 if ( e ) n = type.length - n - 1;
605 type.cur = ( type[n] == a );
606 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
607 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
611 $.hasWord = function(e,a) {
612 if ( e == null ) return false;
613 if ( e.className != null ) e = e.className;
614 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e)
617 $.getAll = function(o,r) {
619 var s = o.childNodes;
620 for ( var i = 0; i < s.length; i++ ) {
621 if ( s[i].nodeType == 1 ) {
629 $.merge = function(a,b) {
631 for ( var j = 0; j < b.length; j++ )
634 for ( var i = 0; i < a.length; i++ ) {
636 for ( var j = 0; j < b.length; j++ )
645 $.grep = function(a,f,s) {
648 for ( var i = 0; i < a.length; i++ )
649 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
654 $.map = function(a,f) {
656 for ( var i = 0; i < a.length; i++ ) {
659 if ( t.constructor != Array ) t = [t];
666 // Bind an event to an element
667 // Original by Dean Edwards
668 function addEvent(element, type, handler) {
669 if ( element.location ) element = window; // Ughhhhh....
670 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
671 if (!element.events) element.events = {};
672 var handlers = element.events[type];
674 handlers = element.events[type] = {};
675 if (element["on" + type])
676 handlers[0] = element["on" + type];
678 handlers[handler.$$guid] = handler;
679 element["on" + type] = handleEvent;
683 // Detach an event or set of events from an element
684 function removeEvent(element, type, handler) {
685 if (element.events) {
686 if (type && element.events[type]) {
688 delete element.events[type][handler.$$guid];
690 for ( var i in element.events[type] )
691 delete element.events[type][i];
694 for ( var i in element.events )
695 removeEvent( element, i );
700 function triggerEvent(element,type) {
701 if ( element["on" + type] )
702 element["on" + type]({ type: type });
705 function handleEvent(event) {
706 var returnValue = true;
707 event = event || fixEvent(window.event);
709 for ( var i in this.events[event.type] )
710 handlers[handlers.length] = this.events[event.type][i];
711 for ( var i = 0; i < handlers.length; i++ ) {
713 if ( handlers[i].constructor == Function ) {
714 this.$$handleEvent = handlers[i];
715 if (this.$$handleEvent(event) === false) {
716 event.preventDefault();
717 event.stopPropagation();
726 function fixEvent(event) {
727 event.preventDefault = fixEvent.preventDefault;
728 event.stopPropagation = fixEvent.stopPropagation;
731 fixEvent.preventDefault = function() {
732 this.returnValue = false;
734 fixEvent.stopPropagation = function() {
735 this.cancelBubble = true;
740 $.fn.text = function(e) {
743 for ( var j = 0; j < e.length; j++ ) {
744 for ( var i = 0; i < e[j].childNodes.length; i++ )
745 t += e[j].childNodes[i].nodeType != 1 ?
746 e[j].childNodes[i].nodeValue :
747 $.fn.text(e[j].childNodes[i].childNodes);
752 setTimeout(function(){
753 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
754 throw "Error: You are overwriting jQuery, please include jQuery last.";