4 * Copyright (c) 2006 John Resig (ejohn.org)
5 * Licensed under the MIT License:
6 * http://www.opensource.org/licenses/mit-license.php
12 /* For JSLint (jslint.com): */
13 /*extern ActiveXObject Prototype setTimeout setInterval clearInterval document window XMLHttpRequest navigator*/
16 var $a = a || $.context || document;
17 var $c = c && c.$jquery && c.get(0) || c;
19 // Since we're using Prototype's $ function,
20 // be nice and have backwards compatability
21 if ( typeof Prototype != "undefined" ) {
22 if ( $a.constructor == String ) {
23 var re = new RegExp( "[^a-zA-Z0-9_-]" );
25 $c = $c && $c.documentElement || document;
26 if ( $c.getElementsByTagName($a).length === 0 ) {
27 var obj = $c.getElementById($a);
28 if ( obj !== null ) { return obj; }
31 } else if ( $a.constructor == Array ) {
32 return $.map( $a, function(b){
33 if ( b.constructor == String ) {
34 return document.getElementById(b);
41 // Load Dynamic Function List
46 // The only two getters
47 size: function() {return this.get().length;},
49 return typeof i == 'undefined' ? this.cur : this.cur[i];
53 for ( var i = 0; i < this.size(); i++ ) {
54 $.apply( this.get(i), f, [i] );
59 return this.each(function(){
70 return h === null && this.size() ?
71 this.get(0).innerHTML : this.set( "innerHTML", h );
74 return h === null && this.size() ?
75 this.get(0).value : this.set( "value", h );
79 return a.constructor != String || b ?
83 $.attr(this.style,j,a[j]);
86 $.attr(this.style,a,b);
88 }) : $.css( this.get(0), a );
91 return this.each(function(){
92 var d = $.getCSS(this,"display");
93 if ( d == "none" || d === '' ) {
101 return this.each(function(){
102 this.style.display = this.$$oldblock ? this.$$oldblock : '';
103 if ( $.getCSS(this,"display") == "none" ) {
104 this.style.display = 'block';
109 return this.each(function(){
110 this.$$oldblock = $.getCSS(this,"display");
111 if ( this.$$oldblock == "none" ) {
112 this.$$oldblock = 'block';
114 this.style.display = 'none';
117 addClass: function(c) {
118 return this.each(function(){
119 if ($.hasWord(this,c)) {
122 this.className += ( this.className.length > 0 ? " " : "" ) + c;
125 removeClass: function(c) {
126 return this.each(function(){
127 this.className = c === null ? '' :
128 this.className.replace(
129 new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
133 toggleClass: function(c) {
134 return this.each(function(){
135 if ($.hasWord(this,c)) {
137 this.className.replace(
138 new RegExp('(\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
140 this.className += ( this.className.length > 0 ? " " : "" ) + c;
145 this.each(function(){this.parentNode.removeChild( this );});
151 var a = $.clean(arguments);
152 return this.each(function(){
153 var b = a[0].cloneNode(true);
154 this.parentNode.insertBefore( b, this );
155 while ( b.firstChild ) {
158 b.appendChild( this );
163 var clone = this.size() > 1;
164 var a = $.clean(arguments);
165 return this.each(function(){
166 for ( var i = 0; i < a.length; i++ ) {
167 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
172 appendTo: function() {
174 return this.each(function(){
175 for ( var i = 0; i < a.length; i++ ) {
176 $(a[i]).append( this );
181 prepend: function() {
182 var clone = this.size() > 1;
183 var a = $.clean(arguments);
184 return this.each(function(){
185 for ( var i = a.length - 1; i >= 0; i-- ) {
186 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
192 var clone = this.size() > 1;
193 var a = $.clean(arguments);
194 return this.each(function(){
195 for ( var i = 0; i < a.length; i++ ) {
196 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
202 var clone = this.size() > 1;
203 var a = $.clean(arguments);
204 return this.each(function(){
205 for ( var i = a.length - 1; i >= 0; i-- ) {
206 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
212 return this.each(function(){
213 while ( this.firstChild ) {
214 this.removeChild( this.firstChild );
219 bind: function(t,f) {
220 return this.each(function(){$.event.add(this,t,f);});
222 unbind: function(t,f) {
223 return this.each(function(){$.event.remove(this,t,f);});
225 trigger: function(t) {
226 return this.each(function(){$.event.trigger(this,t);});
230 var old = [], ret = [];
231 this.each(function(){
232 old[old.length] = this;
233 ret = $.merge( ret, $.Select(t,this) );
244 parent: function(a) {
245 this.cur = $.map(this.cur,function(d){
249 this.cur = $.filter(a,this.cur).r;
254 parents: function(a) {
255 this.cur = $.map(this.cur,$.parents);
257 this.cur = $.filter(a,this.cur).r;
262 siblings: function(a) {
263 // Incorrect, need to exclude current element
264 this.cur = $.map(this.cur,$.sibling);
266 this.cur = $.filter(a,this.cur).r;
271 filter: function(t) {
272 this.cur = $.filter(t,this.cur).r;
276 this.cur = t.constructor == String ?
277 $.filter(t,this.cur,false).r :
278 $.grep(this.cur,function(a){return a != t;});
282 this.cur = $.merge( this.cur, t.constructor == String ?
283 $.Select(t) : t.constructor == Array ? t : [t] );
287 return $.filter(t,this.cur).r.length > 0;
294 // TODO: Remove need to return this
295 for ( var i in $.fn ) {
296 if ( self[i] !== null ) {
297 self["_"+i] = self[i];
302 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
306 for ( var k in self ) {(function(j){
308 if ( $a[j] === null ) {
310 return $.apply(self,self[j],arguments);
323 $.apply = function(o,f,a) {
326 return f.apply( o, a );
329 for (var i = 0; i < a.length; i++) {
333 var r = $.eval('o.$$exec(' + p.join(',') + ')');
339 $.getCSS = function(e,p) {
340 // Adapted from Prototype 1.4.0
341 if ( p == 'height' || p == 'width' ) {
342 if ($.getCSS(e,"display") != 'none') {
343 return p == 'height' ?
344 e.offsetHeight || parseInt(e.style.height,10) :
345 e.offsetWidth || parseInt(e.style.width,10);
348 var ov = els.visibility;
349 var op = els.position;
350 var od = els.display;
351 els.visibility = 'hidden';
352 els.position = 'absolute';
354 var oHeight = e.clientHeight || parseInt(e.style.height,10);
355 var oWidth = e.clientWidth || parseInt(e.style.width,10);
359 return p == 'height' ? oHeight : oWidth;
364 } else if (e.currentStyle) {
365 return e.currentStyle[p];
366 } else if (document.defaultView && document.defaultView.getComputedStyle) {
367 p = p.replace(/([A-Z])/g,"-$1");
369 var s = document.defaultView.getComputedStyle(e,"");
370 var r = s ? s.getPropertyValue(p) : p;
378 $.clean = function(a) {
380 for ( var i = 0; i < a.length; i++ ) {
381 if ( a[i].constructor == String ) {
382 var div = document.createElement("div");
383 div.innerHTML = a[i];
384 for ( var j = 0; j < div.childNodes.length; j++ ) {
385 r[r.length] = div.childNodes[j];
387 } else if ( a[i].length ) {
388 for ( var k = 0; k < a[i].length; k++ ) {
389 r[r.length] = a[i][k];
391 } else if ( a[i] !== null ) {
393 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
400 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
401 '#': "a.getAttribute('id') == m[2]",
405 nth: "m[3] - 0 == i",
408 last: "i == r.length - 1",
411 "first-child": "$.sibling(a,0).cur",
412 "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))",
413 "last-child": "$.sibling(a,0,true).cur",
414 "nth-last-child": "$.sibling(a,m[3],true).cur",
415 "first-of-type": "$.ofType(a,0)",
416 "nth-of-type": "$.ofType(a,m[3])",
417 "last-of-type": "$.ofType(a,0,true)",
418 "nth-last-of-type": "$.ofType(a,m[3],true)",
419 "only-of-type": "$.ofType(a) == 1",
420 "only-child": "$.sibling(a).length == 1",
421 parent: "a.childNodes.length > 0",
422 empty: "a.childNodes.length == 0",
423 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
424 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
425 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
426 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
427 enabled: "a.disabled == false",
428 disabled: "a.disabled",
431 // TODO: Write getAttribute helper
432 ".": "$.hasWord(a,m[2])",
434 "=": "$.attr(a,m[3]) == m[4]",
435 "!=": "$.attr(a,m[3]) != m[4]",
436 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
437 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
438 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
439 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
440 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
441 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
443 "[": "$.Select(m[2],a).length > 0"
448 $.Select = function( t, context ) {
449 context = context || $.context || document;
450 if ( t.constructor != String ) {
454 if ( t.indexOf("//") === 0 ) {
455 context = context.documentElement;
456 t = t.substr(2,t.length);
457 } else if ( t.indexOf("/") === 0 ) {
458 context = context.documentElement;
459 t = t.substr(1,t.length);
460 // FIX Assume the root element is right :(
461 if ( t.indexOf('/') ) {
462 t = t.substr(t.indexOf('/'),t.length);
470 while ( t.length > 0 && last != t ) {
474 t = $.cleanSpaces(t);
476 var re = new RegExp( "^//", "i" );
477 t = t.replace( re, "" );
479 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
480 if ( t.indexOf('/') === 0 ) {
481 t = t.substr(1,t.length);
483 r = $.map( ret, function(a){ return a.parentNode; } );
484 t = t.substr(2,t.length);
485 t = $.cleanSpaces(t);
486 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
487 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
488 t = t.substr(1,t.length);
489 t = $.cleanSpaces(t);
490 } else if ( t.indexOf('+') === 0 ) {
491 r = $.map( ret, function(a){ return $.sibling(a).next; } );
492 t = t.substr(1,t.length);
493 t = $.cleanSpaces(t);
494 } else if ( t.indexOf('~') === 0 ) {
495 r = $.map( ret, function(a){
497 var s = $.sibling(a);
499 for ( var i = s.n; i < s.length; i++ ) {
505 t = t.substr(1,t.length);
506 t = $.cleanSpaces(t);
507 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
508 if ( ret[0] == context ) { ret.shift(); }
509 done = $.merge( done, ret );
511 t = " " + t.substr(1,t.length);
513 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
516 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
517 var oid = document.getElementById(m[2]);
518 r = oid ? [oid] : [];
519 t = t.replace( re, "" );
521 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
523 for ( var i = 0; i < ret.length; i++ ) {
528 r = $.merge( $.getAll(o), r );
530 case 'text': case 'radio': case 'checkbox': case 'hidden':
531 case 'button': case 'submit': case 'image': case 'password':
532 case 'reset': case 'file':
533 r = $.merge( $.grep( $.tag(o,"input"),
534 function(a){ return a.type == m[2]; }), r );
537 r = $.merge( $.tag(o,"input"), r );
538 r = $.merge( $.tag(o,"select"), r );
539 r = $.merge( $.tag(o,"textarea"), r );
542 r = $.merge( r, $.tag(o,m[2]) );
550 var val = $.filter(t,r);
552 t = $.cleanSpaces(val.t);
555 if ( ret && ret[0] == context ) { ret.shift(); }
556 done = $.merge( done, ret );
560 $.tag = function(a,b){
561 return a && typeof a.getElementsByTagName != "undefined" ?
562 a.getElementsByTagName( b ) : [];
565 $.attr = function(o,a,v){
566 if ( a && a.constructor == String ) {
570 'class': 'className',
573 a = (fix[a] && fix[a].replace && fix[a]) || a;
574 var r = new RegExp("-([a-z])","ig");
575 a = a.replace(r,function(z,b){return b.toUpperCase();});
576 if ( typeof v != 'undefined' ) {
578 if ( o.setAttribute ) {
582 return o[a] || o.getAttribute(a) || '';
588 $.filter = function(t,r,not) {
590 if ( not === false ) {
591 g = function(a,f) {return $.grep(a,f,true);};
594 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
595 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
599 m = ['', '@', m[2], m[1], m[3]];
601 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
605 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
609 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
614 t = t.replace( re, "" );
616 if ( m[1] == ":" && m[2] == "not" ) {
617 r = $.filter(m[3],r,false).r;
621 if ( $.g[m[1]].constructor == String ) {
623 } else if ( $.g[m[1]][m[2]] ) {
628 $.eval('f = function(a,i){return ' + f + '}');
634 return { r: r, t: t };
637 $.parents = function(a){
639 var c = a.parentNode;
640 while ( c !== null && c != document ) {
647 $.cleanSpaces = function(t){
648 return t.replace(/^\s+|\s+$/g, '');
651 $.ofType = function(a,n,e) {
652 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
653 if ( e ) { n = t.length - n - 1; }
654 return n !== null ? t[n] == a : t.length;
657 $.sibling = function(a,n,e) {
659 var tmp = a.parentNode.childNodes;
660 for ( var i = 0; i < tmp.length; i++ ) {
661 if ( tmp[i].nodeType == 1 ) {
662 type[type.length] = tmp[i];
665 type.n = type.length - 1;
668 if ( e ) { n = type.length - n - 1; }
669 type.cur = ( type[n] == a );
670 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
671 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
675 $.hasWord = function(e,a) {
676 if ( e === null ) { return false; }
677 if ( e.className !== null ) { e = e.className; }
678 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
681 $.getAll = function(o,r) {
683 var s = o.childNodes;
684 for ( var i = 0; i < s.length; i++ ) {
685 if ( s[i].nodeType == 1 ) {
693 $.merge = function(a,b) {
695 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
697 for ( var i = 0; i < a.length; i++ ) {
699 for ( var j = 0; j < b.length; j++ ) {
700 if ( a[i] == b[j] ) {
712 $.grep = function(a,f,s) {
715 for ( var i = 0; i < a.length; i++ ) {
716 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
724 $.map = function(a,f) {
726 for ( var i = 0; i < a.length; i++ ) {
729 if ( t.constructor != Array ) { t = [t]; }
738 // Bind an event to an element
739 // Original by Dean Edwards
740 $.event.add = function(element, type, handler) {
741 if ( element.location ) { element = window; } // Ughhhhh....
742 if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; }
743 if (!element.events) { element.events = {}; }
744 var handlers = element.events[type];
746 handlers = element.events[type] = {};
747 if (element["on" + type]) {
748 handlers[0] = element["on" + type];
751 handlers[handler.$$guid] = handler;
752 element["on" + type] = $.event.handle;
755 $.event.add.guid = 1;
757 // Detach an event or set of events from an element
758 $.event.remove = function(element, type, handler) {
759 if (element.events) {
760 if (type && element.events[type]) {
762 delete element.events[type][handler.$$guid];
764 for ( var i in element.events[type] ) {
765 delete element.events[type][i];
769 for ( var j in element.events ) {
770 $.event.remove( element, j );
776 $.event.trigger = function(element,type,data) {
777 data = data || [{ type: type }];
778 if ( element && element["on" + type] ) {
779 $.apply( element, element["on" + type], data );
783 $.event.handle = function(event) {
784 if ( !event && !window.event ) { return null; }
786 var returnValue = true, handlers = [];
787 event = event || $.event.fix(window.event);
789 for ( var j in this.events[event.type] ) {
790 handlers[handlers.length] = this.events[event.type][j];
793 for ( var i = 0; i < handlers.length; i++ ) {
795 if ( handlers[i].constructor == Function ) {
796 this.$$handleEvent = handlers[i];
797 if (this.$$handleEvent(event) === false) {
798 event.preventDefault();
799 event.stopPropagation();
808 $.event.fix = function(event) {
809 event.preventDefault = $.event.fix.preventDefault;
810 event.stopPropagation = $.event.fix.stopPropagation;
814 $.event.fix.preventDefault = function() {
815 this.returnValue = false;
818 $.event.fix.stopPropagation = function() {
819 this.cancelBubble = true;
824 $.fn.text = function(e) {
827 for ( var j = 0; j < e.length; j++ ) {
828 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
829 t += e[j].childNodes[i].nodeType != 1 ?
830 e[j].childNodes[i].nodeValue :
831 $.fn.text(e[j].childNodes[i].childNodes);
837 /*setTimeout(function(){
838 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
839 throw "Error: You are overwriting jQuery, please include jQuery last.";