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 ) { 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(){
60 if ( typeof b == 'undefined' ) {
70 return typeof h == 'undefined' && this.size() ?
71 this.get(0).innerHTML : this.set( "innerHTML", h );
74 return typeof h == 'undefined' && 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 ? '' :
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){
310 return $.apply(self,self[j],arguments);
321 $.apply = function(o,f,a) {
324 return f.apply( o, a );
327 for (var i = 0; i < a.length; i++) {
331 var r = eval('o.$$exec(' + p.join(',') + ')');
337 $.getCSS = function(e,p) {
338 // Adapted from Prototype 1.4.0
339 if ( p == 'height' || p == 'width' ) {
340 if ($.getCSS(e,"display") != 'none') {
341 return p == 'height' ?
342 e.offsetHeight || parseInt(e.style.height,10) :
343 e.offsetWidth || parseInt(e.style.width,10);
346 var ov = els.visibility;
347 var op = els.position;
348 var od = els.display;
349 els.visibility = 'hidden';
350 els.position = 'absolute';
352 var oHeight = e.clientHeight || parseInt(e.style.height,10);
353 var oWidth = e.clientWidth || parseInt(e.style.width,10);
357 return p == 'height' ? oHeight : oWidth;
362 } else if (e.currentStyle) {
363 return e.currentStyle[p];
364 } else if (document.defaultView && document.defaultView.getComputedStyle) {
365 p = p.replace(/([A-Z])/g,"-$1");
367 var s = document.defaultView.getComputedStyle(e,"");
368 var r = s ? s.getPropertyValue(p) : p;
376 $.clean = function(a) {
378 for ( var i = 0; i < a.length; i++ ) {
379 if ( a[i].constructor == String ) {
380 var div = document.createElement("div");
381 div.innerHTML = a[i];
382 for ( var j = 0; j < div.childNodes.length; j++ ) {
383 r[r.length] = div.childNodes[j];
385 } else if ( a[i].length ) {
386 for ( var k = 0; k < a[i].length; k++ ) {
387 r[r.length] = a[i][k];
389 } else if ( a[i] !== null ) {
391 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
398 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
399 '#': "a.attributes['id'].nodeValue == m[2]",
403 nth: "m[3] - 0 == i",
406 last: "i == r.length - 1",
409 "first-child": "$.sibling(a,0).cur",
410 "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))",
411 "last-child": "$.sibling(a,0,true).cur",
412 "nth-last-child": "$.sibling(a,m[3],true).cur",
413 "first-of-type": "$.ofType(a,0)",
414 "nth-of-type": "$.ofType(a,m[3])",
415 "last-of-type": "$.ofType(a,0,true)",
416 "nth-last-of-type": "$.ofType(a,m[3],true)",
417 "only-of-type": "$.ofType(a) == 1",
418 "only-child": "$.sibling(a).length == 1",
419 parent: "a.childNodes.length > 0",
420 empty: "a.childNodes.length == 0",
421 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
422 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
423 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
424 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
425 enabled: "a.disabled == false",
426 disabled: "a.disabled",
429 // TODO: Write getAttribute helper
430 ".": "$.hasWord(a,m[2])",
432 "=": "$.attr(a,m[3]) == m[4]",
433 "!=": "$.attr(a,m[3]) != m[4]",
434 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
435 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
436 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
437 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
438 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
439 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
441 "[": "$.Select(m[2],a).length > 0"
446 $.Select = function( t, context ) {
447 context = context || $.context || document;
448 if ( t.constructor != String ) {
452 if ( t.indexOf("//") === 0 ) {
453 context = context.documentElement;
454 t = t.substr(2,t.length);
455 } else if ( t.indexOf("/") === 0 ) {
456 context = context.documentElement;
457 t = t.substr(1,t.length);
458 // FIX Assume the root element is right :(
459 if ( t.indexOf('/') ) {
460 t = t.substr(t.indexOf('/'),t.length);
468 while ( t.length > 0 && last != t ) {
472 t = $.cleanSpaces(t);
474 var re = new RegExp( "^//", "i" );
475 t = t.replace( re, "" );
477 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
478 if ( t.indexOf('/') === 0 ) {
479 t = t.substr(1,t.length);
481 r = $.map( ret, function(a){ return a.parentNode; } );
482 t = t.substr(2,t.length);
483 t = $.cleanSpaces(t);
484 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
485 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
486 t = t.substr(1,t.length);
487 t = $.cleanSpaces(t);
488 } else if ( t.indexOf('+') === 0 ) {
489 r = $.map( ret, function(a){ return $.sibling(a).next; } );
490 t = t.substr(1,t.length);
491 t = $.cleanSpaces(t);
492 } else if ( t.indexOf('~') === 0 ) {
493 r = $.map( ret, function(a){
495 var s = $.sibling(a);
497 for ( var i = s.n; i < s.length; i++ ) {
503 t = t.substr(1,t.length);
504 t = $.cleanSpaces(t);
505 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
506 if ( ret[0] == context ) { ret.shift(); }
507 done = $.merge( done, ret );
509 t = " " + t.substr(1,t.length);
511 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
514 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
515 var oid = document.getElementById(m[2]);
516 r = oid ? [oid] : [];
517 t = t.replace( re, "" );
519 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
521 for ( var i = 0; i < ret.length; i++ ) {
526 r = $.merge( $.getAll(o), r );
528 case 'text': case 'radio': case 'checkbox': case 'hidden':
529 case 'button': case 'submit': case 'image': case 'password':
530 case 'reset': case 'file':
531 r = $.merge( $.grep( $.tag(o,"input"),
532 function(a){ return a.type == m[2]; }), r );
535 r = $.merge( $.tag(o,"input"), r );
536 r = $.merge( $.tag(o,"select"), r );
537 r = $.merge( $.tag(o,"textarea"), r );
540 r = $.merge( r, $.tag(o,m[2]) );
548 var val = $.filter(t,r);
550 t = $.cleanSpaces(val.t);
553 if ( ret && ret[0] == context ) { ret.shift(); }
554 done = $.merge( done, ret );
558 $.tag = function(a,b){
559 return a && typeof a.getElementsByTagName != "undefined" ?
560 a.getElementsByTagName( b ) : [];
563 $.attr = function(o,a,v){
564 if ( a && a.constructor == String ) {
568 'class': 'className',
571 a = (fix[a] && fix[a].replace && fix[a]) || a;
572 var r = new RegExp("-([a-z])","ig");
573 a = a.replace(r,function(z,b){return b.toUpperCase();});
574 if ( typeof v != 'undefined' ) {
576 if ( o.setAttribute ) {
580 return o[a] || o.getAttribute(a) || '';
586 $.filter = function(t,r,not) {
588 if ( not === false ) {
589 g = function(a,f) {return $.grep(a,f,true);};
592 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
593 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
597 m = ['', '@', m[2], m[1], m[3]];
599 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
603 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
607 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
612 t = t.replace( re, "" );
614 if ( m[1] == ":" && m[2] == "not" ) {
615 r = $.filter(m[3],r,false).r;
619 if ( $.g[m[1]].constructor == String ) {
621 } else if ( $.g[m[1]][m[2]] ) {
626 eval('f = function(a,i){return ' + f + '}');
632 return { r: r, t: t };
635 $.parents = function(a){
637 var c = a.parentNode;
638 while ( c !== null && c != document ) {
645 $.cleanSpaces = function(t){
646 return t.replace(/^\s+|\s+$/g, '');
649 $.ofType = function(a,n,e) {
650 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
651 if ( e ) { n = t.length - n - 1; }
652 return typeof n != 'undefined' ? t[n] == a : t.length;
655 $.sibling = function(a,n,e) {
657 var tmp = a.parentNode.childNodes;
658 for ( var i = 0; i < tmp.length; i++ ) {
659 if ( tmp[i].nodeType == 1 ) {
660 type[type.length] = tmp[i];
663 type.n = type.length - 1;
666 if ( e ) { n = type.length - n - 1; }
667 type.cur = ( type[n] == a );
668 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
669 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
673 $.hasWord = function(e,a) {
674 if ( typeof e == 'undefined' ) { return false; }
675 if ( e.className !== null ) { e = e.className; }
676 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
679 $.getAll = function(o,r) {
681 var s = o.childNodes;
682 for ( var i = 0; i < s.length; i++ ) {
683 if ( s[i].nodeType == 1 ) {
691 $.merge = function(a,b) {
693 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
695 for ( var i = 0; i < a.length; i++ ) {
697 for ( var j = 0; j < b.length; j++ ) {
698 if ( a[i] == b[j] ) {
710 $.grep = function(a,f,s) {
712 if ( typeof a != 'undefined' ) {
713 for ( var i = 0; i < a.length; i++ ) {
714 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
722 $.map = function(a,f) {
724 for ( var i = 0; i < a.length; i++ ) {
727 if ( t.constructor != Array ) { t = [t]; }
736 // Bind an event to an element
737 // Original by Dean Edwards
738 $.event.add = function(element, type, handler) {
739 if ( element.location ) { element = window; } // Ughhhhh....
740 if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; }
741 if (!element.events) { element.events = {}; }
742 var handlers = element.events[type];
744 handlers = element.events[type] = {};
745 if (element["on" + type]) {
746 handlers[0] = element["on" + type];
749 handlers[handler.$$guid] = handler;
750 element["on" + type] = $.event.handle;
753 $.event.add.guid = 1;
755 // Detach an event or set of events from an element
756 $.event.remove = function(element, type, handler) {
757 if (element.events) {
758 if (type && element.events[type]) {
760 delete element.events[type][handler.$$guid];
762 for ( var i in element.events[type] ) {
763 delete element.events[type][i];
767 for ( var j in element.events ) {
768 $.event.remove( element, j );
774 $.event.trigger = function(element,type,data) {
775 data = data || [{ type: type }];
776 if ( element && element["on" + type] ) {
777 $.apply( element, element["on" + type], data );
781 $.event.handle = function(event) {
782 if ( !event && !window.event ) { return null; }
784 var returnValue = true, handlers = [];
785 event = event || $.event.fix(window.event);
787 for ( var j in this.events[event.type] ) {
788 handlers[handlers.length] = this.events[event.type][j];
791 for ( var i = 0; i < handlers.length; i++ ) {
793 if ( handlers[i].constructor == Function ) {
794 this.$$handleEvent = handlers[i];
795 if (this.$$handleEvent(event) === false) {
796 event.preventDefault();
797 event.stopPropagation();
806 $.event.fix = function(event) {
807 event.preventDefault = $.event.fix.preventDefault;
808 event.stopPropagation = $.event.fix.stopPropagation;
812 $.event.fix.preventDefault = function() {
813 this.returnValue = false;
816 $.event.fix.stopPropagation = function() {
817 this.cancelBubble = true;
822 $.fn.text = function(e) {
825 for ( var j = 0; j < e.length; j++ ) {
826 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
827 t += e[j].childNodes[i].nodeType != 1 ?
828 e[j].childNodes[i].nodeValue :
829 $.fn.text(e[j].childNodes[i].childNodes);
835 /*setTimeout(function(){
836 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
837 throw "Error: You are overwriting jQuery, please include jQuery last.";