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 var ph = $.browser == "msie" ? 0 :
351 parseInt($.css(e,"paddingTop")) + parseInt($.css(e,"paddingBottom"));
352 var pw = $.browser == "msie" ? 0 :
353 parseInt($.css(e,"paddingLeft")) + parseInt($.css(e,"paddingRight"));
357 if ($.css(e,"display") != 'none') {
358 oHeight = e.offsetHeight || parseInt(e.style.height,10);
359 oWidth = e.offsetWidth || parseInt(e.style.width,10);
362 var ov = els.visibility;
363 var op = els.position;
364 var od = els.display;
365 els.visibility = 'hidden';
366 els.position = 'absolute';
368 oHeight = e.clientHeight || parseInt(e.style.height,10);
369 oWidth = e.clientWidth || parseInt(e.style.width,10);
375 return p == 'height' ?
376 (oHeight - ph < 0 ? 0 : oHeight - ph) :
377 (oWidth - pw < 0 ? 0 : oWidth - pw);
382 } else if (e.currentStyle) {
383 return e.currentStyle[p];
384 } else if (document.defaultView && document.defaultView.getComputedStyle) {
385 p = p.replace(/([A-Z])/g,"-$1");
387 var s = document.defaultView.getComputedStyle(e,"");
388 var r = s ? s.getPropertyValue(p) : p;
396 $.clean = function(a) {
398 for ( var i = 0; i < a.length; i++ ) {
399 if ( a[i].constructor == String ) {
400 if ( a[i].indexOf("<tr") == 0 ) {
402 a[i] = "<table>" + a[i] + "</table>";
404 var div = document.createElement("div");
405 div.innerHTML = a[i];
407 div = div.firstChild.firstChild;
409 for ( var j = 0; j < div.childNodes.length; j++ ) {
410 r[r.length] = div.childNodes[j];
412 } else if ( a[i].length ) {
413 for ( var k = 0; k < a[i].length; k++ ) {
414 r[r.length] = a[i][k];
416 } else if ( a[i] !== null ) {
418 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
425 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
426 '#': "a.attributes['id'].nodeValue == m[2]",
430 nth: "m[3] - 0 == i",
433 last: "i == r.length - 1",
436 "first-child": "$.sibling(a,0).cur",
437 "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))",
438 "last-child": "$.sibling(a,0,true).cur",
439 "nth-last-child": "$.sibling(a,m[3],true).cur",
440 "first-of-type": "$.ofType(a,0)",
441 "nth-of-type": "$.ofType(a,m[3])",
442 "last-of-type": "$.ofType(a,0,true)",
443 "nth-last-of-type": "$.ofType(a,m[3],true)",
444 "only-of-type": "$.ofType(a) == 1",
445 "only-child": "$.sibling(a).length == 1",
446 parent: "a.childNodes.length > 0",
447 empty: "a.childNodes.length == 0",
448 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
449 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
450 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
451 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
452 enabled: "a.disabled == false",
453 disabled: "a.disabled",
456 // TODO: Write getAttribute helper
457 ".": "$.hasWord(a,m[2])",
459 "=": "$.attr(a,m[3]) == m[4]",
460 "!=": "$.attr(a,m[3]) != m[4]",
461 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
462 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
463 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
464 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
465 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
466 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
468 "[": "$.Select(m[2],a).length > 0"
473 $.Select = function( t, context ) {
474 context = context || $.context || document;
475 if ( t.constructor != String ) {
479 if ( t.indexOf("//") === 0 ) {
480 context = context.documentElement;
481 t = t.substr(2,t.length);
482 } else if ( t.indexOf("/") === 0 ) {
483 context = context.documentElement;
484 t = t.substr(1,t.length);
485 // FIX Assume the root element is right :(
486 if ( t.indexOf('/') ) {
487 t = t.substr(t.indexOf('/'),t.length);
495 while ( t.length > 0 && last != t ) {
499 t = $.cleanSpaces(t);
501 var re = new RegExp( "^//", "i" );
502 t = t.replace( re, "" );
504 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
505 if ( t.indexOf('/') === 0 ) {
506 t = t.substr(1,t.length);
508 r = $.map( ret, function(a){ return a.parentNode; } );
509 t = t.substr(2,t.length);
510 t = $.cleanSpaces(t);
511 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
512 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
513 t = t.substr(1,t.length);
514 t = $.cleanSpaces(t);
515 } else if ( t.indexOf('+') === 0 ) {
516 r = $.map( ret, function(a){ return $.sibling(a).next; } );
517 t = t.substr(1,t.length);
518 t = $.cleanSpaces(t);
519 } else if ( t.indexOf('~') === 0 ) {
520 r = $.map( ret, function(a){
522 var s = $.sibling(a);
524 for ( var i = s.n; i < s.length; i++ ) {
530 t = t.substr(1,t.length);
531 t = $.cleanSpaces(t);
532 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
533 if ( ret[0] == context ) { ret.shift(); }
534 done = $.merge( done, ret );
536 t = " " + t.substr(1,t.length);
538 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
541 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
542 var oid = document.getElementById(m[2]);
543 r = ret = oid ? [oid] : [];
544 t = t.replace( re2, "" );
546 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
548 for ( var i = 0; i < ret.length; i++ ) {
553 r = $.merge( $.getAll(o), r );
555 case 'text': case 'radio': case 'checkbox': case 'hidden':
556 case 'button': case 'submit': case 'image': case 'password':
557 case 'reset': case 'file':
558 r = $.merge( $.grep( $.tag(o,"input"),
559 function(a){ return a.type == m[2]; }), r );
562 r = $.merge( $.tag(o,"input"), r );
563 r = $.merge( $.tag(o,"select"), r );
564 r = $.merge( $.tag(o,"textarea"), r );
567 r = $.merge( r, $.tag(o,m[2]) );
576 var val = $.filter(t,r);
578 t = $.cleanSpaces(val.t);
582 if ( ret && ret[0] == context ) { ret.shift(); }
583 done = $.merge( done, ret );
588 $.tag = function(a,b){
589 return a && typeof a.getElementsByTagName != 'undefined' ?
590 a.getElementsByTagName( b ) : [];
593 $.attr = function(o,a,v){
594 if ( a && a.constructor == String ) {
598 'class': 'className',
602 a = (fix[a] && fix[a].replace && fix[a]) || a;
603 var r = new RegExp("-([a-z])","ig");
604 a = a.replace(r,function(z,b){return b.toUpperCase();});
605 if ( typeof v != 'undefined' ) {
607 if ( o.setAttribute ) {
611 return o[a] || o.getAttribute(a) || '';
617 $.filter = function(t,r,not) {
619 if ( not === false ) {
620 g = function(a,f) {return $.grep(a,f,true);};
623 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
624 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
628 m = ['', '@', m[2], m[1], m[3]];
630 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
634 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
638 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
643 t = t.replace( re, "" );
645 if ( m[1] == ":" && m[2] == "not" ) {
646 r = $.filter(m[3],r,false).r;
650 if ( $.g[m[1]].constructor == String ) {
652 } else if ( $.g[m[1]][m[2]] ) {
657 eval('f = function(a,i){return ' + f + '}');
663 return { r: r, t: t };
666 $.parents = function(a){
668 var c = a.parentNode;
669 while ( c !== null && c != document ) {
676 $.cleanSpaces = function(t){
677 return t.replace(/^\s+|\s+$/g, '');
680 $.ofType = function(a,n,e) {
681 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
682 if ( e ) { n = t.length - n - 1; }
683 return typeof n != 'undefined' ? t[n] == a : t.length;
686 $.sibling = function(a,n,e) {
688 var tmp = a.parentNode.childNodes;
689 for ( var i = 0; i < tmp.length; i++ ) {
690 if ( tmp[i].nodeType == 1 ) {
691 type[type.length] = tmp[i];
694 type.n = type.length - 1;
697 if ( e ) { n = type.length - n - 1; }
698 type.cur = ( type[n] == a );
699 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
700 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
704 $.hasWord = function(e,a) {
705 if ( typeof e == 'undefined' ) { return false; }
706 if ( e.className !== null ) { e = e.className; }
707 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
710 $.getAll = function(o,r) {
712 var s = o.childNodes;
713 for ( var i = 0; i < s.length; i++ ) {
714 if ( s[i].nodeType == 1 ) {
722 $.merge = function(a,b) {
724 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
726 for ( var i = 0; i < a.length; i++ ) {
728 for ( var j = 0; j < b.length; j++ ) {
729 if ( a[i] == b[j] ) {
741 $.grep = function(a,f,s) {
743 if ( typeof a != 'undefined' ) {
744 for ( var i = 0; i < a.length; i++ ) {
745 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
753 $.map = function(a,f) {
755 for ( var i = 0; i < a.length; i++ ) {
758 if ( t.constructor != Array ) { t = [t]; }
767 // Bind an event to an element
768 // Original by Dean Edwards
769 $.event.add = function(element, type, handler) {
770 if ( element.location ) { element = window; } // Ughhhhh....
771 if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; }
772 if (!element.events) { element.events = {}; }
773 var handlers = element.events[type];
775 handlers = element.events[type] = {};
776 if (element["on" + type]) {
777 handlers[0] = element["on" + type];
780 handlers[handler.$$guid] = handler;
781 element["on" + type] = $.event.handle;
784 $.event.add.guid = 1;
786 // Detach an event or set of events from an element
787 $.event.remove = function(element, type, handler) {
788 if (element.events) {
789 if (type && element.events[type]) {
791 delete element.events[type][handler.$$guid];
793 for ( var i in element.events[type] ) {
794 delete element.events[type][i];
798 for ( var j in element.events ) {
799 $.event.remove( element, j );
805 $.event.trigger = function(element,type,data) {
806 data = data || [{ type: type }];
807 if ( element && element["on" + type] ) {
808 $.apply( element, element["on" + type], data );
812 $.event.handle = function(event) {
813 if ( !event && !window.event ) { return null; }
815 var returnValue = true, handlers = [];
816 event = event || $.event.fix(window.event);
818 for ( var j in this.events[event.type] ) {
819 handlers[handlers.length] = this.events[event.type][j];
822 for ( var i = 0; i < handlers.length; i++ ) {
823 if ( handlers[i].constructor == Function ) {
824 this.$$handleEvent = handlers[i];
825 if (this.$$handleEvent(event) === false) {
826 event.preventDefault();
827 event.stopPropagation();
835 $.event.fix = function(event) {
836 event.preventDefault = $.event.fix.preventDefault;
837 event.stopPropagation = $.event.fix.stopPropagation;
841 $.event.fix.preventDefault = function() {
842 this.returnValue = false;
845 $.event.fix.stopPropagation = function() {
846 this.cancelBubble = true;
851 $.fn.text = function(e) {
854 for ( var j = 0; j < e.length; j++ ) {
855 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
856 t += e[j].childNodes[i].nodeType != 1 ?
857 e[j].childNodes[i].nodeValue :
858 $.fn.text(e[j].childNodes[i].childNodes);
864 /*setTimeout(function(){
865 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
866 throw "Error: You are overwriting jQuery, please include jQuery last.";