4 * Copyright (c) 2006 John Resig (ejohn.org)
5 * Licensed under the MIT License:
6 * http://www.opensource.org/licenses/mit-license.php
13 var $a = a || $.context || document;
14 var $c = c && c.$jquery && c.get(0) || c;
16 // Since we're using Prototype's $ function,
17 // be nice and have backwards compatability
18 if ( typeof Prototype != "undefined" ) {
19 if ( $a.constructor == String ) {
20 var re = new RegExp( "[^a-zA-Z0-9_-]" );
22 $c = $c && $c.documentElement || document;
23 if ( $c.getElementsByTagName($a).length == 0 ) {
24 var obj = $c.getElementById($a);
25 if ( obj != null ) return obj;
28 } else if ( $a.constructor == Array ) {
29 return $.map( $a, function(b){
30 if ( b.constructor == String )
31 return document.getElementById(b);
37 // Load Dynamic Function List
42 // The only two getters
43 size: function() {return this.get().length},
45 return i == null ? this.cur : this.cur[i];
49 for ( var i = 0; i < this.size(); i++ )
50 $.apply( this.get(i), f, [i] );
54 return this.each(function(){
63 return h == null && this.size() ?
64 this.get(0).innerHTML : this.set( "innerHTML", h );
67 return h == null && this.size() ?
68 this.get(0).value : this.set( "value", h );
72 return a.constructor != String || b ?
76 $.attr(this.style,j,a[j]);
78 $.attr(this.style,a,b);
79 }) : $.css( this.get(0), a );
82 return this.each(function(){
83 var d = $.getCSS(this,"display");
84 if ( d == "none" || d == '' )
91 return this.each(function(){
92 this.style.display = this.$$oldblock ? this.$$oldblock : '';
93 if ( $.getCSS(this,"display") == "none" ) this.style.display = 'block';
97 return this.each(function(){
98 this.$$oldblock = $.getCSS(this,"display");
99 if ( this.$$oldblock == "none" ) this.$$oldblock = 'block';
100 this.style.display = 'none';
103 addClass: function(c) {
104 return this.each(function(){
105 if ($.hasWord(this,c)) return;
106 this.className += ( this.className.length > 0 ? " " : "" ) + c;
109 removeClass: function(c) {
110 return this.each(function(){
114 this.className.replace(
115 new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
119 toggleClass: function(c) {
120 return this.each(function(){
121 if ($.hasWord(this,c))
123 this.className.replace(
124 new RegExp('(\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), '');
126 this.className += ( this.className.length > 0 ? " " : "" ) + c;
130 this.each(function(){this.parentNode.removeChild( this );});
136 var a = $.clean(arguments);
137 return this.each(function(){
138 var b = a[0].cloneNode(true);
139 this.parentNode.insertBefore( b, this );
140 while ( b.firstChild ) b = b.firstChild;
141 b.appendChild( this );
146 var clone = this.size() > 1;
147 var a = $.clean(arguments);
148 return this.each(function(){
149 for ( var i = 0; i < a.length; i++ )
150 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
154 appendTo: function() {
156 return this.each(function(){
157 for ( var i = 0; i < a.length; i++ )
158 $(a[i]).append( this );
162 prepend: function() {
163 var clone = this.size() > 1;
164 var a = $.clean(arguments);
165 return this.each(function(){
166 for ( var i = a.length - 1; i >= 0; i-- )
167 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
172 var clone = this.size() > 1;
173 var a = $.clean(arguments);
174 return this.each(function(){
175 for ( var i = 0; i < a.length; i++ )
176 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
181 var clone = this.size() > 1;
182 var a = $.clean(arguments);
183 return this.each(function(){
184 for ( var i = a.length - 1; i >= 0; i-- )
185 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
190 return this.each(function(){
191 while ( this.firstChild )
192 this.removeChild( this.firstChild );
196 bind: function(t,f) {
197 return this.each(function(){addEvent(this,t,f);});
199 unbind: function(t,f) {
200 return this.each(function(){removeEvent(this,t,f);});
202 trigger: function(t) {
203 return this.each(function(){triggerEvent(this,t);});
207 var old = [], ret = [];
208 this.each(function(){
209 old[old.length] = this;
210 ret = $.merge( ret, $.Select(t,this) );
221 parent: function(a) {
222 this.cur = $.map(this.cur,function(d){
225 if ( a ) this.cur = $.filter(a,this.cur).r;
229 parents: function(a) {
230 this.cur = $.map(this.cur,$.parents);
231 if ( a ) this.cur = $.filter(a,this.cur).r;
235 siblings: function(a) {
236 // Incorrect, need to exclude current element
237 this.cur = $.map(this.cur,$.sibling);
238 if ( a ) this.cur = $.filter(a,this.cur).r;
242 filter: function(t) {
243 this.cur = $.filter(t,this.cur).r;
247 this.cur = t.constructor == String ?
248 $.filter(t,this.cur,false).r :
249 $.grep(this.cur,function(a){return a != t;});
253 this.cur = $.merge( this.cur, t.constructor == String ?
254 $.Select(t) : t.constructor == Array ? t : [t] );
258 return $.filter(t,this.cur).r.length > 0;
265 // TODO: Remove need to return this
266 for ( var i in $.fn ) {
267 if ( self[i] != null )
268 self["_"+i] = self[i];
272 if ( typeof Prototype != "undefined" && $a.constructor != String ) {
273 if ( $c ) $a = self.get();
274 for ( var i in self ) {(function(j){
276 if ( $a[j] == null ) {
278 return $.apply(self,self[j],arguments);
289 $.apply = function(o,f,a) {
292 return f.apply( o, a );
295 for (var i = 0; i < a.length; i++)
298 var r = eval('o.$$exec(' + p.join(',') + ')');
304 $.getCSS = function(e,p) {
305 // Adapted from Prototype 1.4.0
306 if ( p == 'height' || p == 'width' ) {
307 if ($.getCSS(e,"display") != 'none')
308 return p == 'height' ?
309 e.offsetHeight || parseInt(e.style.height) :
310 e.offsetWidth || parseInt(e.style.width);
312 var ov = els.visibility;
313 var op = els.position;
314 var od = els.display;
315 els.visibility = 'hidden';
316 els.position = 'absolute';
318 var oHeight = e.clientHeight || parseInt(e.style.height);
319 var oWidth = e.clientWidth || parseInt(e.style.width);
323 return p == 'height' ? oHeight : oWidth;
328 else if (e.currentStyle)
329 return e.currentStyle[p];
330 else if (document.defaultView && document.defaultView.getComputedStyle) {
331 p = p.replace(/([A-Z])/g,"-$1");
333 var s = document.defaultView.getComputedStyle(e,"");
334 var r = s ? s.getPropertyValue(p) : p;
341 $.clean = function(a) {
343 for ( var i = 0; i < a.length; i++ )
344 if ( a[i].constructor == String ) {
345 var div = document.createElement("div");
346 div.innerHTML = a[i];
347 for ( var j = 0; j < div.childNodes.length; j++ )
348 r[r.length] = div.childNodes[j];
349 } else if ( a[i].length )
350 for ( var j = 0; j < a[i].length; j++ )
351 r[r.length] = a[i][j];
352 else if ( a[i] != null )
354 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
359 '': "m[2] == '*' || a.nodeName.toUpperCase() == m[2].toUpperCase()",
364 nth: "m[3] - 0 == i",
367 last: "i == r.length - 1",
370 "first-child": "$.sibling(a,0).cur",
371 "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))",
372 "last-child": "$.sibling(a,0,true).cur",
373 "nth-last-child": "$.sibling(a,m[3],true).cur",
374 "first-of-type": "$.ofType(a,0)",
375 "nth-of-type": "$.ofType(a,m[3])",
376 "last-of-type": "$.ofType(a,0,true)",
377 "nth-last-of-type": "$.ofType(a,m[3],true)",
378 "only-of-type": "$.ofType(a) == 1",
379 "only-child": "$.sibling(a).length == 1",
380 parent: "a.childNodes.length > 0",
381 empty: "a.childNodes.length == 0",
382 root: "a == ( a.ownerDocument ? a.ownerDocument : document ).documentElement",
383 contains: "(a.innerText || a.innerHTML).indexOf(m[3]) != -1",
384 visible: "(!a.type || a.type != 'hidden') && ($.getCSS(a,'display') != 'none' && $.getCSS(a,'visibility') != 'hidden')",
385 hidden: "(a.type && a.type == 'hidden') || $.getCSS(a,'display') == 'none' || $.getCSS(a,'visibility') == 'hidden'",
386 enabled: "a.disabled == false",
387 disabled: "a.disabled",
390 // TODO: Write getAttribute helper
391 ".": "$.hasWord(a,m[2])",
393 "=": "$.attr(a,m[3]) == m[4]",
394 "!=": "$.attr(a,m[3]) != m[4]",
395 "~=": "$.hasWord($.attr(a,m[3]),m[4])",
396 "|=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
397 "^=": "$.attr(a,m[3]).indexOf(m[4]) == 0",
398 "$=": "$.attr(a,m[3]).substr( $.attr(a,m[3]).length - m[4].length, m[4].length ) == m[4]",
399 "*=": "$.attr(a,m[3]).indexOf(m[4]) >= 0",
400 "": "m[3] == '*' ? a.attributes.length > 0 : $.attr(a,m[3])"
402 "[": "$.Select(m[2],a).length > 0"
407 $.Select = function( t, context ) {
408 context = context || $.context || document;
409 if ( t.constructor != String ) return [t];
411 if ( t.indexOf("//") == 0 ) {
412 context = context.documentElement;
413 t = t.substr(2,t.length);
414 } else if ( t.indexOf("/") == 0 ) {
415 context = context.documentElement;
416 t = t.substr(1,t.length);
417 // FIX Assume the root element is right :(
418 if ( t.indexOf('/') )
419 t = t.substr(t.indexOf('/'),t.length);
426 while ( t.length > 0 && last != t ) {
430 t = $.cleanSpaces(t);
432 var re = new RegExp( "^//", "i" );
433 t = t.replace( re, "" );
435 if ( t.indexOf('..') == 0 || t.indexOf('/..') == 0 ) {
436 if ( t.indexOf('/') == 0 )
437 t = t.substr(1,t.length);
438 r = $.map( ret, function(a){ return a.parentNode; } );
439 t = t.substr(2,t.length);
440 t = $.cleanSpaces(t);
441 } else if ( t.indexOf('>') == 0 || t.indexOf('/') == 0 ) {
442 r = $.map( ret, function(a){ return ( a.childNodes.length > 0 ? $.sibling( a.firstChild ) : null ); } );
443 t = t.substr(1,t.length);
444 t = $.cleanSpaces(t);
445 } else if ( t.indexOf('+') == 0 ) {
446 r = $.map( ret, function(a){ return $.sibling(a).next; } );
447 t = t.substr(1,t.length);
448 t = $.cleanSpaces(t);
449 } else if ( t.indexOf('~') == 0 ) {
450 r = $.map( ret, function(a){
452 var s = $.sibling(a);
454 for ( var i = s.n; i < s.length; i++ )
458 t = t.substr(1,t.length);
459 t = $.cleanSpaces(t);
460 } else if ( t.indexOf(',') == 0 || t.indexOf('|') == 0 ) {
461 if ( ret[0] == context ) ret.shift();
462 done = $.merge( done, ret );
464 t = " " + t.substr(1,t.length);
466 var re = new RegExp( "^([#.]?)([a-z0-9\\*_-]*)", "i" );
469 if ( m[1] == "#" ) { // Ummm, should make this work in all XML docs
470 var oid = document.getElementById(m[2]);
471 r = oid ? [oid] : [];
472 t = t.replace( re, "" );
474 if ( m[2] == "" || m[1] == "." ) m[2] = "*";
476 for ( var i = 0; i < ret.length; i++ ) {
481 r = $.merge( $.getAll(o), r );
483 case 'text': case 'radio': case 'checkbox': case 'hidden':
484 case 'button': case 'submit': case 'image': case 'password':
485 case 'reset': case 'file':
486 r = $.merge( $.grep( $.tag(o,"input"),
487 function(a){ return a.type == m[2] }), r );
490 r = $.merge( $.tag(o,"input"), r );
491 r = $.merge( $.tag(o,"select"), r );
492 r = $.merge( $.tag(o,"textarea"), r );
495 r = $.merge( r, $.tag(o,m[2]) );
503 var val = $.filter(t,r);
505 t = $.cleanSpaces(val.t);
508 if ( ret && ret[0] == context ) ret.shift();
509 done = $.merge( done, ret );
513 $.tag = function(a,b){
514 return a && typeof a.getElementsByTagName != "undefined" ?
515 a.getElementsByTagName( b ) : [];
518 $.attr = function(o,a,v){
519 if ( a && a.constructor == String ) {
523 'class': 'className',
526 a = (fix[a] && fix[a].replace && fix[a]) || a;
527 var r = new RegExp("-([a-z])","ig");
528 a = a.replace(r,function(z,b){return b.toUpperCase();});
531 if ( o.setAttribute ) o.setAttribute(a,v);
533 return o[a] || o.getAttribute(a) || '';
537 $.filter = function(t,r,not) {
539 if ( not == false ) var g = function(a,f) {return $.grep(a,f,true);};
541 while ( t.length > 0 && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
542 var re = new RegExp( "^\\[ *@([a-z0-9\\(\\)_-]+) *([~!\\|\\*$^=]*) *'?\"?([^'\"]*)'?\"? *\\]", "i" );
546 m = ['', '@', m[2], m[1], m[3]];
548 var re = new RegExp( "^(\\[) *([^\\]]*) *\\]", "i" );
552 var re = new RegExp( "^(:)([a-z0-9\\*_-]*)\\( *[\"']?([^ \\)'\"]*)['\"]? *\\)", "i" );
556 var re = new RegExp( "^([:\\.#]*)([a-z0-9\\*_-]*)", "i" );
561 t = t.replace( re, "" );
563 if ( m[1] == ":" && m[2] == "not" )
564 r = $.filter(m[3],r,false).r;
566 if ( $.g[m[1]].constructor == String )
568 else if ( $.g[m[1]][m[2]] )
569 var f = $.g[m[1]][m[2]];
572 eval("f = function(a,i){return " + f + "}");
577 return { r: r, t: t };
580 $.parents = function(a){
582 var c = a.parentNode;
583 while ( c != null && c != document ) {
590 $.cleanSpaces = function(t){
591 return t.replace(/^\s+|\s+$/g, '')
594 $.ofType = function(a,n,e) {
595 var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName});
596 if ( e ) n = t.length - n - 1;
597 return n != null ? t[n] == a : t.length;
600 $.sibling = function(a,n,e) {
602 var tmp = a.parentNode.childNodes;
603 for ( var i = 0; i < tmp.length; i++ ) {
604 if ( tmp[i].nodeType == 1 )
605 type[type.length] = tmp[i];
607 type.n = type.length - 1;
609 if ( e ) n = type.length - n - 1;
610 type.cur = ( type[n] == a );
611 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
612 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
616 $.hasWord = function(e,a) {
617 if ( e == null ) return false;
618 if ( e.className != null ) e = e.className;
619 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e)
622 $.getAll = function(o,r) {
624 var s = o.childNodes;
625 for ( var i = 0; i < s.length; i++ ) {
626 if ( s[i].nodeType == 1 ) {
634 $.merge = function(a,b) {
636 for ( var j = 0; j < b.length; j++ )
639 for ( var i = 0; i < a.length; i++ ) {
641 for ( var j = 0; j < b.length; j++ )
650 $.grep = function(a,f,s) {
653 for ( var i = 0; i < a.length; i++ )
654 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
659 $.map = function(a,f) {
661 for ( var i = 0; i < a.length; i++ ) {
664 if ( t.constructor != Array ) t = [t];
671 // Bind an event to an element
672 // Original by Dean Edwards
673 function addEvent(element, type, handler) {
674 if ( element.location ) element = window; // Ughhhhh....
675 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
676 if (!element.events) element.events = {};
677 var handlers = element.events[type];
679 handlers = element.events[type] = {};
680 if (element["on" + type])
681 handlers[0] = element["on" + type];
683 handlers[handler.$$guid] = handler;
684 element["on" + type] = handleEvent;
688 // Detach an event or set of events from an element
689 function removeEvent(element, type, handler) {
690 if (element.events) {
691 if (type && element.events[type]) {
693 delete element.events[type][handler.$$guid];
695 for ( var i in element.events[type] )
696 delete element.events[type][i];
699 for ( var i in element.events )
700 removeEvent( element, i );
705 function triggerEvent(element,type,data) {
706 data = data || [{ type: type }];
707 if ( element && element["on" + type] )
708 $.apply( element, element["on" + type], data );
711 function handleEvent(event) {
712 var returnValue = true;
713 event = event || fixEvent(window.event);
715 for ( var i in this.events[event.type] )
716 handlers[handlers.length] = this.events[event.type][i];
717 for ( var i = 0; i < handlers.length; i++ ) {
719 if ( handlers[i].constructor == Function ) {
720 this.$$handleEvent = handlers[i];
721 if (this.$$handleEvent(event) === false) {
722 event.preventDefault();
723 event.stopPropagation();
732 function fixEvent(event) {
733 event.preventDefault = fixEvent.preventDefault;
734 event.stopPropagation = fixEvent.stopPropagation;
737 fixEvent.preventDefault = function() {
738 this.returnValue = false;
740 fixEvent.stopPropagation = function() {
741 this.cancelBubble = true;
746 $.fn.text = function(e) {
749 for ( var j = 0; j < e.length; j++ ) {
750 for ( var i = 0; i < e[j].childNodes.length; i++ )
751 t += e[j].childNodes[i].nodeType != 1 ?
752 e[j].childNodes[i].nodeValue :
753 $.fn.text(e[j].childNodes[i].childNodes);
758 setTimeout(function(){
759 if ( typeof Prototype != "undefined" && $.g == null && $.clean == null )
760 throw "Error: You are overwriting jQuery, please include jQuery last.";