little bug in $.fx.fadeTo
[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,1).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,1).show();
48         }) : this._show();
49 };
50
51 $.fn.fadeTo = function(a,ev,o) {
52         o = $.speed(a,o);
53         return a ? this.each(function(){
54                 ef = new $.fx.Opacity(this,o);
55                 ef.custom(ef.cur(),parseFloat(ev));
56                 ef.show();
57         }) : this._show();
58 };
59
60 $.fn.center = function(f) {
61         return this.each(function(){
62                 if ( !f && this.nodeName == 'IMG' &&
63                                  !this.offsetWidth && !this.offsetHeight ) {
64                         var self = this;
65                         setTimeout(function(){
66                                 $(self).center(true);
67                         }, 13);
68                 } else {
69                         var s = this.style;
70                         var p = this.parentNode;
71                         if ( $.css(p,"position") == 'static' ) {
72                                 p.style.position = 'relative';
73                         }
74                         s.position = 'absolute';
75                         s.left = parseInt(($.css(p,"width") - $.css(this,"width"))/2, 10) + "px";
76                         s.top = parseInt(($.css(p,"height") - $.css(this,"height"))/2, 10) + "px";
77                 }
78   });
79 };
80
81 $.setAuto = function(e,p) {
82         var a = e.style[p];
83         var o = $.css(e,p);
84         e.style[p] = 'auto';
85         var n = $.css(e,p);
86         if ( o != n ) {
87                 e.style[p] = a;
88         }
89 };
90
91 /*
92  * I originally wrote fx() as a clone of moo.fx and in the process
93  * of making it small in size the code became illegible to sane
94  * people. You've been warned.
95  */
96
97 $.fx = function(el,op,ty,tz){
98         var z = this;
99         z.el = el.constructor==String?document.getElementById(el):el;
100         var y = z.el.style;
101         z.a = function(){z.el.style[ty]=z.now+z.o.unit;};
102         z.max = function(){return z.el["io"+ty]||z.el["natural"+tz]||z.el["scroll"+tz]||z.cur();};
103         z.cur = function(){return parseInt($.getCSS(z.el,ty),10);};
104         z.show = function(){z.ss("block");z.o.auto=true;z.custom(0,z.max());};
105         z.hide = function(){z.el.$o=$.getCSS(z.el,"overflow");z.el["io"+ty]=this.cur();z.custom(z.cur(),0);};
106         z.ss = function(a){if(y.display!=a){y.display=a;}};
107         z.toggle = function(){if(z.cur()>0){z.hide();}else{z.show();}};
108         z.modify = function(a){z.custom(z.cur(),z.cur()+a);};
109         z.clear = function(){clearInterval(z.timer);z.timer=null;};
110         z.oo = y.overflow;
111         y.overflow = "hidden";
112         z.o = {
113                 unit: "px",
114                 duration: (op && op.duration) || 400,
115                 onComplete: (op && op.onComplete) || op
116         };
117         z.step = function(f,tt){
118                 var t = (new Date()).getTime();
119                 var p = (t - z.s) / z.o.duration;
120                 if (t >= z.o.duration+z.s) {
121                         z.now = tt;
122                         z.clear();
123                         setTimeout(function(){
124                                 y.overflow = z.oo;
125                                 if(y.height=="0px"||y.width=="0px"){z.ss("none");}
126                                 if ( ty != "opacity" && z.o.auto ) {
127                                         $.setAuto( z.el, "height" );
128                                         $.setAuto( z.el, "width" );
129                                 }
130                                 if(z.o.onComplete.constructor == Function){z.el.$_ = z.o.onComplete;z.el.$_();}
131                         },13);
132                 } else {
133                         z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (tt-f) + f;
134                 }
135                 z.a();
136         };
137         z.custom = function(f,t){
138                 if(z.timer) {return null;}
139                 this.now=f;z.a();z.io=z.cur();z.s=(new Date()).getTime();
140                 z.timer=setInterval(function(){z.step(f,t);}, 13);
141         };
142 };
143
144 $.fx.fn = ["show","hide","toggle"];
145 $.fx.ty = ["Height","Width","Left","Top"];
146
147 (function(){
148         for(var $i in $.fx.ty){(function(){
149                 var c = $.fx.ty[$i];
150                 $.fx[c] = function(a,b){
151                         return new $.fx(a,b,c.toLowerCase(),c);
152                 };
153         })();}
154 })();
155
156 $.fx.Opacity = function(a,b,sv){
157         var o = new $.fx(a,b,"opacity");
158         o.cur = function(){return parseFloat(o.el.style.opacity);};
159         o.a = function() {
160                 var e = o.el.style;
161                 if (o.now == 1) { o.now = 0.9999; }
162                 if (window.ActiveXObject) {
163                         e.filter = "alpha(opacity=" + o.now*100 + ")";
164                 }
165                 e.opacity = o.now;
166         };
167         o.io = o.now = (sv || o.cur());
168         o.a();
169         return o;
170 };
171 $.fx.Resize = function(e,o){
172         var z = this;
173         var h = new $.fx.Height(e,o);
174         if(o) { o.onComplete = null; }
175         var w = new $.fx.Width(e,o);
176         function c(a,b,d){return (!a||a==c||b==d);}
177         for(var i in $.fx.fn){(function(){
178                 var j = $.fx.fn[i];
179                 z[j] = function(a,b){
180                         if(c(a,b,"height")) { h[j](); }
181                         if(c(a,b,"width")) { w[j](); }
182                 };
183         })();}
184         z.modify = function(c,d){
185                 h.modify(c);
186                 w.modify(d);
187         };
188 };
189 $.fx.FadeSize = function(e,o){
190         var z = this;
191         var r = new $.fx.Resize(e,o);
192         if(o) { o.onComplete = null; }
193         var p = new $.fx.Opacity(e,o,1);
194         for(var i in $.fx.fn){(function(){
195                 var j = $.fx.fn[i];
196                 z[j] = function(a,b){p[j]();r[j](a,b);};
197         })();}
198 };