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.domManip(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.domManip(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 // Figure out what browser is being used
326 ( /webkit/.test(b) && "safari" ) ||
327 ( /opera/.test(b) && "opera" ) ||
328 ( /msie/.test(b) && "msie" ) ||
329 ( !/compatible/.test(b) && "mozilla" ) ||
332 // Check to see if the W3C box model is being used
333 $.boxModel = ( $.browser != "msie" ||
334 document.compatMode == "CSS1Compat" );
337 $.apply = function(o,f,a) {
340 return f.apply( o, a );
343 for (var i = 0; i < a.length; i++) {
347 var r = eval('o.$$exec(' + p.join(',') + ')');
353 $.getCSS = function(e,p) {
354 // Adapted from Prototype 1.4.0
355 if ( p == 'height' || p == 'width' ) {
357 // Handle extra width/height provided by the W3C box model
358 var ph = (!$.boxModel ? 0 :
359 parseInt($.css(e,"paddingTop")) + parseInt($.css(e,"paddingBottom")) +
360 parseInt($.css(e,"borderTopWidth")) + parseInt($.css(e,"borderBottomWidth"))) || 0;
362 var pw = (!$.boxModel ? 0 :
363 parseInt($.css(e,"paddingLeft")) + parseInt($.css(e,"paddingRight")) +
364 parseInt($.css(e,"borderLeftWidth")) + parseInt($.css(e,"borderRightWidth"))) || 0;
368 if ($.css(e,"display") != 'none') {
369 oHeight = e.offsetHeight || parseInt(e.style.height,10) || 0;
370 oWidth = e.offsetWidth || parseInt(e.style.width,10) || 0;
373 var ov = els.visibility;
374 var op = els.position;
375 var od = els.display;
376 els.visibility = 'hidden';
377 els.position = 'absolute';
379 oHeight = e.clientHeight || parseInt(e.style.height,10);
380 oWidth = e.clientWidth || parseInt(e.style.width,10);
386 return p == 'height' ?
387 (oHeight - ph < 0 ? 0 : oHeight - ph) :
388 (oWidth - pw < 0 ? 0 : oWidth - pw);
393 } else if (e.currentStyle) {
394 return e.currentStyle[p];
395 } else if (document.defaultView && document.defaultView.getComputedStyle) {
396 p = p.replace(/([A-Z])/g,"-$1");
398 var s = document.defaultView.getComputedStyle(e,"");
399 var r = s ? s.getPropertyValue(p) : p;
407 $.clean = function(a) {
409 for ( var i = 0; i < a.length; i++ ) {
410 if ( a[i].constructor == String ) {
412 if ( !a[i].indexOf("<tr") ) {
414 a[i] = "<table>" + a[i] + "</table>";
415 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
417 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
420 var div = document.createElement("div");
421 div.innerHTML = a[i];
424 div = div.firstChild.firstChild;
426 div = div.firstChild;
430 for ( var j = 0; j < div.childNodes.length; j++ ) {
431 r[r.length] = div.childNodes[j];
433 } else if ( a[i].length && !a[i].nodeType ) {
434 for ( var k = 0; k < a[i].length; k++ ) {
435 r[r.length] = a[i][k];
437 } else if ( a[i] !== null ) {
439 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
448 * A wrapper function for each() to be used by append and prepend.
449 * Handles cases where you're trying to modify the inner contents of
450 * a table, when you actually need to work with the tbody.
452 $.fn.domManip = function(fn){
453 return this.each(function(){
456 if ( this.nodeName == 'TABLE' ) {
457 if ( !this.firstChild ) {
458 this.appendChild( document.createElement("tbody") );
460 obj = this.firstChild;
468 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
469 '#': "a.getAttribute('id') && a.getAttribute('id').nodeValue == m[2]",
473 nth: "m[3] - 0 == i",
476 last: "i == r.length - 1",
479 "first-child": "$.sibling(a,0).cur",
480 "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))",
481 "last-child": "$.sibling(a,0,true).cur",
482 "nth-last-child": "$.sibling(a,m[3],true).cur",
483 "first-of-type": "$.ofType(a,0)",
484 "nth-of-type": "$.ofType(a,m[3])",
485 "last-of-type": "$.ofType(a,0,true)",
486 "nth-last-of-type": "$.ofType(a,m[3],true)",
487 "only-of-type": "$.ofType(a) == 1",
488 "only-child": "$.sibling(a).length == 1",
489 parent: "a.childNodes.length > 0",
490 empty: "a.childNodes.length == 0",
491 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
492 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
493 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
494 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
495 enabled: "a.disabled == false",
496 disabled: "a.disabled",
499 ".": "$.hasWord(a,m[2])",
501 "=": "$.attr(a,m[3]) == m[4]",
502 "!=": "$.attr(a,m[3]) != m[4]",
503 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
504 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
505 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
506 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
507 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
508 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
510 "[": "$.Select(m[2],a).length > 0"
513 $.Select = function( t, context ) {
514 context = context || $.context || document;
515 if ( t.constructor != String ) {
519 if ( t.indexOf("//") === 0 ) {
520 context = context.documentElement;
521 t = t.substr(2,t.length);
522 } else if ( t.indexOf("/") === 0 ) {
523 context = context.documentElement;
524 t = t.substr(1,t.length);
525 // FIX Assume the root element is right :(
526 if ( t.indexOf('/') ) {
527 t = t.substr(t.indexOf('/'),t.length);
535 while ( t.length > 0 && last != t ) {
539 t = $.cleanSpaces(t);
541 var re = new RegExp( "^//", "i" );
542 t = t.replace( re, "" );
544 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
545 if ( t.indexOf('/') === 0 ) {
546 t = t.substr(1,t.length);
548 r = $.map( ret, function(a){ return a.parentNode; } );
549 t = t.substr(2,t.length);
550 t = $.cleanSpaces(t);
551 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
552 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
553 t = t.substr(1,t.length);
554 t = $.cleanSpaces(t);
555 } else if ( t.indexOf('+') === 0 ) {
556 r = $.map( ret, function(a){ return $.sibling(a).next; } );
557 t = t.substr(1,t.length);
558 t = $.cleanSpaces(t);
559 } else if ( t.indexOf('~') === 0 ) {
560 r = $.map( ret, function(a){
562 var s = $.sibling(a);
564 for ( var i = s.n; i < s.length; i++ ) {
570 t = t.substr(1,t.length);
571 t = $.cleanSpaces(t);
572 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
573 if ( ret[0] == context ) { ret.shift(); }
574 done = $.merge( done, ret );
576 t = " " + t.substr(1,t.length);
578 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
581 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
582 var oid = document.getElementById(m[2]);
583 r = ret = oid ? [oid] : [];
584 t = t.replace( re2, "" );
586 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
588 for ( var i = 0; i < ret.length; i++ ) {
593 r = $.merge( $.getAll(o), r );
595 case 'text': case 'radio': case 'checkbox': case 'hidden':
596 case 'button': case 'submit': case 'image': case 'password':
597 case 'reset': case 'file':
598 r = $.merge( $.grep( $.tag(o,"input"),
599 function(a){ return a.type == m[2]; }), r );
602 r = $.merge( $.tag(o,"input"), r );
603 r = $.merge( $.tag(o,"select"), r );
604 r = $.merge( $.tag(o,"textarea"), r );
607 r = $.merge( r, $.tag(o,m[2]) );
616 var val = $.filter(t,r);
618 t = $.cleanSpaces(val.t);
622 if ( ret && ret[0] == context ) { ret.shift(); }
623 done = $.merge( done, ret );
628 $.tag = function(a,b){
629 return a && typeof a.getElementsByTagName != 'undefined' ?
630 a.getElementsByTagName( b ) : [];
633 $.attr = function(o,a,v){
634 if ( a && a.constructor == String ) {
638 'class': 'className',
641 a = (fix[a] && fix[a].replace && fix[a]) || a;
642 var r = new RegExp("-([a-z])","ig");
643 a = a.replace(r,function(z,b){return b.toUpperCase();});
644 if ( typeof v != 'undefined' ) {
646 if ( o.setAttribute && a != 'disabled' ) {
650 return o[a] || o.getAttribute(a) || '';
656 $.filter = function(t,r,not) {
658 if ( not === false ) {
659 g = function(a,f) {return $.grep(a,f,true);};
662 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
663 var re = new RegExp( "^\\[ *@([a-z0-9\\*\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
667 m = ['', '@', m[2], m[1], m[3]];
669 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
673 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
677 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
682 t = t.replace( re, "" );
684 if ( m[1] == ":" && m[2] == "not" ) {
685 r = $.filter(m[3],r,false).r;
689 if ( $.g[m[1]].constructor == String ) {
691 } else if ( $.g[m[1]][m[2]] ) {
696 eval('f = function(a,i){return ' + f + '}');
702 return { r: r, t: t };
705 $.parents = function(a){
707 var c = a.parentNode;
708 while ( c !== null && c != document ) {
715 $.cleanSpaces = function(t){
716 return t.replace(/^\s+|\s+$/g, '');
719 $.ofType = function(a,n,e) {
720 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
721 if ( e ) { n = t.length - n - 1; }
722 return typeof n != 'undefined' ? t[n] == a : t.length;
725 $.sibling = function(a,n,e) {
727 var tmp = a.parentNode.childNodes;
728 for ( var i = 0; i < tmp.length; i++ ) {
729 if ( tmp[i].nodeType == 1 ) {
730 type[type.length] = tmp[i];
733 type.n = type.length - 1;
736 if ( e ) { n = type.length - n - 1; }
737 type.cur = ( type[n] == a );
738 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
739 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
743 $.hasWord = function(e,a) {
744 if ( typeof e == 'undefined' ) { return false; }
745 if ( e.className !== null ) { e = e.className; }
746 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
749 $.getAll = function(o,r) {
751 var s = o.childNodes;
752 for ( var i = 0; i < s.length; i++ ) {
753 if ( s[i].nodeType == 1 ) {
761 $.merge = function(a,b) {
763 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
765 for ( var i = 0; i < a.length; i++ ) {
767 for ( var j = 0; j < b.length; j++ ) {
768 if ( a[i] == b[j] ) {
780 $.grep = function(a,f,s) {
782 if ( typeof a != 'undefined' ) {
783 for ( var i = 0; i < a.length; i++ ) {
784 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
792 $.map = function(a,f) {
794 for ( var i = 0; i < a.length; i++ ) {
797 if ( t.constructor != Array ) { t = [t]; }
806 // Bind an event to an element
807 // Original by Dean Edwards
808 $.event.add = function(element, type, handler) {
809 // For whatever reason, IE has trouble passing the window object
810 // around, causing it to be cloned in the process
811 if ( $.browser == "msie" && typeof element.setInterval != "undefined" ) {
815 if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; }
816 if (!element.events) { element.events = {}; }
817 var handlers = element.events[type];
819 handlers = element.events[type] = {};
820 if (element["on" + type]) {
821 handlers[0] = element["on" + type];
824 handlers[handler.$$guid] = handler;
825 element["on" + type] = $.event.handle;
828 $.event.add.guid = 1;
830 // Detach an event or set of events from an element
831 $.event.remove = function(element, type, handler) {
832 if (element.events) {
833 if (type && element.events[type]) {
835 delete element.events[type][handler.$$guid];
837 for ( var i in element.events[type] ) {
838 delete element.events[type][i];
842 for ( var j in element.events ) {
843 $.event.remove( element, j );
849 $.event.trigger = function(element,type,data) {
850 data = data || [ $.event.fix({ type: type }) ];
851 if ( element && element["on" + type] ) {
852 $.apply( element, element["on" + type], data );
856 $.event.handle = function(event) {
857 if ( !event && !window.event ) { return null; }
859 var returnValue = true, handlers = [];
860 event = event || $.event.fix(window.event);
862 for ( var j in this.events[event.type] ) {
863 handlers[handlers.length] = this.events[event.type][j];
866 for ( var i = 0; i < handlers.length; i++ ) {
867 if ( handlers[i].constructor == Function ) {
868 this.$$handleEvent = handlers[i];
869 if (this.$$handleEvent(event) === false) {
870 event.preventDefault();
871 event.stopPropagation();
879 $.event.fix = function(event) {
880 event.preventDefault = $.event.fix.preventDefault;
881 event.stopPropagation = $.event.fix.stopPropagation;
885 $.event.fix.preventDefault = function() {
886 this.returnValue = false;
889 $.event.fix.stopPropagation = function() {
890 this.cancelBubble = true;
895 $.fn.text = function(e) {
898 for ( var j = 0; j < e.length; j++ ) {
899 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
900 t += e[j].childNodes[i].nodeType != 1 ?
901 e[j].childNodes[i].nodeValue :
902 $.fn.text(e[j].childNodes[i].childNodes);
908 /*setTimeout(function(){
909 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
910 throw "Error: You are overwriting jQuery, please include jQuery last.";