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] );
58 return this.each(function(){
59 if ( typeof b == 'undefined' )
67 return typeof h == 'undefined' && this.size() ?
68 this.get(0).innerHTML : this.set( "innerHTML", h );
71 return typeof h == 'undefined' && this.size() ?
72 this.get(0).value : this.set( "value", h );
76 return a.constructor != String || b ?
80 $.attr(this.style,j,a[j]);
82 $.attr(this.style,a,b);
83 }) : $.css( this.get(0), a );
86 return this.each(function(){
87 var d = $.getCSS(this,"display");
88 if ( d == "none" || d === '' )
95 return this.each(function(){
96 this.style.display = this.$$oldblock ? this.$$oldblock : '';
97 if ( $.getCSS(this,"display") == "none" )
98 this.style.display = 'block';
102 return this.each(function(){
103 this.$$oldblock = $.getCSS(this,"display");
104 if ( this.$$oldblock == "none" )
105 this.$$oldblock = 'block';
106 this.style.display = 'none';
109 addClass: function(c) {
110 return this.each(function(){
111 if ($.hasWord(this,c)) return;
112 this.className += ( this.className.length > 0 ? " " : "" ) + c;
115 removeClass: function(c) {
116 return this.each(function(){
117 this.className = !c ? '' :
118 this.className.replace(
119 new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
123 toggleClass: function(c) {
124 return this.each(function(){
125 if ($.hasWord(this,c))
127 this.className.replace(
128 new RegExp('(\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
130 this.className += ( this.className.length > 0 ? " " : "" ) + c;
134 this.each(function(){this.parentNode.removeChild( this );});
140 var a = $.clean(arguments);
141 return this.each(function(){
142 var b = a[0].cloneNode(true);
143 this.parentNode.insertBefore( b, this );
144 while ( b.firstChild )
146 b.appendChild( this );
151 var clone = this.size() > 1;
152 var a = $.clean(arguments);
153 return this.domManip(function(){
154 for ( var i = 0; i < a.length; i++ )
155 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
159 appendTo: function() {
161 return this.each(function(){
162 for ( var i = 0; i < a.length; i++ )
163 $(a[i]).append( this );
167 prepend: function() {
168 var clone = this.size() > 1;
169 var a = $.clean(arguments);
170 return this.domManip(function(){
171 for ( var i = a.length - 1; i >= 0; i-- )
172 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
177 var clone = this.size() > 1;
178 var a = $.clean(arguments);
179 return this.each(function(){
180 for ( var i = 0; i < a.length; i++ )
181 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
186 var clone = this.size() > 1;
187 var a = $.clean(arguments);
188 return this.each(function(){
189 for ( var i = a.length - 1; i >= 0; i-- )
190 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
195 return this.each(function(){
196 while ( this.firstChild )
197 this.removeChild( this.firstChild );
201 bind: function(t,f) {
202 return this.each(function(){$.event.add(this,t,f);});
204 unbind: function(t,f) {
205 return this.each(function(){$.event.remove(this,t,f);});
207 trigger: function(t) {
208 return this.each(function(){$.event.trigger(this,t);});
212 var old = [], ret = [];
213 this.each(function(){
214 old[old.length] = this;
215 ret = $.merge( ret, $.Select(t,this) );
226 parent: function(a) {
227 this.cur = $.map(this.cur,function(d){
230 if ( a ) this.cur = $.filter(a,this.cur).r;
234 parents: function(a) {
235 this.cur = $.map(this.cur,$.parents);
236 if ( a ) this.cur = $.filter(a,this.cur).r;
240 siblings: function(a) {
241 // Incorrect, need to exclude current element
242 this.cur = $.map(this.cur,$.sibling);
243 if ( a ) this.cur = $.filter(a,this.cur).r;
247 filter: function(t) {
248 this.cur = $.filter(t,this.cur).r;
252 this.cur = t.constructor == String ?
253 $.filter(t,this.cur,false).r :
254 $.grep(this.cur,function(a){return a != t;});
258 this.cur = $.merge( this.cur, t.constructor == String ?
259 $.Select(t) : t.constructor == Array ? t : [t] );
263 return $.filter(t,this.cur).r.length > 0;
270 // TODO: Remove need to return this
271 for ( var i in $.fn ) {
272 if ( self[i] !== null )
273 self["_"+i] = self[i];
277 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
278 if ( $c ) $a = self.get();
279 for ( var k in self ) {(function(j){
283 return $.apply(self,self[j],arguments);
294 var b = navigator.userAgent.toLowerCase();
296 // Figure out what browser is being used
298 ( /webkit/.test(b) && "safari" ) ||
299 ( /opera/.test(b) && "opera" ) ||
300 ( /msie/.test(b) && "msie" ) ||
301 ( !/compatible/.test(b) && "mozilla" ) ||
304 // Check to see if the W3C box model is being used
305 $.boxModel = ( $.browser != "msie" ||
306 document.compatMode == "CSS1Compat" );
309 $.apply = function(o,f,a) {
312 return f.apply( o, a );
315 for (var i = 0; i < a.length; i++)
318 var r = eval('o.$$exec(' + p.join(',') + ')');
324 $.getCSS = function(e,p) {
325 // Adapted from Prototype 1.4.0
326 if ( p == 'height' || p == 'width' ) {
328 // Handle extra width/height provided by the W3C box model
329 var ph = (!$.boxModel ? 0 :
330 parseInt($.css(e,"paddingTop")) + parseInt($.css(e,"paddingBottom")) +
331 parseInt($.css(e,"borderTopWidth")) + parseInt($.css(e,"borderBottomWidth"))) || 0;
333 var pw = (!$.boxModel ? 0 :
334 parseInt($.css(e,"paddingLeft")) + parseInt($.css(e,"paddingRight")) +
335 parseInt($.css(e,"borderLeftWidth")) + parseInt($.css(e,"borderRightWidth"))) || 0;
339 if ($.css(e,"display") != 'none') {
340 oHeight = e.offsetHeight || parseInt(e.style.height) || 0;
341 oWidth = e.offsetWidth || parseInt(e.style.width) || 0;
344 var ov = els.visibility;
345 var op = els.position;
346 var od = els.display;
347 els.visibility = 'hidden';
348 els.position = 'absolute';
350 oHeight = e.clientHeight || parseInt(e.style.height);
351 oWidth = e.clientWidth || parseInt(e.style.width);
357 return p == 'height' ?
358 (oHeight - ph < 0 ? 0 : oHeight - ph) :
359 (oWidth - pw < 0 ? 0 : oWidth - pw);
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").toLowerCase();
368 var s = document.defaultView.getComputedStyle(e,"");
369 var r = s ? s.getPropertyValue(p) : null;
376 $.clean = function(a) {
378 for ( var i = 0; i < a.length; i++ ) {
379 if ( a[i].constructor == String ) {
381 if ( !a[i].indexOf("<tr") ) {
383 a[i] = "<table>" + a[i] + "</table>";
384 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
386 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
389 var div = document.createElement("div");
390 div.innerHTML = a[i];
393 div = div.firstChild.firstChild;
395 div = div.firstChild;
399 for ( var j = 0; j < div.childNodes.length; j++ ) {
400 r[r.length] = div.childNodes[j];
402 } else if ( a[i].length && !a[i].nodeType ) {
403 for ( var k = 0; k < a[i].length; k++ ) {
404 r[r.length] = a[i][k];
406 } else if ( a[i] !== null ) {
408 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
417 * A wrapper function for each() to be used by append and prepend.
418 * Handles cases where you're trying to modify the inner contents of
419 * a table, when you actually need to work with the tbody.
421 $.fn.domManip = function(fn){
422 return this.each(function(){
425 if ( this.nodeName == 'TABLE' ) {
426 var tbody = this.getElementsByTagName("tbody");
428 if ( !tbody.length ) {
429 obj = document.createElement("tbody");
430 this.appendChild( obj );
440 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
441 '#': "a.getAttribute('id') && a.getAttribute('id').nodeValue == m[2]",
445 nth: "m[3] - 0 == i",
448 last: "i == r.length - 1",
451 "first-child": "$.sibling(a,0).cur",
452 "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))",
453 "last-child": "$.sibling(a,0,true).cur",
454 "nth-last-child": "$.sibling(a,m[3],true).cur",
455 "first-of-type": "$.ofType(a,0)",
456 "nth-of-type": "$.ofType(a,m[3])",
457 "last-of-type": "$.ofType(a,0,true)",
458 "nth-last-of-type": "$.ofType(a,m[3],true)",
459 "only-of-type": "$.ofType(a) == 1",
460 "only-child": "$.sibling(a).length == 1",
461 parent: "a.childNodes.length > 0",
462 empty: "a.childNodes.length == 0",
463 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
464 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
465 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
466 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
467 enabled: "a.disabled == false",
468 disabled: "a.disabled",
471 ".": "$.hasWord(a,m[2])",
473 "=": "$.attr(a,m[3]) == m[4]",
474 "!=": "$.attr(a,m[3]) != m[4]",
475 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
476 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
477 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
478 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
479 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
480 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
482 "[": "$.Select(m[2],a).length > 0"
485 $.Select = function( t, context ) {
486 context = context || $.context || document;
487 if ( t.constructor != String ) {
491 if ( t.indexOf("//") === 0 ) {
492 context = context.documentElement;
493 t = t.substr(2,t.length);
494 } else if ( t.indexOf("/") === 0 ) {
495 context = context.documentElement;
496 t = t.substr(1,t.length);
497 // FIX Assume the root element is right :(
498 if ( t.indexOf('/') ) {
499 t = t.substr(t.indexOf('/'),t.length);
507 while ( t.length > 0 && last != t ) {
511 t = $.cleanSpaces(t);
513 var re = new RegExp( "^//", "i" );
514 t = t.replace( re, "" );
516 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
517 if ( t.indexOf('/') === 0 ) {
518 t = t.substr(1,t.length);
520 r = $.map( ret, function(a){ return a.parentNode; } );
521 t = t.substr(2,t.length);
522 t = $.cleanSpaces(t);
523 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
524 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
525 t = t.substr(1,t.length);
526 t = $.cleanSpaces(t);
527 } else if ( t.indexOf('+') === 0 ) {
528 r = $.map( ret, function(a){ return $.sibling(a).next; } );
529 t = t.substr(1,t.length);
530 t = $.cleanSpaces(t);
531 } else if ( t.indexOf('~') === 0 ) {
532 r = $.map( ret, function(a){
534 var s = $.sibling(a);
536 for ( var i = s.n; i < s.length; i++ ) {
542 t = t.substr(1,t.length);
543 t = $.cleanSpaces(t);
544 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
545 if ( ret[0] == context ) { ret.shift(); }
546 done = $.merge( done, ret );
548 t = " " + t.substr(1,t.length);
550 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
553 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
554 var oid = document.getElementById(m[2]);
555 r = ret = oid ? [oid] : [];
556 t = t.replace( re2, "" );
558 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
560 for ( var i = 0; i < ret.length; i++ ) {
565 r = $.merge( $.getAll(o), r );
567 case 'text': case 'radio': case 'checkbox': case 'hidden':
568 case 'button': case 'submit': case 'image': case 'password':
569 case 'reset': case 'file':
570 r = $.merge( $.grep( $.tag(o,"input"),
571 function(a){ return a.type == m[2]; }), r );
574 r = $.merge( $.tag(o,"input"), r );
575 r = $.merge( $.tag(o,"select"), r );
576 r = $.merge( $.tag(o,"textarea"), r );
579 r = $.merge( r, $.tag(o,m[2]) );
588 var val = $.filter(t,r);
590 t = $.cleanSpaces(val.t);
594 if ( ret && ret[0] == context ) { ret.shift(); }
595 done = $.merge( done, ret );
600 $.tag = function(a,b){
601 return a && typeof a.getElementsByTagName != 'undefined' ?
602 a.getElementsByTagName( b ) : [];
605 $.attr = function(o,a,v){
606 if ( a && a.constructor == String ) {
610 'class': 'className',
613 a = (fix[a] && fix[a].replace && fix[a]) || a;
614 var r = new RegExp("-([a-z])","ig");
615 a = a.replace(r,function(z,b){return b.toUpperCase();});
616 if ( typeof v != 'undefined' ) {
618 if ( o.setAttribute && a != 'disabled' ) {
622 return o[a] || o.getAttribute(a) || '';
628 $.filter = function(t,r,not) {
630 if ( not === false ) {
631 g = function(a,f) {return $.grep(a,f,true);};
634 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
635 var re = new RegExp( "^\\[ *@([a-z0-9\\*\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
639 m = ['', '@', m[2], m[1], m[3]];
641 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
645 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
649 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
654 t = t.replace( re, "" );
656 if ( m[1] == ":" && m[2] == "not" ) {
657 r = $.filter(m[3],r,false).r;
661 if ( $.g[m[1]].constructor == String ) {
663 } else if ( $.g[m[1]][m[2]] ) {
668 eval('f = function(a,i){return ' + f + '}');
674 return { r: r, t: t };
677 $.parents = function(a){
679 var c = a.parentNode;
680 while ( c !== null && c != document ) {
687 $.cleanSpaces = function(t){
688 return t.replace(/^\s+|\s+$/g, '');
691 $.ofType = function(a,n,e) {
692 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
693 if ( e ) { n = t.length - n - 1; }
694 return typeof n != 'undefined' ? t[n] == a : t.length;
697 $.sibling = function(a,n,e) {
699 var tmp = a.parentNode.childNodes;
700 for ( var i = 0; i < tmp.length; i++ ) {
701 if ( tmp[i].nodeType == 1 ) {
702 type[type.length] = tmp[i];
705 type.n = type.length - 1;
708 if ( e ) { n = type.length - n - 1; }
709 type.cur = ( type[n] == a );
710 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
711 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
715 $.hasWord = function(e,a) {
716 if ( typeof e == 'undefined' ) { return false; }
717 if ( e.className !== null ) { e = e.className; }
718 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
721 $.getAll = function(o,r) {
723 var s = o.childNodes;
724 for ( var i = 0; i < s.length; i++ ) {
725 if ( s[i].nodeType == 1 ) {
733 $.merge = function(a,b) {
735 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
737 for ( var i = 0; i < a.length; i++ ) {
739 for ( var j = 0; j < b.length; j++ ) {
740 if ( a[i] == b[j] ) {
752 $.grep = function(a,f,s) {
754 if ( typeof a != 'undefined' ) {
755 for ( var i = 0; i < a.length; i++ ) {
756 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
764 $.map = function(a,f) {
766 for ( var i = 0; i < a.length; i++ ) {
769 if ( t.constructor != Array ) { t = [t]; }
778 // Bind an event to an element
779 // Original by Dean Edwards
780 $.event.add = function(element, type, handler) {
781 // For whatever reason, IE has trouble passing the window object
782 // around, causing it to be cloned in the process
783 if ( $.browser == "msie" && typeof element.setInterval != "undefined" ) {
787 if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; }
788 if (!element.events) { element.events = {}; }
789 var handlers = element.events[type];
791 handlers = element.events[type] = {};
792 if (element["on" + type]) {
793 handlers[0] = element["on" + type];
796 handlers[handler.$$guid] = handler;
797 element["on" + type] = $.event.handle;
800 $.event.add.guid = 1;
802 // Detach an event or set of events from an element
803 $.event.remove = function(element, type, handler) {
804 if (element.events) {
805 if (type && element.events[type]) {
807 delete element.events[type][handler.$$guid];
809 for ( var i in element.events[type] ) {
810 delete element.events[type][i];
814 for ( var j in element.events ) {
815 $.event.remove( element, j );
821 $.event.trigger = function(element,type,data) {
822 data = data || [ $.event.fix({ type: type }) ];
823 if ( element && element["on" + type] ) {
824 $.apply( element, element["on" + type], data );
828 $.event.handle = function(event) {
829 if ( !event && !window.event ) { return null; }
831 var returnValue = true, handlers = [];
832 event = event || $.event.fix(window.event);
834 for ( var j in this.events[event.type] ) {
835 handlers[handlers.length] = this.events[event.type][j];
838 for ( var i = 0; i < handlers.length; i++ ) {
839 if ( handlers[i].constructor == Function ) {
840 this.$$handleEvent = handlers[i];
841 if (this.$$handleEvent(event) === false) {
842 event.preventDefault();
843 event.stopPropagation();
851 $.event.fix = function(event) {
852 event.preventDefault = $.event.fix.preventDefault;
853 event.stopPropagation = $.event.fix.stopPropagation;
857 $.event.fix.preventDefault = function() {
858 this.returnValue = false;
861 $.event.fix.stopPropagation = function() {
862 this.cancelBubble = true;
867 $.fn.text = function(e) {
870 for ( var j = 0; j < e.length; j++ ) {
871 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
872 t += e[j].childNodes[i].nodeType != 1 ?
873 e[j].childNodes[i].nodeValue :
874 $.fn.text(e[j].childNodes[i].childNodes);
880 /*setTimeout(function(){
881 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
882 throw "Error: You are overwriting jQuery, please include jQuery last.";