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 if ( !this.firstChild ) {
427 this.appendChild( document.createElement("tbody") );
429 obj = this.firstChild;
437 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
438 '#': "a.getAttribute('id') && a.getAttribute('id').nodeValue == m[2]",
442 nth: "m[3] - 0 == i",
445 last: "i == r.length - 1",
448 "first-child": "$.sibling(a,0).cur",
449 "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))",
450 "last-child": "$.sibling(a,0,true).cur",
451 "nth-last-child": "$.sibling(a,m[3],true).cur",
452 "first-of-type": "$.ofType(a,0)",
453 "nth-of-type": "$.ofType(a,m[3])",
454 "last-of-type": "$.ofType(a,0,true)",
455 "nth-last-of-type": "$.ofType(a,m[3],true)",
456 "only-of-type": "$.ofType(a) == 1",
457 "only-child": "$.sibling(a).length == 1",
458 parent: "a.childNodes.length > 0",
459 empty: "a.childNodes.length == 0",
460 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
461 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
462 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
463 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
464 enabled: "a.disabled == false",
465 disabled: "a.disabled",
468 ".": "$.hasWord(a,m[2])",
470 "=": "$.attr(a,m[3]) == m[4]",
471 "!=": "$.attr(a,m[3]) != m[4]",
472 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
473 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
474 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
475 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
476 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
477 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
479 "[": "$.Select(m[2],a).length > 0"
482 $.Select = function( t, context ) {
483 context = context || $.context || document;
484 if ( t.constructor != String ) {
488 if ( t.indexOf("//") === 0 ) {
489 context = context.documentElement;
490 t = t.substr(2,t.length);
491 } else if ( t.indexOf("/") === 0 ) {
492 context = context.documentElement;
493 t = t.substr(1,t.length);
494 // FIX Assume the root element is right :(
495 if ( t.indexOf('/') ) {
496 t = t.substr(t.indexOf('/'),t.length);
504 while ( t.length > 0 && last != t ) {
508 t = $.cleanSpaces(t);
510 var re = new RegExp( "^//", "i" );
511 t = t.replace( re, "" );
513 if ( t.indexOf('..') === 0 || t.indexOf('/..') === 0 ) {
514 if ( t.indexOf('/') === 0 ) {
515 t = t.substr(1,t.length);
517 r = $.map( ret, function(a){ return a.parentNode; } );
518 t = t.substr(2,t.length);
519 t = $.cleanSpaces(t);
520 } else if ( t.indexOf('>') === 0 || t.indexOf('/') === 0 ) {
521 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
522 t = t.substr(1,t.length);
523 t = $.cleanSpaces(t);
524 } else if ( t.indexOf('+') === 0 ) {
525 r = $.map( ret, function(a){ return $.sibling(a).next; } );
526 t = t.substr(1,t.length);
527 t = $.cleanSpaces(t);
528 } else if ( t.indexOf('~') === 0 ) {
529 r = $.map( ret, function(a){
531 var s = $.sibling(a);
533 for ( var i = s.n; i < s.length; i++ ) {
539 t = t.substr(1,t.length);
540 t = $.cleanSpaces(t);
541 } else if ( t.indexOf(',') === 0 || t.indexOf('|') === 0 ) {
542 if ( ret[0] == context ) { ret.shift(); }
543 done = $.merge( done, ret );
545 t = " " + t.substr(1,t.length);
547 var re2 = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
550 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
551 var oid = document.getElementById(m[2]);
552 r = ret = oid ? [oid] : [];
553 t = t.replace( re2, "" );
555 if ( m[2] === "" || m[1] == "." ) { m[2] = "*"; }
557 for ( var i = 0; i < ret.length; i++ ) {
562 r = $.merge( $.getAll(o), r );
564 case 'text': case 'radio': case 'checkbox': case 'hidden':
565 case 'button': case 'submit': case 'image': case 'password':
566 case 'reset': case 'file':
567 r = $.merge( $.grep( $.tag(o,"input"),
568 function(a){ return a.type == m[2]; }), r );
571 r = $.merge( $.tag(o,"input"), r );
572 r = $.merge( $.tag(o,"select"), r );
573 r = $.merge( $.tag(o,"textarea"), r );
576 r = $.merge( r, $.tag(o,m[2]) );
585 var val = $.filter(t,r);
587 t = $.cleanSpaces(val.t);
591 if ( ret && ret[0] == context ) { ret.shift(); }
592 done = $.merge( done, ret );
597 $.tag = function(a,b){
598 return a && typeof a.getElementsByTagName != 'undefined' ?
599 a.getElementsByTagName( b ) : [];
602 $.attr = function(o,a,v){
603 if ( a && a.constructor == String ) {
607 'class': 'className',
610 a = (fix[a] && fix[a].replace && fix[a]) || a;
611 var r = new RegExp("-([a-z])","ig");
612 a = a.replace(r,function(z,b){return b.toUpperCase();});
613 if ( typeof v != 'undefined' ) {
615 if ( o.setAttribute && a != 'disabled' ) {
619 return o[a] || o.getAttribute(a) || '';
625 $.filter = function(t,r,not) {
627 if ( not === false ) {
628 g = function(a,f) {return $.grep(a,f,true);};
631 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
632 var re = new RegExp( "^\\[ *@([a-z0-9\\*\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
636 m = ['', '@', m[2], m[1], m[3]];
638 re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
642 re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
646 re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
651 t = t.replace( re, "" );
653 if ( m[1] == ":" && m[2] == "not" ) {
654 r = $.filter(m[3],r,false).r;
658 if ( $.g[m[1]].constructor == String ) {
660 } else if ( $.g[m[1]][m[2]] ) {
665 eval('f = function(a,i){return ' + f + '}');
671 return { r: r, t: t };
674 $.parents = function(a){
676 var c = a.parentNode;
677 while ( c !== null && c != document ) {
684 $.cleanSpaces = function(t){
685 return t.replace(/^\s+|\s+$/g, '');
688 $.ofType = function(a,n,e) {
689 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName;});
690 if ( e ) { n = t.length - n - 1; }
691 return typeof n != 'undefined' ? t[n] == a : t.length;
694 $.sibling = function(a,n,e) {
696 var tmp = a.parentNode.childNodes;
697 for ( var i = 0; i < tmp.length; i++ ) {
698 if ( tmp[i].nodeType == 1 ) {
699 type[type.length] = tmp[i];
702 type.n = type.length - 1;
705 if ( e ) { n = type.length - n - 1; }
706 type.cur = ( type[n] == a );
707 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
708 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
712 $.hasWord = function(e,a) {
713 if ( typeof e == 'undefined' ) { return false; }
714 if ( e.className !== null ) { e = e.className; }
715 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
718 $.getAll = function(o,r) {
720 var s = o.childNodes;
721 for ( var i = 0; i < s.length; i++ ) {
722 if ( s[i].nodeType == 1 ) {
730 $.merge = function(a,b) {
732 for ( var k = 0; k < b.length; k++ ) { d[k] = b[k]; }
734 for ( var i = 0; i < a.length; i++ ) {
736 for ( var j = 0; j < b.length; j++ ) {
737 if ( a[i] == b[j] ) {
749 $.grep = function(a,f,s) {
751 if ( typeof a != 'undefined' ) {
752 for ( var i = 0; i < a.length; i++ ) {
753 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) {
761 $.map = function(a,f) {
763 for ( var i = 0; i < a.length; i++ ) {
766 if ( t.constructor != Array ) { t = [t]; }
775 // Bind an event to an element
776 // Original by Dean Edwards
777 $.event.add = function(element, type, handler) {
778 // For whatever reason, IE has trouble passing the window object
779 // around, causing it to be cloned in the process
780 if ( $.browser == "msie" && typeof element.setInterval != "undefined" ) {
784 if (!handler.$$guid) { handler.$$guid = $.event.add.guid++; }
785 if (!element.events) { element.events = {}; }
786 var handlers = element.events[type];
788 handlers = element.events[type] = {};
789 if (element["on" + type]) {
790 handlers[0] = element["on" + type];
793 handlers[handler.$$guid] = handler;
794 element["on" + type] = $.event.handle;
797 $.event.add.guid = 1;
799 // Detach an event or set of events from an element
800 $.event.remove = function(element, type, handler) {
801 if (element.events) {
802 if (type && element.events[type]) {
804 delete element.events[type][handler.$$guid];
806 for ( var i in element.events[type] ) {
807 delete element.events[type][i];
811 for ( var j in element.events ) {
812 $.event.remove( element, j );
818 $.event.trigger = function(element,type,data) {
819 data = data || [ $.event.fix({ type: type }) ];
820 if ( element && element["on" + type] ) {
821 $.apply( element, element["on" + type], data );
825 $.event.handle = function(event) {
826 if ( !event && !window.event ) { return null; }
828 var returnValue = true, handlers = [];
829 event = event || $.event.fix(window.event);
831 for ( var j in this.events[event.type] ) {
832 handlers[handlers.length] = this.events[event.type][j];
835 for ( var i = 0; i < handlers.length; i++ ) {
836 if ( handlers[i].constructor == Function ) {
837 this.$$handleEvent = handlers[i];
838 if (this.$$handleEvent(event) === false) {
839 event.preventDefault();
840 event.stopPropagation();
848 $.event.fix = function(event) {
849 event.preventDefault = $.event.fix.preventDefault;
850 event.stopPropagation = $.event.fix.stopPropagation;
854 $.event.fix.preventDefault = function() {
855 this.returnValue = false;
858 $.event.fix.stopPropagation = function() {
859 this.cancelBubble = true;
864 $.fn.text = function(e) {
867 for ( var j = 0; j < e.length; j++ ) {
868 for ( var i = 0; i < e[j].childNodes.length; i++ ) {
869 t += e[j].childNodes[i].nodeType != 1 ?
870 e[j].childNodes[i].nodeValue :
871 $.fn.text(e[j].childNodes[i].childNodes);
877 /*setTimeout(function(){
878 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
879 throw "Error: You are overwriting jQuery, please include jQuery last.";