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 a.constructor != String || b ?
74 $.attr(this.style,j,a[j]);
76 $.attr(this.style,a,b);
77 }) : $.css( this.get(0), a );
80 return this.each(function(){
81 var d = $.getCSS(this,"display");
82 if ( d == "none" || d == '' )
89 return this.each(function(){
90 this.style.display = this.$$oldblock ? this.$$oldblock : '';
91 if ( $.getCSS(this,"display") == "none" ) this.style.display = 'block';
95 return this.each(function(){
96 this.$$oldblock = $.getCSS(this,"display");
97 if ( this.$$oldblock == "none" ) this.$$oldblock = 'block';
98 this.style.display = 'none';
101 addClass: function(c) {
102 return this.each(function(){
103 if ($.hasWord(this,c)) return;
104 this.className += ( this.className.length > 0 ? " " : "" ) + c;
107 removeClass: function(c) {
108 return this.each(function(){
109 this.className = c == null ? '' :
110 this.className.replace(
111 new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
115 toggleClass: function(c) {
116 return this.each(function(){
117 if ($.hasWord(this,c))
119 this.className.replace(
120 new RegExp('(\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
122 this.className += ( this.className.length > 0 ? " " : "" ) + c;
126 this.each(function(){this.parentNode.removeChild( this );});
132 var a = $.clean(arguments);
133 return this.each(function(){
134 var b = a[0].cloneNode(true);
135 this.parentNode.insertBefore( b, this );
136 while ( b.firstChild ) b = b.firstChild;
137 b.appendChild( this );
142 var clone = this.size() > 1;
143 var a = $.clean(arguments);
144 return this.each(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.each(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(){addEvent(this,t,f);});
195 unbind: function(t,f) {
196 return this.each(function(){removeEvent(this,t,f);});
198 trigger: function(t) {
199 return this.each(function(){triggerEvent(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,function(d){
221 if ( a ) this.cur = $.filter(a,this.cur).r;
225 parents: function(a) {
226 this.cur = $.map(this.cur,$.parents);
227 if ( a ) this.cur = $.filter(a,this.cur).r;
231 siblings: function(a) {
232 // Incorrect, need to exclude current element
233 this.cur = $.map(this.cur,$.sibling);
234 if ( a ) this.cur = $.filter(a,this.cur).r;
238 filter: function(t) {
239 this.cur = $.filter(t,this.cur).r;
243 this.cur = t.constructor == String ?
244 $.filter(t,this.cur,false).r :
245 $.grep(this.cur,function(a){return a != t;});
249 this.cur = $.merge( this.cur, t.constructor == String ?
250 $.Select(t) : t.constructor == Array ? t : [t] );
254 return $.filter(t,this.cur).r.length > 0;
261 // TODO: Remove need to return this
262 for ( var i in $.fn ) {
263 if ( self[i] != null )
264 self["_"+i] = self[i];
268 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
269 if ( $c ) $a = self.get();
270 for ( var i in self ) {(function(j){
272 if ( $a[j] == null ) {
274 return $.apply(self,self[j],arguments);
285 $.apply = function(o,f,a) {
288 return f.apply( o, a );
291 for (var i = 0; i < a.length; i++)
294 var r = eval('o.$$exec(' + p.join(',') + ')');
300 $.getCSS = function(e,p) {
301 // Adapted from Prototype 1.4.0
302 if ( p == 'height' || p == 'width' ) {
303 if ($.getCSS(e,"display") != 'none')
304 return p == 'height' ?
305 e.offsetHeight || parseInt(e.style.height) :
306 e.offsetWidth || parseInt(e.style.width);
308 var ov = els.visibility;
309 var op = els.position;
310 var od = els.display;
311 els.visibility = 'hidden';
312 els.position = 'absolute';
314 var oHeight = e.clientHeight || parseInt(e.style.height);
315 var oWidth = e.clientWidth || parseInt(e.style.width);
319 return p == 'height' ? oHeight : oWidth;
324 else if (e.currentStyle)
325 return e.currentStyle[p];
326 else if (document.defaultView && document.defaultView.getComputedStyle) {
327 p = p.replace(/([A-Z])/g,"-$1");
329 var s = document.defaultView.getComputedStyle(e,"");
330 var r = s ? s.getPropertyValue(p) : p;
337 $.clean = function(a) {
339 for ( var i = 0; i < a.length; i++ )
340 if ( a[i].constructor == String ) {
341 var div = document.createElement("div");
342 div.innerHTML = a[i];
343 for ( var j = 0; j < div.childNodes.length; j++ )
344 r[r.length] = div.childNodes[j];
345 } else if ( a[i].length )
346 for ( var j = 0; j < a[i].length; j++ )
347 r[r.length] = a[i][j];
348 else if ( a[i] != null )
350 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
355 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
360 nth: "m[3] - 0 == i",
363 last: "i == r.length - 1",
366 "first-child": "$.sibling(a,0).cur",
367 "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))",
368 "last-child": "$.sibling(a,0,true).cur",
369 "nth-last-child": "$.sibling(a,m[3],true).cur",
370 "first-of-type": "$.ofType(a,0)",
371 "nth-of-type": "$.ofType(a,m[3])",
372 "last-of-type": "$.ofType(a,0,true)",
373 "nth-last-of-type": "$.ofType(a,m[3],true)",
374 "only-of-type": "$.ofType(a) == 1",
375 "only-child": "$.sibling(a).length == 1",
376 parent: "a.childNodes.length > 0",
377 empty: "a.childNodes.length == 0",
378 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
379 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
380 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
381 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
382 enabled: "a.disabled == false",
383 disabled: "a.disabled",
386 // TODO: Write getAttribute helper
387 ".": "$.hasWord(a,m[2])",
389 "=": "$.attr(a,m[3]) == m[4]",
390 "!=": "$.attr(a,m[3]) != m[4]",
391 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
392 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
393 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
394 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
395 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
396 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
398 "[": "$.Select(m[2],a).length > 0"
403 $.Select = function( t, context ) {
404 context = context || $.context || document;
405 if ( t.constructor != String ) return [t];
407 if ( t.indexOf("//") == 0 ) {
408 context = context.documentElement;
409 t = t.substr(2,t.length);
410 } else if ( t.indexOf("/") == 0 ) {
411 context = context.documentElement;
412 t = t.substr(1,t.length);
413 // FIX Assume the root element is right :(
414 if ( t.indexOf('/') )
415 t = t.substr(t.indexOf('/'),t.length);
422 while ( t.length > 0 && last != t ) {
426 t = $.cleanSpaces(t);
428 var re = new RegExp( "^//", "i" );
429 t = t.replace( re, "" );
431 if ( t.indexOf('..') == 0 || t.indexOf('/..') == 0 ) {
432 if ( t.indexOf('/') == 0 )
433 t = t.substr(1,t.length);
434 r = $.map( ret, function(a){ return a.parentNode; } );
435 t = t.substr(2,t.length);
436 t = $.cleanSpaces(t);
437 } else if ( t.indexOf('>') == 0 || t.indexOf('/') == 0 ) {
438 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
439 t = t.substr(1,t.length);
440 t = $.cleanSpaces(t);
441 } else if ( t.indexOf('+') == 0 ) {
442 r = $.map( ret, function(a){ return $.sibling(a).next; } );
443 t = t.substr(1,t.length);
444 t = $.cleanSpaces(t);
445 } else if ( t.indexOf('~') == 0 ) {
446 r = $.map( ret, function(a){
448 var s = $.sibling(a);
450 for ( var i = s.n; i < s.length; i++ )
454 t = t.substr(1,t.length);
455 t = $.cleanSpaces(t);
456 } else if ( t.indexOf(',') == 0 || t.indexOf('|') == 0 ) {
457 if ( ret[0] == context ) ret.shift();
458 done = $.merge( done, ret );
460 t = " " + t.substr(1,t.length);
462 var re = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
465 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
466 var oid = document.getElementById(m[2]);
467 r = oid ? [oid] : [];
468 t = t.replace( re, "" );
470 if ( m[2] == "" || m[1] == "." ) m[2] = "*";
472 for ( var i = 0; i < ret.length; i++ ) {
477 r = $.merge( $.getAll(o), r );
479 case 'text': case 'radio': case 'checkbox': case 'hidden':
480 case 'button': case 'submit': case 'image': case 'password':
481 case 'reset': case 'file':
482 r = $.merge( $.grep( $.tag(o,"input"),
483 function(a){ return a.type == m[2] }), r );
486 r = $.merge( $.tag(o,"input"), r );
487 r = $.merge( $.tag(o,"select"), r );
488 r = $.merge( $.tag(o,"textarea"), r );
491 r = $.merge( r, $.tag(o,m[2]) );
499 var val = $.filter(t,r);
501 t = $.cleanSpaces(val.t);
504 if ( ret && ret[0] == context ) ret.shift();
505 done = $.merge( done, ret );
509 $.tag = function(a,b){
510 return a && typeof a.getElementsByTagName != "undefined" ?
511 a.getElementsByTagName( b ) : [];
514 $.attr = function(o,a,v){
515 if ( a && a.constructor == String ) {
519 'class': 'className',
522 a = (fix[a] && fix[a].replace && fix[a]) || a;
523 var r = new RegExp("-([a-z])","ig");
524 a = a.replace(r,function(z,b){return b.toUpperCase();});
527 if ( o.setAttribute ) o.setAttribute(a,v);
529 return o[a] || o.getAttribute(a) || '';
533 $.filter = function(t,r,not) {
535 if ( not == false ) var g = function(a,f) {return $.grep(a,f,true);};
537 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
538 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
542 m = ['', '@', m[2], m[1], m[3]];
544 var re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
548 var re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
552 var re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
557 t = t.replace( re, "" );
559 if ( m[1] == ":" && m[2] == "not" )
560 r = $.filter(m[3],r,false).r;
562 if ( $.g[m[1]].constructor == String )
564 else if ( $.g[m[1]][m[2]] )
565 var f = $.g[m[1]][m[2]];
568 eval("f = function(a,i){return " + f + "}");
573 return { r: r, t: t };
576 $.parents = function(a){
578 var c = a.parentNode;
579 while ( c != null && c != document ) {
586 $.cleanSpaces = function(t){
587 return t.replace(/^\s+|\s+$/g, '')
590 $.ofType = function(a,n,e) {
591 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName});
592 if ( e ) n = t.length - n - 1;
593 return n != null ? t[n] == a : t.length;
596 $.sibling = function(a,n,e) {
598 var tmp = a.parentNode.childNodes;
599 for ( var i = 0; i < tmp.length; i++ ) {
600 if ( tmp[i].nodeType == 1 )
601 type[type.length] = tmp[i];
603 type.n = type.length - 1;
605 if ( e ) n = type.length - n - 1;
606 type.cur = ( type[n] == a );
607 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
608 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
612 $.hasWord = function(e,a) {
613 if ( e == null ) return false;
614 if ( e.className != null ) e = e.className;
615 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e)
618 $.getAll = function(o,r) {
620 var s = o.childNodes;
621 for ( var i = 0; i < s.length; i++ ) {
622 if ( s[i].nodeType == 1 ) {
630 $.merge = function(a,b) {
632 for ( var j = 0; j < b.length; j++ )
635 for ( var i = 0; i < a.length; i++ ) {
637 for ( var j = 0; j < b.length; j++ )
646 $.grep = function(a,f,s) {
649 for ( var i = 0; i < a.length; i++ )
650 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
655 $.map = function(a,f) {
657 for ( var i = 0; i < a.length; i++ ) {
660 if ( t.constructor != Array ) t = [t];
667 // Bind an event to an element
668 // Original by Dean Edwards
669 function addEvent(element, type, handler) {
670 if ( element.location ) element = window; // Ughhhhh....
671 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
672 if (!element.events) element.events = {};
673 var handlers = element.events[type];
675 handlers = element.events[type] = {};
676 if (element["on" + type])
677 handlers[0] = element["on" + type];
679 handlers[handler.$$guid] = handler;
680 element["on" + type] = handleEvent;
684 // Detach an event or set of events from an element
685 function removeEvent(element, type, handler) {
686 if (element.events) {
687 if (type && element.events[type]) {
689 delete element.events[type][handler.$$guid];
691 for ( var i in element.events[type] )
692 delete element.events[type][i];
695 for ( var i in element.events )
696 removeEvent( element, i );
701 function triggerEvent(element,type,data) {
702 data = data || [{ type: type }];
703 if ( element && element["on" + type] )
704 $.apply( element, element["on" + type], data );
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.";