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);
322 var b = navigator.userAgent.toLowerCase();
324 ( /safari/.test(b) && "safari" ) ||
325 ( /opera/.test(b) && "opera" ) ||
326 ( /msie/.test(b) && "msie" ) ||
327 ( !/compatible/.test(b) && "mozilla" ) ||
331 $.apply = function(o,f,a) {
334 return f.apply( o, a );
337 for (var i = 0; i < a.length; i++) {
341 var r = eval('o.$$exec(' + p.join(',') + ')');
347 $.getCSS = function(e,p) {
348 // Adapted from Prototype 1.4.0
349 if ( p == 'height' || p == 'width' ) {
350 if ($.getCSS(e,"display") != 'none') {
351 return p == 'height' ?
352 e.offsetHeight || parseInt(e.style.height,10) :
353 e.offsetWidth || parseInt(e.style.width,10);
356 var ov = els.visibility;
357 var op = els.position;
358 var od = els.display;
359 els.visibility = 'hidden';
360 els.position = 'absolute';
362 var oHeight = e.clientHeight || parseInt(e.style.height,10);
363 var oWidth = e.clientWidth || parseInt(e.style.width,10);
367 return p == 'height' ? oHeight : oWidth;
372 } else if (e.currentStyle) {
373 return e.currentStyle[p];
374 } else if (document.defaultView && document.defaultView.getComputedStyle) {
375 p = p.replace(/([A-Z])/g,"-$1");
377 var s = document.defaultView.getComputedStyle(e,"");
378 var r = s ? s.getPropertyValue(p) : p;
386 $.clean = function(a) {
388 for ( var i = 0; i < a.length; i++ ) {
389 if ( a[i].constructor == String ) {
390 var div = document.createElement("div");
391 div.innerHTML = a[i];
392 for ( var j = 0; j < div.childNodes.length; j++ ) {
393 r[r.length] = div.childNodes[j];
395 } else if ( a[i].length ) {
396 for ( var k = 0; k < a[i].length; k++ ) {
397 r[r.length] = a[i][k];
399 } else if ( a[i] !== null ) {
401 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
408 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
409 '#': "a.attributes['id'].nodeValue == m[2]",
413 nth: "m[3] - 0 == i",
416 last: "i == r.length - 1",
419 "first-child": "$.sibling(a,0).cur",
420 "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))",
421 "last-child": "$.sibling(a,0,true).cur",
422 "nth-last-child": "$.sibling(a,m[3],true).cur",
423 "first-of-type": "$.ofType(a,0)",
424 "nth-of-type": "$.ofType(a,m[3])",
425 "last-of-type": "$.ofType(a,0,true)",
426 "nth-last-of-type": "$.ofType(a,m[3],true)",
427 "only-of-type": "$.ofType(a) == 1",
428 "only-child": "$.sibling(a).length == 1",
429 parent: "a.childNodes.length > 0",
430 empty: "a.childNodes.length == 0",
431 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
432 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
433 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
434 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
435 enabled: "a.disabled == false",
436 disabled: "a.disabled",
439 // TODO: Write getAttribute helper
440 ".": "$.hasWord(a,m[2])",
442 "=": "$.attr(a,m[3]) == m[4]",
443 "!=": "$.attr(a,m[3]) != m[4]",
444 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
445 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
446 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
447 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
448 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
449 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
451 "[": "$.Select(m[2],a).length > 0"
456 $.Select = function( t, context ) {
457 context = context || $.context || document;
458 if ( t.constructor != String ) {
462 if ( t.indexOf("//") === 0 ) {
463 context = context.documentElement;
464 t = t.substr(2,t.length);
465 } else if ( t.indexOf("/") === 0 ) {
466 context = context.documentElement;
467 t = t.substr(1,t.length);
468 // FIX Assume the root element is right :(
469 if ( t.indexOf('/') ) {
470 t = t.substr(t.indexOf('/'),t.length);
478 while ( t.length > 0 && last != t ) {
482 t = $.cleanSpaces(t);
484 var re = new RegExp( "^//", "i" );
485 t = t.replace( re, "" );
487 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
488 if ( t.indexOf('/') === 0 ) {
489 t = t.substr(1,t.length);
491 r = $.map( ret, function(a){ return a.parentNode; } );
492 t = t.substr(2,t.length);
493 t = $.cleanSpaces(t);
494 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
495 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
496 t = t.substr(1,t.length);
497 t = $.cleanSpaces(t);
498 } else if ( t.indexOf('+') === 0 ) {
499 r = $.map( ret, function(a){ return $.sibling(a).next; } );
500 t = t.substr(1,t.length);
501 t = $.cleanSpaces(t);
502 } else if ( t.indexOf('~') === 0 ) {
503 r = $.map( ret, function(a){
505 var s = $.sibling(a);
507 for ( var i = s.n; i < s.length; i++ ) {
513 t = t.substr(1,t.length);
514 t = $.cleanSpaces(t);
515 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
516 if ( ret[0] == context ) { ret.shift(); }
517 done = $.merge( done, ret );
519 t = " " + t.substr(1,t.length);
521 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
524 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
525 var oid = document.getElementById(m[2]);
526 r = oid ? [oid] : [];
527 t = t.replace( re2, "" );
529 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
531 for ( var i = 0; i < ret.length; i++ ) {
536 r = $.merge( $.getAll(o), r );
538 case 'text': case 'radio': case 'checkbox': case 'hidden':
539 case 'button': case 'submit': case 'image': case 'password':
540 case 'reset': case 'file':
541 r = $.merge( $.grep( $.tag(o,"input"),
542 function(a){ return a.type == m[2]; }), r );
545 r = $.merge( $.tag(o,"input"), r );
546 r = $.merge( $.tag(o,"select"), r );
547 r = $.merge( $.tag(o,"textarea"), r );
550 r = $.merge( r, $.tag(o,m[2]) );
559 var val = $.filter(t,r);
561 t = $.cleanSpaces(val.t);
565 if ( ret && ret[0] == context ) { ret.shift(); }
566 done = $.merge( done, ret );
570 $.tag = function(a,b){
571 return a && typeof a.getElementsByTagName != "undefined" ?
572 a.getElementsByTagName( b ) : [];
575 $.attr = function(o,a,v){
576 if ( a && a.constructor == String ) {
580 'class': 'className',
583 a = (fix[a] && fix[a].replace && fix[a]) || a;
584 var r = new RegExp("-([a-z])","ig");
585 a = a.replace(r,function(z,b){return b.toUpperCase();});
586 if ( typeof v != 'undefined' ) {
588 if ( o.setAttribute ) {
592 return o[a] || o.getAttribute(a) || '';
598 $.filter = function(t,r,not) {
600 if ( not === false ) {
601 g = function(a,f) {return $.grep(a,f,true);};
604 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
605 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
609 m = ['', '@', m[2], m[1], m[3]];
611 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
615 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
619 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
624 t = t.replace( re, "" );
626 if ( m[1] == ":" && m[2] == "not" ) {
627 r = $.filter(m[3],r,false).r;
631 if ( $.g[m[1]].constructor == String ) {
633 } else if ( $.g[m[1]][m[2]] ) {
638 eval('f = function(a,i){return ' + f + '}');
644 return { r: r, t: t };
647 $.parents = function(a){
649 var c = a.parentNode;
650 while ( c !== null && c != document ) {
657 $.cleanSpaces = function(t){
658 return t.replace(/^\s+|\s+$/g, '');
661 $.ofType = function(a,n,e) {
662 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
663 if ( e ) { n = t.length - n - 1; }
664 return typeof n != 'undefined' ? t[n] == a : t.length;
667 $.sibling = function(a,n,e) {
669 var tmp = a.parentNode.childNodes;
670 for ( var i = 0; i < tmp.length; i++ ) {
671 if ( tmp[i].nodeType == 1 ) {
672 type[type.length] = tmp[i];
675 type.n = type.length - 1;
678 if ( e ) { n = type.length - n - 1; }
679 type.cur = ( type[n] == a );
680 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
681 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
685 $.hasWord = function(e,a) {
686 if ( typeof e == 'undefined' ) { return false; }
687 if ( e.className !== null ) { e = e.className; }
688 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
691 $.getAll = function(o,r) {
693 var s = o.childNodes;
694 for ( var i = 0; i < s.length; i++ ) {
695 if ( s[i].nodeType == 1 ) {
703 $.merge = function(a,b) {
705 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
707 for ( var i = 0; i < a.length; i++ ) {
709 for ( var j = 0; j < b.length; j++ ) {
710 if ( a[i] == b[j] ) {
722 $.grep = function(a,f,s) {
724 if ( typeof a != 'undefined' ) {
725 for ( var i = 0; i < a.length; i++ ) {
726 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
734 $.map = function(a,f) {
736 for ( var i = 0; i < a.length; i++ ) {
739 if ( t.constructor != Array ) { t = [t]; }
748 // Bind an event to an element
749 // Original by Dean Edwards
750 $.event.add = function(element, type, handler) {
751 if ( element.location ) { element = window; } // Ughhhhh....
752 if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; }
753 if (!element.events) { element.events = {}; }
754 var handlers = element.events[type];
756 handlers = element.events[type] = {};
757 if (element["on" + type]) {
758 handlers[0] = element["on" + type];
761 handlers[handler.$$guid] = handler;
762 element["on" + type] = $.event.handle;
765 $.event.add.guid = 1;
767 // Detach an event or set of events from an element
768 $.event.remove = function(element, type, handler) {
769 if (element.events) {
770 if (type && element.events[type]) {
772 delete element.events[type][handler.$$guid];
774 for ( var i in element.events[type] ) {
775 delete element.events[type][i];
779 for ( var j in element.events ) {
780 $.event.remove( element, j );
786 $.event.trigger = function(element,type,data) {
787 data = data || [{ type: type }];
788 if ( element && element["on" + type] ) {
789 $.apply( element, element["on" + type], data );
793 $.event.handle = function(event) {
794 if ( !event && !window.event ) { return null; }
796 var returnValue = true, handlers = [];
797 event = event || $.event.fix(window.event);
799 for ( var j in this.events[event.type] ) {
800 handlers[handlers.length] = this.events[event.type][j];
803 for ( var i = 0; i < handlers.length; i++ ) {
804 if ( handlers[i].constructor == Function ) {
805 this.$$handleEvent = handlers[i];
806 if (this.$$handleEvent(event) === false) {
807 event.preventDefault();
808 event.stopPropagation();
816 $.event.fix = function(event) {
817 event.preventDefault = $.event.fix.preventDefault;
818 event.stopPropagation = $.event.fix.stopPropagation;
822 $.event.fix.preventDefault = function() {
823 this.returnValue = false;
826 $.event.fix.stopPropagation = function() {
827 this.cancelBubble = true;
832 $.fn.text = function(e) {
835 for ( var j = 0; j < e.length; j++ ) {
836 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
837 t += e[j].childNodes[i].nodeType != 1 ?
838 e[j].childNodes[i].nodeValue :
839 $.fn.text(e[j].childNodes[i].childNodes);
845 /*setTimeout(function(){
846 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
847 throw "Error: You are overwriting jQuery, please include jQuery last.";