jQuery is now JSLint (jslint.com) compatible, save for the eval stuff. Can't there b
[jquery.git] / fx / fx.js
1 $.speed = function(s,o) {
2         if ( o && o.constructor == Function ) { o = { onComplete: o }; }
3         o = o || {};
4         var ss = {"crawl":1200,"xslow":850,"slow":600,"medium":400,"fast":200,"xfast":75,"normal":400};
5         o.duration = typeof s == "number" ? s : ss[s] || 400;
6         return o;
7 };
8
9 $.fn.hide = function(a,o) {
10         o = $.speed(a,o);
11         return a ? this.each(function(){
12                 new fx.FadeSize(this,o).hide();
13         }) : this._hide();
14 };
15
16 $.fn.show = function(a,o) {
17         o = $.speed(a,o);
18         return a ? this.each(function(){
19                 new fx.FadeSize(this,o).show();
20         }) : this._show();
21 };
22
23 $.fn.slideDown = function(a,o) {
24         o = $.speed(a,o);
25         return this.each(function(){
26                 new fx.Resize(this,o).show("height");
27         });
28 };
29
30 $.fn.slideUp = function(a,o) {
31         o = $.speed(a,o);
32         return this.each(function(){
33                 new fx.Resize(this,o).hide("height");
34         });
35 };
36
37 $.fn.fadeOut = function(a,o) {
38         o = $.speed(a,o);
39         return a ? this.each(function(){
40                 new fx.Opacity(this,o).hide();
41         }) : this._hide();
42 };
43
44 $.fn.fadeIn = function(a,o) {
45         o = $.speed(a,o);
46         return a ? this.each(function(){
47                 new fx.Opacity(this,o).show();
48         }) : this._show();
49 };
50
51 $.fn.center = function(f) {
52         return this.each(function(){
53                 if ( !f && this.nodeName == 'IMG' &&
54                                  !this.offsetWidth && !this.offsetHeight ) {
55                         var self = this;
56                         setTimeout(function(){
57                                 $(self).center(true);
58                         }, 13);
59                 } else {
60                         var s = this.style;
61                         var p = this.parentNode;
62                         if ( $.css(p,"position") == 'static' ) {
63                                 p.style.position = 'relative';
64                         }
65                         s.position = 'absolute';
66                         s.left = parseInt(($.css(p,"width") - $.css(this,"width"))/2, 10) + "px";
67                         s.top = parseInt(($.css(p,"height") - $.css(this,"height"))/2, 10) + "px";
68                 }
69   });
70 };
71
72 $.setAuto = function(e,p) {
73         var a = e.style[p];
74         var o = $.css(e,p);
75         e.style[p] = 'auto';
76         var n = $.css(e,p);
77         if ( o != n ) {
78                 e.style[p] = a;
79         }
80 };
81
82 /*
83  * I originally wrote fx() as a clone of moo.fx and in the process
84  * of making it small in size the code became illegible to sane 
85  * people. You've been warned.
86  */
87
88 function fx(el,op,ty,tz){
89         var z = this;
90         z.a = function(){z.el.style[ty]=z.now+z.o.unit;};
91         z.max = function(){return z.el["io"+ty]||z.el["natural"+tz]||z.el["scroll"+tz]||z.cur();};
92         z.cur = function(){return parseInt($.getCSS(z.el,ty),10);};
93         z.show = function(){z.ss("block");z.o.auto=true;z.custom(0,z.max());};
94         z.hide = function(){z.el.$o=$.getCSS(z.el,"overflow");z.el["io"+ty]=this.cur();z.custom(z.cur(),0);};
95         z.ss = function(a){if(y.display!=a){y.display=a;}};
96         z.toggle = function(){if(z.cur()>0){z.hide();}else{z.show();}};
97         z.modify = function(a){z.custom(z.cur(),z.cur()+a);};
98         z.clear = function(){clearInterval(z.timer);z.timer=null;};
99         z.el = el.constructor==String?document.getElementById(el):el;
100         var y = z.el.style;
101         z.oo = y.overflow;
102         y.overflow = "hidden";
103         z.o = {
104                 unit: "px",
105                 duration: (op && op.duration) || 400,
106                 onComplete: (op && op.onComplete) || op
107         };
108         z.step = function(f,tt){
109                 var t = (new Date()).getTime();
110                 var p = (t - z.s) / z.o.duration;
111                 if (t >= z.o.duration+z.s) {
112                         z.now = tt;
113                         z.clear();
114                         setTimeout(function(){
115                                 y.overflow = z.oo;
116                                 if(y.height=="0px"||y.width=="0px"){z.ss("none");}
117                                 if ( ty != "opacity" && z.o.auto ) {
118                                         $.setAuto( z.el, "height" );
119                                         $.setAuto( z.el, "width" );
120                                 }
121                                 if(z.o.onComplete.constructor == Function){z.el.$_ = z.o.onComplete;z.el.$_();}
122                         },13);
123                 } else {
124                         z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (tt-f) + f;
125                 }
126                 z.a();
127         };
128         z.custom = function(f,t){
129                 if(z.timer) {return null;}
130                 this.now=f;z.a();z.io=z.cur();z.s=(new Date()).getTime();
131                 z.timer=setInterval(function(){z.step(f,t);}, 13);
132         };
133 }
134 fx.fn = ["show","hide","toggle"];
135 fx.ty = ["Height","Width","Left","Top"];
136 for(var $i in fx.ty){(function(){
137         var c = fx.ty[$i];
138         fx[c] = function(a,b){
139                 return new fx(a,b,c.toLowerCase(),c);
140         };
141 })();}
142 fx.Opacity = function(a,b){
143         var o = new fx(a,b,"opacity");
144         o.cur = function(){return parseFloat(o.el.style.opacity);};
145         o.a = function() {
146                 var e = o.el.style;
147                 if (o.now == 1) { o.now = 0.9999; }
148                 if (window.ActiveXObject) {
149                         e.filter = "alpha(opacity=" + o.now*100 + ")";
150                 }
151                 e.opacity = o.now;
152         };
153         o.io = o.now = 1;
154         o.a();
155         return o;
156 };
157 fx.Resize = function(e,o){
158         var z = this;
159         var h = new fx.Height(e,o);
160         if(o) { o.onComplete = null; }
161         var w = new fx.Width(e,o);
162         function c(a,b,d){return (!a||a==c||b==d);}
163         for(var i in fx.fn){(function(){
164                 var j = fx.fn[i];
165                 z[j] = function(a,b){
166                         if(c(a,b,"height")) { h[j](); }
167                         if(c(a,b,"width")) { w[j](); }
168                 };
169         })();}
170         z.modify = function(c,d){
171                 h.modify(c);
172                 w.modify(d);
173         };
174 };
175 fx.FadeSize = function(e,o){
176         var z = this;
177         var r = new fx.Resize(e,o);
178         if(o) { o.onComplete = null; }
179         var p = new fx.Opacity(e,o);
180         for(var i in fx.fn){(function(){
181                 var j = fx.fn[i];
182                 z[j] = function(a,b){p[j]();r[j](a,b);};
183         })();}
184 };