4 * Copyright (c) 2006 John Resig (ejohn.org)
5 * Licensed under the MIT License:
6 * http://www.opensource.org/licenses/mit-license.php
13 var $a = a || $.context || document;
14 var $c = c && c.$jquery && c.get(0) || c;
16 // Since we're using Prototype's $ function,
17 // be nice and have backwards compatability
18 if ( typeof Prototype != "undefined" ) {
19 if ( $a.constructor == String ) {
20 var re = new RegExp( "[^a-zA-Z0-9_-]" );
22 $c = $c && $c.documentElement || document;
23 if ( $c.getElementsByTagName($a).length === 0 ) {
24 var obj = $c.getElementById($a);
25 if ( obj !== null ) { return obj; }
28 } else if ( $a.constructor == Array ) {
29 return $.map( $a, function(b){
30 if ( b.constructor == String ) {
31 return document.getElementById(b);
38 // Load Dynamic Function List
43 // The only two getters
44 size: function() {return this.get().length;},
46 return i === null ? this.cur : this.cur[i];
50 for ( var i = 0; i < this.size(); i++ ) {
51 $.apply( this.get(i), f, [i] );
56 return this.each(function(){
67 return h === null && this.size() ?
68 this.get(0).innerHTML : this.set( "innerHTML", h );
71 return h === null && this.size() ?
72 this.get(0).value : this.set( "value", h );
76 return a.constructor != String || b ?
80 $.attr(this.style,j,a[j]);
83 $.attr(this.style,a,b);
85 }) : $.css( this.get(0), a );
88 return this.each(function(){
89 var d = $.getCSS(this,"display");
90 if ( d == "none" || d === '' ) {
98 return this.each(function(){
99 this.style.display = this.$$oldblock ? this.$$oldblock : '';
100 if ( $.getCSS(this,"display") == "none" ) {
101 this.style.display = 'block';
106 return this.each(function(){
107 this.$$oldblock = $.getCSS(this,"display");
108 if ( this.$$oldblock == "none" ) {
109 this.$$oldblock = 'block';
111 this.style.display = 'none';
114 addClass: function(c) {
115 return this.each(function(){
116 if ($.hasWord(this,c)) {
119 this.className += ( this.className.length > 0 ? " " : "" ) + c;
122 removeClass: function(c) {
123 return this.each(function(){
124 this.className = c === null ? '' :
125 this.className.replace(
126 new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
130 toggleClass: function(c) {
131 return this.each(function(){
132 if ($.hasWord(this,c)) {
134 this.className.replace(
135 new RegExp('(\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
137 this.className += ( this.className.length > 0 ? " " : "" ) + c;
142 this.each(function(){this.parentNode.removeChild( this );});
148 var a = $.clean(arguments);
149 return this.each(function(){
150 var b = a[0].cloneNode(true);
151 this.parentNode.insertBefore( b, this );
152 while ( b.firstChild ) {
155 b.appendChild( this );
160 var clone = this.size() > 1;
161 var a = $.clean(arguments);
162 return this.each(function(){
163 for ( var i = 0; i < a.length; i++ ) {
164 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
169 appendTo: function() {
171 return this.each(function(){
172 for ( var i = 0; i < a.length; i++ ) {
173 $(a[i]).append( this );
178 prepend: function() {
179 var clone = this.size() > 1;
180 var a = $.clean(arguments);
181 return this.each(function(){
182 for ( var i = a.length - 1; i >= 0; i-- ) {
183 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
189 var clone = this.size() > 1;
190 var a = $.clean(arguments);
191 return this.each(function(){
192 for ( var i = 0; i < a.length; i++ ) {
193 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
199 var clone = this.size() > 1;
200 var a = $.clean(arguments);
201 return this.each(function(){
202 for ( var i = a.length - 1; i >= 0; i-- ) {
203 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
209 return this.each(function(){
210 while ( this.firstChild ) {
211 this.removeChild( this.firstChild );
216 bind: function(t,f) {
217 return this.each(function(){addEvent(this,t,f);});
219 unbind: function(t,f) {
220 return this.each(function(){removeEvent(this,t,f);});
222 trigger: function(t) {
223 return this.each(function(){triggerEvent(this,t);});
227 var old = [], ret = [];
228 this.each(function(){
229 old[old.length] = this;
230 ret = $.merge( ret, $.Select(t,this) );
241 parent: function(a) {
242 this.cur = $.map(this.cur,function(d){
246 this.cur = $.filter(a,this.cur).r;
251 parents: function(a) {
252 this.cur = $.map(this.cur,$.parents);
254 this.cur = $.filter(a,this.cur).r;
259 siblings: function(a) {
260 // Incorrect, need to exclude current element
261 this.cur = $.map(this.cur,$.sibling);
263 this.cur = $.filter(a,this.cur).r;
268 filter: function(t) {
269 this.cur = $.filter(t,this.cur).r;
273 this.cur = t.constructor == String ?
274 $.filter(t,this.cur,false).r :
275 $.grep(this.cur,function(a){return a != t;});
279 this.cur = $.merge( this.cur, t.constructor == String ?
280 $.Select(t) : t.constructor == Array ? t : [t] );
284 return $.filter(t,this.cur).r.length > 0;
291 // TODO: Remove need to return this
292 for ( var i in $.fn ) {
293 if ( self[i] !== null ) {
294 self["_"+i] = self[i];
299 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
303 for ( var k in self ) {(function(j){
305 if ( $a[j] === null ) {
307 return $.apply(self,self[j],arguments);
318 $.apply = function(o,f,a) {
321 return f.apply( o, a );
324 for (var i = 0; i < a.length; i++) {
328 var r = eval('o.$$exec(' + p.join(',') + ')');
334 $.getCSS = function(e,p) {
335 // Adapted from Prototype 1.4.0
336 if ( p == 'height' || p == 'width' ) {
337 if ($.getCSS(e,"display") != 'none') {
338 return p == 'height' ?
339 e.offsetHeight || parseInt(e.style.height,10) :
340 e.offsetWidth || parseInt(e.style.width,10);
343 var ov = els.visibility;
344 var op = els.position;
345 var od = els.display;
346 els.visibility = 'hidden';
347 els.position = 'absolute';
349 var oHeight = e.clientHeight || parseInt(e.style.height,10);
350 var oWidth = e.clientWidth || parseInt(e.style.width,10);
354 return p == 'height' ? oHeight : oWidth;
359 } else if (e.currentStyle) {
360 return e.currentStyle[p];
361 } else if (document.defaultView && document.defaultView.getComputedStyle) {
362 p = p.replace(/([A-Z])/g,"-$1");
364 var s = document.defaultView.getComputedStyle(e,"");
365 var r = s ? s.getPropertyValue(p) : p;
373 $.clean = function(a) {
375 for ( var i = 0; i < a.length; i++ ) {
376 if ( a[i].constructor == String ) {
377 var div = document.createElement("div");
378 div.innerHTML = a[i];
379 for ( var j = 0; j < div.childNodes.length; j++ ) {
380 r[r.length] = div.childNodes[j];
382 } else if ( a[i].length ) {
383 for ( var k = 0; k < a[i].length; k++ ) {
384 r[r.length] = a[i][k];
386 } else if ( a[i] !== null ) {
388 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
395 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
400 nth: "m[3] - 0 == i",
403 last: "i == r.length - 1",
406 "first-child": "$.sibling(a,0).cur",
407 "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))",
408 "last-child": "$.sibling(a,0,true).cur",
409 "nth-last-child": "$.sibling(a,m[3],true).cur",
410 "first-of-type": "$.ofType(a,0)",
411 "nth-of-type": "$.ofType(a,m[3])",
412 "last-of-type": "$.ofType(a,0,true)",
413 "nth-last-of-type": "$.ofType(a,m[3],true)",
414 "only-of-type": "$.ofType(a) == 1",
415 "only-child": "$.sibling(a).length == 1",
416 parent: "a.childNodes.length > 0",
417 empty: "a.childNodes.length == 0",
418 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
419 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
420 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
421 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
422 enabled: "a.disabled == false",
423 disabled: "a.disabled",
426 // TODO: Write getAttribute helper
427 ".": "$.hasWord(a,m[2])",
429 "=": "$.attr(a,m[3]) == m[4]",
430 "!=": "$.attr(a,m[3]) != m[4]",
431 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
432 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
433 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
434 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
435 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
436 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
438 "[": "$.Select(m[2],a).length > 0"
443 $.Select = function( t, context ) {
444 context = context || $.context || document;
445 if ( t.constructor != String ) {
449 if ( t.indexOf("//") === 0 ) {
450 context = context.documentElement;
451 t = t.substr(2,t.length);
452 } else if ( t.indexOf("/") === 0 ) {
453 context = context.documentElement;
454 t = t.substr(1,t.length);
455 // FIX Assume the root element is right :(
456 if ( t.indexOf('/') ) {
457 t = t.substr(t.indexOf('/'),t.length);
465 while ( t.length > 0 && last != t ) {
469 t = $.cleanSpaces(t);
471 var re = new RegExp( "^//", "i" );
472 t = t.replace( re, "" );
474 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
475 if ( t.indexOf('/') === 0 ) {
476 t = t.substr(1,t.length);
478 r = $.map( ret, function(a){ return a.parentNode; } );
479 t = t.substr(2,t.length);
480 t = $.cleanSpaces(t);
481 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
482 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
483 t = t.substr(1,t.length);
484 t = $.cleanSpaces(t);
485 } else if ( t.indexOf('+') === 0 ) {
486 r = $.map( ret, function(a){ return $.sibling(a).next; } );
487 t = t.substr(1,t.length);
488 t = $.cleanSpaces(t);
489 } else if ( t.indexOf('~') === 0 ) {
490 r = $.map( ret, function(a){
492 var s = $.sibling(a);
494 for ( var i = s.n; i < s.length; i++ ) {
500 t = t.substr(1,t.length);
501 t = $.cleanSpaces(t);
502 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
503 if ( ret[0] == context ) { ret.shift(); }
504 done = $.merge( done, ret );
506 t = " " + t.substr(1,t.length);
508 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
511 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
512 var oid = document.getElementById(m[2]);
513 r = oid ? [oid] : [];
514 t = t.replace( re, "" );
516 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
518 for ( var i = 0; i < ret.length; i++ ) {
523 r = $.merge( $.getAll(o), r );
525 case 'text': case 'radio': case 'checkbox': case 'hidden':
526 case 'button': case 'submit': case 'image': case 'password':
527 case 'reset': case 'file':
528 r = $.merge( $.grep( $.tag(o,"input"),
529 function(a){ return a.type == m[2]; }), r );
532 r = $.merge( $.tag(o,"input"), r );
533 r = $.merge( $.tag(o,"select"), r );
534 r = $.merge( $.tag(o,"textarea"), r );
537 r = $.merge( r, $.tag(o,m[2]) );
545 var val = $.filter(t,r);
547 t = $.cleanSpaces(val.t);
550 if ( ret && ret[0] == context ) { ret.shift(); }
551 done = $.merge( done, ret );
555 $.tag = function(a,b){
556 return a && typeof a.getElementsByTagName != "undefined" ?
557 a.getElementsByTagName( b ) : [];
560 $.attr = function(o,a,v){
561 if ( a && a.constructor == String ) {
565 'class': 'className',
568 a = (fix[a] && fix[a].replace && fix[a]) || a;
569 var r = new RegExp("-([a-z])","ig");
570 a = a.replace(r,function(z,b){return b.toUpperCase();});
573 if ( o.setAttribute ) {
577 return o[a] || o.getAttribute(a) || '';
583 $.filter = function(t,r,not) {
585 if ( not === false ) {
586 g = function(a,f) {return $.grep(a,f,true);};
589 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
590 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
594 m = ['', '@', m[2], m[1], m[3]];
596 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
600 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
604 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
609 t = t.replace( re, "" );
611 if ( m[1] == ":" && m[2] == "not" ) {
612 r = $.filter(m[3],r,false).r;
616 if ( $.g[m[1]].constructor == String ) {
618 } else if ( $.g[m[1]][m[2]] ) {
623 f = new Function('a','i','return ' + f);
629 return { r: r, t: t };
632 $.parents = function(a){
634 var c = a.parentNode;
635 while ( c !== null && c != document ) {
642 $.cleanSpaces = function(t){
643 return t.replace(/^\s+|\s+$/g, '');
646 $.ofType = function(a,n,e) {
647 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
648 if ( e ) { n = t.length - n - 1; }
649 return n !== null ? t[n] == a : t.length;
652 $.sibling = function(a,n,e) {
654 var tmp = a.parentNode.childNodes;
655 for ( var i = 0; i < tmp.length; i++ ) {
656 if ( tmp[i].nodeType == 1 ) {
657 type[type.length] = tmp[i];
660 type.n = type.length - 1;
663 if ( e ) { n = type.length - n - 1; }
664 type.cur = ( type[n] == a );
665 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
666 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
670 $.hasWord = function(e,a) {
671 if ( e === null ) { return false; }
672 if ( e.className !== null ) { e = e.className; }
673 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
676 $.getAll = function(o,r) {
678 var s = o.childNodes;
679 for ( var i = 0; i < s.length; i++ ) {
680 if ( s[i].nodeType == 1 ) {
688 $.merge = function(a,b) {
690 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
692 for ( var i = 0; i < a.length; i++ ) {
694 for ( var j = 0; j < b.length; j++ ) {
695 if ( a[i] == b[j] ) {
707 $.grep = function(a,f,s) {
710 for ( var i = 0; i < a.length; i++ ) {
711 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
719 $.map = function(a,f) {
721 for ( var i = 0; i < a.length; i++ ) {
724 if ( t.constructor != Array ) { t = [t]; }
731 // Bind an event to an element
732 // Original by Dean Edwards
733 function addEvent(element, type, handler) {
734 if ( element.location ) { element = window; } // Ughhhhh....
735 if (!handler.$$guid) { handler.$$guid = addEvent.guid++; }
736 if (!element.events) { element.events = {}; }
737 var handlers = element.events[type];
739 handlers = element.events[type] = {};
740 if (element["on" + type]) {
741 handlers[0] = element["on" + type];
744 handlers[handler.$$guid] = handler;
745 element["on" + type] = handleEvent;
750 // Detach an event or set of events from an element
751 function removeEvent(element, type, handler) {
752 if (element.events) {
753 if (type && element.events[type]) {
755 delete element.events[type][handler.$$guid];
757 for ( var i in element.events[type] ) {
758 delete element.events[type][i];
762 for ( var j in element.events ) {
763 removeEvent( element, j );
769 function triggerEvent(element,type,data) {
770 data = data || [{ type: type }];
771 if ( element && element["on" + type] ) {
772 $.apply( element, element["on" + type], data );
776 function handleEvent(event) {
777 var returnValue = true;
778 event = event || fixEvent(window.event);
780 for ( var j in this.events[event.type] ) {
781 handlers[handlers.length] = this.events[event.type][j];
783 for ( var i = 0; i < handlers.length; i++ ) {
785 if ( handlers[i].constructor == Function ) {
786 this.$$handleEvent = handlers[i];
787 if (this.$$handleEvent(event) === false) {
788 event.preventDefault();
789 event.stopPropagation();
798 function fixEvent(event) {
799 event.preventDefault = fixEvent.preventDefault;
800 event.stopPropagation = fixEvent.stopPropagation;
804 fixEvent.preventDefault = function() {
805 this.returnValue = false;
808 fixEvent.stopPropagation = function() {
809 this.cancelBubble = true;
814 $.fn.text = function(e) {
817 for ( var j = 0; j < e.length; j++ ) {
818 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
819 t += e[j].childNodes[i].nodeType != 1 ?
820 e[j].childNodes[i].nodeValue :
821 $.fn.text(e[j].childNodes[i].childNodes);
827 /*setTimeout(function(){
828 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
829 throw "Error: You are overwriting jQuery, please include jQuery last.";